| CODE |
//main.c #include <ez8f642c.h> #include <stdio.h> #include <sio.h> #include "RNA.H" #include "main.h" //Initializes LED ports - Port A void init_led_gpio(void) { PAADDR = 0x01; // PA Data Dir = output: updated PACTL = 0x00; // PA Out Ctrl = push-pull PAOUT = 0X00; // OUTPUT : updated } // Turns off ALL LED's void turn_off_led(void) { PAOUT |= 0x07; // Turn off all three leds } //Let the games begin! int main() { init_led_gpio(); //Init PORT A turn_off_led(); //Turn off led while(1) //Loop for infinity and beyond { PAOUT &= 0xFE; //Turn red led on - PA0 Delay(500); //Keep on for 500 ms turn_off_led(); //Turn leds off Delay(500); //Keep off for 500 ms PAOUT &= 0xFD; //Turn yellow led on - PA1 Delay(500); //Keep on for 500 ms turn_off_led(); //Turn leds off Delay(500); //Keep off for 500 ms PAOUT &= 0xFB; //Turn green led on - PA2 (Why is FB instead of FC?) Delay(500); //Keep off for 500 ms turn_off_led(); //Turn leds off Delay(500); //Keep off for 500 ms } } //main.h #ifndef MAIN #define MAIN //prototype functions - so nothing gets out of whack! void init_led_gpio(void); void turn_off_led(void); #endif //RNA.C #include "main.h" #include "RNA.H" // Example: // To pause for 2 ms, use: // Delay(2); void Delay(unsigned long num_millis) { // this function waits anywhere from 0 sec to 8.29 hours // num_millis can be from 0 to 29,826,161 // It looks for a disabled timer. If it can't find one, it // approximates the delay with for() loops. long c=0,c_old=0,t_old=0,t; unsigned long c1=0; int i,t_num=-1; for (i=0;i<3;i++) { if((*(unsigned char volatile far*)(0xF07+(i<<3)))==0) { t_num=i; break; } } if (t_num>-1) // if the above loop sets t_num, then use the selected counter to pause... { num_millis=(num_millis<<7)+(num_millis<<4); //num_millis=num_millis*144; DisableTimer(t_num); // disable counter 0 in case it's running EnableTimer(t_num,7,65535); // Setup counter 0 with a prescale value of 2^7 and a reload value of 55296 while(c<num_millis) { t=GetTime(t_num); if (t-t_old<0) c1+=65536; c=c1+t; t_old=t; } DisableTimer(t_num); } else { // ... otherwise loop to emulate the timer... for (i=0;i<1000;i++) { for (t_num=0;t_num<(num_millis<<8);t_num++) {} } } } // This function is for "Future work"... void uDelay(unsigned long num_micros) { // The minimum amount of delay is 100 uSec due to function overhead. long t,mt; mt=num_micros/5; for (t=500;t<mt;t++) {} } // An interrupt example... #pragma interrupt void isr_timer1(void) { static unsigned char flag1=0; IRQ0E0 &= 0xBF; // IRQ0E1 &= 0xBF; // Disable Timer1 Interrupts EI (); DI (); IRQ0E0 &= 0xBF; // IRQ0E1 |= 0x40; // Enable Timer1 Level1 priority } void init_timer1(unsigned int reload_val,unsigned char prescale) { SET_VECTOR(TIMER1, isr_timer1); /*Pass the vector number and the ISR*/ T1CTL=0; // disable timer T1CTL=(prescale<<3)+1; T1H=0; T1L=1; T1RH=(reload_val>>8); T1RL=reload_val &= 0xFF; PCAF = 0x03; //Port C Timer1 Alternate Function IRQ0ENH = 0x40; // IRQ0 EnableH IRQ0ENL = 0x40; // IRQ0 EnableL; Timer1 is in Level1 priority T1CTL|=0x80; } void EnableTimerInterrupt(void) { EI(); } // Once timer is enabled with EnableTimer(...), the current time can be // retrieved with GetTime(...) unsigned int GetTime(unsigned char num) { unsigned int val1,val2,num_s=(num<<3); val1=(*(unsigned char volatile far*)0xF00+num_s); //val1=TxH; val2=(*(unsigned char volatile far*)0xF01+num_s); //val2=TxL; return (val2 + (val1<<8)); } void EnableTimer(unsigned char num, unsigned char prescale_power,unsigned int reload_value) { unsigned char num_s=(num<<3); unsigned char temp11=0; (*(unsigned char volatile far*)(0xF07+num_s))=0x00; //TxCTL = 0 -- disable timer (*(unsigned char volatile far*)(0xF07+num_s))|=1; //Set to continuous mode (*(unsigned char volatile far*)(0xF07+num_s))|=(prescale_power<<3); //TxCTL=prescale_power<<3 (*(unsigned char volatile far*)(0xF00+num_s))=0x00; //TxH = 0 (*(unsigned char volatile far*)(0xF01+num_s))=0x01; //TxL = 0 (*(unsigned char volatile far*)(0xF03+num_s))=reload_value-((reload_value>>8)<<8); // TxRL = upper half of reload_value; (*(unsigned char volatile far*)(0xF02+num_s))=(reload_value>>8); // TxRH = lower half of reload_value; (*(unsigned char volatile far*)(0xF07+num_s))|=0x80; // TxCTL|=0x80; --enable timer } void DisableTimer(unsigned char num) { unsigned int num_s=(num<<3); (*(unsigned char volatile far*)(0xF07+num_s))=0x00; // TxCTL &= 0; } //RNA.H // RNA.H is a library of constants and functions put together by // Richard Rhodes and David Franck. #include <ez8.h> #define PORT_A 0x00 #define PORT_B 0x04 #define PORT_C 0x08 #define PORT_D 0x0C #define PORT_E 0x10 #define PORT_F 0x14 #define PORT_G 0x18 #define PORT_H 0x1C void Delay(unsigned long); void uDelay(unsigned long); void EnableTimer(unsigned char num, unsigned char prescale_power,unsigned int reload_value); void DisableTimer(unsigned char num); unsigned int GetTime(unsigned char num); void init_timer1(unsigned int reload_val,unsigned char prescale); void isr_timer1(void); void EnableTimerInterrupt(void); |