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

Discussion in 'Embedded Systems and Microcontrollers' started by Alasttt, Jan 19, 2016.

  1. Alasttt

    Thread Starter Member

    May 13, 2015
    68
    0
    I am trying to write a basic program to transmit character 1 to a pc. below is my code.
    Code (C):
    1.  
    2. #define F_CPU 20000000UL  // Clock Frequency
    3.  
    4. #include <inttypes.h>
    5. #include <avr/io.h>
    6. #include <avr/interrupt.h>
    7. #include <avr/sleep.h>
    8. #include <util/delay.h>
    9.  
    10.  
    11. #define Bd_Rate  9600UL // define baud
    12. #define Bd_Reg  (F_CPU / (16 * Bd_Rate)) - 1 // set baudrate value for UBRR
    13. #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.
    14.  
    15.  
    16.  
    17.  
    18. void Tx_Character (char data)
    19. {
    20.     while (!( UCSR0A & (1<<UDRE0)));                // wait while register is free
    21.     UDR0 = data;                                   // load data in the register
    22. }
    23.  
    24. int main (void)
    25. {
    26.     sei(); // enable global interrupts
    27.     UBRR0 = 129;   // set the baud rate
    28.     UCSR0B |= 1<<TXEN0 | 1<<TXCIE0 ; // enable transmitter and interuppt for usart
    29.  
    30.  
    31.     DDRD  = 0xFF;   // All outputs
    32.  
    33.    while(1){
    34.  
    35.       asm("nop");
    36.       Tx_Character('1');
    37.        
    38.    }
    39. }
    40.  
    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: Jan 20, 2016
  2. Picbuster

    Member

    Dec 2, 2013
    373
    50
    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.
     
  3. Alasttt

    Thread Starter Member

    May 13, 2015
    68
    0
    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
    ??.
     
  4. Picbuster

    Member

    Dec 2, 2013
    373
    50
    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.
     
  5. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,644
    759
    Use the letter "u".

    Edit : capital U /Edit
     
    Last edited: Jan 20, 2016
  6. SSV

    New Member

    Jan 19, 2016
    1
    0
    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?
     
Loading...