Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] USART for AtMega32 not working . please help

Status
Not open for further replies.

rahulkalpsts107

Newbie level 4
Joined
May 21, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,336
Hi All,

I have an issue with UART which i have been struggling for a long time .
None of my uart messages are displayed . I have interfaced a serial lcd that accepts incoming uart at 9600bps

This is my code :
Code:
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include<avr/io.h>					//HEADER FILE FOR AVR INPUT OUTPUT
#include<compat/deprecated.h>		//HEADER FILE FOR FUNCTIONS LIKE SBI AND CBI
#include<util/delay.h>				//HEADER FILE FOR DELAY

/*----------------------------------------------------------------
-----------------MAIN PROGRAM-------------------------------------
-----------------------------------------------------------------*/
#include <avr/io.h> 
#include <avr/interrupt.h> 
#include <stdio.h> 


#ifndef UART_H 
#define UART_H 

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


//No need to call this function directly, it's just here for printf(). 
//Outputs a single character to the UART when the transmit buffer is empty. 
//This is the function that we bind to printf so it can output the string 
//that printf generates. The FILE parameter is required by printf for behind- 
//the-scenes bookkeeping. *Note that we can write a function that will put a 
//single char anywhere we want -- parallel interface, SPI, I2C, etc.* 


//Set up the registers that control the UART and bind put_uart_char 
//as our output function. 

#endif
int put_uart_char(char c, FILE *fd) 
{
  while (!(UCSRA & _BV(UDRE)));  //Wait for transmit buffer to become empty. 
  UDR = c; //Write the character to the UART. 
  return 0; 
}

void uart_init(int baud) 
{
  /*  Set baud rate  */
UBRRH = (unsigned char )(baud>>8);
UBRRL = (unsigned char )baud;
/*  Enable receiver and transmitter  */
UCSRB = (1<<RXEN)|(1<<TXEN);
/*  Set frame format: 8data, 1stop bit */
UCSRC = (1<<URSEL)|(3<<UCSZ0);
}

void USART_Transmit( unsigned char  data )
{
/*  Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/*  Put data into buffer, sends the data  */
UDR = data;
  
  //Call fdevopen. This is what binds printf to put_uart_char. The first 
  //parameter is the address of our function that will output a single 
  //character, the second parameter is an optional parameter that is 
  //used for get functions, ie. receiving a character from the UART. 
  //This is the function that uses malloc. 
  
  fdevopen(&put_uart_char, NULL); 
  
  //Enable the interrupts globally. 
  
  SREG |= _BV(SREG_I); 
}
int main(void)
{
	int i,temp;
	DDRB=0XFF;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//ALL OTHERS ARE OUTPUT
	
	DDRD=0XF0;						//SET DATA DIRECTION REGISTER
									//SET 1 for OUTPUT PORT
									//SET 0 FOR INPUT PORT
									//PD.1, PD.2 AND PD.3 ARE INPUT
									//ALL OTHERS ARE OUTPUT

	sbi(PORTD,0);					//ENABLE PULL UP FOR SW1
	sbi(PORTD,1);					//ENABLE PULL UP FOR SW2
	sbi(PORTD,2);					//ENABLE PULL UP FOR SWITCH INT0
	sbi(PORTD,3);					//ENABLE PULL UP FOR SWITCH INT1
	int k = 200;
	uart_init(25);
	_delay_ms(400);
	while(1)
	{
		
		
			PORTB = 0x0;
			_delay_ms(400);
			PORTB = 0xFF;

		
	k--;
	put_uart_char(0xCA , NULL);

	}
	while(1);
	return(0);
}

My Fuse settings currently are HIGH : 0x99 LOW : 0xEE

The Serial LCD just takes vcc,gnd,and rxd . My TXD is connected to its RXD and my circuit is at +5v , even serial lcd is configured to work at that level.
This is configured for external 4Mhz clock during build time .

Please help me with this . I really dont know what the issue is.
Thankyou

Regards,
Rahul
 

A schematic of what you have explained would simplify the matters. My advice would be to make a proteus simulation an test your code there. I am a little confused by your explanation of using USART to communicate with a lcd so i cannot help you more without a schematic.

- - - Updated - - -

For reference on using USART consult the following link
http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers/
http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers-reading-and-writing-data/
 

I'm Interfacing a development board to a serial LCD . This serial LCD works on uart .
The board has 4 leads RXD,TXD,GND,VCC for usart. The board operates on 5v.
I Have interfaced the Serial LCD to my Board . I Have posted a pic of the Connections .
First i tried connecting to my pc via Rs232 - usb . Using a terminal software i was not able to see anything on the Serial LCD .
Even using the microcontroller . I sometimes see junk data basically by guessing the clock speed . At 4MHZ External clock . The UBBR value is 25 .
I'm using JTAG ICE to burn the software .

I Have also used some UART Libraries but could not actually get it working .

Any insight on proteus simulation ?? IMG_2277.JPG

Regards,
Rahul R
 

This is the way I initialize my USART on ATMEGA128
may be you miss :
Code:
// Define baud rate
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

void usart_init( unsigned int ubrr )
{
	/* Set baud rate */

	UBRR0H = (unsigned char)(ubrr>>8);
	UBRR0L = (unsigned char)ubrr;
	/* Enable receiver and transmitter */
	UCSR0B = (1<<RXEN)|(1<<TXEN);
	/* Set frame format: 8data, 2stop bit */
	UCSR0C = (1<<USBS)|(3<<UCSZ0);
}
 

Does your Serial LCD takes TTL data on TXD and RXD pins or RS232 data. If RS232 then you need a MAX232 between uC and Serial LCD RxD and TxD pins. If TTL, then you can Connect Rx and Tx pins of uC to TxD and RxD pins of Serial LCD. Can you post datasheet of your LCD module? Both your uC and LCD should work at the same voltage.
 

Shall i give you the link ?
Since i dont have any pdf . I bought these things online

- - - Updated - - -

Thanks for the reply .
Serial LCd http://robokits.co.in/shop/index.php?main_page=product_info&cPath=6_64&products_id=191
uC http://robokits.co.in/shop/index.php?main_page=product_info&cPath=6&products_id=14

Also just see the picture posted for more details above.

Regards,
Rahul R
 

Any one on this .. please help ??

- - - Updated - - -

Well it looks like they work on TTL so there is no need of rs232 . But can you check in the below url just to be sure ..
 

Finally i got it to work . At UBBR value 29 i was able to display 0 - 9 . The clock rating is same at 4mhz . but however after every writes , i need to give __delay__ms(1020) . otherwise i'm seeing junk .
But its not stable .. only at sometimes i see all the values printed
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top