PDA

View Full Version : problem with uart of atmega16



superchiku
04-01-2008, 07:08 PM
hey ppl recently i got a new sharp ir , show i decided calibrate it by using uart , but when i got into hyperterminal but the hyperterminal window remained completely blank ..

my code is like this

# include<stdio.h>
# include<avr/io.h>
# include<util/delay.h>
# include<avr/interrupt.h>

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






unsigned int a;

void adcinitialize();
void uartinitialize();
void adcgetvalue();

int main()
{
adcinitialize();
uartinitialize();
while(1)
{
adcgetvalue();
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR = a; // Send out the byte value in the variable "a"
while((UCSRA &(1<< UDRE))==0) {};

}


return(1);
}



void adcinitialize()
{

ADCSRA|=_BV(7)|_BV(2)|_BV(1);
ADMUX=0x60;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
a=ADCH;

}

void uartinitialize()
{
UCSRB|=(1<<RXEN)|(1<<TXEN);
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
UBRRL=BAUD_PRESCALE;
UBRRH=(BAUD_PRESCALE>>;
}
void adcgetvalue()
{

ADMUX=0x61;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
a=ADCH;
}


can anyone tell me whats the fault with my code , i need to know abt it as quickly as possible , oh yes my mcu is clocked at 8 mhz internal clock thanks..

yogi
04-01-2008, 11:42 PM
read this
http://www.roboticsindia.com/modules.php?name=Forums&file=viewtopic&t=1361

superchiku
04-02-2008, 02:20 AM
well this code is what i have written for uart communication plzz rectify my mistakes




# 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);
a=ADCH;

}

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++;
}
printvalue(p);
}

void printvalue(char *l)
{
while (pgm_read_byte(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++;
}
}

yogi
04-02-2008, 12:21 PM
Hi superchiku,

first check with small program to test the uart of avr and hyperterminal. Mostly the problem with fuse bits only. Your microcontroller will be working on 1Mhz. Take some time and go through the links.

superchiku
04-02-2008, 12:44 PM
well i checked the fuse bits, they are declared correctly hifh fuse byte as 0xd9 and low as 0xd4 which is the required setting for 8 mhz internal oscillator

elecrom
04-02-2008, 11:17 PM
Keep in mind that 8MHz oscillator drifts heavily with temperature and VCC. Don't rely on it for UART communication unless you are using very low baud rates (<9600). 1% error in baud rate can cause bit errors.

Instead of hyperterm you can use Terminal provided in CodeVisionAVR.

superchiku
04-03-2008, 12:47 AM
Thank u all so much for helping me, i fnally got the uart to work

but i have a doubt, my friend told me that if i would have used interrupts then this problem wouldn't have been there,but as far as i know that interrupts only stop normal execution when the interrupt signal is sensed by the microcontroller

since i have no knowledge how interrupts work i would like to ask u all one last question , How would have interrupts made my life easier in uart serial communication or adc or whatever..?

aachoo
04-03-2008, 04:44 PM
I have seen this new AVR manufacturer which claims to
be very inexpensive. I wonder if anybody has tried it
His website doesn't seems pretty interesting though

http://www.geocities.com/ash22jain