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] Serial communication using USART

Status
Not open for further replies.

iVenky

Advanced Member level 2
Advanced Member level 2
Joined
Jul 11, 2011
Messages
584
Helped
37
Reputation
76
Reaction score
35
Trophy points
1,318
Location
College Station, Texas
Activity points
6,124
I am using Atmega 16. Here's the circuit diagram.+


I have used HyperTerminal software to send the data from my computer. ( I followed as it was given in this video:
AVR Microcontroller (Atmega16) Serial Communication (USART) - YouTube )

Here's the code



// Program to receive data from USART and displaying it on LCD
/*
Receive data from serial port and display it on LCD
LCD DATA port----PORT A
ctrl port------PORT B
rs-------PB0
rw-------PB1
en-------PB2
using external clock frequency 12MHz
*/

#define F_CPU 12000000UL
#define USART_BAUDRATE 9600 // Baud Rate value
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

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

#define LCD_DATA PORTA //LCD data port

#define ctrl PORTC
#define en PC2 // enable signal
#define rw PC1 // read/write signal
#define rs PC0 // register select signal

void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void LCD_clear();

void usart_init();
void usart_putch(unsigned char send);
unsigned int usart_getch();

int main()
{
unsigned char value;
DDRA=0xff; // LCD_DATA port as output port
DDRC=0x07;
_delay_ms(100); // signal as out put
init_LCD(); //initialization of LCD
_delay_ms(50); // delay of 50 milli seconds
usart_init(); // initialization of USART
while(1)
{
value=usart_getch(); // Call a function to get data from serial port
LCD_cmd(0xC0); // to go in second line and zeroth position on LCD
LCD_write(value); // write data to LCD
}
return 0;
}

void init_LCD(void)
{
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(50);

LCD_cmd(0x01); // clear LCD
_delay_ms(50);

LCD_cmd(0x0E); // cursor ON
_delay_ms(50);

LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position
_delay_ms(50);
return;
}

void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(50);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}

void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(50);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}

void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes

UBRRL = BAUD_PRESCALE;
// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}

unsigned int usart_getch()
{
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data has been received and is ready to be read from UDR
return(UDR); // return the byte
}



I see the cursor in the LCD screen so I guess there is no problem with the lcd code but the problem is that I can't send the data from my computer. When I type the data in HyperTerminal nothing happens in the LCD screen. Where does the problem lie?
 

Read post #2 of the following thread to see if the problem lies on serial communication or not, using the loopback test:

https://www.edaboard.com/threads/222911/

I use a flat end screwdriver to implemet the loopback test, you just have to be a little careful not to touch other pins.


Hope that helped.
 

I checked this link but I couldn't fully understand.
https://www.reliableracing.com/downloads/LoopBackTest.pdf
I couldn't understand how you jump those pins. What is jump by the way? You have asked me to type in hyperterminal but what will happen after typing in hyperterminal?

---------- Post added at 09:22 ---------- Previous post was at 09:17 ----------

Hey if I get "connnected" at the bottom of the hyperterminal window does that mean my com1 is working perfectly?

Before I posted this question also, hyperterminal window showed connected.
So, where lies my problem?

---------- Post added at 09:23 ---------- Previous post was at 09:22 ----------

what do you think about my circuit? Is it okay? Especially the max232 part
 

The problem may be your operating frequency. With 12MHz you will get 0.2% error on 9600 baud. Change the crystal to 11.0592MHz which has 0% error.
 

if you see "connected" in the hyperterminal, then it means that hyperterminal is working fine , but still cant guarentee if it can communicate with other external devices.

So you have to test the serial driver inside the PC by doing a loop back test... in this you connect a serial cable to PC serial port and short pins 2 and 3. and type from keyboard in hyperterminal window.. if the pins tx and rx are working fine then it will print back the characters you type back onto the screen... this shows that the cable and the pc port are working properly and can be interfaced to external devices... the same setting of number of databits, number of stop bits, parity, has to be set in the external device same as hyperterminal settings...

the max232 data sheet shows hoe the capacitors have to be connected and the capacitor ranges from 1Uf to 10uF. sometimes the device uses a cross connection type to communicate with end device..

then if you feel the pc works properly then does not communicate with the external devices then you can change the tx and rx connection and one end and try to communicate...
 
  • Like
Reactions: iVenky

    iVenky

    Points: 2
    Helpful Answer Positive Rating
Can I short those two pins in the usual ways (connecting both of them in the bread board)?
 

No need to solder...take a screwdriver and touch pins 2 and 3 of the connector.... you can see the numbers written inside of the connector.....
 

Will anything happen if I short them using breadboard? Because I need to open the cover of db9 once again and in addition I need another person to hold the screwdriver.
 

You can do it with a screwdriver by yourself it is easy, as long you are careful. But yes, you can short it in any way you like, no problem.
 

I checked by shorting them but I didn't get the echo. I want to know where the problem lies whether com1 port or in db9. Can I check it directly with the com1 instead of db9?
 

Of course! Short the same pins on port and see what will happen...
 

When I type 'a' in the hyperterminal I am getting a on the screen. I mean I see only one 'a'. What should be the problem?

---------- Post added at 18:49 ---------- Previous post was at 18:48 ----------

I would be happy if you have some video about this.

---------- Post added at 18:49 ---------- Previous post was at 18:49 ----------

I would be happy if there is some video about loopback test. Thanks in advance.
 

iVenky said:
When I type 'a' in the hyperterminal I am getting a on the screen. I mean I see only one 'a'. What should be the problem?

If you have the local echo option disabled (check hyperteminal settings), then this is what you expect to see, it means that the serial communication is working fine.
 

I enabled the local echo option

---------- Post added at 19:03 ---------- Previous post was at 19:02 ----------

I connected the 2 and 3 pins of the db9 using screwdriver.
 

When I type 'a' in the hyperterminal I am getting a on the screen. I mean I see only one 'a'. What should be the problem?I would be happy if there is some video about loopback test.

If you type one 'a' you should only see one 'a'.

If you type just 'ab', what do you see? It should be just 'ab'.

In other words you should see exactly what you type, no more, no less.

You should also make sure you have NO flow control or "None" selected when configuring the serial port in Hyperterminal.

Here is a quick guide on RS-232 Loopback Testing:



**broken link removed**


BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
is there any other way to check if com1 port is working fine?

---------- Post added at 19:04 ---------- Previous post was at 19:03 ----------

Bigdogguru I had enabled the local echo option.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top