#include <avr/io.h>
#include <string.h>
// This is the arbitrary number that corresponds to baud
// rates. A lookup table for these numbers is found on
// page 168 of the datasheet.
#define BAUD_CONST 12
// Current heading to be sent to the master uC
extern volatile unsigned int current_heading;
/*******************************************************************************
*
* FUNCTION: USART_Init()
*
* PURPOSE: This function initializes the USART for receiving and
* transmitting. It sets the baud rate, frame format and
* enables rx/tx.
*
* CALLED FROM: main.c/main()
*
* PARAMETERS: Nothing.
*
* RETURNS: Nothing.
*
* COMMENTS: Nothing.
*
*******************************************************************************/
void USART_Init(void)
{
// Set the baud rate. [136]
UBRRH = (unsigned char) (BAUD_CONST >> 8);
UBRRL = (unsigned char) BAUD_CONST;
// Sets the RXEN (receiving) and TXEN (sending) bits in
// the UCSRB register. [154]
UCSRB = (1 << RXEN)|(1 << TXEN);
// Set frame format: 8 data, 1 stop bit.
UCSRC = (1 << URSEL)|(3 << UCSZ0);
}
/*******************************************************************************
*
* FUNCTION: USART_Transmit()
*
* PURPOSE: This function waits for the transmit buffer to be empty and
* then sends data to it when space is available.
*
* CALLED FROM: serial_coms.c/print()
*
* PARAMETERS: The data that is to be sent to the transmit buffer when
* room is available.
*
* RETURNS: Nothing.
*
* COMMENTS: This is used solely as an internal function to modularize
* the code. This function sends one character out at a time,
* with each character being fed via print().
*
*******************************************************************************/
void USART_Transmit(unsigned char data)
{
// Wait for empty transmit buffer.
while(!(UCSRA & (1 << UDRE)));
// Put data in buffer/send data. [140]
UDR = data;
}
/*******************************************************************************
*
* FUNCTION: USART_Receive()
*
* PURPOSE: This function waits for data to get to the receiving buffer
* and then returns that data.
*
* CALLED FROM: Anywhere where data is being received from the USART.
*
* PARAMETERS: Nothing.
*
* RETURNS: Data that is received.
*
* COMMENTS: Nothing.
*
*******************************************************************************/
unsigned char USART_Receive(void)
{
// Wait for data to get here.
while(!(UCSRA & (1 << RXC)));
// Get/return the data from the buffer.
return UDR;
}
/*******************************************************************************
*
* FUNCTION: print()
*
* PURPOSE: This function prepares data to be sent to USART_Transmit
* for output.
*
* CALLED FROM: Anywhere where data is being sent to the USART.
*
* PARAMETERS: Nothing.
*
* RETURNS: Nothing.
*
* COMMENTS: Nothing.
*
*******************************************************************************/
void print(char *text)
{
int i = 0;
for(i = 0; i < strlen(text); i++)
{
USART_Transmit(text[i]);
}
}