PDA

View Full Version : AVR serial port problem



yogi
02-13-2006, 06:31 AM
I got some probs when i pgm serial port. I use atmega32,8MHz,9600bps. I just want to transfer byte from PC to uC and uC to PC. I don't understand why this code is not working. I used WinAVR, where it compiled sucessfully. If you have any working code plz mail me on njbibin@yahoo.co.in



#include <avr/io.h>
void init_ports(void);

void init_usart(void);
main(void)
{
int a;
init_ports();
init_usart();//for initializing usart
while(1)
{
UDR=0X45;
a=UDR;
PORTB=a;
}

return(0);
}


void init_ports(void)
{
DDRB=0XFF; /*setting Portb for output*/
PORTB=0XFF; /*firt make all 1 to check whether data reached or not*/
DDRC=0XFF;//setting PortC for output
PORTC=0X00;//putting all 0 to port C
}


void init_usart(void)
{
UCSRB = 0x00; //disable while setting baud rate
//setting the baud rate 9600 baud=fsc/(16*baud)-1
UBRRH=0X00;
UBRRL=51;
UCSRA = 0x00;
UCSRC = 0x86;
//setting UCSRC
//UCSRC=0b10000011;/* acessing UCSRC,asynchronous,parity disable,1-stop bit,8-bit*/
//UCSRA=0X00; /*DISABLE U2X8*/
UCSRB=0b0001100;/* receiver and transmitter enabled*/
}


The strange things happend to me is that PORTB=0x00 and no data is coming to PORTB. Max232 is working fine. Computer serial port is working with a serial mouse.

My connections

computer uC
pin2 TXC
PIN3 RXD
PIN5 GND


eventhough i short txc=rxd of uC then also it is not working. Can anyone tell me the reason. If u have working code plz post it here. I just want transfer, no buffering, no interrupts. Thanks in advance


Bibin John
www.bibinjohn.tk

devpriya
02-13-2006, 10:11 AM
I dont know the solution to your problem but i am also working on the same thing so if you can paste your full circuit with the pin details of the DB9 or DB25 connector and MAX 232 included then I may try this out and tell you.

yogi
02-13-2006, 11:15 AM
Hi dev,

have u got serial port worked in WinAVR. Nothing in ckt, because i checked it, Max232 is surely working fine. But the probs i am finding with program, even i shorted transmitter and receiver then also it is not working. If you have succedded in this plz paste the code here. Plz paste also the computer program.

Bibin John
www.bibinjohn.tk

rao
02-13-2006, 11:49 AM
even I had the same problem with 8MHz serial UART transmission..

It is related to the UBRR.. the error rate with an 8MHz is high..

for 9600 bps, I used a 3.6864 MHz.. it gives a perfect round figure UBRR of 23 with error percentage of 0 whereas an 8 Mhz crystal with 9600 baud gives a UBRR of 51.0833333 i.e an error of 8.33 percentage.. too much for successful communication..

as it is visible in your code.. you ae using a round figure UBRR of 51.. that doesnt work.. too much deviation and error.. use 3.6864 Mhz crystal with UBRR set as 23 and it will work guarantee...



Rao

yogi
02-13-2006, 12:34 PM
Hi Rao,

