00001 /* This file has been prepared for Doxygen automatic documentation generation.*/ 00024 // Include files 00025 #include "Main.h" 00026 #include "UART.h" 00027 00028 // Global variables 00029 unsigned char RX_byte_nr; // counts the number of received bytes 00030 unsigned char sPreamble[] = "STK502"; // the preamble to detect start of frame 00031 unsigned char ReceiveBuffer[50]; // recevive buffer to temporary store the received data 00032 unsigned char TransmitBuffer[50]; // transmit buffer to temporary store the data to be transmitted 00033 unsigned char TX_byte_nr; // counts the number of transmitted bytes 00034 unsigned char Bytes_to_send; // cointains the number of bytes to send 00035 unsigned char RX_Packet_complete = FALSE; // if received a whole packet it's TRUE, else it will be FALSE 00036 unsigned char RX_Preamble_complete = FALSE; // if preamble has been received it's TRUE, else it will be FALSE 00037 unsigned char Dec_L; // Decimal low byte 00038 unsigned char Dec_M; // Decimal medium byte 00039 unsigned char Dec_H; // Decimal high byte 00040 00041 00045 void UART_init(unsigned char BaudRate) 00046 { 00047 RX_byte_nr = 0; 00048 UBRR0L = BaudRate; // set the baudrate that is choosen 00049 UCSR0A = (1<<U2X0); // double the USART Transmission Speed 00050 UCSR0B = (1<<RXCIE0) | (1<<TXCIE0) | (1<<RXEN0) | (1<<TXEN0); // enable Receiver and Transmitter and interrupt 00051 UCSR0C = (3<<UCSZ00); // character size = 8 bit 00052 } 00053 00054 00058 void Store_RX_data(void) 00059 { 00060 unsigned char ASCII_Cnt = 0; // variable to count the number of ascii-bytes 00061 unsigned char HEX_Cnt = 0; // variable to count the number of Hex-bytes 00062 unsigned char HEX_byte; // variable to store the Hex-byte 00063 00064 // loop until the Packet is converted from ascii to Hex and loaded in the correct places in SRAM 00065 while(RX_Packet_complete) 00066 { 00067 HEX_byte = 0; // clear "HEX_byte" 00068 00069 // loop to convert ASCII to Hex 00070 // it will loop until a ascii space charater (0x20) or a ascii line feed (0x0D) appears 00071 while((ReceiveBuffer[ASCII_Cnt] != 0x20) & (ReceiveBuffer[ASCII_Cnt] != 0x0D)) 00072 { 00073 HEX_byte *= 10; // multiply Hex-byte with 10 00074 HEX_byte += (ReceiveBuffer[ASCII_Cnt] - '0'); // store and add the ascii-byte 00075 ASCII_Cnt++; // increment ascii byte counter 00076 } // one ascii-byte has been converted to a Hex-byte 00077 00078 if(ASCII_Cnt) // if any byte where converted 00079 { 00080 *(&HOUR + HEX_Cnt) = HEX_byte; // store the Hex-byte in SRAM 00081 HEX_Cnt++; // increment Hex-byte counter 00082 } 00083 00084 if(ReceiveBuffer[ASCII_Cnt] == 0x20) // if the ascii byte was a space charater 00085 ASCII_Cnt++; // increment ascii byte counter 00086 else // else it means the end of packet 00087 { 00088 RX_Packet_complete = FALSE; // indicate that a new packet can be converted 00089 } 00090 } //the whole ReceiveBuffer has been converted Hex-bytes and stored in the SRAM-locations 00091 } 00092 00093 00096 void Send_TX_data(void) 00097 { 00098 unsigned char Nr_bytes_in_TX_buffer; 00099 unsigned char Cnt; 00100 unsigned char Nr_ASCII_bytes; 00101 00102 if(!(UCSR0B & 0x20)) // do not make a new transmit-buffer if there's an on-going transmition (UDRIE-bit is set) 00103 { 00104 //FILL THE TRANSMITBUFFER WITH NEW DATA 00105 00106 Nr_bytes_in_TX_buffer = 0; // clear number of bytes in buffer 00107 00108 // load preamble byte in the transmit-buffer 00109 while(sPreamble[Nr_bytes_in_TX_buffer]) 00110 { 00111 TransmitBuffer[Nr_bytes_in_TX_buffer] = sPreamble[Nr_bytes_in_TX_buffer]; //put the preamble to the Transmit buffer 00112 Nr_bytes_in_TX_buffer++; 00113 }// finished loading the preamble byte 00114 00115 Cnt = 0; // clear "Cnt", which is used to count the number of bytes loaded from SRAM in the loop below 00116 00117 // load data bytes in the transmit-buffer 00118 while(Cnt < Nr_of_hex_bytes_to_send) // loop until "Cnt" is equal to "Nr_of_hex_bytes_to_send 00119 { 00120 TransmitBuffer[Nr_bytes_in_TX_buffer++] = 0x20; // add one space character 00121 00122 Nr_ASCII_bytes = HEX2ASCII(*(&HOUR + Cnt++)); // format Hex-byte to ACSII data-bytes 00123 00124 while(Nr_ASCII_bytes) // loop while there's ASCII to left 00125 { 00126 Nr_ASCII_bytes--; // decrement "Nr_ASCII_bytes" 00127 TransmitBuffer[Nr_bytes_in_TX_buffer++] = *(&Dec_L + Nr_ASCII_bytes); // (hopefully) load ASCII-bytes in the transmit-buffer 00128 } 00129 }// finished loading data bytes in the transmit-buffer 00130 00131 TransmitBuffer[Nr_bytes_in_TX_buffer++] = 0x20; // add one space character 00132 TransmitBuffer[Nr_bytes_in_TX_buffer++] = REVISION_H; // add the revisions number in the transmit packet 00133 TransmitBuffer[Nr_bytes_in_TX_buffer++] = REVISION_L; // add the revisions number in the transmit packet 00134 00135 TransmitBuffer[Nr_bytes_in_TX_buffer++] = 0x0D; // load ASCII for carriage return 00136 TransmitBuffer[Nr_bytes_in_TX_buffer++] = 0x0A; // load ASCII for line feed 00137 TX_byte_nr = 0; // no bytes sent 00138 Bytes_to_send = Nr_bytes_in_TX_buffer; // set number of bytes to send = nr_bytes_in-TX_buffer 00139 00140 UCSR0B |= (1<<UDRIE0); // Enable UDRE interrupt (starts transfer) 00141 } 00142 } 00143 00144 00150 unsigned char HEX2ASCII(unsigned char Hex) 00151 { 00152 unsigned char Nr_ASCII_bytes = 2; 00153 00154 Dec_L = 0; //clear decimal-bytes 00155 Dec_M = 0; 00156 Dec_H = 0; 00157 00158 while(Hex) // loop until the Hex is zero 00159 { 00160 Dec_L = Hex; // store decimal-byte to Dec_L 00161 Hex -= 10; // subtract 10 from the Hex-byte 00162 if(!(SREG & 0x01)) // if carry flag is not set 00163 { 00164 Dec_M++; // incrase Dec_M-byte 00165 if(Dec_M > 9) // if Dec_M-byte over 100 decimal 00166 { 00167 Dec_M = 0; // clear Dec_M 00168 Dec_H++; // increase Dec_H 00169 } 00170 if(!Hex) // if the Hex is zero 00171 Dec_L = 0; // clear Dec_L 00172 } 00173 else 00174 Hex = 0; 00175 } 00176 00177 Dec_L += 0x30; // add 0x30 to get the right ascii-value 00178 Dec_M += 0x30; // add 0x30 to get the right ascii-value 00179 00180 if(Dec_H) // if over 100d 00181 { 00182 Dec_H += 0x30; // add 0x30 to get the right ascii-value 00183 Nr_ASCII_bytes++; // increase number of bytes 00184 } 00185 00186 return Nr_ASCII_bytes; // return number of bytes 00187 } 00188 00189 00192 #pragma vector = USART0_RXC_vect 00193 __interrupt void USART0_RXC_interrupt(void) 00194 { 00195 unsigned char RX_byte; 00196 00197 RX_byte = UDR0; // read the UDR0 register 00198 00199 if((RX_byte == sPreamble[RX_byte_nr]) & !RX_Preamble_complete) // if received byte matches predicted preamble byte 00200 { // and Preamble string hasn't been received 00201 RX_byte_nr++; 00202 if(RX_byte_nr == (sizeof(sPreamble) - 1)) // if all preamble bytes has been received 00203 { 00204 RX_Preamble_complete = TRUE; // indicate that the preable byte is OK 00205 RX_byte_nr = 0; 00206 } 00207 } 00208 else if(RX_Preamble_complete) // if the Preamble string has been received 00209 { 00210 ReceiveBuffer[RX_byte_nr++] = RX_byte; // store the received byte to the receivebuffer 00211 00212 if(RX_byte == 0x0D) // if Packet_length is zero, send back a buffer immediately 00213 { 00214 ReceiveBuffer[RX_byte_nr++] = RX_byte; // store the received byte in to the reveivebuffer 00215 RX_Preamble_complete = FALSE; // set the RX_Preamble_complete to FALSE, so the function will start to search for a new preamble 00216 RX_Packet_complete = TRUE; // set RX_Packet_complete so the converting of the received packet can start 00217 RX_byte_nr = 0; 00218 } 00219 } 00220 else 00221 RX_byte_nr = 0; 00222 } 00223 00224 00227 #pragma vector = USART0_UDRE_vect 00228 __interrupt void USART0_UDRE_interrupt(void) 00229 { 00230 if (TX_byte_nr < Bytes_to_send) 00231 { 00232 UDR0 = (TransmitBuffer[TX_byte_nr++]); //Put byte from Transmit buffer to USART I/O Data Register 00233 } 00234 else 00235 { 00236 UCSR0B &= ~(1<<UDRIE0); // Disable UDRE interrupt, transmission finshied 00237 } 00238 }
1.4.5