Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

uart.h File Reference


Detailed Description

Header file for uart.c.

Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Name
RELEASE_1_0
Revision
1.2
RCSfile
uart.h,v
Date
2006/05/08 12:25:58

Definition in file uart.h.

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

Included by dependency graph

Go to the source code of this file.

Defines

#define CLR_UDRIE   (UCSR0B &= ~(1<<UDRIE0))
#define SET_UDRIE   (UCSR0B |= (1<<UDRIE0))
#define UART_RX_BUFFER_MASK   ( UART_RX_BUFFER_SIZE - 1 )
#define UART_RX_BUFFER_SIZE   32
#define UART_TX_BUFFER_MASK   ( UART_TX_BUFFER_SIZE - 1 )
#define UART_TX_BUFFER_SIZE   64

Functions

void InitUART (void)
 Init of uart.

void uart_FlushRxBuffer (void)
 Empties the uart RX buffer.

__interrupt void UART_RX_interrupt (void)
 RX interrupt handler.

void uart_SendByte (unsigned char data)
 send a byte.

void uart_SendInt (int Tall)
 Sends a integer.

void uart_SendString (unsigned char Tab[])
 Sends a string.

__interrupt void UART_TX_interrupt (void)
 TX interrupt handler.


Variables

unsigned char UART_RxBuffer [UART_RX_BUFFER_SIZE]
 Buffer with received string from uart.


Define Documentation

#define CLR_UDRIE   (UCSR0B &= ~(1<<UDRIE0))
 

Definition at line 37 of file uart.h.

Referenced by UART_TX_interrupt().

#define SET_UDRIE   (UCSR0B |= (1<<UDRIE0))
 

Definition at line 36 of file uart.h.

Referenced by uart_SendByte().

#define UART_RX_BUFFER_MASK   ( UART_RX_BUFFER_SIZE - 1 )
 

Definition at line 25 of file uart.h.

#define UART_RX_BUFFER_SIZE   32
 

Definition at line 24 of file uart.h.

Referenced by UART_RX_interrupt().

#define UART_TX_BUFFER_MASK   ( UART_TX_BUFFER_SIZE - 1 )
 

Definition at line 31 of file uart.h.

Referenced by uart_SendByte(), and UART_TX_interrupt().

#define UART_TX_BUFFER_SIZE   64
 

Definition at line 30 of file uart.h.


Function Documentation

void InitUART void   ) 
 

Init of uart.

Setup uart. The BAUD value must be modified according to clock frqequency. Refer to datasheet for details.

Definition at line 45 of file uart.c.

References BAUD, and UART_RxPtr.

Referenced by Init().

00046 { 00047 // Set baud rate. 19.2 kbps trasfer speed running at 8 MHz. 00048 //#define BAUD 25 00049 // Set baud rate. 19.2 kbps trasfer speed running at 3.6864 MHz. 00050 #define BAUD 11 00051 00052 UBRR0H = (unsigned char)(BAUD>>8); 00053 UBRR0L = (unsigned char)BAUD; 00054 /* Enable receiver and transmitter, rx and tx int */ 00055 UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(1<<TXCIE0); 00056 /* Set frame format: 8data, 1stop bit */ 00057 UCSR0C = (3<<UCSZ00); 00058 00059 // Flush Buffers 00060 UART_RxPtr = 0; 00061 UART_TxTail = 0; 00062 UART_TxHead = 0; 00063 }

void uart_FlushRxBuffer void   ) 
 

Empties the uart RX buffer.

Empties the uart RX buffer.

Returns:
x Integer to be sent.

Definition at line 135 of file uart.c.

References UART_RxBuffer, and UART_RxPtr.

Referenced by main().

00135 { 00136 UART_RxPtr = 0; 00137 UART_RxBuffer[UART_RxPtr] = 0; 00138 }

__interrupt void UART_RX_interrupt void   ) 
 

RX interrupt handler.

RX interrupt handler. RX interrupt always enabled.

Definition at line 146 of file uart.c.

References GLOBAL_FLAGS::cmd, FALSE, GLOBAL_FLAGS::running, status, TRUE, UART_RX_BUFFER_SIZE, UART_RxBuffer, UART_RxPtr, and uart_SendByte().

