aameer
Full Member level 4
Hello Friends
i am communicating data from PC to ATmega16 . i am unable to get the output.here is the code please check and help me soon.On my LCd i am getting some values and it is transmitting some junk values.
i am communicating data from PC to ATmega16 . i am unable to get the output.here is the code please check and help me soon.On my LCd i am getting some values and it is transmitting some junk values.
Code:
#define F_CPU 8000000UL//using 8Mhz crystal oscillator
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include<avr/io.h>
#include<util/delay.h>
#define lcd_data_port PORTB //LCD data port
#define ctrl PORTA
#define lcd_en PA1 // enable signal
#define lcd_rs PA0 // register select signal
void lcd_initial(void);
void lcd_cmd(unsigned short cmd_value);
void lcd_data(unsigned short data_value);
void lcd_data_string(unsigned char *string);
void usart_init();
void usart_putch(unsigned char send);
unsigned int usart_getch();
int main(void)
{
unsigned char value;
DDRB=0xff;
DDRA=0x03;
lcd_initial(); // initialization of LCD
_delay_ms(50); // delay of 50 mili seconds
lcd_cmd(0x84);
lcd_data_string("LCD TEST"); // function to print string on LCD
lcd_cmd(0xc0);
lcd_data_string("with AVR"); // function to print string on LCD
usart_init(); // initialization of USART
while(1)
{
value=usart_getch(); // get data from serial port
lcd_cmd(0x01);
lcd_data(value);
usart_putch(value);
}
return 0;
}
void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN);
// Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}
void usart_putch(unsigned char send)
{
while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..
// for more data to be written to it
UDR = send; // Send the byte
}
unsigned int usart_getch()
{
unsigned char value;
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data have been received and is ready to be read from UDR
return(UDR); // return the byte
}
void lcd_initial(void)
{
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_cmd(0x38);
lcd_cmd(0x0e);
}
void lcd_cmd(unsigned short cmd_value)
{
lcd_data_port=cmd_value;
ctrl =(0<<lcd_rs)|(1<<lcd_en);
_delay_ms(5);
ctrl =(0<<lcd_rs)|(0<<lcd_en);
}
void lcd_data(unsigned short data_value)
{
lcd_data_port= data_value;
ctrl = (1<<lcd_rs)|(1<<lcd_en);
_delay_ms(5);
ctrl = (1<<lcd_rs)|(0<<lcd_en);
}
void lcd_data_string(unsigned char *string)//store address value of the string in pointer *str
{
int i=0;
while(string[i]!='\0') // loop will go on till the NULL character in the string
{
lcd_data(string[i]); // sending data on LCD byte by byte
i++;
}
}