#include "skp26.h" #include "sfr262.h" void main(void); const char message[6][9]={//characters to be displayed. {"Welcome "},{"ECGR6090"}, {"halassal"},{"salsassm"} }; /*define the LEDs and switches 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;//varaible to be used in solving the bouncing problem. int cnt=0; int det=0; int SW2_p=0; int SW3_p=0; int SW4_p=0; int rx_ok=1; int A4_done=0; int rx_array[8]={0,0,0,0,0,0,0,0};//array to store the received bits. const int st_bit[3]={1,0,1};//starting bits to compare with. const int sp_bit[2]={1,0};//stop bits to compare with. void A0A1A4_init(void){//function to initialize timers. ta0=0x014D;//to generate 30khz ta1=0x0013; ta4=0x0255;//to get 10 msec. ta0mr=0x04;//timers mode to generate 30KHz ta1mr=0x0D; ta4mr=0x0D;//event count mode to get 10 msec. udf=0x00; trgsr=0xC2; asm("FCLR I"); ta0ic=0x03; ta1ic=0x05; ta4ic=0x06; tabsr=0x00; asm("FSET I"); } void P6_1_init(void){ pd6_1=0;//port as input pu14=1; } void det_first(void){ if(p6_1==0){ det=1;//indicate that the first bit is received tabsr=0x13;//starting timers. rx_array[0]=1; } } void fill(void){//function responsible for sampling the input port. for(cnt=1;cnt<=7;cnt++){ while(!A4_done);//waiting for the subroutine. if(p6_1==0){ rx_array[cnt]=1; } else{ rx_array[cnt]=0; } A4_done=0;//reset the variable. } tabsr=0x00; A0A1A4_init(); } void task_exec(void){//function to compare the received and expected patterns together for(cnt=0;cnt<=2;cnt++){//starting bits if(rx_array[cnt]!=st_bit[cnt]) rx_ok=0; } for(cnt=6;cnt<=7;cnt++){//stop bits if(rx_array[cnt]!=sp_bit[cnt-6]) rx_ok=0; } if(rx_ok==1){//no errors on the start and stop bits. if((rx_array[3]==1)&&(rx_array[4]==0)&&(rx_array[5]==1)) SW2_p=1; else if((rx_array[3]==0)&&(rx_array[4]==1)&&(rx_array[5]==0)) SW3_p=1; else if((rx_array[3]==1)&&(rx_array[4]==1)&&(rx_array[5]==0)) SW4_p=1; else rx_ok=0; } if((rx_ok==1)&&(SW2_p==1)){//switch 2 is pressed. disp_ctrlw(LCD_CLEAR); disp_ctrlw(LCD_CLEAR); display(0,"SW2 IS "); display(1,"PRESSED "); } else if((rx_ok==1)&&(SW3_p==1)){//switch 3 is pressed. disp_ctrlw(LCD_CLEAR); disp_ctrlw(LCD_CLEAR); display(0,"SW3 IS "); display(1,"PRESSED "); } else if((rx_ok==1)&&(SW4_p==1)){//switch 4 is pressed. disp_ctrlw(LCD_CLEAR); disp_ctrlw(LCD_CLEAR); display(0,"SW4 IS "); display(1,"PRESSED "); } else{//display error message. disp_ctrlw(LCD_CLEAR); disp_ctrlw(LCD_CLEAR); display(0,"TX/RX "); display(1,"FAILED "); } LED_Y=1; do{//waite for a period of time to solve the oversaturated problem. m=m+1; }while(m<=8000000); disp_ctrlw(LCD_CLEAR); disp_ctrlw(LCD_CLEAR); display(0,"Ready "); } #pragma INTERRUPT A4_isr void A4_isr(void){//subroutine to set a varaible every 10 msec. A4_done=1; } void init_switches(void){//function to define switches as inputs. pd10_5=pd10_6=pd10_7=0; } void init_LEDs(void){//LEDs as outputs. pd7_0=pd7_1=pd7_2=1; } void main(void){//main function to call other functions. init_disp(); display(0,message[0]); display(1,message[1]); init_switches(); init_LEDs(); A0A1A4_init(); P6_1_init(); ALL_LED_OFF while(1){ while(!det){ det_first(); LED_Y=0; } fill(); task_exec(); det=0; rx_ok=1; SW2_p=0; SW3_p=0; SW4_p=0; m=0; cnt=0; } }