I try to create millis code using Timer0 on ATTiny861. It is not working. Can someone help to detect where is the error. The code can build without error.
* * GccApplication6.c * ATTiny861 test the millis functions in Atmel Studio 7 * using Timer0 * Fuse setting L:62, H:DF, E: FF */ #define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> // Store value of millisecond since uC started volatile unsigned long millis_value; unsigned long PreviousMillis = 0; unsigned long CurrentTimeInterval = 3000; //1000 = 1 sec // This function is used to get millis_value unsigned long millis() { unsigned long m; // Disable interrupts while we read millis_value or we might get an // inconsistent value (e.g. in the middle of a write to millis_value) asm ("cli"); m = millis_value; asm ("sei"); return m; } // Timer 0 output compare interrupt service routine ISR(TIMER0_OVF_vect) { // Increment millisecond every 1ms millis_value++; } int main(void) { DDRB = 0xFF; /* Set timer 0 on CTC mode that will compare match every 1ms */ // Timer/Counter 0 initialization // Mode: CTC top=OCR0A // TCCR0A = TCW0 | ICEN0 | INCC0 | ICES0 | ACIC0 | - | - | CTC0 // 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 TCCR0A = 0x01; // TCCR0B = |- | -| - | TSM | PSR0 | CS02 | CS01 | CS00 // | 0|0 | 0 | 0 | 0 | 0 | 1 | 1 //divide by 64 TCCR0B = 0x03; TCNT0L = 0x00; // 0CR0A value = (0.001 s x Fcpu/N) - 1 // OCR0A = 0x7C;//DEC IS 124, (0.001 x 8M/64)-1 /* Enable compare match interrupt for timer 0 */ // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK = 0x02;//TOIE0 IS SET /* Enable global interrupt */ asm("sei"); while (1) { unsigned long currentMillis = millis(); unsigned long elapsedTime = currentMillis - PreviousMillis; if (elapsedTime >= CurrentTimeInterval) {//if Time lapsed PORTB |= (1 << PB4);//led on _delay_ms(1000); PORTB &= ~(1 << PB4);//led off _delay_ms(1000); PreviousMillis = millis();} } }
Kindly help. Appreciated.