Monday, June 9, 2008

NEC REMOTE Receiver for home devices

NEC is a protocle used in CD VCD DVD players specialy JAPANESE. NEC protocle sends 32 bits data over infrared in PPM modulation here is a code for atmega 16 
/*****************************************************

Project : REMOTE RECEIVER
Chip type           : ATmega16L
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega16.h>
#include <delay.h>
void lcd_display_int(int num);
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>  
bit repeat_flag,start_flag;  
unsigned char     byt,address,command,address_1,command_1,address_0,command_0;
int tik;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
 TCCR0=0x01; 
 
 if((tik>=65)&&(tik<74))
 { 
   address=1;
   repeat_flag=0;
   start_flag=1;
   address_1=0;
   command_1=0;
   address_0=0;
   command_0=0;
   address=0;
   command=0;
  }
 if((tik>=116)&&(tik<139))
 { 
   address=2;
   repeat_flag=1;
   start_flag=0;
   
  }  
 if((tik>=10)&&(tik<13)&&(start_flag==1))
 {
 byt++;
  if(byt<9)
   {
    address_1=(address_1 << 1);
    address_1=address+1;
    
    }
  if((byt>=9)&&(byt<17))
    {
    address_0=(address_0 << 1);
    address_0=address_0+1;
    }
  if((byt>=17)&&(byt<25))
    {
    command_1=(command_1 << 1);
    command_1=command_1+1;
    }
  if(byt>=25)
    {
    command_0=(command_0 << 1);
    command_0=command_0+1;
    }  
  
  }
 
 
 if((tik>=5)&&(tik<7)&&(start_flag==1))  
 { 
 byt++;
  if(byt>=9)
    {
    address_1=(address_1 << 1);
    }  
   if((byt>=9)&&(byt<17))
    {
    command_0=(command_0 << 1);
    } 
   if((byt>=17)&&(byt<25))
    {
    command_1=(command_1 << 1);
    }  
   if(byt>=25)
    {
    command_0=(command_0 << 1);
    } 
 
 }
  
 tik=0;
 if(byt==32)
 {
 // address=address_1;
  command=command_1;
  byt=0;
  repeat_flag=0;
  start_flag=0;
   TCCR0=0x00;  
   
   
 } 
  
  
}

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here 

 tik++;
 if(tik>=600)
 {tik=0;
 repeat_flag=0;
 start_flag=0;
 address_1=0;
 command_1=0;
 address_0=0;
 command_0=0;
 address=0;
 command=0;
 
 TCCR0=0x00;
 }
 

}

// Declare your global variables here

void main(void)
{
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
       lcd_clear();
       lcd_gotoxy(0,0);
     //  lcd_putsf("xt "); 
     //  lcd_display_int(command);  
       
        //SWITCHING AND DISPLAY
       
       
       if(command==144)
       {
       if(PORTB.0)
       {
lcd_putsf("Device 1 switched OFF");
       PORTB.0=0;
       }
       else
       {
lcd_putsf("Device 1 switched ON");
       PORTB.0=1;
       }    
        }
       
         
       
       if(command==184)
       {
       if(PORTB.1)
       {
lcd_putsf("Device 2 switched OFF");
       PORTB.1=0;
       }
       else
       {
lcd_putsf("Device 2 switched ON");
       PORTB.1=1;
       }     
       }
       
       
           if(command==248)
           {
       if(PORTB.2)
       {
lcd_putsf("Device 3 switched OFF");
       PORTB.2=0;
       }
       else
       {
lcd_putsf("Device 3 switched ON");
       PORTB.2=1;
       }
         }        
       
       
       
             if(command==176)
             {
       if(PORTB.3)
       {
lcd_putsf("Device 4 switched OFF");
       PORTB.3=0;
       }
       else
       {
lcd_putsf("Device 4 switched ON");
       PORTB.3=1;
       }  }     
       
       
       
               if(command==152)  
               {
       if(PORTB.4)
       {lcd_putsf("Device 5 switched OFF");
       PORTB.4=0;
       }
       else
       {lcd_putsf("Device 5 switched ON");
       PORTB.4=1;
       }  }   
       
       
       
               if(command==216) 
               {
       if(PORTB.5)
       {lcd_putsf("Device 6 switched OFF");
       PORTB.5=0;
       }
       else
       {lcd_putsf("Device 6 switched ON");
       PORTB.5=1;
       }   }        
       
       
                if(command==136)
                {
       if(PORTB.6)
       {lcd_putsf("Device 7 switched OFF");
       PORTB.6=0;
       }
        else
       {lcd_putsf("Device 7 switched ON");
       PORTB.6=1;
       }      }
                    
              
              
              if(command==168) 
              {
       if(PORTB.7)
                        {lcd_putsf("Device 8 switched OFF");
                        PORTB.7=0;
                        }
                else
                        {lcd_putsf("Device 8 switched ON");
                        PORTB.7=1;
                        }   }
             
  
       delay_ms(100);
      };
}
 

void lcd_display_int(int num)
{
int disp[5]={'\0'},i=0;

do
{
disp[i] = num%10;
i++;
num = num/10;
}
while(num!=0);

for(i = i - 1; i >= 0 ; i--)
{
lcd_putchar(disp[i]+48);
}

}