m0v3
Posts: 1
Joined: Thu Nov 29, 2012 11:56 pm

uart <> atmega32

Fri Jan 25, 2013 3:43 am

Hey guys. To start, i'm kinda noob on electronics and stuff, so explain everything thoroughly if you can.

So, I wan't to connect RPi to Atmega32 via UART. I have RPi with minicom, set on 9600 baud rate 8 bit, no parity, 1 stop bit, no flow control hardware/software. Atmega ( working on 3.3v ) - 8Mhz clock 9600 baud 8 bit, no parity, 1 stop bit. Connected RPi's 8 pin (TX) to Atmega's 14 (RX), and RPi's 10 to Atm 15. Fuses - Low ( 0xE4 ) High ( 0xD9 ). Code below. Everytime I start everything, all I'm getting is random chars. What I'm doing wrong ?
Atmega's code:

Code: Select all

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

#define F_CPU 8000000UL
#include <util/delay.h>

void USARTInit(uint16_t ubrr_value)
{

   UBRRL = ubrr_value;
   UBRRH = (ubrr_value>>8);

   /*
   >> Asynchronous mode
   >> No Parity
   >> 1 StopBit
   >> char size 8
   */

   UCSRC=(1<<URSEL)|(3<<UCSZ0);

   UCSRB=(1<<RXEN)|(1<<TXEN);


}

char USARTReadChar()
{
	while(!(UCSRA & (1<<RXC)))
	{
		//Do nothing
	}
	return UDR;
}
void USARTWriteChar(char data)
{
	while(!(UCSRA & (1<<UDRE)))
	{
		//Do nothing
	}
	UDR=data;
}

void main()
{
	char data;
	USARTInit(51);		//UBRR = 51
	while(1)
	{
		data=USARTReadChar();
		USARTWriteChar('data');
	}
}

stedew
Posts: 4
Joined: Sun Oct 21, 2012 9:37 am

Re: uart <> atmega32

Sun Jan 27, 2013 9:07 pm

Hmm,
I don't know but maybe your question is a AVR one. in that case you can better ask it on thier forum (avrfreaks). A working fun example is this one (i just scratched it together)

Code: Select all

#include <avr/io.h>
#include <inttypes.h>
#include "UART.c" // "Library from @Peter Fleury "
//#define F_CPU 8000000UL		//Please assign in global symbol for GCC
#include <util/delay.h>			// Lib from avr Libc

#define BAUD 9600 //this is the baud rate that we want 
#define MYUBRR (((((F_CPU * 10) / (16L * BAUD)) + 5) / 10) - 1)//Optimized formula (works for mega32)

#define WithInterrupts

void USARTInit(uint16_t ubrr_value)
{

   UBRRL = ubrr_value;
   UBRRH = (ubrr_value>>8);

   /*
   >> Asynchronous mode
   >> No Parity
   >> 1 StopBit
   >> char size 8
   */

   UCSRC=(1<<URSEL)|(3<<UCSZ0);

   UCSRB=(1<<RXEN)|(1<<TXEN);


}

char USARTReadChar()
{
   while(!(UCSRA & (1<<RXC)))
   {
      //Do nothing
   }
   return UDR;
}
void USARTWriteChar(char data)
{
   while(!(UCSRA & (1<<UDRE)))
   {
      //Do nothing
   }
   UDR=data;
}

int main()
{

	uint8_t temp=0;
	uint8_t data =0;
   
#ifdef WithInterrupts
	unsigned int c;
   uart_init(MYUBRR);
   uart_flush();
   sei();
#else
	USARTInit(MYUBRR);      //UBRR = 51
#endif  
   

   while(1)
   {

#ifdef WithInterrupts
	if (uart_available()!=0)
	{
		//Char in buffer echo it ...error are not tested here 
		data = uart_getc();
		uart_putc(data + temp);
		
	} 
	else
	{
		//Nothing to do 
	}
	if (temp <0x0f)
	{
		temp++;
	}else
	{
		temp=0;
	}

		  		
	PORTB =~data; //Make a visual on port B
	_delay_ms(100);

	 
#else


      data=USARTReadChar();//µcontroller will drop dead until something received
	  USARTWriteChar(data + temp);

	  		if (temp <0x0f)
	  		{
		  		temp++;
	  		}else
	  		{
		  		temp=0;
	  		}

	  		PORTB =~temp; //Make a visual on port B 
	  		_delay_ms(100);

#endif 
   }
   
  
}
try this it will work (you need the uartlib from peter fleury ) but i suppose you will manage..

Return to “Interfacing (DSI, CSI, I2C, etc.)”