In atmel datasheet they show only error of .2% not 8%. Is it is too much for serial communication. Thanks for ur help. first i had to find a crystal of that frequency in Allahabad(can't say i get). I red the RS-232, it is always reading 255. How can u make sure that microcontroller uart is working, i know u have good experience in it. Can u put some code here. Anyway i am going to change crystal.

Bibin John
www.bibinjohn.tk

rao
02-13-2006, 12:49 PM
If you have access to the CRO..... you can see +10/-10 waveform from serial port, entering MAX232.. and on the other side you can see corresponding 0/5V from the MAX getting into the uC and then you can see no action in the uC.. that makes sure MAX is working.. to check if uC UART is working.. for that you seriously need to get your UBRR right.. use the same code.. as my code for UART was in assembly.. not very different...

Rao

yogi
02-13-2006, 01:52 PM
i don't have a CRO , but i confirmed that max232 is working, bcz -9.65 from serial port is received as 5V and input 5V to max is giving -8.6V. From tht i infered tht max is working properly. Now i have to see for uC, for that i have to bring crystal. I will test it today. I used the program from www.beyondlogic .org to see and another terminal program, both showing 255 on display.

I know assembly also. Till now i was programming in assembly. But it is better to do C code because no application require that much speed and memory utilization. Can u put that assembly code.If you have the computer pgm u have used plz put it also.

Thanks for ur advice

Bibin John
www.bibinjohn.tk

yogi
02-14-2006, 08:40 PM
I had done the coding again with new crystal 3.6864Mhz

First time it worked properly. Next time when i restarted my system it is always reading 255. Can u give me some troubleshooting tips. uC is working fine. I used same pgm on uC as well as computer

yogi
02-15-2006, 06:09 AM
Hi Rao,

Now i changed crystal to 3.6864 and put UBRRL=23. Some numbers are receiving in my system, but tht data is not the data the original data. Sometime Turbo C is not reading properly. Here is tc pgm

/* Name : Sample Comm's Program - Polled Version - termpoll.c */
/* Written By : Craig Peacock <cpeacock@senet.com.au> */
/* Date : Saturday 22nd February 1997 */

/* Copyright 1997 CRAIG PEACOCK <cpeacock@senet.com.au> */

/* See http://www.senet.com.au/~cpeacock/serial1.htm */
/* For More Information */

#include <dos.h>
#include <stdio.h>
#include <conio.h>

#define PORT1 0x3F8

/* Defines Serial Ports Base Address */
/* COM1 0x3F8 */
/* COM2 0x2F8 */
/* COM3 0x3E8 */
/* COM4 0x2E8 */

void main(void)
{
int c;
int ch;
outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */

/* PORT 1 - Communication Settings */

outportb(PORT1 + 3 , 0x80); /* SET DLAB ON */
outportb(PORT1 + 0 , 0x0C); /* Set Baud rate - Divisor Latch Low Byte */
/* Default 0x03 = 38,400 BPS */
/* 0x01 = 115,200 BPS */
/* 0x02 = 57,600 BPS */
/* 0x06 = 19,200 BPS */
/* 0x0C = 9,600 BPS */
/* 0x18 = 4,800 BPS */
/* 0x30 = 2,400 BPS */
outportb(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */
outportb(PORT1 + 3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
outportb(PORT1 + 2 , 0xC7); /* FIFO Control Register */
outportb(PORT1 + 4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */

printf("\nSample Comm's Program. Press ESC to quit \n");

do { c = inportb(PORT1 + 5); /* Check to see if char has been */
/* received. */
if (c & 1) {ch = inportb(PORT1); /* If so, then get Char */
printf("%d\t",ch);
//delay(1);
} /* Print Char to Screen */


if (kbhit()){ch = getch(); /* If key pressed, get Char */
outportb(PORT1, ch);} /* Send Char to Serial Port */

} while (ch !=27); /* Quit when ESC (ASC 27) is pressed */
}





atmega32,9600,8-N-1

uc program
/* Program to test whether the usart of avr is working
f=8MHz,baud=9600, TXD=RXD(shorted), just send a
character and see it is receiving or not*/

#include <avr/io.h>
#include<avr/delay.h>
void init_ports(void);

void init_usart(void);
main(void)
{
int a;
init_ports();
init_usart();//for initializing usart
while(1)
{
UDR=0XAA;
while (!(UCSRA & _BV(UDRE)));
_delay_ms(500);
UDR=65;
PORTA=~(PORTA);

/*
while(!(UCSRA & _BV(RXC)));
PORTA=~(PORTA);
a=UDR;
PORTB=a;
_delay_ms(500);
*/
}
return(0);
}


void init_ports(void)
{
DDRB=0XFF; /*setting Portb for output*/
PORTB=0XFF; /*firt make all 1 to check whether data reached or not*/
DDRA=0XFF;//setting PortC for output
PORTA=0X00;//putting all 0 to port C
}


void init_usart(void)
{
UCSRB = 0x00; //disable while setting baud rate
//setting the baud rate 9600 baud=fsc/(16*baud)-1
UBRRH=0X00;
UBRRL=51;
UCSRA = 0x00;
UCSRC = 0x86;
//setting UCSRC
UCSRC=0b10000011;/* acessing UCSRC,asynchronous,parity disable,1-stop bit,8-bit*/
//UCSRA=0X00; /*DISABLE U2X8*/
UCSRB=0b0001100;/* receiver and transmitter enabled*/
}


can u now tell wht is the probs. I am getting 0,128,128,0,0,0. There is some probs with synchronisation. I used one other software also. It also reads different from the original data.

yogi
02-17-2006, 03:23 AM
atlast i have done it

The problem was fuse settings. Actually my uC was using internal 1Mhz crystal. I thought it was external crystal. I changed baud=2400 8-n-1 and ubrr=25.

Then it worked. Thanks for all your helps

yogi
08-06-2006, 02:53 PM
In this link I have put all the troubleshooting tips for serial port
http://booksbybibin.14.forumer.com/viewtopic.php?t=119

if you want to do fuse bits for serial port communication, here is wht i have done
http://booksbybibin.14.forumer.com/viewtopic.php?t=106

Bibin John
www.bibinjohn.tk