Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with AVR ATmega128

Status
Not open for further replies.
hello,
i have problem with ATMEGA128 too. I want to do simple "Hello World" using AVR Studio 5.0. i wrote this program:


#include <avr/io.h>
#include <stdio.h>

static int uart_putchar(char c, FILE *stream);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
_FDEV_SETUP_WRITE);


int main(void)
{
init_uart();
stdout = &mystdout;
printf("Hello, world!\n");

/* while(1)
{
//TODO:: Please write your application code
}
*/
}

static int uart_putchar(char c, FILE *stream)
{
if (c == "\n")
uart_putchar("\r", stream);
loop_until_bit_is_set(UCSR1A, UDRE);
UDR1 = c;
return 0;
}

static int init_uart()
{
UCSR1A = 0x20;
UCSR1B = 0x18; //Transmitter & Receiver enable
UCSR1C = 0x06;
UBRR1H = 0x00;
UBRR1L = 0x0C; //Baud Rate 38400, frequency 10000 MHz
return 0;
}



I use COM-Port terminal. But somehow it didn't work at all. On COM-Port Terminal there is nothing. Could you please help me? Thank you for your help
 
First the sequence for the USART initialisation is not correct.
Code:
static int init_uart(void)
{
UCSR1A = 0x22;
UCSR1B = 0x18; //Transmitter & Receiver enable
UCSR1C = 0x06;
UBRR1H = 0x00;
UBRR1L = 0x02; //Baud Rate 38400, frequency 1 MHz
return 0;
}
should work better.
Additional any function has to be daclared before You can use it.
You can achive that, by setting the used functions before the "main" function, or you had to declare a function prototype at the top of the programm.
Code:
static int init_uart(void);
 
Last edited:
Hy All!

I've got a problem with Atmega128' USART too.

I adopted a code from another project ( from a guy who use atmega8)

this project use the USART0 (because i can't use the USART1 for asynchronus operation )

I'm using AVR studio 4. and here's the code:

#define F_CPU 16000000UL
#define USART_BAUDRATE 9600
#define UBRR_ERTEK ((FCPU/ (USART_BAUDRATE *16UL))-1)

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <string.h>
#include <avr/pgmspace.h>
void KonfigUART() {
UBRR0L = UBRR_ERTEK;
UBRR0H = (UBRR_ERTEK>>8);
UCSR0C |= (1<<UMSEL0) | (1<<UCSZ00) | 1<UCSZ01);
UCSR0B |= (1<<RXEN0) | (1 << TXEN0);
}

char UARTDataRec()
{
while(!(UCSR0A &(1<<RXC0)))
{
}
return UDR0;
}

void UARTDataSend(char data)
{
while(!(UCSR0A & ( 1<<UDRE0)))
{
}
UDR0 = data;
}
int main(void)
{
char data;
KonfigUART();
while(1) {
data=UARTDataRec();
UARTDataSend('[');
UARTDataSend(data);
UARTDataSend(']');
}

}
 
1. I would suggest You to simulate Your code with AVR Studio. So You can test if it works correct, or hang up at one Step.
2. Check the fuses! You are not in ATMEGA 103 Mode? Clock configuration is correct?
3. Take a look into the Datasheet to check proper settings for the USART.
 
I checked the Clock configuration in the datasheet already, it seems to be correct. I simulated the code with AVR studio.. it works correct..
I checked the fuses and I'm not in Atmega103 Mode... If I change the baudrate in Hyper Terminal I always recieve another characters...
 
do you have access to an oscilloscope so that you could check what is actually going on? How long are your wires? Check that the fuse that divides clock by 8 is not set.
If you dont have an oscilloscope then you can use the _delay_ms(500) function and blink a led with 1s period to check that your system clock is really what you think it is, you might accidentally be running of the internal 8mhz oscillator. Try setting UBRR0L to 51 and see if it helps.
 
When characters will be shown at the PC, the ATMEGA give out something.
Possible Errors are wrong polarity, mistmatch Baudrate, wrong Handshake or false frame format.
How You have Interfaced the ATMEGA?
Are You using a MAX232 level shifter, an FT232 USB Interface, or something other stuff?
Be aware the MAX232 reverses the signal levels, but that's correct to connect with the PC's COM port.
 
I'm using a board for Atmega128, We're using this board in the Uni. There are an USB B I/O, so I'm not using MAX232 or FT232USB Interface... may this is the Problem ? I was thinking about my UARTconf function maybe the UBRR0 register has the bad balue?..


#define UBRR_VALUE ((F_CPU/ (USART_BAUDRATE *16))-1) //maybe the AVR studio can't calculate correctly ?? But the Datasheet includes this equation....

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <string.h>
#include <avr/pgmspace.h>
void KonfigUART() {
UBRR0L = UBRR_VALUE;
UBRR0H = (UBRR_VALUE>>8);
UCSR0C |= (1<<UMSEL0) | (1<<UCSZ00) | ( 1<UCSZ01);
UCSR0B |= (1<<RXEN0) | (1 << TXEN0);
UCSR0A = (1<<U2X0);
}
 
breaking news: I was thinkin' about the fuses config... and there was a mistake : my ext.oscillator's freq. is 16Mhz... but there was set the INT.OSC.1MGhz....
So now may it's working but now the AVR 4 Studio was stopped.... :D... incredible...

btw, Thank you guys for the help :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top