All pastes #806348 Raw Edit

timer

public c v1 · immutable
·published 2007-12-05 03:46 UTC
rendered paste body
#include <avr/interrupt.h>#include <avr/io.h>#include <avr/sleep.h>#include <avr/wdt.h>// GLOBAL VARIABLES	volatile unsigned int seconds = 3000;// MAINint main (void){	wdt_disable(); // disable watchdog	// port initialization	DDRB = 0xFF; // portb = output	PORTB = 0xFF; // all leds off	DDRC = 0x00; // portd = input	PINC = 0xFF; // all buttons off		// button interrupt initialization	PCICR |= (1 << PCIE1); // enable PORTC interrupts	PCMSK1 |= 0x3F; // enable PCINT8 - PCINT13	sei(); // enable global interrupts	// SLEEP	sleep_enable(); // enable sleep	set_sleep_mode(SLEEP_MODE_PWR_DOWN);	sleep_mode();	cli(); // disable global interrupts	// START BUTTON	while(PINC & (1 << 1));	PORTB = 0x00;	// timer interrupt initialization	// count = (1 / target freq) / (prescale / frequency)	TCCR1B |= (1 << CS12); // prescale = 256	TCCR1B |= (1 << WGM12); // enable clear on timer compare	TIMSK1 |= (1 << OCIE1A); // output compare A match interrupt enabled	OCR1A = 31250; // compare value A. 31250 equals 1Hz at 8Mhz with prescaler at 256	// watchdog timer initialization	wdt_enable(WDTO_8S); // watchdog timeout = 8 seconds	WDTCSR |= (1 << WDE); // timeout action = reset	sei(); // reenable global interrupts	while(1);}// TIMER INTERRUPTISR(TIMER1_COMPA_vect){	//PORTB = ~(seconds % 60); // seconds	PORTB = ~(seconds / 60); // minutes	// conditional prevents underflow 	if(seconds > 0) {		seconds--;		wdt_reset(); // kick the dog	}	else {		PORTB = 0x00; // toggle all leds		PRR |= (1 << PRTIM1); // power reduction timer/counter1 (turn off timer)	}}// BUTTON INTERRUPTISR(PCINT1_vect){	PORTB = PINC;}