/******************************************************* *Authors: Sami Lasassmeh. *Date: 12/18/2003 *Description: C-code to generate different types of square waves to meet the *project requirements using timers. *************************************************************/ #include "skp26.h" #include "sfr262.h" void main(void); void display_ad_value(void); const char message[6][9]={//array of characters to appear on the display. {"Welcome "},{"ECGR6090"}, {"halassal"},{"salsassm"}}; /*connect switches and LEDs to I/O ports*/ #define LED_R p7_0 #define LED_Y p7_1 #define LED_G p7_2 #define SW2 p10_5 #define SW3 p10_6 #define SW4 p10_7 unsigned long int m=0;//used to generate a delay and solve the bouncing //problem int cnt=0; int A1_done=0;//variable to set by an ISR. int A4_done=0;//variable to set by an ISR. const int sws_array[3][8]={//3 arrays holding the codes for 3 switches. {1,0,1,1,0,1,1,0}, {1,0,1,0,1,0,1,0}, {1,0,1,1,1,0,1,0}}; void A0A1A2A4_init(void){//function to initialize timers. ta0=0x014D;//load timer A0 by this value to generate 30khz. ta1=0x0027;// load timer A by this value to generate 20 pulses at 30khz. ta4=0x0253;// load timer A4 by this value to shutdown for 10msec. ta2=0x014D;// load timer A2 by this value to generate 30khz when it is //triggered. ta0mr=0x04;//Timer A0 work in timer mode. ta1mr=0x0D;// Timer A1 work in event counter mode. ta4mr=0x0D;// Timer A4 work in event counter mode. ta2mr=0x04;// Timer A2 work in timer mode. udf=0x00;//down count. trgsr=0xC2;//A0 trigger A1 and A4 asm("FCLR I");//disable interrupts /*assign priorities*/ ta0ic=0x03; ta1ic=0x05; ta4ic=0x06; ta2ic=0x02; tabsr=0x00; asm("FSET I");//enable interrupts. } void snd_fun(void){//function to meet the requirements for each pressed switch. if(!SW2){//switch2 is pressed LED_Y=0; m=0; do{m=m+1;//delay to solve bouncing problem. }while(m<=250000); for(cnt=0;cnt<=7;cnt++){ switch(sws_array[0][cnt]){//switch if the bit is 0 or 1. case 1:tabsr=0x17;//if it is 1, start all the timers while(!A1_done);//wait for 20 pulses to be generated tabsr=tabsr&0x13;//stop A2 while(!A4_done);//shutdown for 9.34msec. tabsr=0x00;//stop all timers /*reinitialize all variables and registers*/ A1_done=0; A4_done=0; ta0=0x014D; ta1=0x0027; ta4=0x0253; ta2=0x014D; break; case 0:tabsr=0x13;//if it is 0, start A0, A1, and A4. while(!A4_done);//shutdown for 10msec. /*reinitialize all varialbles*/ tabsr=0x00; A1_done=0; A4_done=0; ta0=0x014D; ta1=0x0027; ta4=0x0253; ta2=0x014D; break; } } } if(!SW3){//switch3 is pressed LED_Y=0; m=0; do{m=m+1;//delay to solve bouncing problem. }while(m<=250000); for(cnt=0;cnt<=7;cnt++){ switch(sws_array[1][cnt]){//switch if the bit is 0 or 1. case 1:tabsr=0x17;//if it is 1, start all the timers while(!A1_done);//wait for 20 pulses to be generated tabsr=tabsr&0x13;//stop A2 while(!A4_done);//shutdown for 9.34msec. tabsr=0x00;//stop all timers /*reinitialize all variables and registers*/ A1_done=0; A4_done=0; ta0=0x014D; ta1=0x0027; ta4=0x0253; ta2=0x014D; break; case 0:tabsr=0x13;//if it is 0, start A0, A1, and A4. while(!A4_done);//shutdown for 10msec. /*reinitialize all varialbles*/ tabsr=0x00; A1_done=0; A4_done=0; ta0=0x014D; ta1=0x0027; ta4=0x0253; ta2=0x014D; break; } } } if(!SW4){//switch4 is pressed LED_Y=0; m=0; do{m=m+1;//delay to solve bouncing problem. }while(m<=250000); for(cnt=0;cnt<=7;cnt++){ switch(sws_array[2][cnt]){//switch if the bit is 0 or 1. case 1:tabsr=0x17;//if it is 1, start all the timers while(!A1_done);//wait for 20 pulses to be generated tabsr=tabsr&0x13;//stop A2 while(!A4_done);//shutdown for 9.34msec. tabsr=0x00;//stop all timers /*reinitialize all variables and registers*/ A1_done=0; A4_done=0; ta0=0x014D; ta1=0x0027; ta4=0x0253; ta2=0x014D; break; case 0:tabsr=0x13;//if it is 0, start A0, A1, and A4. while(!A4_done);//shutdown for 10msec. /*reinitialize all varialbles*/ tabsr=0x00; A1_done=0; A4_done=0; ta0=0x014D; ta1=0x0027; ta4=0x0253; ta2=0x014D; break; } } } LED_Y=1; } #pragma INTERRUPT A1_isr void A1_isr(void){ A1_done=1;//set this variable after generating 20 pulses. } #pragma INTERRUPT A4_isr void A4_isr(void){ A4_done=1;//set this variable after 10msec. } void init_switches(void){ pd10_5=pd10_6=pd10_7=0;//set ports as inputs } void init_LEDs(void){ pd7_0=pd7_1=pd7_2=1;//set ports as outputs. } void main(void){//main function init_disp();//initialize the display display(0,message[0]); display(1,message[1]); init_switches();//initialize switches. init_LEDs();//initialize LEDs A0A1A2A4_init();//initialize timers ALL_LED_OFF//turn all LEDs off while(1){//infinite loop waiting calling the snd_fun function. snd_fun(); } }