PDA

View Full Version : UART Problem. Atmega16, Codevision AVR



mk
07-08-2009, 01:35 PM
I'm using CodevisionAVR to program Atmega16 and connect it to my PC via UART.
I'm using Internal Clock at 4Mhz, and 9600bps, 8 data bit, 1 stop bit, no parity settings.

Ive gone through the previous posts and have inferred that internal clocks can sometimes be quite unreliable and we should use external clocks.

But, my MCU is connected to the PC, and is transmitting data. That makes me think that clock is not a problem But the problem is that, whatever data I send, its recieved as 'X'.

I used,

printf("Using UART on Mega16");

But on the terminal window of CodevisionAVR, it recieved it as a continous string of x (when mode is ASCII), and same on hyperterminal.

Can someone tell me where I'm going wrong?

vikas
07-08-2009, 02:19 PM
What is the ACSII value , Is it really that of X.

Try using a software called Termite for Serial port stuff , it better than HT And free.

IN Termite it will show if non printable characters are coming through ( shown in brackets with their value ).

Mostly i think that ur UART speed is causing the issues.

Try shorting RX-TX on ur Avr and put some data in HT/Termite is it being echoed back correctly ?

debu
07-08-2009, 04:59 PM
@mk: Apart from vikas's suggestions, Post you code and the fusebits (I suspect that they are the problem) that you are using.

Regards,

Debu :)

mk
07-08-2009, 11:38 PM
Thanks for the replies.

@Vikas: I downloaded Termite. And here is the result Im getting for the code segment,

printf("Testing UART of Atmega16");
delay_ms(200);

Result: [00]x[1E]x[1E][00]x[00]x[1E]xx[00][00]x[00]x[1E][00][00]x[00][00][00]xx[1E]x[00]x[00][00][00]x[1E]x[1E][00]x[00]x[1E]xx[00][00]x[00]x[1E][00][00]x[00][00][00]xx[1E]x[00]x[00][00]



Ive tried loopback test using Termite, And HT. Its working fine without the MCU.

Ive reduced the baud rate to 1200bps, but still the problem is same as before.

@debu: Code-




/************************************************** ***
This program was produced by the
CodeWizardAVR V2.03.3 Evaluation
Automatic Program Generator
Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date : 7/8/2009
Author : Freeware, for evaluation and non-commercial use only
Company :
Comments:


Chip type : ATmega16
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/

#include <mega16.h>
#include <delay.h>

// Standard Input/Output functions
#include <stdio.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 1200
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0xCF;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

while (1)
{
printf("Testing UART of Atmega16");
delay_ms(200);
};
}






Fuse Bit Settings:

(I cant paste the image here, so i'm writing only the programmed bits.)

Programmed-

JTAGEN
BOOTSZ1
BOOTSZ0
SUT0
CKSEL3
CKSEL2
CKSEL1

Unprogrammed-

BOOTLOCK 12, 11, 02, 01
LOCK 2,1
OCDEN
CKOPT
EESAVE
BOOTRST
BODLEVEL
BODEN
SUT1
CKSEL0

These are same as default, as I didn't touch these settings.

[/img]

debu
07-09-2009, 08:42 AM
@mk: Caught your problem! As I suspected the fuse settings are wrong. You have enabled the Internal 1 MHz oscillator. The baud calculation for the registers is according to 4 MHz.

To rectify this problem, Program CLKSEL2 and CLKSEL3, and leave CLKSEL1 and CLKSEL0 unprogrammed. Leave the rest as you have mentioned. Do post the result.

Hope this helps,

Regards,

Debu :)

mk
07-09-2009, 11:52 AM
Are you sure that I should do this? Because I'm not using any external clock as mentioned earlier. And I've set the internal clock frequency as 4MHz from the CodeVision AVR code wizard dialog box.

I hope that resetting the fuses as you told would not leave my MCU expecting external clock as I'm not using it.

debu
07-09-2009, 12:04 PM
@mk: Read the ATMEGA16 datasheet. Page 29.

Regards,

Debu

mk
07-09-2009, 12:13 PM
Hey thats gr8! You're a genious. I changed my clock settings from the menu option from 4MHz to 1 MHz, and its working fine now!

But that leaves me confused. Earlier when I had set my UART to run according to 4MHz, then also I'd set 4 MHz in the same place where I set it to 1MHz now. Does codevision not automaticly set the Fuse Bits according to what range for Internal Clock we have set?

karthikprasad
07-10-2009, 07:39 AM
Nope the codevision dialog uses the clock dialog only for calculating the values and does not actually set em

U have to use another program for setting the fusebits
this will helpu calculate the fusebits
http://www.engbedded.com/cgi-bin/fcx.cgi?P_PREV=&P=ATmega32

use another software like ponyprog for setting the fusebits

mk
07-11-2009, 11:47 AM
Hey, thanks a lot to you all! It worked!