so i was trying to do a simple delay_ms in attiny426, here is the code:
#include <atmel_start.h> unsigned long millis_counter = 0; unsigend long millis(void); void delay_ms(unsigned long time_delay); ISR(TCA0_OVF_vect) { /* Insert your TCA overflow interrupt handling code */ millis_counter++; /* The interrupt flag has to be cleared manually */ TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm; } int main(void) { /* Initializes MCU, drivers and middleware */ atmel_start_init(); sei(); /* Replace with your application code */ while (1) { delay_ms(20); } } unsigned long millis(void){ return millis_counter; } void delay_ms(unsigned long time_delay) { unsigned long timer_millis = millis(); while(millis() - timer_millis < time_delay); PORTA_toggle_pin_level(1); }
the timer works well but it gets stuck on while, it seems timer_millis isn't initialized as it should:
if i change delay_ms to this:
void delay_ms(unsigned long time_delay) { while(true) { unsigned long timer_millis = millis(); if(timer_millis >= time_delay) { PORTA_toggle_pin_level(1); millis_counter = 0; break; } } }
it works as it should, but i want to avoid reseting millis_counter if possible.
this didn't work either:
void delay_ms(unsigned long time_delay) { unsigned long timer_millis = millis(); while(true) { if(millis() - timer_millis >= time_delay) break; } PORTA_toggle_pin_level(1); }
i don't know if i'm overlooking something or what is the problem.
i atteched the project.