Sei sulla pagina 1di 3

/******************************************

*******************************************
USART.c
"Using 8-N-1 serial port parameter setting
or configuration in asynchronous mode."
Created: 19-03-2013 12:19:26
Author: Hitesh
*******************************************
*******************************************
itoa() added on 22 June 2013
*******************************************
******************************************/
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#define BAUDRATE 9600 // The baud rate we want to use
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
//Declaration of functions
void USART_init (void); //function to initialize
unsigned char USART_receive (void); //function to receive one char in arduin
o
void USART_send (unsigned char data); //function to send one character from ar
duino
void USART_putstring (char *Stringptr); //function to send a whole string
char String[]="Data coming from serial communication";
/*String[] is in fact an array but when we put the text between the " " symbols
the compiler threats it as a String and automatically puts the null termination
character in the end of the text*/
int main(void)
{
USART_init(); //Call the USART initialization code
int count = 0;
unsigned char buffer[7]; //buffer for itoa()
while(1)
{
count++;
itoa(count, buffer, 10); //integer to string conversing
USART_putstring("\r\n "); // Send CR LF (Carriage Return & Line Feed to ter
minal)
USART_putstring(buffer); // Send whats in buffer
USART_putstring(" "); // Sends space
USART_putstring(String); /*Pass the string to the USART_putstring func
tion
and sends it over the serial*/

_delay_ms(1000);
_delay_ms(1000);
_delay_ms(1000);
_delay_ms(1000);
_delay_ms(1000);
/*Delay for 5 seconds so it will re-send the string
every 5 seconds. We can also put some more operations
here :) like turning a sequence of leds on and then off e.t.c.
_delay_ms(5000);
note:why would the delay of 5 second work? - _delay_ms() can provide
max 1 sec delay, it'll work with decreased resolution In this mode
_delay_ms() will work with a resol ution of 1/10 ms, providing
delays up to 6.5535 seconds (independent from CPU frequency).*/
}
return 0;
}
//Definitions of initially declared functions
void USART_init(void)
{
UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
UBRR0L = (uint8_t)(BAUD_PRESCALLER);
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
UCSR0C = (3<<UCSZ00);
}
/*uint8_t is the same as a byte.its shorthand for: a type of
unsigned integer of length 8 bits.
UBRR0H and UBRR0L are the resisters where we set the baud rate
but not directly , in these two resistors we put values dependent
on the baud rate we use and cpu's (crystal) frequency and to find
those values we have a formula defined earlier as BAUD_PRESCALLER .
It is also given in the data sheet of ATmega328p which we are using
in the chapter named USART.
UCSR0B is the resister which controls if
Rx(receive, this is activated by RXEN0 bit)
and
Tx(transmit or send, this is activated by TXENO bit)
pins are activated or not. Also this is where we can enable interrupts.
UCSR0C is resister that as some more configuration bits,
this is a more protocol specific i.e. this is where we configure
data bits length, parity check and number of stop bits
In 8-N-1 setting there are eight (8) data bits, no (N) parity bit,
and one (1) stop bit.*/
unsigned char USART_receive(void)
{
while(!(UCSR0A & (1<<RXC0)));
return UDR0;
}
/*In the first line the while loop is used to pool the receive register
,and if there is new data in that register it return the data*/
void USART_send( unsigned char data)
{
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
/*The first line is checking if there is space in the
Atmega send buffer(the atmega chip as a small 3 byte
hardware buffer in the TX/send channel) to put a new char/byte,
if yes the char/byte is loaded into the UDR0 register
which makes part of the USART hardware and if something
is loaded into that register it will be sent by the USART.*/
void USART_putstring(char *StringPtr)
{
while(*StringPtr != 0x00) /*Here we check if there is still more cha
rs to send,
this is done checking the actual char an
d see if it is
different from the null char*/
{
USART_send(*StringPtr); //Using the simple send function we send o
ne char at a time
StringPtr++; //We increment the pointer so we can read
the next char
}
}

Potrebbero piacerti anche