Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

atmega16 interface with USART

Status
Not open for further replies.

aameer

Full Member level 4
Joined
May 12, 2010
Messages
216
Helped
33
Reputation
64
Reaction score
32
Trophy points
1,308
Location
Bangalore
Activity points
2,503
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.
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++;
	}
	
}
 

How are you testing your code, on real hardware or on proteus.
If you are using Proteus then please upload your design file also.
 

I had tested you code on Proteus and it looks okay and is working fine.

I changed these lines in code, for looping back the uart data.
Code:
while(1)
	{

		value=usart_getch(); // get data from serial port
		usart_putch(value);
		lcd_cmd(0x01);
		lcd_data(value);
	}

And it works fine, can you show your hardware and apart from this most important thing is fuse setting.
Did you set your fuse setting correctly.

- - - Updated - - -

As You are saying values are junk, it means your oscillator is not configured properly, make sure that you had set each fuse properly.
 

I am new to AVR. i don't know what is fuse setting ..can u explain me
i am using external 8 MHz oscillator
 

Okay, I will help you.
First tell me which software you are using to write your code and how you are programming your controller.

- - - Updated - - -

If you are using Atmek Studio for programming then go to Tools-> Device Programming and follow the image.

Example.png
 

i am using AVR studio 4 for programming and to transferhex file i am using Khazama AVR programmer.exe

- - - Updated - - -

Code:
How to program target device using AVR Dude programmer ?
1. Plug AVR dude to PC using USB A-B cable.
2. PC detects new hardware and request for the drivers.
3. Insert the CD and select the driver.
4. After successfull Installation, New device is configured as LibUSB-Win32 device.
5. Now install Kahazama AVR Programmer.
6. Select the Target uC.
7. Load the hex file.
8. Click AutoProgram.
9. Now hex file is burn't into controller.

Pinouts of AVR Programmer

Standars ISP Programmer header
MOSI 1         2 - V-TARGET
N/C  3         4 - GND
RESET 5        6 - GND
SCK   7        8 - GND
MISO  9        10 - GND

Pinout of ICSP Cable
BLACK ------------ GND
BROWN--------------5V
RED----------------RST
ORANGE-------------SCK
YELLOW-------------MISO
GREEN--------------MOSI
 

In Khazama programmer, there is option to set all these things you just have to found it by clicking on the appropriate menu in the IDE.
As per this link, http://khazama.com/project/programmer/

1.png
 

    V

    Points: 2
    Helpful Answer Positive Rating
Hello friend thanks for all the help you did. i just downloaded the version 1.7.0 but not getting what to set the fuse settings. Please guide me and i am sorry to say that i have to leave the office now as it closes now. Please guide me if possible tomorrow.
 

You are from Bangalore and you are working that late night.
That's really strange.
I will find a way out to program it using some other programming software and will tell you as soon.
I have seen that option of configuring the fuse settings.
Will tell you.
 

Hello sir i found the error. if i test the above code with 1200 baud rate i am getting the output but i have mentioned 9600 baud rate.Why is acting like that i am totally confused.Please Reply

- - - Updated - - -

hello sir.i got it in fuse settings i sorry to program external oscillator i was mentioning internal oscillator 1mhz
 

So your problem is solved now or not.

I had told you this was just due to wrong oscillator setting in fuse bits.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top