I didn't receive what I'm expecting via UART communication

46 views Asked by At

I'm trying to communicate my ATmega32A with my pc via UART communication using USB to TTL UART Uploader Module CH340.

I'm not reciveing or sending what I'm expecting, Can you please help me with that?

  • The Crystal I'm using is 16MHz

Here is my conections: enter image description here

Here is my code: Memory Map:

#define UCSRA (*(volatile unsigned char *) 0x2B)
#define RXC   7
#define TXC     6
#define UDRE    5
#define FE    4
#define DOR    3
#define PE    2
#define U2X    1
#define MPCM    0

#define UCSRB (*(volatile unsigned char *) 0x2A)
#define RXCIE   7
#define TXCIE     6
#define UDRIE    5
#define RXEN    4
#define TXEN    3
#define UCSZ2    2
#define RXB8    1
#define TXB8    0

#define UCSRC (*(volatile unsigned char *) 0x40)
#define URSEL   7
#define UMSEL     6
#define UPM1    5
#define UPM0    4
#define USBS    3
#define UCSZ1    2
#define UCSZ0    1
#define UCPOL    0

#define UBRRL (*(volatile unsigned char *) 0x29)
#define UBRRH (*(volatile unsigned char *) 0x40)

#define UDR (*(volatile unsigned char *) 0x2C)

UART Interface:

void UART_Init (void);
void UART_Send (u8 data);
u8 UART_Recive (void);

UART Prog:

#include UART_Interface_SubFolder_Path
#include LCD_Interface_SubFolder_Path

void UART_Init (void)
{
    //frame 1 STOP 8 Data No Parity
    UBRRL=103; //Baudrate 9600  16MHZ crystal
    CLR_BIT(UCSRA,U2X); // Normal speed
    SET_BIT(UCSRB,TXEN);
    SET_BIT(UCSRB,RXEN);    
}

void UART_Send (u8 data)
{
    while(!GET_BIT(UCSRA,UDRE));
    UDR=data;
    LCD_Cursor(1,5);
    LCD_Write_Char(UDR);
}

u8 UART_Recive (void)
{
    while(!GET_BIT(UCSRA,RXC));
    return UDR;
}

Main:

#include UART_Interface_Path_main
#include LCD_Interface_Path_main

#define F_CPU 666666              // 16MHZ prescaller8 =16000000/24
#include <util/delay.h>           

//#include <avr/io.h>

int main(void) {
    
    /*disable JTAG : To make PortC pins work*/
    MCUCSR |= (1 << JTD);
    MCUCSR |= (1 << JTD);
    
    u8 data1=61,data2;
    DIO_Inti();
    //OUTPUT_Inti ();
    LCD_Inti();
    UART_Init();
    
    LCD_Cursor(0,0);
    LCD_Write_String("abinno");
    LCD_Cursor(1,0);
    LCD_Write_String("Send   -Recive   ");
    
    while (1) {
        
        OUTPUT_Write(OUTPUT11,ON);
        _delay_ms(500);
        OUTPUT_Write(OUTPUT11,OFF);
        _delay_ms(500);
        
        
        UART_Send('A');
        
        data2 = UART_Recive();
        LCD_Cursor(1,15);
        LCD_Write_Char(data2);
    
    }

    return 0;
}

I tried to got the data in real simulation using Dockligth and that is what I got. enter image description here

enter image description here

I tried to got the data in proteus simulation and that is what I got. enter image description here

As you can see MCU send 'A' and virtual terminal got it (00 00 00) then I send Q in virtual terminal but I had to press enter to send it to MCU (71 0D) and the LCD didn't print Q beside Recive.

I don't know what is the problem can you please help.

0

There are 0 answers