Basic Uart 0><0><0><0><0><0><0

Thread Starter

Alasttt

Joined May 13, 2015
68
I am trying to write a basic program to transmit character 1 to a pc. below is my code.
C:
#define F_CPU 20000000UL  // Clock Frequency

#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>


#define Bd_Rate  9600UL // define baud
#define Bd_Reg  (F_CPU / (16 * Bd_Rate)) - 1 // set baudrate value for UBRR
#define timeperiod (1/(F_CPU)) // at a clock of 20MHZ this gives time period of 0.05uS. For one bit of length 70us need the timer to = 70/0.05 = 1400.




void Tx_Character (char data)
{
    while (!( UCSR0A & (1<<UDRE0)));                // wait while register is free
    UDR0 = data;                                   // load data in the register
}

int main (void)
{
    sei(); // enable global interrupts
    UBRR0 = 129;   // set the baud rate
    UCSR0B |= 1<<TXEN0 | 1<<TXCIE0 ; // enable transmitter and interuppt for usart
 

    DDRD  = 0xFF;   // All outputs

   while(1){

      asm("nop");
      Tx_Character('1');
       
   }
}
I have set the 9600 baud rate on brays terminal and the correct stop bits data bits, by default the uC is 8 data bits no parity and 1 stop bit. All I get on brays is a continuous stream of 0><0><0><0><0><0><0><0><0><0><0

Any thoughts?, this is very basic and I am disappointed that I cant seem to get it working

MOD note: Added code tags.
 
Last edited by a moderator:

Picbuster

Joined Dec 2, 2013
1,047
This looks like a over or under run connect a scope and look at the signal.
next question what is yr receiver TTL or V24? and did you comply to signal levels.
If you have no scope try different receiver baud rates.
 

Thread Starter

Alasttt

Joined May 13, 2015
68
This looks like a over or under run connect a scope and look at the signal.
next question what is yr receiver TTL or V24? and did you comply to signal levels.
If you have no scope try different receiver baud rates.
A scope shows a square wave going high and low so the output Tx is correct. The chip i'm using to transmit is an atmega328p the receiver is the usb port. Still getting the same on brays
??.
 

Picbuster

Joined Dec 2, 2013
1,047
A serial ttl output can not be connected without other hw to usb port.
Use an alternating bit pattern like the v in stead of 1.
Your scope should show a pulse train not a square wave. (take a 0,1ms time base and trigger on the dwn going signal.
If you still get this square wave then force UDR0=118; // char v in the register
and see what happened.
 

SSV

Joined Jan 19, 2016
1
You did the interrupt enable . But you have not function for it.

UCSR0B |= 1<<TXEN0 | 1<<TXCIE0 ;

Can you describe schematic. What device are you using?
 
Top