added interrupt handling for detecting power ok

This commit is contained in:
King Kévin 2013-10-13 16:47:46 +02:00
parent d21ed4cdd3
commit e4cb5d1f8f
1 changed files with 24 additions and 2 deletions

View File

@ -4,15 +4,36 @@
#include <string.h> /* Strings */
#include <avr/io.h> /* AVR device-specific IO definitions */
#include <util/delay.h> /* Convenience functions for busy-wait delay loops */
#include <avr/interrupt.h> /* Interrupts */
#include "main.h"
#include "uart.h"
/* global variables */
volatile uint8_t pwr_ok;
/* PWR_OK interrupt */
ISR(PCINT0_vect) { /* PCINT1 is actually triggering PCI0 Interrupt Vector */
if (pwr_ok!=(PINB&(1<<PWR_OK))) { /* did the PWR_OK pin state changed */
pwr_ok = PINB&(1<<PWR_OK); /* save new state */
if (pwr_ok) {
puts("power ok");
} else {
puts("power ko");
}
}
}
static void ioinit(void)
{
/* configure power */
DDRB = ~(1<<PWR_OK)|(1<<nPS_ON); /* PWR_OK is input, nPS_ON is output */
PORTB |= (1<<nPS_ON); /* witch off power supply */
DDRB |= (1<<nPS_ON); /* nPS_ON is output */
PORTB |= (1<<nPS_ON); /* switch off power supply */
DDRB &= ~(1<<PWR_OK); /* PWR_ON (PB1/PCINT1) is input */
pwr_ok = PINB&(1<<PWR_OK); /* save state */
PCIFR &= ~(1<<PCIF0); /* clear interrupt flag */
PCICR |= (1<<PCIE0); /* enable interrupt for PCINT[7:0] */
PCMSK0 |= (1<<PCINT1); /* enable interrupt for PCINT1 */
/* configure peripherals */
DDRB &= ~(1<<IR); /* IR receiver is input */
@ -43,6 +64,7 @@ static void ioinit(void)
/* start with LED on */
OCR0A = 0xff;
sei(); /* enable interrupts */
}
int main(void)