Regarding USART !!

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

After going through the atmega16 datasheet , I was able to compile my UART program (given below)

#include

#define FOSC 1843200
#define BAUD 9600
#define MYUBBR FOSC/16/BAUD-1

void USART_Init(unsigned int ubrr)
{
	UBRRH = (unsigned char)(ubrr>>8);
	UBRRL = (unsigned char)ubrr;
	UCSRB = (1<<RXEN)|(1<<TXEN);
	// 8 data , 2 stop bit
	UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

void USART_Transmit(unsigned char data)
{
	while(!(UCSRA & (1<<UDRE)));
	UDR = data;
} 

unsigned char USART_Receive(void)
{
	while(!(UCSRA & (1<<RXC)));
	return UDR;
}

int main(void)
{
	USART_Init(MYUBBR);
	while(1)
	{
		USART_Transmit(USART_Receive());
	}
}

Now, when I use minicom to view the characters at baud rate 9600 I am unable to get the relevant characters. But when I tried it at 4800 baud rate I am able to get the same characters. Why is it so?

I was going through Dean's Tutorial their I found something related to this but it was not helping me much.

Thanks,

Anmol Kumar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What happens if you simply set MYUBRR to 11?

(just to check that it's not being incorretly calculated).

But as the CPU appears to be running at about half the speed you think it is I think there's a strong chance that it's not running at the 1.8432MHz you believe but is still operating on the 1MHz internal oscillator (though it must be running a bit slower than that in fact)

How have you got the fuses set?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello clawson,

What happens if you simply set MYUBRR to 11? Its showing the same behaviour ie running at 4800 baud rate.

As I was going through the datasheet of at mega16 its mentioned that the default clock frequency is 1 MHz but how can I use this one at 9600 baud rate (refer table page 168) it results in -7.0% error. But still I tried it at this clock ie 1 MHz its not working with 9600 baud rate but with 4800 its returning the same character (so may be some hint to you). So, I think its working again with 4800 baud rate.

How have you got the fuses set? Right now my fuse settings are as follows :-

avrdude: Device signature = 0x1e9403
avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 19
avrdude: erasing chip

avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 19
avrdude: safemode: Fuses OK

Have a look at my this avrdude configuration may be helpful to you (as I am unable to understand it right now waiting for the time to come)

avrdude: Version 5.5, compiled on Dec 18 2007 at 18:02:54
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

         System wide configuration file is "/usr/local/etc/avrdude.conf"
         User configuration file is "/root/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port            : /dev/ttyS0
         Using Programmer      : stk500v2
         AVR Part              : ATMEGA16
         Chip Erase delay      : 9000 us
         PAGEL                 : PD7
         BS2                   : PA0
         RESET disposition     : dedicated
         RETRY pulse           : SCK
         serial program mode   : yes
         parallel program mode : yes
         Timeout               : 200
         StabDelay             : 100
         CmdexeDelay           : 25
         SyncLoops             : 32
         ByteDelay             : 0
         PollIndex             : 3
         PollValue             : 0x53
         Memory Detail         :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom         4    10   128    0 no        512    4      0  9000  9000 0xff 0xff
           flash         33     6   128    0 yes     16384  128    128  4500  4500 0xff 0xff
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           calibration    0     0     0    0 no          4    0      0     0     0 0x00 0x00

         Programmer Type : STK500V2
         Description     : Atmel STK500 Version 2.x firmware
         Programmer Model: STK500
         Hardware Version: 2
         Firmware Version: 2.10
         Topcard         : Unknown
         Vtarget         : 5.1 V
         Varef           : 5.0 V
         Oscillator      : 3.686 MHz
         SCK period      : 10.9 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9403
avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 19
avrdude: erasing chip

avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 19
avrdude: safemode: Fuses OK

avrdude done.  Thank you.

Thanks again for quick reply of yours !!

Bye Bye

Anmol Kumar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK well if lfuse is E1 then it's still set to the default (see table 106 in the datasheet). It looks to me that for a 1.8M crystal you want CKSEL3..1 set to 110. So try setting lfuse to 0xED instead. IN fact to set a slow/safe startup time you probably also want the two SUT bits set. So maybe try 0xFD

As always, be cautious setting fuses - if you get it wrong you may "lose" the chip

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I used the default fuse setting of mega16 ie. E1 and 99. And after that I compiled my program at 4800, 8N1 and 1 MHz its working fine. Now let me know how can I try it at 9600 baud as I see in the table 68 (on page 168 of datasheet) it results in -7% error. I can use 9600 baud at UBRR value equals to 11,12,23,25,47 but for that I have to change to those matched frequecies in the table, Now let me know how can I set to those values , which fuse settings to use?

I used the above fuse setting ie. lbyte to 0xFD and hbyte to 0x99. at 1.8MHz I am getting the following as output on the minicom (local echo on ie. a corresponds to y and b to r ...)

Quote:
aybrcsdteuf~gwhhiijjkkllmmnnoopxqyrzsst|u}vvw.xxyyzz

see some of the characters are ok.

Surely you have some good suggestions to me , I am waiting for the same.

Thanks,

Anmol Kumar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Got it , working fine at 3.68 MHz. Have a look at my settings are they OK?

Any Suggestions !!

Thanks,

Anmol kumar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm lost - why do you need any more suggestions or verification of your settings if "working fine at 3.68 MHz" ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK thanks for replying. I need your guidance in future also...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

while interacting with wavecom gsm modem , when I am sending a message it works fine. But when I try to read the response of particular command (and try to send it through SMS) In the received message I am getting the command as well as the response.
But I need only the response to be received in SMS.

eg.

when I send at+cgmi and message received is at+cgmi WAVECOM MODEM.

My code :-

main(void)
{
	char send_sms[] = "at+cmgs=\"+919911000000\"\r\n";
	char manufacturer[] = "AT+CGMI\r\n";
	UARTInit(MYUBRR);
	
	SendString(manufacturer);
	read_usart();
	SendString(send_sms);
	delay();
	SendString(GetArray);
	UART_SendChar(26);
}

void read_usart(void) 
{
	int char_count = 0;
	int rx_chars = 160;
	u8 ch;
	memset(GetArray,'\0',160);  
	while(1)
	{
		ch = rxUART();
		if(char_count