ATtiny45 IR remote receiver















Please,Click Google Adsense for support this blog.

code:
#include <TinyWireM.h>                  // I2C Master lib for ATTinys which use USI
#define PCF8574_ID 0x20                 //slave address (PCF8574):[0][1][0][0][A2][A1][A0][R/W]
#define ATcommand 0x20                  //LCD command   EN=1 ; RS=0       0010  0000
#define ATdata 0x30                     //LCD data      EN=1 ; RS=1       0011  0000
#define IRpin_PIN      PINB
#define IRpin          4
#define MAXPULSE 6500     //65000
#define RESOLUTION 2       //20 
unsigned int pulses[100][2];  // pair is high and low pulse 
unsigned int currentpulse = 0; // index for pulses we're storing

void PCF8574_Write(unsigned char data)
{
  TinyWireM.beginTransmission(PCF8574_ID);
  TinyWireM.send(data);                 // Access Command Register
  TinyWireM.endTransmission();          // Send to the slave 
  //delayMicroseconds(16); //  minimun~16-20
}
void Write_LCD_Byte(char command_data ,int value) //bool lcd_command ,
{
  int iMSB=0;
  int iLSB=0;
  iMSB = (value & 0xF0)>>4;
  iLSB = (value & 0x0F);
  PCF8574_Write(command_data|iMSB);//0xFF
  PCF8574_Write(0x00|iMSB);
  PCF8574_Write(command_data|iLSB);
  PCF8574_Write(0x00|iLSB);
  delay(1);
}
char IntToChar(int Int){
  if(Int<10)
  {
    char Char;
    Char=Int|0x30;
    return(Char); 
  }
  else if(Int>9)
  {
    if(Int==0x0A)
    {
      return('A'); 
    }
    else if(Int==0x0B)
    {
      return('B'); 
    }
    else if(Int==0x0C)
    {
      return('C'); 
    }
    else if(Int==0x0D)
    {
      return('D'); 
    }
    else if(Int==0x0E)
    {
      return('E'); 
    }
    else if(Int==0x0F)
    {
      return('F'); 
    }
  }
}
void initial_LCD(void)
{
  PCF8574_Write(0x00);//ให้ขา E = 0 ให้ขา RS= 0 กำหนดเป็นคำสั่ง  ให้ขา R/W = 0 เขียนข้อมูล
  PCF8574_Write(0x23);      //1    0x20 + 0x03   
  PCF8574_Write(0x03);
  PCF8574_Write(0x23);     //2
  PCF8574_Write(0x03);
  PCF8574_Write(0x23);     //3
  PCF8574_Write(0x03);
  PCF8574_Write(0x22);//Function Set (BIT5=1) [001DNF00 ],Sets buss size 4bit (D=0)ข้อมูลเฉพาะ D4-D7 จะถูกส่งออกไปที่บัส
  PCF8574_Write(0x02);
  Write_LCD_Byte(ATcommand,0x28);// 00101000 : [001DNF00 ],Sets buss size 4bit (D=0),number of lines=2 (N=1),font size =5x7(F=0)
  Write_LCD_Byte(ATcommand,0x0C);//00001100 : [00001DUB] ,Display On (D=1),Cursor Off (U=0) ,blinking block cursor Off (B=0)
  Write_LCD_Byte(ATcommand,0x06);//00000110 : [000001MS] , Cursor direction right (M=1);no scroll (S=0) 
  Write_LCD_Byte(ATcommand,0x01);//clear LCD
}
//--------------------------end LCD library---------------------
void setup()
{
  pinMode(3, OUTPUT);   
  TinyWireM.begin();                    // initialize I2C lib
  initial_LCD();                        //initial LCD
  pinMode(IRpin, INPUT);
  Write_LCD_Byte(ATcommand,0x80|0x40);// line 1 เริ่มตำแหน่ง 00
  Write_LCD_Byte(ATdata,'I');//A-P:count test
  Write_LCD_Byte(ATdata,'R');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,'R');
  Write_LCD_Byte(ATdata,'e');
  Write_LCD_Byte(ATdata,'c');
  Write_LCD_Byte(ATdata,'i');
  Write_LCD_Byte(ATdata,'v');
  Write_LCD_Byte(ATdata,'e');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,'=');
}
void loop()
{    
  unsigned int highpulse, lowpulse;  // temporary storage timing
  highpulse = lowpulse = 0; // start out with no pulse length 
  
  while (IRpin_PIN & (1 << IRpin)) {
     highpulse++;
     delayMicroseconds(RESOLUTION);
     if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][0] = highpulse;
  while (! (IRpin_PIN & _BV(IRpin))) {
     lowpulse++;
     delayMicroseconds(RESOLUTION);
     if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][1] = lowpulse;
  currentpulse++;
}
//=======================================================
void printpulses(void) {
  
  unsigned long int buffir=0;
  for (int i = 0; i < 32; i++) 
  {
    buffir=buffir<<1;
    if((pulses[i][0] * RESOLUTION)>0&&(pulses[i][0] * RESOLUTION)<500)
    {
      buffir|=0; 

    }else 
    {
      
      buffir|=1;
    }
  }
  Write_LCD_Byte(ATcommand,0x80|0x4B);// line 1 เริ่มตำแหน่ง 00
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  if(buffir==0xC03FCC33)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'1');//A-P:count test
  }
  else if(buffir==0xC03FC639)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'2');//A-P:count test
  }
  else if(buffir==0xC03FDEA1)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'3');//A-P:count test
  }
  else if(buffir==0xC03FC43B)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'4');//A-P:count test
  }
  else if(buffir==0xC03FCE31)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'5');//A-P:count test
  }
  else if(buffir==0xC03FD6A9)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'6');//A-P:count test
  }
  else if(buffir==0xC03FD0AF)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'7');//A-P:count test
  }
  else if(buffir==0xC03FD2AD)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'8');//A-P:count test
  }
  else if(buffir==0xC03FD4AB)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'9');//A-P:count test
  }
  else if(buffir==0xC03FDA25)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'0');//A-P:count test
  }
  else if(buffir==0xC03FE619)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'1');
    Write_LCD_Byte(ATdata,'0');
    Write_LCD_Byte(ATdata,'0');
    Write_LCD_Byte(ATdata,'+');
  }
  else if(buffir==0xC03FEC13)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'2');
    Write_LCD_Byte(ATdata,'0');
    Write_LCD_Byte(ATdata,'0');
    Write_LCD_Byte(ATdata,'+');
  }
  else if(buffir==0xC03FF807)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'V');
    Write_LCD_Byte(ATdata,'O');
    Write_LCD_Byte(ATdata,'L');
    Write_LCD_Byte(ATdata,'-');
  }
  else if(buffir==0xC03FEA15)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'V');
    Write_LCD_Byte(ATdata,'O');
    Write_LCD_Byte(ATdata,'L');
    Write_LCD_Byte(ATdata,'+');
  }
  else if(buffir==0xC03FE41B)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'E');
    Write_LCD_Byte(ATdata,'Q');
  }
  else if(buffir==0xC03FC8B7)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'P');
    Write_LCD_Byte(ATdata,'R');
    Write_LCD_Byte(ATdata,'E');
    Write_LCD_Byte(ATdata,'V');
  }
  else if(buffir==0xC03FC0BF)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'N');
    Write_LCD_Byte(ATdata,'E');
    Write_LCD_Byte(ATdata,'X');
    Write_LCD_Byte(ATdata,'T');
  }
  else if(buffir==0xC03FF08F)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4B);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'P');
    Write_LCD_Byte(ATdata,'L');
    Write_LCD_Byte(ATdata,'/');
    Write_LCD_Byte(ATdata,'P');
    Write_LCD_Byte(ATdata,'A');
  }
  else if(buffir==0xC03FE897)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'C');
    Write_LCD_Byte(ATdata,'H');
    Write_LCD_Byte(ATdata,'-');
  }
  else if(buffir==0xC03FD8A7)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'C');
    Write_LCD_Byte(ATdata,'H');
  }
  else if(buffir==0xC03FF887)
  {
    Write_LCD_Byte(ATcommand,0x80|0x4C);// line 1 เริ่มตำแหน่ง 00
    Write_LCD_Byte(ATdata,'C');
    Write_LCD_Byte(ATdata,'H');
    Write_LCD_Byte(ATdata,'+');
  }
  Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
  for(int iCount=0;iCount<8;iCount++)
  {
     Write_LCD_Byte(ATdata,IntToChar(buffir&0x0000000F));
     buffir=buffir>>4;
  }  
  
}