C51 COMPILER V6.00 RF 05/30/2001 17:04:13 PAGE 1 C51 COMPILER 6.00, COMPILATION OF MODULE RF OBJECT MODULE PLACED IN .\rf.OBJ COMPILER INVOKED BY: d:\app\keil\eval\C51\BIN\C51.EXE .\rf.c BROWSE OBJECTEXTEND DEBUG stmt level source 1 /* rf.c*/ 2 #include "../shared.h" 3 #include "door.h" 4 #include "rf.h" 5 6 7 /* buffers */ 8 static unsigned char in_buffer [IN_BUFFER_SIZE]; 9 static unsigned char out_buffer [OUT_BUFFER_SIZE]; 10 11 /* total # of bytes received into in_buffer */ 12 static unsigned char bytes_received = 0; 13 /* # of bytes sent from out_buffer */ 14 static unsigned char bytes_sent = 0; 15 /* total # of bytes to send from out_buffer */ 16 static unsigned char bytes_to_send = 0; 17 18 19 /* # of 10ms before timeout */ 20 static unsigned char overflow_count = 0; 21 /* time out flag */ 22 static bit timed_out = 0; 23 char value = 0x01; 24 25 char xdata *ledaddress = 0x0f000; // the FPGA is configured to memory map led to this address 26 27 static void switch_to(unsigned char nextState) { 28 1 TR0 = 0; 29 1 TR1 = 0; 30 1 bytes_to_send = 0; 31 1 state = nextState; 32 1 } 33 34 35 void RF_Task() { 36 1 P1 = 0x00; 37 1 *ledaddress = 0x5B; //3 38 1 RF_init(); 39 1 40 1 //generate request packet 41 1 format_request(out_buffer); 42 1 43 1 //send address byte of packet to test if RF board is busy 44 1 bytes_received = 0; 45 1 bytes_sent = 0; 46 1 bytes_to_send = 1; 47 1 set_timeout(5); 48 1 TI = 1; 49 1 50 1 //wait till RF board is NOT busy 51 1 while(1) { 52 2 // *ledaddress = 0x52;//7 53 2 if (bytes_received > 0) { 54 3 if(in_buffer[0] == out_buffer[0]) { 55 4 unset_timeout(); C51 COMPILER V6.00 RF 05/30/2001 17:04:13 PAGE 2 56 4 //address byte echoed; send the rest of packet 57 4 bytes_to_send = HEADER_LENGTH + REQUEST_LENGTH; 58 4 //assert(bytes_sent == 1); 59 4 TI = 1; 60 4 break; 61 4 } 62 3 bytes_received = 0; 63 3 } else if (timed_out) { 64 3 //test again 65 3 bytes_sent = 0; 66 3 set_timeout(5); 67 3 TI = 1; 68 3 } 69 2 } 70 1 71 1 72 1 //wait till whole packet has been sent 73 1 while(bytes_sent < bytes_to_send) ; 74 1 75 1 *ledaddress = 0x3A; //4 76 1 77 1 bytes_received = 0; 78 1 //wait for reply packet (includes BadgeID) 79 1 set_timeout(50);//0.5 sec time-out 80 1 while(1) { 81 2 if(bytes_received >= HEADER_LENGTH + RESPONSE_LENGTH) { 82 3 unset_timeout(); 83 3 //pass badgeID to FPGA for checking 84 3 badgeID = in_buffer[HEADER_LENGTH + RESPONSE_LENGTH -1 -1]; 85 3 P1 = badgeID; 86 3 switch_to(CHECKING_ID); 87 3 88 3 *ledaddress = 0x6B; //5 89 3 // *ledaddress = badgeID; // to be deleted later 90 3 return; 91 3 } else if(timed_out) { 92 3 unset_timeout(); 93 3 // switch_to(DETECTING_USER); 94 3 *ledaddress = 0x6F; //6 95 3 return; 96 3 } 97 2 } 98 1 }/* end RF_Task */ 99 100 101 static void set_timeout(unsigned char n) { 102 1 overflow_count = n; 103 1 timed_out = 0; 104 1 TH0 = RF_RELOAD_HI; 105 1 TL0 = RF_RELOAD_LO; 106 1 TR0 = 1; 107 1 } 108 109 110 static void unset_timeout() { 111 1 TR0 = 0; 112 1 TF0 = 0; 113 1 timed_out = 0; 114 1 } 115 116 117 void RF_Timer0_handler() { C51 COMPILER V6.00 RF 05/30/2001 17:04:13 PAGE 3 118 1 if(--overflow_count==0) { 119 2 timed_out = 1; 120 2 TR0 = 0; 121 2 } 122 1 TH0 = RF_RELOAD_HI; 123 1 TL0 = RF_RELOAD_LO; 124 1 } //end of RF_Timer0_handler() 125 126 127 void RF_Serial_handler() { 128 1 if(RI==1) { 129 2 RI = 0; 130 2 if(bytes_received < IN_BUFFER_SIZE) 131 2 in_buffer[bytes_received++] = SBUF; 132 2 } else { 133 2 TI = 0; 134 2 if(bytes_sent < bytes_to_send) 135 2 SBUF = out_buffer[bytes_sent++]; 136 2 } 137 1 } /* end of RF_Serial_handler() */ 138 139 140 /* create the request packet in out_buffer */ 141 static void format_request(char* out_buffer) { 142 1 out_buffer[0] = BADGE_ADDRESS; 143 1 out_buffer[0] <<= 4; 144 1 out_buffer[0] |= 4; 145 1 146 1 out_buffer[1] = 8; //telemetry mode 147 1 out_buffer[2] = REQUEST_LENGTH; 148 1 //out_buffer[3] = REQUEST_CODE; 149 1 out_buffer[3] = 2; 150 1 //out_buffer[4] = REQUEST_CODE; 151 1 out_buffer[4] = badgeID; 152 1 out_buffer[5] = 3; 153 1 } //end of format_reply 154 155 156 static void RF_init() { 157 1 158 1 /* interrupt settings */ 159 1 EA = 1; 160 1 ET0 = 1; 161 1 ET1 = 0; 162 1 ES = 1; 163 1 164 1 /* set serial */ 165 1 SM0 = 0; 166 1 SM1 = 1; 167 1 REN = 1; 168 1 169 1 /* setup timer0 as a 10 ms counter, timer1 as 19.2 kbps baud rate generator, assuming a 25MHz clock */ 170 1 TMOD = 0x21; 171 1 PCON = PCON | 0x80; 172 1 TH0 = RF_RELOAD_HI; 173 1 TL0 = RF_RELOAD_LO; 174 1 TH1 = 249; 175 1 TR1 = 1; 176 1 177 1 178 1 }/* end of RF_init() */ 179 C51 COMPILER V6.00 RF 05/30/2001 17:04:13 PAGE 4 180 MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 359 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 23 3 IDATA SIZE = ---- ---- BIT SIZE = 1 ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)