UART.h File Reference


Detailed Description

Defines and prototypes for UART.c.

Application note:
AVR064: A Temperature Monitoring System with LCD Output
Documentation:
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Name
RELEASE_1_1
Revision
1.3
RCSfile
UART.h,v
Date
2006/02/16 18:11:22

Definition in file UART.h.

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

unsigned char HEX2ASCII (unsigned char)
 Converts a Hex-byte to 2 or 3 decimal-bytes.
void Send_TX_data (void)
 Send data from the TransmitBuffer.
void Store_RX_data (void)
 Convert from ASCII to HEX and store data from the ReceiveBuffer to the SRAM locations.
void UART_init (unsigned char)
 Initialise the UART.


Function Documentation

unsigned char HEX2ASCII unsigned char  Hex  ) 
 

Converts a Hex-byte to 2 or 3 decimal-bytes.

Parameters:
Hex - the Hex-byte to be converted
Returns:
Number of decimal-bytes and ASCII-converted hex in Dec_L, Dec_M and possibly Dec_H

Definition at line 150 of file UART.c.

References Dec_H, Dec_L, and Dec_M.

Referenced by Send_TX_data().

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 }

void Send_TX_data void   ) 
 

Send data from the TransmitBuffer.

Definition at line 96 of file UART.c.

References Bytes_to_send, Dec_L, HEX2ASCII(), HOUR, Nr_of_hex_bytes_to_send, REVISION_H, REVISION_L, sPreamble, TransmitBuffer, and TX_byte_nr.

Referenced by main().

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 }

Here is the call graph for this function:

void Store_RX_data void   ) 
 

Convert from ASCII to HEX and store data from the ReceiveBuffer to the SRAM locations.

Definition at line 58 of file UART.c.

References FALSE, HOUR, ReceiveBuffer, and RX_Packet_complete.

Referenced by main().

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 }

void UART_init unsigned char  BaudRate  ) 
 

Initialise the UART.

Parameters:
BaudRate - used to set the baudrate of the UART

Definition at line 45 of file UART.c.

References RX_byte_nr.

Referenced by Initialization().

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 }


Generated on Fri Feb 17 12:28:30 2006 for AVR064: A Temperature Monitoring System with LCD Output by  doxygen 1.4.5