www.pudn.com > pt2272.zip > Main__.c, change:2006-10-30,size:6017b


/***************************************************** 
Project : PT2272 series Simulator 
Version : 1.0 
Date    : 2006/10/28 
Author  : behzad khazama                             
Company : www.khazama.com                             
Comments: finally implemented at 1385/07/07 -- 04:43AM IRAN - SABZEVAR        
Chip type           : ATmega32 
Clock frequency     : 12.000000 MHz 
 
Some Important Notice about Calibration.      
  with a 2.2Mega OHM Oscillator Calibrator risistor  
  used with PT2262(Pin 15,16) , 
  -> Timer1 overflow must trigger every 120us. 
  see Timer1 overflow routin for more. 
 
  THIS Project is AS-IS and FREE to USE and FREE to Modify. 
  BUT Please put my sites link into your projects that use this code. 
  and finally I Implement this code very rush and it maybe have some BUGs.   
  SO : ANY BUGs OR SUGGESTIONs ARE WELCOME. 
 
  Pin connections: 
	INPUT Code(insted DIN of PT2272) is "INT0 PORTD.2" 
	LCD 2*16 -> PORTA as discribed on CodevisionAvr	 
	 
 
  Fell Free and contact me by : 
   my contact Number : +98-09155714862 
   or via eMail       : behzad@khazama.com 
*****************************************************/ 
 
#include <mega32.h> 
#include <delay.h> 
// Standard Input/Output functions 
#include <stdio.h> 
 
// Alphanumeric LCD Module functions 
#asm 
   .equ __lcd_port=0x1B ;PORTA 
#endasm 
#include <lcd.h> 
 
// 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>13 && HTime<30) 
     if(LTime>570 && LTime<700) 
         return(1);             
 return(0);      
} 
 
unsigned char isZero(void){ 
 if (HTime<LTime) 
    if(HTime>13 && HTime<30) 
     if(LTime>50 && LTime<70) 
         return(1);             
 return(0);      
} 
 
unsigned char isOne(void){ 
 if (HTime>LTime) 
    if(LTime>13 && LTime<30) 
     if(HTime>50 && HTime<70) 
         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()){ 
          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") 
 
while (1) 
      { 
      if(DetectTruePacket){ 
           lcd_gotoxy(0,0);      
           lcd_puts(bufer);      
           DetectTruePacket=0; 
      }; 
      }; 
} 
#include <mega32.h> 
 
void init(void){ 
// Input/Output Ports initialization 
// Port A initialization 
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In  
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T  
PORTA=0x00; 
DDRA=0x00; 
 
// Port B initialization 
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In  
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T  
PORTB=0x00; 
DDRB=0x00; 
 
// Port C initialization 
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In  
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T  
PORTC=0x00; 
DDRC=0x00; 
 
// Port D initialization 
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In  
// State7=T State6=T State5=T State4=T State3=T State2=P State1=T State0=T  
PORTD=0x04; 
DDRD=0x00; 
 
// Timer/Counter 0 initialization 
// Clock source: System Clock 
// Clock value: Timer 0 Stopped 
// Mode: Normal top=FFh 
// OC0 output: Disconnected 
TCCR0=0x00; 
TCNT0=0x00; 
OCR0=0x00; 
 
// Timer/Counter 1 initialization 
// Clock source: System Clock 
// Clock value: 12000.000 kHz 
// Mode: Normal top=FFFFh 
// OC1A output: Discon. 
// OC1B output: Discon. 
// Noise Canceler: Off 
// Input Capture on Falling Edge 
// Timer 1 Overflow Interrupt: On 
// Input Capture Interrupt: Off 
// Compare A Match Interrupt: Off 
// Compare B Match Interrupt: Off 
TCCR1A=0x00; 
TCCR1B=0x01; 
TCNT1H=0xFF; 
TCNT1L=0x9B; 
ICR1H=0x00; 
ICR1L=0x00; 
OCR1AH=0x00; 
OCR1AL=0x00; 
OCR1BH=0x00; 
OCR1BL=0x00; 
 
// Timer/Counter 2 initialization 
// Clock source: System Clock 
// Clock value: Timer 2 Stopped 
// Mode: Normal top=FFh 
// OC2 output: Disconnected 
ASSR=0x00; 
TCCR2=0x00; 
TCNT2=0x00; 
OCR2=0x00; 
 
// External Interrupt(s) initialization 
// INT0: On 
// INT0 Mode: Rising Edge 
// INT1: Off 
// INT2: Off 
GICR|=0x40; 
MCUCR=0x03; 
MCUCSR=0x00; 
GIFR=0x40; 
 
// Timer(s)/Counter(s) Interrupt(s) initialization 
TIMSK=0x04; 
 
// Analog Comparator initialization 
// Analog Comparator: Off 
// Analog Comparator Input Capture by Timer/Counter 1: Off 
ACSR=0x80; 
SFIOR=0x00; 
 
}