Problem with Serial Communication

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

Hi,

I am trying to do a simple serial communication interface between ATmega16 and my laptop. Since my laptop does not have a serial port, I am using a Bafo USB-serial adapter cable.

I am using the following circuit as seen in the attachment.

I am using Bray's terminal to send characters to my AVR. But i'm seeing that nothing is echoed back to the Bray's terminal. I double checked the code and the circuit connections and everything was just the way it should be. I am running my ATmega16 on a 16MHz external crystal and I also verified that the clock frequency is correct through this small program to toggle my LED every second.

/**
 * Program to toggle LED at 1 Hz or once every second for an input clock freq. of 16 MHz
 *
 * Timer resolution = Prescale/Input frequency
 * Input freq. = 16 MHz
 *
 * Target timer count = (((1/Target freq.)/Timer resolution) - 1)
 * 
 * Here target freq. = 1 Hz or once every second
 *
 */




#include 

int main(void) 
{
	DDRA |= _BV(PA4);
	TCCR1B |= _BV(CS12) | _BV(CS10);	//Use Prescaler of 1024

	while(1)
	{
		if(TCNT1 >= 15624)
		{
			PORTA ^= _BV(PA4);
			TCNT1 = 0;
		}
	}
}

The code for the serial communication is as below:

#include 
//#include "pinsconfig.h"
#include "lcd.h"

#define F_CPU 16000000UL
#define serial_port PORTD
#define USART_BAUD_RATE 9600
#define BAUD_PRESCALE ((F_CPU / (USART_BAUD_RATE * 16UL)) - 1)

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

int main(void) {
	
	lcd_init(LCD_DISP_ON);	//initialize display, cursor off

	lcd_clrscr();
	PORTA = 0x00;
	DDRA |= _BV(PA4);
	DDRA |= _BV(PA5);
	unsigned char receivedByte;
	usart_init();
	while(1) {
		lcd_gotoxy(0,0);
		lcd_putc('X');

		PORTA |= _BV(PA5);

		receivedByte = usart_getch();	//get data from serial port

		usart_putch(receivedByte);	//send data back to PC

		lcd_gotoxy(0,0);
		lcd_putc(receivedByte);

		lcd_command(LCD_DISP_ON_CURSOR);	//turn on cursor

		lcd_command(LCD_DISP_ON);
	}
	return 0;
}

void usart_init(void) 
{
	UCSRB |= (1 << RXEN) | (1 << TXEN);	//Turn on transmission and reception

	UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); //Use 8-bit character sizes

	//UBRR value = 103 which is 0x67 is hexadecimal

	UBRRH = (BAUD_PRESCALE >> 8);

	UBRRL = BAUD_PRESCALE;
}

void usart_putch(unsigned char send) {
	
	while(!(UCSRA & (1 << UDRE)));	//Do nothing until UDR is ready
										//for more data to be written to it
	UDR = send;	//send the byte
}

unsigned int usart_getch(void) {
	
	while(!(UCSRA & (1 << RXC)));	//Do nothing until data has been received and is ready to be read from UDR

	return UDR;	//return the byte
}

I thought maybe the MAX232 chip may not be working, so I checked the voltages at pin 2 and 6 respectively. At pin 2 I get 3.16 Volts and at pin 6 I get 1.5 VOlts. Since these are way below the required +/- 9 volts range I replaced the MAX232 with another on and I still see the same voltages.

What could be the problem?

Thanks,
Sumair

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

Show us your wiring diagram for the chip.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Something doesn't sound right with the max232 circuit. I'd suggest you double check your circuit with the max232 datasheet (note all max232s are not the same - use the datasheet from the actual chip manufacturer) then check your wiring.

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

I am using MAX232N chip and i checked the wiring diagram in the datasheet and it matches with the circuit diagram provided earlier. I have attached the picture taken from the MAX232N datasheet.

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

Quote:

with the circuit diagram provided earlier.

Where?

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

Oops my bad, i think there was some problem with the attachments. I'm re-sending them again.

Attachment(s): 

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

The capacitor at pin 2 of the MAX is reversed.

PS: And where are the decoupling capacitors?

Stefan Ernst

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

The cap on pin 2 should go to ground. Are you wiring all this up on a proto board? Take a picture.

