CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

IR remote control on 12F675

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
chrislu



Joined: 05 Mar 2019
Posts: 1

View user's profile Send private message

IR remote control on 12F675
PostPosted: Tue Mar 05, 2019 10:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 06, 2019 1:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 06, 2019 6:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 06, 2019 8:26 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group