UART.c File Reference


Detailed Description

UART transmission.

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.c,v
Date
2006/02/16 18:11:22

Definition in file UART.c.

#include "Main.h"
#include "UART.h"

Include dependency graph for UART.c:

Go to the source code of this file.

Functions

unsigned char HEX2ASCII (unsigned char Hex)
 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 BaudRate)
 Initialise the UART.
__interrupt void USART0_RXC_interrupt (void)
 USART0 Receive Interrupt Routine.
__interrupt void USART0_UDRE_interrupt (void)
 USART0 Data Register Empty Interrupt Routine.

Variables

unsigned char Bytes_to_send
unsigned char Dec_H
unsigned char Dec_L
unsigned char Dec_M
unsigned char ReceiveBuffer [50]
unsigned char RX_byte_nr
unsigned char RX_Packet_complete = FALSE
unsigned char RX_Preamble_complete = FALSE
unsigned char sPreamble [] = "STK502"
unsigned char TransmitBuffer [50]
unsigned char TX_byte_nr


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 }

__interrupt void USART0_RXC_interrupt void   ) 
 

USART0 Receive Interrupt Routine.

Definition at line 193 of file UART.c.

References FALSE, ReceiveBuffer, RX_byte_nr, RX_Packet_complete, RX_Preamble_complete, sPreamble, and TRUE.

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 }

__interrupt void USART0_UDRE_interrupt void   ) 
 

USART0 Data Register Empty Interrupt Routine.

Definition at line 228 of file UART.c.

References Bytes_to_send, TransmitBuffer, and TX_byte_nr.

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 }


Variable Documentation

unsigned char Bytes_to_send
 

Definition at line 34 of file UART.c.

Referenced by Send_TX_data(), and USART0_UDRE_interrupt().

unsigned char Dec_H
 

Definition at line 39 of file UART.c.

Referenced by HEX2ASCII().

unsigned char Dec_L
 

Definition at line 37 of file UART.c.

Referenced by HEX2ASCII(), and Send_TX_data().

unsigned char Dec_M
 

Definition at line 38 of file UART.c.

Referenced by HEX2ASCII().

unsigned char ReceiveBuffer[50]
 

Definition at line 31 of file UART.c.

Referenced by Store_RX_data(), and USART0_RXC_interrupt().

unsigned char RX_byte_nr
 

Definition at line 29 of file UART.c.

Referenced by UART_init(), and USART0_RXC_interrupt().

unsigned char RX_Packet_complete = FALSE
 

Definition at line 35 of file UART.c.

Referenced by Store_RX_data(), and USART0_RXC_interrupt().

unsigned char RX_Preamble_complete = FALSE
 

Definition at line 36 of file UART.c.

Referenced by USART0_RXC_interrupt().

unsigned char sPreamble[] = "STK502"
 

Definition at line 30 of file UART.c.

Referenced by Send_TX_data(), and USART0_RXC_interrupt().

unsigned char TransmitBuffer[50]
 

Definition at line 32 of file UART.c.

Referenced by Send_TX_data(), and USART0_UDRE_interrupt().

unsigned char TX_byte_nr
 

Definition at line 33 of file UART.c.

Referenced by Send_TX_data(), and USART0_UDRE_interrupt().


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