Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ATmega328P serial communication problem

Status
Not open for further replies.

Cantafford

Member
Hello,

I wrote a code that's supposed to send some serial data via the USART of ATmega328P to a virtual serial port which I emulated in a program called VSP.
The serial port works just fine I have tested it with MATLAB. I have set the baud rates to 9600(both in the code with the given formula in the datasheet and in Proteus simulater under the port's proprieties).

This is the code which is supposed to send the number '8' via the port with a delay of 1 second between transmissions:

Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>

#define F_CPU 8000000
#define BAUD 9600
#define BRC ( (F_CPU/16/BAUD) -1)     // baud rate calculator

#include <util/delay.h>

int main(void)
{
    UBRR0H = (BRC >> 8); // defining the baud rate
    UBRR0L = BRC;
  
    UCSR0B = (1 << TXEN0);
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // number of bits transciever and reciever use
  
  

    while(1)
    {
        UDR0 = '8'; // UDR0 is used for transmiting and recieving data(write to it or read from it)
        _delay_ms(1000);
      
    }
}

This is how I connected it in Proteus:
4qmmc5.png


I can't see character 8 which is supposed to be send on the virtual terminal. Am I doing something wrong in the code or in the interfacing of the virtual terminal with the PIC in Proteus? Please help me identify the issue if possible. Thank you very much.
 
Try this -
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000    // CPU speed - always define prior to including util/delay.h
#include <util/delay.h>

#define BAUD 9600
#define BRC ((F_CPU / BAUD) / 8) - 1

int main(void){
    UBRR0H = 0x00;    // set bit rate
    UBRR0L = BRC;
    UCSR0A = 0b00000010;    // double bit rate enabled
    UCSR0B = 0b00001000;    // TxD enable
    UCSR0C = 0b00000110;    // asynchronous mode, 8N1 frame format

    while(1){
        UDR0 = '8';    // send '8'
        _delay_ms(1000);    // 1 second wait
    }
    return 0;
}

Actual bit rate is 9615 as there is no way to get an even 9600 with bit divisible crystals (most devices that operate at 9600bps use an oscillator speed of 11.059MHz or an even multiple of). But this only results in a 0.1% bit error rate.
 
Last edited:
Did you ever get this working?
Hey. After you given me these instructions I tried the solution briefly and it didn't work but maybe I have not done everything correct(I've tested it in a hurry then haven't had time to look at it again since I was busy with other projects and some exams). I will let you know when I test it again. Again thank you for your solution and sorry for the late reply.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top