Saturday, March 17, 2012

Receive string through UART

In this tutorial I am going to show you how to receive a string through UART. In this program we are going to make use of the previous function which were written for receiving the character through UART. Don't forget to put NULL character at the end of the string.....
#include<avr/io.h>

void UART1_Init()
{
//set baud rate = 9600 bps
UBRR1H = 0x00;
UBRR1L = 103;         //0x67

//set 1 stop bit, no parity bit and 8 bit charachter size
UCSR1C = 0x06;         //(1 << UCSZ1)|(1 << UCSZ0)  

//enable transmission and reception
UCSR1B = 0x18;         //(1 << RXEN)|(1 << TXEN)
}

//Transmit character through UART
void UART1_Tx_Char(unsigned char data)
{

//put the data to be transmitted into the UDR register
UDR1 = data;

//wait until the transmission is completed
 while(!(UCSR1A&(1<<UDRE)));
}



//Transmit string through UART
void UART1_Tx_Str(unsigned char * str)
{

while(*str)
{
UDR1 = *str++;
while(!(UCSR1A&(1<<UDRE);

}

//Receive a character through UART
unsigned char UART1_Rx_Char()
{
//wait for the charater
while(!(UCSR1A & (1<<RXC)));

//return the received charater
return(UDR1);
}

//Receive string through UART
unsigned char * UART1_Rx_Str()
{
 unsigned char string[20], x, i = 0;

//receive the characters until ENTER is pressed (ASCII for ENTER = 13)
while((x = UART1_Rx_Char()) != 13)
{
 //and store the received characters into the array string[] one-by-one
string[i++] = x;
}

//insert NULL to terminate the string
string[i] = '\0';

//return the received string
return(string);
}

void main()
{
//initialize the UART circuitry
UART1_Init();

//Transmit the Received string onto the UART again
UART1_Tx_Str( UART1_Rx_Str());
}

Receive Character Through UART

In this tutorial we are going to receive a single character through UART and display that character over PORTC, to which we can connect the LEDs.
(these codes have been written for ATmega128 micro-controller which has two UART circuitry, UART0 & UART1, the same code can be used with other AVR micro-controllers with some minor changes)
#include < avr/io.h >

//Initialize UART circuitry
void UART1_Init()
{
//set the baud rate as 9600 bits per second
UBRR1H = 0x00;
UBRR1L = 0x67; //or 103 in decimal

//set character size as 8-bit, no parity bit, one stop bit
UCSR1C = 0x06 // (1 << UCSZ1)|(1 << UCSZ0);

//enable reception and transmission
UCSR1B = 0x18 // (1 << RXEN)|(1 << TXEN);
}

void main(){
//define PORTC as an output port to display received char
DDRC = 0xFF;

//initialize UART circuitry

 UART1_Init();

//wait for the reception to be completed
while(! (UCSR1A & (1 << RXC)));

//display the received character on PORTC
PORTC = UDR1;
}

Thursday, March 15, 2012

Transmit Character Through UART

In this tutorial I am going to show you how to transmit a character through UART. First of all we have to initialize the UART circuitry of our micro-controller by providing proper values to the UART registers in AVR. There are basically 5 registers for UART....
(these codes have been written for ATmega128 micro-controller which has two UART circuitry, UART0 & UART1, the same code can be used with other AVR micro-controllers with some minor changes)

*********************************************************************************
Transmit a Character Through UART
*********************************************************************************

//Include AVR Header file
 #include < avr/io.h >
 
//Initialize UART circuitry
void UART1_Init()
{
     //set the baud rate as 9600 bits per second
     UBRR1H = 0x00;
     UBRR1L = 0x67;     //or 103 in decimal
     
     //set character size as 8-bit, no parity bit, one stop bit 
     UCSR1C = 0x06     // (1 << UCSZ1)|(1 << UCSZ0);
     
     //enable reception and transmission
     UCSR1B = 0x18     // (1 << RXEN)|(1 << TXEN);



//Start main function
void main()
{
   
     UART1_Init();
     
     //put the desired character in UDR register
     UDR1 = '@';

     //wait for the transmission to be completed
     while(! (UCSR1A & (1 << UDRE)));
}

Friday, March 9, 2012

Transmit String Through UART

In this tutorial i am going to show you how to transmit a string through UART. For that you first have to initialize the UART circuitry in your micro-controller. For initializing we have to update UART registers in AVR. There are basically 5 registers for UART in AVR....

1. UBRR register for setting up the baud rate for the transmission. Its a 16 bit value and the AVR is an 8 bit micro-controller, so this 16 bit value is stored in two 8 bit registers (UBRRnL and UBRRnH)
2 And 3 control registers (UCSRnA, UCSRnB and UCSRnC).
3. ATmega 128 is having 2 UART ciruitry (UART0 and UART1) and for each circuitry we are having different registers, for ex: UBRR1L for UART1 and UBRR0L for UART0.

(these codes have been written for ATmega128 micro-controller which has two UART circuitry, UART0 & UART1, the same code can be used with other AVR micro-controllers with some minor changes)

#include <avr/io.h>

void UART1_Init()

{
//set baud rate = 9600 bps
UBRR1H = 0x00;
UBRR1L = 103;        //0x67

//set 1 stop bit, no parity bit and 8 bit charachter size
UCSR1C = 0x06;         //(1<<UCSZ1)|(1<<UCSZ0)

//enable transmission and reception
UCSR1B = 0x18;         //(1<<RXEN)|(1<<TXEN)
}

void main()
{
int i=0;

//define the string to be transmitted
char string[] = "MoMos lOveRs";

while(string[i] != '\0')

{
//transmit the string by putting characters into UDR one by one
UDR1 = string[i++];

//wait until UDR is empty to take new byte
while(!(UCSR1A & (1<<UDRE)));
}

//send the NULL character
UDR1 = '\0';

//wait until UDR is empty to take new byte
while(!(UCSR1A & (1<<UDRE)));
}

Generating LED Patterns


In this tutorial i am going to show you, how to generate some pattern using LEDs. The micro controller is ATmega128 and the 8 LEDs are connected to the PORTC of the micro controller.
The code is written in such a way that first the LEDs will be glowing  in Right-to-Left fashion and then from Left-to-Right fashion and will continue for ever.


*********************************************************************************
Generating LED Pattern
*********************************************************************************

//Include AVR Header file
 #include < avr/io.h >
 
//Include DELAY Header file
#include < util/delay.h >

//Start main function
void main()
{
   
     int i = 0;

     //Set a particular port as output
     DDRC = 0xFF;            //PORTC output
 
     //infinite loop
     while(1)              
     {
          //Loop 8 times from Right to Left  
         while( i < 8)      
         {
              //setting 1 bit at a time in PORTC
              PORTC = (1 << i++);     

             //apply 100 ms delay
             _delay_ms(100);

         }
          //Loop 8 times from Left to Right  
         while( i )      
         {

              //setting 1 bit at a time in PORTC
              PORTC = (1 << --i);     

             //apply 100 ms delay
             _delay_ms(100);

         }  
     }
}

Thursday, March 1, 2012

Blinking LEDs

In this tutorial I am going to show you, how to blink LEDs using AVR micro controller. I am using ATmega128/ ATmega64 (both of the micro controller can be used interchangeably as they differ in the size of flash memory only). The 8 LEDs which I am going to blink/toggle are connected to PORTC of the micro  controller and they will toggle after 100 ms. So let's start with it............

*********************************************************************************
Blinking LEDs
*********************************************************************************

//Include AVR Header file
 #include"avr/io.h"
 
//Include DELAY Header file
#include"util/delay.h" 

//Start main function
void main()
{
   
     //Set a particular port as input or output
     DDRC = 0xFF;            //PORTC output
 
     //infinite loop
     while(1)              
     {
          //set all the pins of PORTC to 1  
         PORTC = 0xFF;      
 
          //apply 100 ms delay
         _delay_ms(100);        

         //set all the pins of PORTC to 0  
         PORTC = 0x00;            

         //apply 100 ms delay
         _delay_ms(100);      
     }
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews