122 lines
4.0 KiB
C
122 lines
4.0 KiB
C
/*================================================================================================
|
|
|
|
usart.c SiWiCom copyright 2006 v1.0 (2006-05-22)
|
|
|
|
Name: Christoffer Martinsson E-mail: cm@cmtec.se
|
|
Name: Joakim Nilsson E-mail: mail@jopin.se
|
|
|
|
Description: Routines for USART
|
|
|
|
USART configuration is done by hardware.h witch must contain the following definitions:
|
|
(X=letter i.e PORTX=PORTB, n=number i.e PXn=PB4)
|
|
|
|
USART_DDR DDRX (Example: #define USART_DDR DDRD)
|
|
USART_PORT PORTX (Example: #define USART_PORT PORTD)
|
|
USART_RX PXn (Example: #define USART_RX PD0)
|
|
USART_TX PXn (Example: #define USART_TX PD1)
|
|
|
|
================================================================================================*/
|
|
#include <hardware.h>
|
|
|
|
#include <inttypes.h>
|
|
#include <avr/io.h>
|
|
|
|
#include <usart.h>
|
|
|
|
/*================================================================================================
|
|
Functions
|
|
================================================================================================*/
|
|
/*================================================================================================
|
|
usartSetBaud
|
|
|
|
Description: Set baudrate. Input value should be acording to following formula:
|
|
baud = ((F_CPU/(8*BAUDRATE))-1)
|
|
|
|
Input: baud (uint8_t ((F_CPU/(8*BAUDRATE))-1))
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void usartSetBaud(uint16_t baud)
|
|
{
|
|
UCSR0A |= (1<<U2X0);
|
|
UBRR0H = (uint8_t )(baud>>8);
|
|
UBRR0L = (uint8_t )(baud);
|
|
}
|
|
/*================================================================================================
|
|
usartReceive
|
|
|
|
Description: Get byte from input buffer.
|
|
|
|
Input: -
|
|
Return: UDR (uint8_t)
|
|
------------------------------------------------------------------------------------------------*/
|
|
uint8_t usartReceive( void )
|
|
{
|
|
/* Wait for data to be received */
|
|
while ( !(UCSR0A & (1<<RXC0)) );
|
|
/* Get and return received data from buffer */
|
|
return UDR0;
|
|
}
|
|
/*================================================================================================
|
|
usartSend
|
|
|
|
Description: Send byte to USART.
|
|
|
|
Input: data (uint8_t data byte to be send)
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void usartSend(uint8_t data)
|
|
{
|
|
/* Wait for empty transmit buffer */
|
|
while ( !( UCSR0A & (1<<UDRE0)) );
|
|
/* Put data into buffer, sends the data */
|
|
UDR0 = data;
|
|
}
|
|
/*================================================================================================
|
|
usartSendString
|
|
|
|
Description: Write a string of characters to the USART.
|
|
|
|
Input: *stringPointer (uint8_t array)
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void usartSendString (uint8_t *stringPointer)
|
|
{
|
|
|
|
while(*stringPointer)
|
|
{
|
|
usartSend(*stringPointer);
|
|
stringPointer++;
|
|
}
|
|
|
|
}
|
|
/*================================================================================================
|
|
usartEnable
|
|
|
|
Description: Enable USART function.
|
|
|
|
Input: -
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void usartEnable(void)
|
|
{
|
|
USART_DDR |= (1<<USART_TX);
|
|
UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
|
|
}
|
|
/*================================================================================================
|
|
usartDisable
|
|
|
|
Description: Disable USART function.
|
|
|
|
Input: -
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void usartDisable(void)
|
|
{
|
|
USART_DDR &= ~(1<<USART_TX);
|
|
UCSR0B &= ~((1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0));
|
|
USART_PORT &= ~(1<<USART_TX);
|
|
}
|
|
/*================================================================================================
|
|
End
|
|
================================================================================================*/
|