#include <mega8.h>
#include <delay.h>
// Standard Input/Output functions
#include <stdio.h>
#include <stdlib.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTB
#endasm
#include <alcd.h>
#define xtal 8000000
// Declare your global variables and FUNCTIONS here
extern void init(void);
unsigned char edge=1;
unsigned int HTime=0,LTime=0 ;
unsigned long Tm=0;
unsigned char bufer[13];
unsigned char Tmp[24];
unsigned char DetectTruePacket=0;
#pragma warn-
void FinalAnalyse(void){
unsigned char i=0;
for(i=0;i<12;i++){
if (Tmp[i*2]==0 && Tmp[(i*2)+1]==0){
bufer[i]='0';
}
else if(Tmp[i*2]==1 && Tmp[(i*2)+1]==1){
bufer[i]='1';
}
else{
bufer[i]='F';
}
};
bufer[12]=0; // terminate string
}
#pragma warn+
unsigned char isSync(void){
if (HTime<LTime)
if(HTime>4 && HTime<8)
if(LTime>112 && LTime<224)
return(1);
return(0);
}
unsigned char isZero(void){
if (HTime<LTime)
if(HTime>4 && HTime<8)
if(LTime>12 && LTime<24)
return(1);
return(0);
}
unsigned char isOne(void){
if (HTime>LTime)
if(LTime>4 && LTime<8)
if(HTime>12 && HTime<24)
return(1);
return(0);
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
/* TCNT1 = 0xFF9B that mean 65535-100
so this routin will trigger every 120us on 12MHZ.
if your project is using different clock rate
u must modify TCNT1 rigisters to project work fine.*/
TCNT1H=0xFF;
TCNT1L=0x9B;
Tm++;
}
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
static unsigned char bitCnt=0;
if(edge>0){
LTime=Tm;
Tm=0;
MCUCR = 2; // Set interrupt on falling edge
edge = 0;
if(isSync()){
lcd_gotoxy(0,0);
sprintf(bufer,"%d ",bitCnt);
lcd_puts(bufer);
if(bitCnt==24){
FinalAnalyse() ;
DetectTruePacket=1;
};
bitCnt=0;
} else if(isZero()){
Tmp[bitCnt++]=0;
} else if(isOne()){
Tmp[bitCnt++]=1;
} else{
bitCnt=0;
}
}else{
HTime=Tm;
Tm=0;
MCUCR = 3; // Set interrupt on rising edge
edge = 1;
};
}
void main(void)
{
init();
lcd_init(16);
#asm("sei")
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("be name KHODA");
delay_ms(1000);
lcd_clear();
while (1)
{
if(DetectTruePacket){
lcd_gotoxy(0,0);
lcd_puts(bufer);
DetectTruePacket=0;
};
};
}