C51 COMPILER V6.00 BADGE 05/25/2001 19:09:12 PAGE 1 C51 COMPILER 6.00, COMPILATION OF MODULE BADGE OBJECT MODULE PLACED IN .\badge.OBJ COMPILER INVOKED BY: d:\app\keil\eval\C51\BIN\C51.EXE .\badge.c OBJECTEXTEND DEBUG stmt level source 1 2 //#include 3 #include 4 #include "../shared.h" 5 6 /* possible states of the badge */ 7 #define RECEIVING_REQUEST 1 8 #define SENDING_REPLY 2 9 10 /* reload value for 16-bit 10 ms counter assuming 22MHz clock */ 11 #define RELOAD_HI 0xB8 12 #define RELOAD_LO 0x63 13 14 #define IN_BUFFER_SIZE 8 15 #define OUT_BUFFER_SIZE 8 16 17 /* buffers */ 18 unsigned char in_buffer [IN_BUFFER_SIZE]; 19 unsigned char out_buffer [OUT_BUFFER_SIZE]; 20 21 /* total # of bytes received into in_buffer */ 22 unsigned char bytes_received = 0; 23 /* # of bytes sent from out_buffer */ 24 unsigned char bytes_sent = 0; 25 /* total # of bytes to send from out_buffer */ 26 unsigned char bytes_to_send = 0; 27 28 /* 50 ms time out flag */ 29 unsigned char timed_out = 0; 30 31 /* badge ID */ 32 unsigned char badgeID = 'l'; 33 34 /* create reply packet in out_buffer according to in_buffer */ 35 void format_reply(char* in_buffer, char* out_buffer); 36 37 /* set SFR values on startup */ 38 void init(); 39 40 /* send address byte of packet to test if RF board is busy */ 41 void send_address_byte(); 42 43 44 void main() 45 { 46 1 /* state of badge */ 47 1 unsigned char state = RECEIVING_REQUEST; 48 1 //P1 = 0x00; 49 1 50 1 init(); 51 1 52 1 /* loop forever to serve requests */ 53 1 for(;;) { 54 2 switch(state) { 55 3 case RECEIVING_REQUEST: C51 COMPILER V6.00 BADGE 05/25/2001 19:09:12 PAGE 2 56 3 if(bytes_received >= HEADER_LENGTH + REQUEST_LENGTH) { 57 4 state = SENDING_REPLY; 58 4 } 59 3 break; 60 3 case SENDING_REPLY: 61 3 /* for demo: read ID from port 1 */ 62 3 badgeID = P1; 63 3 64 3 bytes_received = 0; 65 3 format_reply(in_buffer, out_buffer); 66 3 67 3 //send address byte of packet to test if RF board is busy 68 3 send_address_byte(); 69 3 //wait till RF board is NOT busy 70 3 while(1) { 71 4 if (bytes_received > 0) { 72 5 if(in_buffer[0] == out_buffer[0]) { 73 6 //address byte echoed; send the rest of packet 74 6 TR0 = 0; 75 6 bytes_to_send = HEADER_LENGTH + RESPONSE_LENGTH; 76 6 //assert(bytes_sent == 1); 77 6 TI = 1; 78 6 break; 79 6 } 80 5 bytes_received = 0; 81 5 } else if (timed_out) { 82 5 //test again 83 5 send_address_byte(); 84 5 } 85 4 } 86 3 87 3 //wait till whole packet's been sent 88 3 while(bytes_sent < bytes_to_send) ; 89 3 90 3 bytes_received = 0; 91 3 state = RECEIVING_REQUEST; 92 3 break; 93 3 default: 94 3 bytes_received = 0; 95 3 state = RECEIVING_REQUEST; 96 3 } 97 2 98 2 } 99 1 }/* end of main */ 100 101 102 103 /* create the reply packet in out_buffer */ 104 void format_reply(char* in_buffer, char* out_buffer) { 105 1 //swap the upper 4 bits with the lower 4 106 1 char temp = in_buffer[0] << 4; 107 1 out_buffer[0] = temp | in_buffer[0] >> 4; 108 1 109 1 out_buffer[1] = 8; //telemetry 110 1 out_buffer[2] = RESPONSE_LENGTH; 111 1 out_buffer[3] = 2; 112 1 out_buffer[4] = RESPONSE_CODE; 113 1 out_buffer[5] = badgeID; 114 1 out_buffer[6] = 3; 115 1 } 116 117 C51 COMPILER V6.00 BADGE 05/25/2001 19:09:12 PAGE 3 118 //send address byte of packet to test if RF board is busy 119 void send_address_byte() { 120 1 timed_out = 0; 121 1 bytes_sent = 0; 122 1 bytes_to_send = 1; 123 1 TH0 = RELOAD_HI; 124 1 TL0 = RELOAD_LO; 125 1 TR0 = 1; //set time-out 126 1 TI = 1; 127 1 } 128 129 130 void Timer0_ISR () interrupt 1 { 131 1 static unsigned char count = 0; 132 1 if(++count == 5) { 133 2 TR0 = 0; 134 2 count = 0; 135 2 timed_out = 1; 136 2 } 137 1 TH0 = RELOAD_HI; 138 1 TL0 = RELOAD_LO; 139 1 } /* end of Timer0_ISR() */ 140 141 142 143 void Serial_ISR() interrupt 4 { 144 1 if(RI==1) { 145 2 RI = 0; 146 2 if(bytes_received < IN_BUFFER_SIZE) 147 2 in_buffer[bytes_received++] = SBUF; 148 2 } else { 149 2 TI = 0; 150 2 if(bytes_sent < bytes_to_send) 151 2 SBUF = out_buffer[bytes_sent++]; 152 2 } 153 1 }/* end of Serial_ISR() */ 154 155 156 void init() { 157 1 /* interrupt settings */ 158 1 EA = 1; 159 1 ET0 = 1; 160 1 ET1 = 0; 161 1 ES = 1; 162 1 163 1 /* set serial */ 164 1 SM0 = 0; 165 1 SM1 = 1; 166 1 REN = 1; 167 1 168 1 /* setup timer0 as a 50 ms counter, timer1 as 19.2 kbps baud rate generator, assuming a 22MHz clock */ 169 1 TMOD = 0x21; 170 1 PCON = PCON | 0x80; 171 1 TH0 = RELOAD_HI; 172 1 TL0 = RELOAD_LO; 173 1 TH1 = 250; 174 1 TR1 = 1; 175 1 }/* end of init() */ 176 177 178 179 C51 COMPILER V6.00 BADGE 05/25/2001 19:09:12 PAGE 4 180 181 MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 335 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 22 7 IDATA SIZE = ---- ---- BIT SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)