#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#include "uart.h"
#include "IRremote.h"
unsigned char dimmerFlag = 0;
void Interrupt0_Init(void);
void Interrupt1_Init(void);
void overflow1(void);
void DimmerPortPinsInit(void);
void operateDimmer(unsigned char port);
void main()
{
unsigned char IRData = 0;
unsigned char rc5data = 0;
_delay_ms(20);
UART_Init(25);
UART_TransmitString("Testing dimmer in interrupt and overflow: \r\n");
//set the switch in port pins
DimmerPortPinsInit();
//set the interrupt0
Interrupt0_Init();
//set the interrupt1
Interrupt1_Init();
//set the overflow1
overflow1();
IRData = 0; dimmerFlag = 0;
sei();
while(1)
{
IRData = 0;
UART_TransmitString("IR data witing \r\n");
while (RC5BitHigh());
UART_TransmitString("IR data received \r\n");
rc5data = rc5decode();
IRData = (rc5data & 0xFF);
switch(IRData)
{
case 110:
operateDimmer(1);
_delay_ms(250);
break;
case 109:
operateDimmer(2);
break;
case 102:
operateDimmer(3);
break;
default:
UART_TransmitString("IR ERROR \r\n");
}
_delay_ms(250);
}
}
SIGNAL(SIG_INTERRUPT0)
{
// send signal to dimmer ckt
operateDimmer(1);
}
SIGNAL(SIG_INTERRUPT1)
{
// send signal to dimmer ckt
operateDimmer(2);
}
SIGNAL(SIG_OVERFLOW1)
{
// send signal to dimmer ckt
TCNT1 = 0xFFFE;
operateDimmer(3);
}
void Interrupt0_Init(void)
{
//set the interrupt0 falling edge
MCUCR |= _BV(1);
//set the interrupt0
GICR |= _BV(6);
}
void Interrupt1_Init(void)
{
//set the interrupt1 falling edge
MCUCR |= _BV(3);
//set the interrupt1
GICR |= _BV(7);
}
void overflow1(void)
{
//set the timer2 in falling edge
TCCR1A = 0x00;
TCCR1B = 0x06;
TCNT1 = 0xFFFE;
//set the timer overflow interrupt enable
TIMSK |= _BV(2);
}
void DimmerPortPinsInit(void)
{
DDRB |= _BV(1) | _BV(2) | _BV(3);
PORTB |= _BV(1) | _BV(2) | _BV(3);
_delay_ms(50);
PORTB &= ~( _BV(1) | _BV(2) | _BV(3));
}
void operateDimmer(unsigned char port)
{
// send signedl to dimmer ckt
UART_TransmitString("Switch :");
UART_TransmitByte(port+48);
UART_TransmitByte('\r');
UART_TransmitByte('\n');
PORTB |= _BV(port);
_delay_ms(50);
PORTB &= ~(_BV(port));
_delay_ms(250);
}