简体   繁体   中英

Why a led in arduino uno is not toggling and is always in the "on" state?

An Arduino Uno LED is not toggling and is always in the "on" state, what could be wrong?

#include <avr/io.h>

void delay_timer0(){
  
  TCNT0 = 0x00;
  
  TCCR0A = 0x00;
  TCCR0B = 0x01;
  
  while((TIFR0 & 0x01) == 0);
  
  TCCR0A = 0x00;
  TCCR0B = 0x00;
  
  TIFR0 = 0x01;
}

This is the infinite loop of toggling under the main method...

int main(void){
  
  DDRB = DDRB | (1<<5);  //output pb5
  
  while(1){
    PORTB = PORTB ^ (1<<5); //toggle bit
    delay_timer0();
  }
}

I bet it is a delay function problem

try

#include <util/delay.h>

/* .... */

  while(1){
    PORTB = PORTB ^ (1<<5); //toggle bit
    delay_ms(500);
  }

I found the error. I have worked with tinkercad and in that simulator the timer doesn't work. But I have configured this in real environment and it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM