/*****************************************************
Chip type : ATmega16L
Program type : Application
Clock frequency : 7.372800 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
void setup_AVR();
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#include <mega16.h>
#include <stdio.h>
#include <io.h>
unsigned char temp=0;
unsigned char command[10]={0};
unsigned char system[10]={0};
unsigned char timerH=0;
unsigned char timerL=0;
int bitcnt; //number of bits waiting to be recieved
unsigned char ref1; // 3/4 bit_time
unsigned char ref2; // 5/4 bit_time
int i=6;
int j=5;
int k;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
timerL++; //increments every 255* 1/7.372800 seconds
temp++;
if(temp==255) {temp=0;timerH++;} //increments every 255*255* 1/7.372800 seconds
}
void main(void)
{
setup_AVR();
#asm("sei")
while (1)
{
initialize: temp=0;
timerH=0;
detect1: timerL=0;
detect2: if(timerH>15) //If no start bit detected within 132ms
goto fault;
if(timerL>102) //If line high for more than 3.5ms
goto start1; //then wait for the first start bit
if(!CHECKBIT(PIND,2)) //If line is :
goto detect1; // PIND=0 low - jump to detect1 and reset the timer
else goto detect2; // PIND=1 high - jump to detect2 and recheck for 3.5 ms of ideal high
start1: while(CHECKBIT(PIND,2))//keep checking for the first start bit
{
if(timerH>15) //If no start bit detected within 132ms
goto fault; //then exit printf("5");
}
timerL=0; //Measure halflength of start bit
start2: while(!CHECKBIT(PIND,2))//Measure halflength of start bit (low level length)
{
if(timerL>26) //If startbit longer than 1.1ms,
goto fault ; // exit
}
//timerL = 1/2 bit time
ref1 =3.0*timerL/2.0; //ref1 = 3/4 bit time
ref2 =5.0*ref1/3.0; //ref2 = 5/4 bit time
timerL=0;
start3: while(CHECKBIT(PIND,2))//check if second starting bit is recieved
{
if(timerL>ref1) //If rising edge is too long
goto fault; //exit
}
bitcnt =12; //Receive 12 bits
j=5; //For command
i=5; //For system
sample: timerL =0 ;
while(timerL<ref1); // wait untill first 1/4 of next bit
while(bitcnt>0)
{
//Now recieving first 7 bits including the toggle bit
while(bitcnt>6)
{ if (PIND.2==0){ //The bit recieved is 0
system[i]=0; //store the bit in system
i-- ;bitcnt--; //prepare for the next bit
while(!CHECKBIT(PIND,2)) //wait untill the next edge
{
if(timerL>ref2) goto fault;
}
goto sample; //wait for the next bit
}
else { //The bit recieved is 1
system[i]=1; //store the bit in system
i-- ;bitcnt--; //prepare for the next bit
while(CHECKBIT(PIND,2)) //wait untill the next edge
{
if(timerL>ref2) goto fault;
}
goto sample; //wait for the next bit
}
}
//Now recieving last 6 bits
if(PIND.2==0){ //The bit recieved is 0
command[j]=0; //store the bit in command
j-- ;bitcnt--; //prepare for the next bit
while(!CHECKBIT(PIND,2)) //wait untill the next edge
{
if(timerL>ref2) goto fault;
}
goto sample; //wait for the next bit
}
else { //The bit recieved is 1
command[j]=1; //store the bit in command
j-- ;bitcnt--; //prepare for the next bit
while(CHECKBIT(PIND,2)) //wait untill the next edge
{
if(timerL>ref2) goto fault;
}
goto sample; //wait for the next bit
}
}
printf("aa");
for(k=5;k>=0;k--)
printf("%d",command[k]);
printf("bb");
for(k=5;k>=0;k--)
printf("%d",system[k]);
fault: goto initialize;
};
}
void setup_AVR()
{
PORTD=0x00;
DDRD=0x00;
TCCR0=0x01;
TCNT0=0x00;
OCR0=0x00;
TIMSK=0x01;
ACSR=0x80;
SFIOR=0x00;
}