/* **************************************************************************
; *
; Filename: SIRC *
; Date: May 29, 2009 *
; File Version: 001 *
; *
; Author: Jason Lopez *
; Company: AtomSoft *
; *
;****************************************************************************
; Notes:
; I am delaying in this code a nice even number every 200uS. Mainly because if
; i count each pulse it would be accurate but too many counts for a char type
; and i dont want to use a int. So i can get a 600 count by skipping every 300uS
; and the count will be a nice low number...3
;
; For a 1.2mS aka HIGH i should get a count of 6 and for the start 2.4mS i should
; get a count of 12. Since this isnt too accurate i range it about 1-2 numbers
; above or below the standard.
;
;****************************************************************************
*/
#pragma CLOCK_FREQ 4000000
#include <system.h>
#include <PIC12F629.h>
#pragma DATA _CONFIG, _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
unsigned char lTime;
unsigned char MyAdd;
unsigned char MyCmd;
unsigned char MainAdd;
unsigned char MainCmd;
#define irPin gpio.GP5
#define RecordIt gpio.GP2
#define RelayPin gpio.4
#define progStat gpio.1
void main(void);
void GetSIRC(unsigned char *address, unsigned char *command);
char WriteEE(char address, char data);
char ReadEE(char address);
char ReadEE(char address){
eeadr = address;
eecon1.RD = 1;
return eedata;
}
char WriteEE(char address, char data){
char tmp;
eeadr = address;
eedata = data;
eecon1.WREN = 1;
eecon2 = 0x55;
eecon2 = 0xAA;
eecon1.WR = 1;
while(!pir1.EEIF);
pir1.EEIF = 0;
tmp = ReadEE(address);
if(tmp == data)
return 0;
else
return 1; //ERROR
}
void main(void){
char tmp;
char IsSet = 0;
cmcon = 7; // comparator off, digital I/O
trisio = 0b00100100; // GP0 output, GP1 input
gpio = 0; // make all outputs '0'
delay_ms(250);
if(RecordIt == 0){
progStat = 1;
GetSIRC(&MyAdd,&MyCmd);
if(WriteEE(0x00,MyAdd))
goto MainApp;
if(WriteEE(0x01,MyCmd))
goto MainApp;
progStat = 0;
delay_ms(250);
progStat = 1;
delay_ms(250);
progStat = 0;
delay_ms(250);
progStat = 1;
delay_ms(250);
progStat = 0;
delay_ms(250);
}
MainApp:
MainAdd = ReadEE(0x00);
MainCmd = ReadEE(0x01);
while(1){
GetSIRC(&MyAdd,&MyCmd);
if(MyAdd == 0x01)
if(MyCmd == 0x00){
if(IsSet == 0){
RelayPin = 1;
IsSet = 1;
} else {
RelayPin = 0;
IsSet = 0;
}
}
delay_ms(250);
delay_ms(250);
}
}
void GetSIRC(unsigned char *address, unsigned char *command){
unsigned char ir_add;
unsigned char ir_cmd;
char x;
StartLook:
ir_add = ir_cmd = 0;
while(irPin); //wait for it to be low
lTime = 0; //reset the counter
while(irPin == 0){ //while the pin is low which is our pulse count
lTime++; //increment every 200uS until pin is high
delay_100us(2); //200uS delay
}
if(lTime <= 10) //Start too short
goto StartLook; //Restart
if(lTime >= 14) //Start too long
goto StartLook; //Restart
lTime = 0;
for(x=0;x<7;x++){ //repeat 7 times for command
ir_cmd >>= 1; //if it was skipped or is done ORing then shift over the 1
while(irPin); //wait for it to be low
lTime = 0; //reset the counter
while(irPin == 0){ //while the pin is low which is our pulse count
lTime++; //increment every 200uS until pin is high
delay_100us(2); //200uS delay
}
if(lTime >= 6) //If its high then OR a 1 in else skip
ir_cmd |= 0x40; //if its less than 6 its a 0 so dont OR it
}
for(x=0;x<5;x++){ //repeat 5 times for address/device
ir_add >>= 1; //if it was skipped or is done ORing then shift over the 1
while(irPin); //wait for it to be low
lTime = 0; //reset the counter
while(irPin == 0){ //while the pin is low which is our pulse count
lTime++; //increment every 200uS until pin is high
delay_100us(2); //200uS delay
}
if(lTime >= 6) //If its high then OR a 1 in else skip
ir_add |= 0x10; //if its less than 6 its a 0 so dont OR it
}
*address = ir_add;
*command = ir_cmd;
}