Running the code below, when I send any character in the Serial arduino is not printing "a" back. I think it's something wrong with the timer1 code but it should work cause this code was given by my teacher in C class.



void setup() {


    TCCR1A = 0;// set entire TCCR1A register to 0
    TCCR1B = 0;// same for TCCR1B
    TCNT1  = 0;//initialize counter value to 0
    // set compare match register for 1000000hz increments with 8 bits prescaler
    OCR1A = 1;// = (16*10^6) / (1000000*8) - 1 (must be <65536)
    // turn on CTC mode
    TCCR1B |= (1 << WGM12);
    // Set CS11 bit for 8 prescaler. Each timer has a different bit code to each prescaler
    TCCR1B |= (1 << CS11);  
    // enable timer compare interrupt
    TIMSK1 |= (1 << OCIE1A);


void loop() {

    if (Serial.available()) {



  • 1
    does it work when you take all the timer register code out? – BrettAM Mar 5 '15 at 17:34
  • yes it works fine. – Sitepor500.com.br Mar 5 '15 at 17:57
  • 3
    OCR1A = 1 is a really really low value. This will mean almost all the time is spend inside the ISR routine (called once every 16 clock cycles). Try with something higher first (e.g. 255). – Gerben Mar 5 '15 at 20:37
  • Another question is: what will go inside your ISR? Its empty here but what do you have planned for it. Spending too long in an ISR is bad news – portforwardpodcast Apr 7 '15 at 8:18

This is a more complicated question than the standard Arduino questions, so I took a lot of time to look into it and find out the exact answer--since I was also curious. I have posted a very thorough answer to your question here on Stack Exchange, so please look there: https://stackoverflow.com/questions/28880226/timer1-arduino-makes-serial-not-work/28887781#28887781

I should also note that Gerben's comment is spot-on. I recommend a value no less than 20, since you configured the counter to increment once per 0.5us. This way, a value of 20 only calls the ISR every 10us, which should keep the ISR from occurring so often that it locks up your code. As you'll see in my link above, the other main problem is you forgot to define your ISR function that is required when you enable the interrupt.

If this meets your needs, please put a green check next to my answer to mark it as correct.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.