//--------------------------------------------------------------//
// !!!ADVARSEL!!! //
// //
// LUFTSLOTTKODE //
// //
// - bør støpes inn med smeltelim og aldri tulles mer med! //
// //
// Dewald de Bruyn - eksamensperioden høst 2007 //
// //
// !!!ADVARSEL!!! //
//--------------------------------------------------------------//
#include <avr/io.h>
#include <avr/interrupt.h>
#define true 1
#define false 0
#define inputPin (PINB & (1<<PIN4))
#define MENUBUTTON 0x0F
#define PLAYBUTTON 0x09
#define LEFTBUTTON 0x01
#define RIGHTBUTTON 0x08
#define UPBUTTON 0x02
#define DOWNBUTTON 0x04
#define TIMEOUT 16
char interrupt_received;
unsigned int timerTicks;
int output = 0;
void receiveNrBits(const int);
int checkForPlayMenuRev(void);
ISR(PCINT0_vect){
TCNT0 = 0x00;
timerTicks = 0;
interrupt_received = true; //Set interrupt receive flag?
}
ISR(TIM0_OVF_vect){
timerTicks++;
if(timerTicks == TIMEOUT){
timerTicks = 0;
PORTB = 0x00;
}
}
unsigned int startReceive(){
if(inputPin != 0)return 0;
while((TCNT0 < 0xFF) && inputPin == 0);
if(TCNT0 != 0xFF)return 0;
while(inputPin == 0);
unsigned int tmp = TCNT0;
TCNT0 = 0x00;
if(((tmp>>4) < 0x05)||((tmp>>4) > 0x06))return 0;
while(inputPin != 0);
tmp = TCNT0;
TCNT0 = 0x00;
if((tmp>>4) < 0x0A){
return 0;
}
receiveNrBits(33);
while(inputPin != 0);
tmp = TCNT0;
TCNT0 = 0x00;
if((tmp>>4) == 0x03){
//Received Play, Menu or Rev
for(int i = 0; i < 3; i++){
receiveNrBits(1);
if(checkForPlayMenuRev()){
if(i == 0)return MENUBUTTON;
if(i == 1)return PLAYBUTTON;
if(i == 2)return LEFTBUTTON;
}
}
return 0;
}
else{
//Received plus, minus or fwd
receiveNrBits(1);
while(inputPin != 0);
tmp = TCNT0;
TCNT0 = 0x00;
if((tmp>>4) != 0x03)return DOWNBUTTON;
receiveNrBits(1);
while(inputPin != 0);
tmp = TCNT0;
TCNT0 = 0x00;
if((tmp>>4) == 0x03)return RIGHTBUTTON;
if((tmp>>4) != 0x03)return UPBUTTON;
}
return 0;
}
int checkForPlayMenuRev(){
while(inputPin != 0);
unsigned int tmp = TCNT0;
TCNT0 = 0x00;
if((tmp>>4) == 0x03){
return true;
}
else return false;
}
void receiveNrBits(int nr){
for(int i = 0; i < nr; i++){
if(inputPin != 0){
while(inputPin != 0);
}
else{
while(inputPin == 0);
}
TCNT0 = 0x00;
}
}
int main(){
DDRB = 0x0F;
GIMSK |= 1<<PCIE;
PCMSK |= 1<<PCINT4;
TCCR0B |= 1<<CS02;
TIMSK0 |= 1<<TOIE0;
sei();
while(1){
if(interrupt_received == true){
GIMSK &= ~(1<<PCIE); //Disable pin interrupts
interrupt_received= false; //Clear interrupt receive flag
unsigned int result = startReceive(); //Start receiving sequence
if(result > 0)output = result;
PORTB = (output & 0x0F);
GIMSK |= 1<<PCIE;
}
}
} |