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 * */ #includeint 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