Author Topic: USART Help Atmega8  (Read 4823 times)

0 Members and 1 Guest are viewing this topic.

Offline HePETopic starter

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
USART Help Atmega8
« on: February 18, 2009, 02:09:46 PM »
Hi Guys,

Can somebody give me a kickstart on using the USART going to Hyperteminal ? A simple working code with initialization with a transmit function( transmitting an int value to hyperterminal). Comments on te code  will be highly appreciated.

I saw the tutorials that we have here but it's more towards the $50 robot.

Specifics:
Atmega8 @ 1 Mhz
Hyperterminal @ 9600

This will save me so much time.Thanks in advance.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: USART Help Atmega8
« Reply #1 on: February 18, 2009, 02:35:54 PM »
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline HePETopic starter

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
Re: USART Help Atmega8
« Reply #2 on: February 18, 2009, 03:21:37 PM »
yes I did. But I want to learn how to initialize the registers from scratch.

I would just like to have some sort of working experimental code. Just the bare essentials initialization, a polling main, with a transmit function that I can use to send data to H-teminal. Basically a code that I will run and then alter later. I'll be altering it for the means of studying (i.e. like... what did I do wrong ... why doesn't it work now) 

Thanks

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: USART Help Atmega8
« Reply #3 on: February 19, 2009, 01:51:14 AM »
The code I posted over here: http://www.societyofrobots.com/robotforum/index.php?topic=7029.0 with Webbot's fix (follow the posts) is the absolute minimum code you need to write to initialise and use the USART. You'll need to read the Datasheet and look at the used registers to figure out how you configure things for other baud rates, frame formats, other USART ports (I'm using USART port 1 because I'm using it for the Axon and the Axon has the USB connection on USART1)

It's a lot of leg-work if you don't want to use ready-made library functions, be prepared (ie: AvrLib + rprintf)!

Offline superchiku

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
Re: USART Help Atmega8
« Reply #4 on: February 19, 2009, 03:04:56 AM »
i have got a sample code for u which will work on atmega 16 try to read the code and find out wat are the registers used and in which way they are used...ull get ur answer...

Code: [Select]

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

# include<util/delay.h>
# include<avr/interrupt.h>
# include<stdlib.h>
# include<string.h>
# include<avr/pgmspace.h>



#define USART_BAUDRATE 38400
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL)))- 1)


 char u[11]="adcvalue\r\n";
 char *d;
 char b[10];
 char *p;
 
 unsigned int a;

void adcinitialize();
void uartinitialize();
void adcgetvalue();
void transferusart();
void printvalue(char *);

 int main(void)
  {
   adcinitialize();
   uartinitialize();
   while(1)
    {
adcgetvalue();
     
p=itoa(a,b,10);

strcat_P(b,PSTR("\r\n"));

d=&u[0];

transferusart();

     
     }       
   

   return(0);
   }
 
 
 
 void adcinitialize()
  {
   
  ADCSRA|=_BV(7)|_BV(ADPS2)|_BV(ADPS1);
  ADMUX=0x60;
  ADCSRA|=(1<<ADSC);
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF);
  TCCR1B|=(1<<CS12);
 
  }
 
  void uartinitialize()
   {
    UCSRB= (1<<RXEN) | (1<<TXEN);
    UCSRC= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
    UBRRL=BAUD_PRESCALE;
UBRRH=(BAUD_PRESCALE>>8);
   }
void adcgetvalue()
 {
 
   ADMUX=0x61;
   ADCSRA|=(1<<ADSC);
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF);
  a=ADCH;
 }
 
void transferusart()
 {
   
   while (*d != '\0')
    {

     while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
      UDR =*d ; // Echo back the received byte back to the computer
      d++;
}
TCNT1=0;
while(TCNT1<=60000){};
TCNT1=0;
 printvalue(p);
 }
 
 void printvalue(char *l)
  {
   
   while (*l != 0x00)
    {
 

  while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
      UDR =*l ; // Echo back the received byte back to the computer
     
     
     l++;
    }
TCNT1=0;
while(TCNT1<=60000){};
TCNT1=0;
  }
 

   


JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

 


Get Your Ad Here