Imagecraft compiler user

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

Quote:
The capacitor at pin 2 of the MAX is reversed.

PS: And where are the decoupling capacitors?

I reversed the capacitor polarity at pin 2 and also added a decoupling capacitor between pin 16 and ground. In spite of this I'm not able to see any characters received in Bray's terminal.

Quote:
The cap on pin 2 should go to ground. Are you wiring all this up on a proto board?

Yes I'm wiring this up on my bread board. I have attached pictures of them.

Attachment(s): 

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

I'm using 1 microfarad/63 volts capacitors for the MAX232N and for the decoupling capacitor I'm using a 10 microfarad/63 volts capacitor.

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

Do a loop test.
1. Short pins 2,3 on DB9 (MAX232 disconnected). The terminal should echo typed characters.
2. Connect MAX back.
Short pins 11,12 on MAX232 (Avr disconnected). Again see if the terminal echoes chars.

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

ssyed_mcu wrote:
I am trying to do a simple serial communication interface between ATmega16 and my laptop. Since my laptop does not have a serial port, I am using a Bafo USB-serial adapter cable.

I am using Bray's terminal to send characters to my AVR. But i'm seeing that nothing is echoed back to the Bray's terminal.

Use a loop-back test (image attached) to verify that your cable and level translator are working correctly.

Edit:
Hey Visovian, you beat me by seconds.
Great minds think alike, and so do ours. :)

Attachment(s): 

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

It won't work, read below.

Yes, MAX232 pin 2 capacitor is connected wrong, and there are no bypass capacitors at all.

Also the 10uF capacitor on AVR pin 9 (reset, who knows) is overkill. Since your schematic is missing pin names, I won't start analyzing pin names to know if they are connected correctly.

But perhaps the most biggest problem is that you have not powered the analog half of the AVR with AVCC pin, so PORTA where your LCD is connected won't work. Your AVR may already be broken because only half of it was powered. So if you think something does not work properly, it might actually be broken.

And the LCD backlight is given 5V directly. I hope the backlight has internal current limit, or two things will happen: either it sucks so much current it brings the 5V supply voltage down, or if you have a lot of current available, the LCD backlight gets fried. Normally you would put a resistor to limit current to the backlight LEDs, to about few tens of milliamps depending on your display. I have used displays with maximum current limit near about 100mA, but one with red backlight could normally consume near 200mA so maximum was near 300mA.

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

Quote:
decoupling capacitor I'm using a 10 microfarad/63 volts capacitor.
Completely useless! You need 100nF, and NO bigger is NOT better.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:
Do a loop test.
1. Short pins 2,3 on DB9 (MAX232 disconnected). The terminal should echo typed characters.

I tried this and the terminal echoes back the characters.

Quote:
2. Connect MAX back.
Short pins 11,12 on MAX232 (Avr disconnected). Again see if the terminal echoes chars.

This time I don't see any characters echoed back in the terminal. Does this mean the MAX232 is not working?

Also I'm confused with the pin 2 configuration of the capacitor. The positive terminal of the capcitor is connected to pin 2 o MAX232 and the negative terminal of capacitor is connected to ground. I this correct?

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

Quote:
Also the 10uF capacitor on AVR pin 9 (reset, who knows) is overkill. Since your schematic is missing pin names, I won't start analyzing pin names to know if they are connected correctly.

The 10 uF decoupling capacitor is for the MAX232 and not the AVR. My AVR development board already has the necessary decoupling capacitors. Regarding the LCD, the development board comes with the LCD panel and 4 LEDs for debugging purpose. If you see my code I posted earlier, before I start the serial communication, I am sending a character 'X' to the LCD to test whether the LCD works, and it shows the 'X' which means the AVR and the LCD are working fine. Also please disregard the LCD wiring diagram in the circuit above as I am only using the USART communication part of that circuit.

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

Hi guys,

I finally figured out the problem. I had wired the circuit wrongly for the MAX232. After pin 8 I started counting the pin 9 on the top right hand side of the PDIP package chip instead of from below, which lead to wrong wiring of the chip. Fortunately, my MAX232 chip was not damaged. I'm able to communicate with the Brays terminal now.

I'm so embarrassed now. My apologies for this stupid mistake. Anyways thanks for all your help.

Regards,
Sumair