Please, be gentle. :)
Not much to it. Just trying to create a simple clock timer. The flashing part has been tested. I assume the counter variables are working. I had a little trouble wrapping my head around it, but I think I have it. Made some elementary comments to help me remember what I did. Only output is an LED that flashes marking the minute marks. Looking for advice, or hints in convention, syntax, use, shortcuts, better ways to implement, etc.
Device is an attiny85, burned with Atmel Studio 7 on a AVRISP mkii. No external clock, obviously.
/* * timer.c * * Created: 7/19/2016 10:51:59 AM * Author : Sherri */ #include <avr/io.h> #include <avr/interrupt.h> #define F_CPU 1000000UL int tim_seg=0, mins = 0, ten_mins = 0, hrs = 0; //initialize global clock variables int main(void) { // ---------timer setup-------------- // USE Timer 0 // Step #1 set the parameters for the timer TCCR0 registers TCCR0A = (1<<WGM01); //value sets timer to CTC mode TCCR0B = (1<<CS01); // value sets prescaler to clkio/8 will give 250 ticks; 2ms @1Mhz // Step #2 Choose the number of ticks to compare to the timer OCR0A = F_CPU/8 * 0.002 - 1;; // value timer is compared to = number of ticks-1, in this case 249 // OCR0B is a second value for a second compare if desired. // Step #3 Enable the interrupt for the timer OCR0A. When OCR0A matches the timer, the ISR function will execute TIMSK = (1<<OCIE0A); // Step #4 enable the AVR interrupt bit sei(); // Step #5 Add the ISR routine for OCR0A (See below Main module) // -------------LED setup---------------- DDRB = 0x01; PORTB = (1<<PB1); /* Replace with your application code */ while (1) { } } ISR(TIMER0_COMPA_vect){ if(tim_seg<29999){ tim_seg++; if(mins<9){ mins++; }else{ mins=0; if(ten_mins<5){ ten_mins++; }else{ ten_mins=0; hrs++; } } }else{ tim_seg=0; PORTB ^= (1<<PB1); } }