00147 { 00148 unsigned char data; 00149 00150 // Read the received data. 00151 data = UDR0; 00152 00153 if(status.running == FALSE){ 00154 // If backspace. 00155 if(data == '\b') 00156 { 00157 if(UART_RxPtr) 00158 // Done if not at beginning of buffer. 00159 { 00160 uart_SendByte('\b'); 00161 uart_SendByte(' '); 00162 uart_SendByte('\b'); 00163 UART_RxPtr--; 00164 UART_RxBuffer[UART_RxPtr]=0x00; 00165 } 00166 } 00167 // Not backspace. 00168 else 00169 { 00170 // Put the data into RxBuf 00171 // and place 0x00 after it. If buffer is full, 00172 // data is written to UART_RX_BUFFER_SIZE - 1. 00173 if(UART_RxPtr < (UART_RX_BUFFER_SIZE - 1)){ 00174 UART_RxBuffer[UART_RxPtr] = data; 00175 UART_RxBuffer[UART_RxPtr + 1]=0x00; 00176 UART_RxPtr++; 00177 } 00178 else 00179 { 00180 UART_RxBuffer[UART_RxPtr - 1] = data; 00181 uart_SendByte('\b'); 00182 } 00183 // If enter. 00184 if(data == 13){ 00185 status.cmd = TRUE; 00186 } 00187 else 00188 uart_SendByte(data); 00189 } 00190 } 00191 }

Here is the call graph for this function:

void uart_SendByte unsigned char  data  ) 
 

send a byte.

Puts a byte in TX buffer and starts uart TX interrupt. If TX buffer is full it will hang until space.

Parameters:
data Data to be sent.

Definition at line 73 of file uart.c.

References SET_UDRIE, and UART_TX_BUFFER_MASK.

Referenced by ShowHelp(), UART_RX_interrupt(), uart_SendInt(), and uart_SendString().

00074 { 00075 unsigned char tmphead; 00076 00077 // Calculate buffer index 00078 tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK; 00079 // Wait for free space in buffer 00080 while ( tmphead == UART_TxTail ) 00081 ; 00082 // Store data in buffer 00083 UART_TxBuffer[tmphead] = data; 00084 // Store new index 00085 UART_TxHead = tmphead; 00086 // Enable UDRE interrupt 00087 SET_UDRIE; 00088 }

void uart_SendInt int  x  ) 
 

Sends a integer.

Converts a integer to ASCII and sends it using uart_SendByte. If TX buffer is full it will hang until space.

Parameters:
x Integer to be sent.

Definition at line 111 of file uart.c.

References uart_SendByte().

Referenced by ShowData().

00112 { 00113 static const char dec[] = "0123456789"; 00114 unsigned int div_val = 10000; 00115 00116 if (x < 0){ 00117 x = - x; 00118 uart_SendByte('-'); 00119 } 00120 while (div_val > 1 && div_val > x) 00121 div_val /= 10; 00122 do{ 00123 uart_SendByte (dec[x / div_val]); 00124 x %= div_val; 00125 div_val /= 10; 00126 }while(div_val); 00127 }

Here is the call graph for this function:

void uart_SendString unsigned char  Str[]  ) 
 

Sends a string.

Loops thru a string and send each byte with uart_SendByte. If TX buffer is full it will hang until space.

Parameters:
Str String to be sent.

Definition at line 97 of file uart.c.

References uart_SendByte().

Referenced by main(), and ShowData().

00098 { 00099 unsigned char n = 0; 00100 while(Str[n]) 00101 uart_SendByte(Str[n++]); 00102 }

Here is the call graph for this function:

__interrupt void UART_TX_interrupt void   ) 
 

TX interrupt handler.

TX interrupt handler. TX interrupt turned on by uart_SendByte, turned off when TX buffer is empty.

Definition at line 200 of file uart.c.

References CLR_UDRIE, and UART_TX_BUFFER_MASK.

00201 { 00202 unsigned char UART_TxTail_tmp; 00203 UART_TxTail_tmp = UART_TxTail; 00204 00205 // Check if all data is transmitted 00206 if ( UART_TxHead != UART_TxTail_tmp ) 00207 { 00208 // Calculate buffer index 00209 UART_TxTail_tmp = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK; 00210 // Store new index 00211 UART_TxTail = UART_TxTail_tmp; 00212 // Start transmition 00213 UDR0= UART_TxBuffer[ UART_TxTail_tmp]; 00214 } 00215 else 00216 // Disable UDRE interrupt 00217 CLR_UDRIE; 00218 }


Variable Documentation

unsigned char UART_RxBuffer[UART_RX_BUFFER_SIZE]
 

Buffer with received string from uart.

Definition at line 46 of file uart.h.

Referenced by main(), uart_FlushRxBuffer(), and UART_RX_interrupt().


Generated on Mon May 8 15:05:04 2006 for AVR446 - Linear speed control of stepper motor by doxygen 1.3.7