View previous topic :: View next topic |
Author |
Message |
chrislu
Joined: 05 Mar 2019 Posts: 1
|
IR remote control on 12F675 |
Posted: Tue Mar 05, 2019 10:17 pm |
|
|
Hi bro. I'm new at writting ccs code on PIC. I want to use 12F675 to decode NEC IR CODE. But I'm trouble now, the code can compile but it doesn't work when I program the hex file into PIC12F675. Can everyone help me to check the code? thx!
PS: this code is refer to the PIC12F1822, LINK is here:http://ccspicc.blogspot.com/2016/09/nec-ir-transmitter-receiver-circuit-pic-c-code.html
code is here:
Code: |
// Extended NEC protocol IR remote control decoder using PIC12F675 CCS PIC C code
#include <12F675.h>
//#fuses NOMCLR INTRC_IO
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#use fast_io(A)
#define IR_Sensor PIN_A3
unsigned int32 ir_code;
short nec_remote_read(){
unsigned int16 count = 0;
unsigned int8 i;
// Check 9ms pulse (remote control sends logic high)
SET_TIMER1(0);
while(!input(IR_Sensor) && (count < 9500))
count = GET_TIMER1();
if( (count > 9499) || (count < 8500))
return FALSE;
// Check 4.5ms space (remote control sends logic low)
SET_TIMER1(0);
count = 0;
while((input(IR_Sensor)) && (count < 5000))
count = GET_TIMER1();
if( (count > 4999) || (count < 4000))
return FALSE;
// Read message (32 bits)
for(i = 0; i < 32; i++){
SET_TIMER1(0);
count = 0;
while(!input(IR_Sensor) && (count < 650))
count = GET_TIMER1();
if( (count > 649) || (count < 500))
return FALSE;
count = 0;
SET_TIMER1(0);
while((input(IR_Sensor)) && (count < 1800))
count = GET_TIMER1();
if( (count > 1799) || (count < 400))
return FALSE;
if( count > 1000) // If space width > 1ms
bit_set(ir_code, (31 - i)); // Write 1 to bit (31 - i)
else // If space width < 1ms
bit_clear(ir_code, (31 - i)); // Write 0 to bit (31 - i)
}
return TRUE;
}
void main() {
//setup_oscillator(OSC_4MHZ); // Set internal oscillator to 32MHz (8MHz and PLL)
output_a(0);
set_tris_a(8);
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
while(TRUE){
while(input(IR_Sensor)); // Wait until IR_Sensor pin falls
if(nec_remote_read()){
if(ir_code == 0x40BF00FF)
output_toggle(PIN_A0);
if(ir_code == 0x40BF807F)
output_toggle(PIN_A1);
if(ir_code == 0x40BF40BF)
output_toggle(PIN_A2);
if(ir_code == 0x40BF20DF)
output_toggle(PIN_A4);
if(ir_code == 0x40BFA05F)
output_toggle(PIN_A5);
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Mar 06, 2019 1:20 am |
|
|
The glaring initial difference is the clock speed.
All the timings are dependant on this. He is running his chip at 32MHz.
You are running yours at 4MHz.....
You are going to need to change all the timing values by a factor of
eight. However you may well find that even then it needs custom
tweaking to actually work, since your chip is going to be so slow
relative to the speed the data edges actually arrive. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Mar 06, 2019 6:37 am |
|
|
Also... even when you change the 'numbers' by a factor of 8, it might still not work. The internal oscillator isn't exactly 4.0000000000MHz, it's close ( +-1-2%) AND will vary over temperature and time. I don't know what the spec is for the 'timing' of the IR rcvr. If it say '+-5%' and the PIC is running at +2%, you should be OK.
For accurate timing, you need to use an external crystal and 2 caps. I'd suggest a 16MHz xtal.That will run the PIC at close to max speed and you just have to change the 'timing values' by a factor of 2 not 8.
This does use 2 I/O pins, so maybe you can't use for this project but keep it in mind for future PIC projects, especially those that use serial communications faster than 9600 Baud. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Mar 06, 2019 8:26 am |
|
|
The code here:
<http://www.ccsinfo.com/forum/viewtopic.php?t=53596&highlight=nec>
is slightly different, but coded for 4MHz.
As Temtronic says there might be an issue with timing accuracy, but the
protocol is designed to support significant timing errors, so you should be OK. |
|
|
|