element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum USART Trigger not working
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 5 replies
  • Subscribers 392 subscribers
  • Views 1544 views
  • Users 0 members are here
  • usart
  • avr
  • trigger
  • atmega328
Related

USART Trigger not working

icongdo
icongdo over 10 years ago

I've been trying to debug this program. I am using an ATmega328p My USART trigger isn't getting called? Maybe I'm missing something?

Hardware using PuTTY to send message or simply echo 1> COM1

 

 

#include <avr/io.h>

#include <avr/interrupt.h>

#include <util/setbaud.h>

 

 

#ifndef BAUD                         

#define BAUD  9600                    

#endif

 

 

int main(void) {

 

  DDRB = 0x1;

 

 

  UCSR0B = (1 << TXEN0) | (1 << RXEN0) | ( 1 << RXCIE0 );

  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); 

 

 

  UBRR0H = UBRRH_VALUE;                      

  UBRR0L = UBRRL_VALUE;

 

  sei( );

          

  while (1) { }

  return (0);

}



 

ISR( USART_RX_vect ) {

  PORTB ^= 0x1;

}



  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to icongdo +1 verified
    OK, i did not look there so its good you have that covered one thought on the re-trigger is reading from the port in some way once the trigger has occured so that it will reset the trigger. without searching…
  • icongdo
    icongdo over 10 years ago in reply to Robert Peter Oakes +1
    Yes this is what I suspected. For future references. Writing directly to RCX0 flag will not stop the interrupt from reoccurring ADCSRA &= ~( 1 << RXC0 ); Taken from the data sheet: When interrupt-driven…
Parents
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    you dont seem to be setting a baud rate, when you copied the code you probably missed it

     

    try adding this

    #define MYUBRR ( ( F_CPU / ( BAUD * 8UL ) ) - 1 ) // datasheet ( f / 8b - 1 ) w/ u2x

    after the baud rate declaration

     

    and then

     

    UBRR0H = ( MYUBRR >> 8 );
    UBRR0L = MYUBRR;

     

    rather than what you have as the firrst thing you do in the main() routine

    you have:

      UBRR0H = UBRRH_VALUE;                     

      UBRR0L = UBRRL_VALUE;


    I dont see the values used to set the baud rate in you code being initialized anywhere (UBRRH_VALUE etc)


    you can also try setting the data double rate bit to a zero (Or 1) with this command

    UCSR0A |= ( 1 << U2X0 );


    hope this helps


    I got most of the info from a quick look here  http://www.avrfreaks.net/forum/arduino-uno-uart-problem


    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • icongdo
    0 icongdo over 10 years ago in reply to Robert Peter Oakes

    UBRRH_VALUE & UBRRL_VALUE are defined in <util/setbaud.h> http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__util__setbaud.html

    I actually got it to trigger ( must have been connection ) one time but after it doesn't anymore . Any thoughts?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to icongdo

    OK, i did not look there so its good you have that covered

     

    one thought on the re-trigger is reading from the port in some way once the trigger has occured so that it will reset the trigger. without searching through the data sheets, it is often the case that the recieved charcter will trigger the interupt but at this point further triggers are dissabled untill the character is read from the buffer

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • icongdo
    0 icongdo over 10 years ago in reply to Robert Peter Oakes

    Yes this is what I suspected. For future references.

    Writing directly to RCX0 flag will not stop the interrupt from reoccurring

     

    ADCSRA &= ~( 1 << RXC0 );

     

    Taken from the data sheet:

    When interrupt-driven data reception is used, the receive complete routine must read the received data from UDRn in order to clear the RXCn Flag, otherwise a new interrupt will occur once the interrupt routine terminates.

    The solution then was to just retrieve the data from UDR0

    uint8_t byte = UDR0; // It clears RXC0 for you

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • icongdo
    0 icongdo over 10 years ago in reply to Robert Peter Oakes

    Yes this is what I suspected. For future references.

    Writing directly to RCX0 flag will not stop the interrupt from reoccurring

     

    ADCSRA &= ~( 1 << RXC0 );

     

    Taken from the data sheet:

    When interrupt-driven data reception is used, the receive complete routine must read the received data from UDRn in order to clear the RXCn Flag, otherwise a new interrupt will occur once the interrupt routine terminates.

    The solution then was to just retrieve the data from UDR0

    uint8_t byte = UDR0; // It clears RXC0 for you

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube