USART TRANSMITION PROBLEM

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

I am stuck in the simplest program. is there any mistake in code.I am sending but don't know what am i getting, plz see attachment to see what i m getting

C code 

 

#ifndef F_CPU

#define F_CPU 1000000UL

#endif

 

#include <avr/io.h>

#include <util/delay.h>

 

#define is_bit_set(byte,bit) (byte & (1<<bit))

#define is_bit_clr(byte,bit) (!(byte & (1<<bit)))

#define BUAD 9600

#define BRC ((F_CPU/16/BUAD) - 1)

 

int main(void)

{

UBRR0H = (BRC >> 8);

UBRR0L =  BRC;

    UCSR0B = (1 << TXEN0); //ENABLE TX

UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // SET TO 8-BIT

    while (1) 

    {

UDR0='A';

 

_delay_ms(100);

    }

}

 

thanks for help

Attachment(s): 

This topic has a solution.
Last Edited: Fri. Jun 1, 2018 - 09:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which MCU are you using???

At 1MHZ and 9600 baud you need to set the U2X bit in UCSRA. And load UBBR0 with a value of 12.

Your current set up has a 7% error so you will more then likely receive garbage on the receiving end

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

This is a handy table when dealing with the USART

Attachment(s): 

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

hey thanks for your reply 

now I have set baud rate by command UBRR0 = 12; and UCSR0A &= ~(1 << U2X0);

and now i m getting this 

Attachment(s): 

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

Hassan6432 wrote:

UCSR0A &= ~(1 << U2X0);

That will clear the bit...not set it

Last Edited: Wed. May 30, 2018 - 11:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok so I changed my baud rate to 4800 by changing #define BUAD 9600 to #define BUAD 4800 and now it's working fine

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

MB I set it and now its working fine but can you tell me why this is happening. because if I change UBRR0=6 and clear U2X0 it again give my garbage value.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hassan6432 wrote:

MB I set it and now its working fine but can you tell me why this is happening. because if I change UBRR0=6 and clear U2X0 it again give my garbage value.

If you look at the table i posted it says that at 1MHZ clock speed UBBR0 = 6 and U2X0 cleared you have a -7% error rate in timing which is why you get the wrong characters in terminal.

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

thanks for your help can you please help me one more time smiley . now I doing it with the help of interrupt and it is transmitting nothing I set 4800 baud rate to reduce error.

i have also replaced UBRR0H = (BRC >> 8);  UBRR0L =  BRC; by UBRR0=12; results are same 

C code

 

 #ifndef F_CPU

 #define F_CPU 1000000UL

 #endif

 

 #include <avr/io.h>

 

 #include <avr/interrupt.h>

 

 #define BUAD 4800

 #define BRC ((F_CPU/16/BUAD) - 1)

 

 int main(void)

 {

 

 UBRR0H = (BRC >> 8);

 UBRR0L =  BRC;

 

 UCSR0B |= (1 << TXEN0) | (1<<TXCIE0); //ENABLE TX and interrupt

 UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00); // SET TO 8-BIT

 UCSR0A &= ~(1 << U2X0); // using /16 baud rate

 sei();

 while (1)

 {}

 }

 ISR(USART_TX_vect)

 {

  UDR0='A';  

 }

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

Hassan6432 wrote:
it is transmitting nothing

That's because there is nothing to trigger the ISR(), but once triggered, it looks like it will continuously send the letter "A" forever....

 

It looks like your writing code with out having a plan for what the code is to do!

Jim

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

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

thanks, I just write UDR0='A'; in main and its good to go.

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

The usart ISR triggers on the reception of data....so if you connected transmit and receive lines to your terminal program. It would send 'A' only after you sent a character from the terminal program to your MCU

EDIT SORRY just noticed you were using the TX ISR....the info above would be for the RX ISR

Last Edited: Thu. May 31, 2018 - 05:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for helping. can you please see this post also i m always stuck in uart

https://www.avrfreaks.net/forum/usart-multiple-byte-transmission-problem

Topic locked