Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Numbers look good to me.Hey guys, i can read a NEC IR signal no problem... i was wondering since i want to make a NEC IR remote... is the below math correct ?
38KHz = 0.00002631 = 26.31uS
1/3 DUTY = 0.00000877 = 8.77uS
2/3 DUTY = 0.00001754 = 17.54uS
1 Pulse = 1/3 HIGH and 2/3 LOW
That should work. Thanks.but you can simply put your PIC in SLEEP MODE and then use a interrupt to wake it up
T0CON = 0x88; //Timer0 ON , No PRESCALER
//.........
IRPIN = 1;
while(TMR0() < 18); // 18 cycles is supposed to be 9uS
CLRTIME(); // clears TMR0L and TMR0H
IRPIN = 0;
while(TMR0() < 35); //35 cycles is supposed to be 17.5uS
CLRTIME(); // clears TMR0L and TMR0H
T0CON = 0x88; //Timer0 ON , No PRESCALER
//.........
IRPIN = 1;
[B] while(TMR0() < 18); // 44uS which is more like 88 Cycles[/B]
CLRTIME(); // clears TMR0L and TMR0H
IRPIN = 0;
[B] while(TMR0() < 35); //88uS which is more like 176 Cycles[/B]
CLRTIME(); // clears TMR0L and TMR0H
#include <p18Cxxx.h>
#include <delays.h>
#include <stdio.h>
#pragma config WDT = OFF, OSC = INTIO67, MCLRE = ON
void Delay_mS(int x);
void Delay_uS(int x);
#define IRTRIS TRISAbits.TRISA1
#define IRPIN LATAbits.LATA1
void SendNEC(unsigned char add, unsigned char cmd);
void PulseStart(void);
void Pulse0 (void);
void Pulse1 (void);
void Pulse560 (void);
#define ADDRESS 0x20
#define VOLUP 0x40
#define VOLDN 0xC0
#define INPUT 0xD0
void CLRTIME (void)
{
INTCONbits.TMR0IF = 0;
TMR0L = 0;
TMR0H = 0;
}
unsigned int TMR0(void)
{
unsigned int TIME = 0;
unsigned int TIMEL = TMR0L;
unsigned int TIMEH = TMR0H;
TIME = (TIMEL | (TIMEH<<8));
//CLRTIME();
return TIME;
}
void main(void)
{
OSCCON = 0x72;
while(!OSCCONbits.IOFS);
CMCON = 0x07;
ADCON0 = 0x00;
ADCON1 = 0x0F;
TRISA = 0;
TRISB = 0;
TRISC = 0;
T0CON = 0x88;
SendNEC(ADDRESS, VOLUP);
while(1)
{
}
}
void SendNEC(unsigned char add, unsigned char cmd)
{
unsigned char x;
unsigned char addI = ~add;
unsigned char cmdI = ~cmd;
PulseStart(); //Delay 9ms HIGH then Delay 4.5ms LOW
for(x=0;x<8;x++) //Send add Byte
{
Pulse560();
if(1 & add)
Pulse1();
else
Pulse0();
add >>= 1;
}
for(x=0;x<8;x++)//Send add Byte inverted
{
Pulse560();
if(1 & addI)
Pulse1();
else
Pulse0();
addI >>= 1;
}
for(x=0;x<8;x++)//Send cmd Byte
{
Pulse560();
if(1 & cmd)
Pulse1();
else
Pulse0();
cmd >>= 1;
}
for(x=0;x<8;x++)//Send cmd Byte inverted
{
Pulse560();
if(1 & cmdI)
Pulse1();
else
Pulse0();
cmdI >>= 1;
}
IRPIN = 0; //END LOW
}
void PulseStart(void)
{
unsigned char x,y = 0;
for(y=0;y<16;y++){
CLRTIME();
Pulse560();
}
IRPIN = 0;
for(y=0;y<8;y++){
for(x=0;x<21;x++){
CLRTIME();
IRPIN = 0;
while(TMR0() < 47);
}
}
CLRTIME();
}
void Pulse0 (void)
{
unsigned char x = 0;
for(x=0;x<21;x++){
IRPIN = 0;
while(TMR0() < 47);
CLRTIME();
}
CLRTIME();
}
void Pulse1 (void)
{
// Pulsed 2.25mS of 38KHz data
unsigned char x = 0;
for(x=0;x<63;x++){
IRPIN = 0;
while(TMR0() < 47);
CLRTIME();
}
CLRTIME();
}
void Pulse560 (void)
{
// Pulsed 560uS
unsigned char x = 0;
for(x=0;x<21;x++){
IRPIN = 1;
while(TMR0() < 12);
CLRTIME();
IRPIN = 0;
while(TMR0() < 35);
CLRTIME();
}
CLRTIME();
}
#include <p18Cxxx.h>
#include <delays.h>
#include <stdio.h>
#pragma config WDT = OFF, OSC = HSPLL, MCLRE = ON
void Delay_mS(int x);
void Delay_uS(int x);
#define IRTRIS TRISAbits.TRISA1
#define IRPIN LATAbits.LATA1
void SendNEC(unsigned char add, unsigned char cmd);
void PulseStart(void);
void Pulse0 (void);
void Pulse1 (void);
void Pulse560 (void);
#define ADDRESS 0x20
#define VOLUP 0x40
#define VOLDN 0xC0
#define INPUT 0xD0
void CLRTIME (void)
{
INTCONbits.TMR0IF = 0;
TMR0L = 0;
TMR0H = 0;
}
unsigned int TMR0(void)
{
unsigned int TIME = 0;
unsigned int TIMEL = 0;
unsigned int TIMEH = 0;
TIMEL = TMR0L;
TIMEH = TMR0H;
TIMEH <<=8;
TIME = (TIMEL | TIMEH);
//CLRTIME();
return TIME;
}
void main(void)
{
//OSCCON = 0x72;
//while(!OSCCONbits.IOFS);
CMCON = 0x07;
ADCON0 = 0x00;
ADCON1 = 0x0F;
TRISA = 0;
TRISB = 0;
TRISC = 0;
T0CON = 0x88;
while(1)
{
SendNEC(ADDRESS, VOLUP);
Delay_mS(1000);
}
}
void Delay_mS(int x)
{
while(x)
{
Delay1KTCYx(2);
x--;
}
}
void Delay_uS(int x)
{
while(x)
{
Nop();
x--;
}
}
void SendNEC(unsigned char add, unsigned char cmd)
{
unsigned char x;
unsigned char addI = ~add;
unsigned char cmdI = ~cmd;
PulseStart(); //Delay 9ms HIGH then Delay 4.5ms LOW
for(x=0;x<8;x++) //Send add Byte
{
Pulse560();
if(1 & add)
Pulse1();
else
Pulse0();
add >>= 1;
}
for(x=0;x<8;x++)//Send add Byte inverted
{
Pulse560();
if(1 & addI)
Pulse1();
else
Pulse0();
addI >>= 1;
}
for(x=0;x<8;x++)//Send cmd Byte
{
Pulse560();
if(1 & cmd)
Pulse1();
else
Pulse0();
cmd >>= 1;
}
for(x=0;x<8;x++)//Send cmd Byte inverted
{
Pulse560();
if(1 & cmdI)
Pulse1();
else
Pulse0();
cmdI >>= 1;
}
IRPIN = 0; //END LOW
}
void PulseStart(void)
{
unsigned char x,y,z = 0;
for(y=0;y<16;y++){
Pulse560();
}
IRPIN = 0;
for(y=0;y<8;y++){
for(x=0;x<21;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 80);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < 154);
}
}
}
void Pulse0 (void)
{
unsigned char x,y = 0;
for(x=0;x<21;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 80);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < 154);
}
}
void Pulse1 (void)
{
// Pulsed 2.25mS of 38KHz data
unsigned char x,y = 0;
for(x=0;x<63;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 80);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < 154);
}
}
void Pulse560 (void)
{
// Pulsed 560uS
unsigned char x = 0;
for(x=0;x<21;x++){
IRPIN = 1;
TMR0L = 0;
while(TMR0L < 80);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < 154);
}
TMR0L = 0;
}
#include <p18Cxxx.h>
#include <delays.h>
#include <stdio.h>
#pragma config WDT = OFF, OSC = HSPLL//, MCLRE = ON
void Delay_mS(int x);
void Delay_uS(int x);
#define IRTRIS TRISAbits.TRISA1
#define IRPIN LATAbits.LATA1
void SendNEC(unsigned char add, unsigned char cmd);
void PulseStart(void);
void Pulse0 (void);
void Pulse1 (void);
void Pulse560 (void);
#define WaitA 77
#define WaitB 146
#define ADDRESS 0x20
#define VOLUP 0x40
#define VOLDN 0xC0
#define INPUT 0xD0
void CLRTIME (void)
{
INTCONbits.TMR0IF = 0;
TMR0L = 0;
TMR0H = 0;
}
unsigned int TMR0(void)
{
unsigned int TIME = 0;
unsigned int TIMEL = 0;
unsigned int TIMEH = 0;
TIMEL = TMR0L;
TIMEH = TMR0H;
TIMEH <<=8;
TIME = (TIMEL | TIMEH);
//CLRTIME();
return TIME;
}
void main(void)
{
//OSCCON = 0x72;
//while(!OSCCONbits.IOFS);
// CMCON = 0x07;
ADCON0 = 0x00;
ADCON1 = 0x0F;
TRISA = 0;
TRISB = 0;
TRISC = 0;
T0CON = 0x88;
while(1)
{
SendNEC(ADDRESS, VOLUP);
Delay_mS(3000);
}
}
void Delay_mS(int x)
{
while(x)
{
Delay1KTCYx(2);
x--;
}
}
void Delay_uS(int x)
{
while(x)
{
Nop();
x--;
}
}
void SendNEC(unsigned char add, unsigned char cmd)
{
unsigned char x;
unsigned char addI = ~add;
unsigned char cmdI = ~cmd;
PulseStart(); //Delay 9ms HIGH then Delay 4.5ms LOW
for(x=0;x<8;x++) //Send add Byte
{
Pulse560();
if(0x80 & add)
Pulse1();
else
Pulse0();
add <<= 1;
}
for(x=0;x<8;x++)//Send add Byte inverted
{
Pulse560();
if(0x80 & addI)
Pulse1();
else
Pulse0();
addI <<= 1;
}
for(x=0;x<8;x++)//Send cmd Byte
{
Pulse560();
if(0x80 & cmd)
Pulse1();
else
Pulse0();
cmd <<= 1;
}
for(x=0;x<8;x++)//Send cmd Byte inverted
{
Pulse560();
if(0x80 & cmdI)
Pulse1();
else
Pulse0();
cmdI <<= 1;
}
IRPIN = 0; //END LOW
}
////////////////////////////////////
// START
////////////////////////////////////
void PulseStart(void)
{
unsigned char x,y,z = 0;
for(y=0;y<16;y++){
Pulse560();
}
for(y=0;y<8;y++){
for(x=0;x<21;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
}
void Pulse0 (void)
{
unsigned char x,y = 0;
for(x=0;x<21;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
void Pulse1 (void)
{
// Pulsed 2.25mS of 38KHz data
unsigned char x,y = 0;
for(x=0;x<64;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
void Pulse560 (void)
{
// Pulsed 560uS
unsigned char x = 0;
for(x=0;x<22;x++){
IRPIN = 1;
TMR0L = 0;
while(TMR0L < WaitA);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < WaitB);
}
TMR0L = 0;
}
#include <p18Cxxx.h>
#include <delays.h>
#include <stdio.h>
#pragma config WDT = OFF, OSC = HSPLL//, MCLRE = ON
void Delay_mS(int x);
void Delay_uS(int x);
#define IRTRIS TRISAbits.TRISA1
#define IRPIN LATAbits.LATA1
void SendNEC(unsigned char add, unsigned char cmd);
void PulseStart(void);
void Pulse0 (void);
void Pulse1 (void);
void Pulse560 (void);
#define WaitA 80
#define WaitB 150
#define ADDRESS 0x20
#define VOLUP 0x40
#define VOLDN 0xC0
#define INPUT 0xD0
void CLRTIME (void)
{
INTCONbits.TMR0IF = 0;
TMR0L = 0;
TMR0H = 0;
}
unsigned int TMR0(void)
{
unsigned int TIME = 0;
unsigned int TIMEL = 0;
unsigned int TIMEH = 0;
TIMEL = TMR0L;
TIMEH = TMR0H;
TIMEH <<=8;
TIME = (TIMEL | TIMEH);
//CLRTIME();
return TIME;
}
void main(void)
{
//OSCCON = 0x72;
//while(!OSCCONbits.IOFS);
// CMCON = 0x07;
ADCON0 = 0x00;
ADCON1 = 0x0F;
TRISA = 0;
TRISB = 0;
TRISC = 0;
T0CON = 0x88;
while(1)
{
SendNEC(ADDRESS, VOLUP);
Delay_mS(3000);
}
}
void Delay_mS(int x)
{
while(x)
{
Delay1KTCYx(2);
x--;
}
}
void Delay_uS(int x)
{
while(x)
{
Nop();
x--;
}
}
void SendNEC(unsigned char add, unsigned char cmd)
{
unsigned char x;
unsigned char addI = ~add;
unsigned char cmdI = ~cmd;
PulseStart(); //Delay 9ms HIGH then Delay 4.5ms LOW
for(x=0;x<8;x++) //Send add Byte
{
Pulse560();
if(0x80 & add)
Pulse1();
else
Pulse0();
add <<= 1;
}
for(x=0;x<8;x++)//Send add Byte inverted
{
Pulse560();
if(0x80 & addI)
Pulse1();
else
Pulse0();
addI <<= 1;
}
for(x=0;x<8;x++)//Send cmd Byte
{
Pulse560();
if(0x80 & cmd)
Pulse1();
else
Pulse0();
cmd <<= 1;
}
[B]for(x=0;x<9;x++)//Send cmd Byte inverted[/B]
{
Pulse560();
if(0x80 & cmdI)
Pulse1();
else
Pulse0();
cmdI <<= 1;
}
//IRPIN = 0; //END LOW
}
////////////////////////////////////
// START
////////////////////////////////////
void PulseStart(void)
{
unsigned char x,y,z = 0;
for(y=0;y<16;y++){
Pulse560();
}
for(y=0;y<8;y++){
for(x=0;x<22;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
}
void Pulse0 (void)
{
unsigned char x,y = 0;
for(x=0;x<22;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
void Pulse1 (void)
{
// Pulsed 2.25mS of 38KHz data
unsigned char x,y = 0;
for(x=0;x<66;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
void Pulse560 (void)
{
// Pulsed 560uS
unsigned char x = 0;
for(x=0;x<21;x++){
IRPIN = 1;
TMR0L = 0;
while(TMR0L < WaitA);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < WaitB);
}
TMR0L = 0;
}
#include <p18Cxxx.h>
#include <delays.h>
#include <stdio.h>
#pragma config WDT = OFF, OSC = HSPLL//, MCLRE = ON
void Delay_mS(int x);
void Delay_uS(int x);
#define IRTRIS TRISAbits.TRISA1
#define IRPIN LATAbits.LATA1
void SendNEC(unsigned char add, unsigned char cmd);
void PulseStart(void);
void Pulse0 (void);
void Pulse1 (void);
void Pulse560 (void);
unsigned int GetAN0(void);
#define WaitA 80
#define WaitB 150
#define ADDRESS 0x20
#define VOLUP 0x40
#define VOLDN 0xC0
#define INPUT 0xD0
//KEYS
#define UP 0x8000
#define DWN 0x3000
#define PC 0x1F00
#define TV 0x3FC0
void CLRTIME (void)
{
INTCONbits.TMR0IF = 0;
TMR0L = 0;
TMR0H = 0;
}
unsigned int TMR0(void)
{
unsigned int TIME = 0;
unsigned int TIMEL = 0;
unsigned int TIMEH = 0;
TIMEL = TMR0L;
TIMEH = TMR0H;
TIMEH <<=8;
TIME = (TIMEL | TIMEH);
//CLRTIME();
return TIME;
}
void main(void)
{
unsigned int KEY;
//OSCCON = 0x72;
//while(!OSCCONbits.IOFS);
// CMCON = 0x07;
ADCON0 = 0x01; //AN0 is ANALOG //Fastest
ADCON1 = 0x0E;
TRISA = 1;//AN0 = INPUT
TRISB = 0;
TRISC = 0;
T0CON = 0x88;
while(1)
{
KEY=GetAN0();
switch(KEY)
{
case UP:
SendNEC(ADDRESS, VOLUP);
break;
case DWN:
SendNEC(ADDRESS, VOLDN);
break;
case PC:
SendNEC(ADDRESS, INPUT);
Delay_mS(500);
SendNEC(ADDRESS, INPUT);
break;
case TV:
SendNEC(ADDRESS, INPUT);
Delay_mS(500);
break;
}
//SendNEC(ADDRESS, VOLUP);
Delay_mS(100);
}
}
unsigned int GetAN0(void)
{
volatile unsigned int temp = 0;
volatile unsigned int temp2 = 0;
ADCON0bits.GO = 1;
Delay10TCYx(2);
while(ADCON0bits.DONE != 0);
temp2 = ADRESL;
temp = ADRESH;
temp = temp << 8;
temp |= temp2;
return temp;
}
void Delay_mS(int x)
{
while(x)
{
Delay1KTCYx(2);
x--;
}
}
void Delay_uS(int x)
{
while(x)
{
Nop();
x--;
}
}
void SendNEC(unsigned char add, unsigned char cmd)
{
unsigned char x;
unsigned char addI = ~add;
unsigned char cmdI = ~cmd;
PulseStart(); //Delay 9ms HIGH then Delay 4.5ms LOW
for(x=0;x<8;x++) //Send add Byte
{
Pulse560();
if(0x80 & add)
Pulse1();
else
Pulse0();
add <<= 1;
}
for(x=0;x<8;x++)//Send add Byte inverted
{
Pulse560();
if(0x80 & addI)
Pulse1();
else
Pulse0();
addI <<= 1;
}
for(x=0;x<8;x++)//Send cmd Byte
{
Pulse560();
if(0x80 & cmd)
Pulse1();
else
Pulse0();
cmd <<= 1;
}
for(x=0;x<9;x++)//Send cmd Byte inverted
{
Pulse560();
if(0x80 & cmdI)
Pulse1();
else
Pulse0();
cmdI <<= 1;
}
//IRPIN = 0; //END LOW
}
////////////////////////////////////
// START
////////////////////////////////////
void PulseStart(void)
{
unsigned char x,y,z = 0;
for(y=0;y<16;y++){
Pulse560();
}
for(y=0;y<8;y++){
for(x=0;x<22;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
}
void Pulse0 (void)
{
unsigned char x,y = 0;
for(x=0;x<22;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
void Pulse1 (void)
{
// Pulsed 2.25mS of 38KHz data
unsigned char x,y = 0;
for(x=0;x<66;x++){
TMR0L = 0;
IRPIN = 0;
while(TMR0L < 223);
}
}
void Pulse560 (void)
{
// Pulsed 560uS
unsigned char x = 0;
for(x=0;x<21;x++){
IRPIN = 1;
TMR0L = 0;
while(TMR0L < WaitA);
IRPIN = 0;
TMR0L = 0;
while(TMR0L < WaitB);
}
TMR0L = 0;
}