Arduino IR problem

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
78L05
 
Posts: 1
Joined: Mon Dec 23, 2013 8:02 am

Arduino IR problem

Post by 78L05 »

Hello everybody,
I'm not sure this is correct place to ask my question, but I will try.
I want to decode IR signal from one remote controller and then play it with some other circuit which use PIC microcontroller.
I used this code to decode IR signal:

Code: Select all


/* Raw IR decoder sketch!
This sketch/program uses the Arduno and a PNA4602 to
decode IR received. This can be used to make a IR receiver
(by looking for a particular code)
or transmitter (by pulsing an IR LED at ~38KHz for the
durations detected
Code is public domain, check out www.ladyada.net and adafruit.com
for more tutorials!
*/

// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN PIND
#define IRpin 2
// for MEGA use these!
//#define IRpin_PIN PINE
//#define IRpin 4

// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000

// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20

// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2]; // pair is high and low pulse
uint8_t currentpulse = 0; // index for pulses we're storing

void setup(void) {
  Serial.begin(9600);
  Serial.println("Ready to decode IR!");
}

void loop(void) {
  uint16_t highpulse, lowpulse; // temporary storage timing
  highpulse = lowpulse = 0; // start out with no pulse length
  
  
// while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
     // pin is still HIGH

     // count off another few microseconds
     highpulse++;
     delayMicroseconds(RESOLUTION);

     // If the pulse is too long, we 'timed out' - either nothing
     // was received or the code is finished, so print what
     // we've grabbed so far, and then reset
     if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  // we didn't time out so lets stash the reading
  pulses[currentpulse][0] = highpulse;
  
  // same as above
  while (! (IRpin_PIN & _BV(IRpin))) {
     // pin is still LOW
     lowpulse++;
     delayMicroseconds(RESOLUTION);
     if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][1] = lowpulse;

  // we read one high-low pulse successfully, continue!
  currentpulse++;
}

void printpulses(void) {
  Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
  for (uint8_t i = 0; i < currentpulse; i++) {
    Serial.print(pulses[i][0] * RESOLUTION, DEC);
    Serial.print(" usec, ");
    Serial.print(pulses[i][1] * RESOLUTION, DEC);
    Serial.println(" usec");
  }
 
  // print it in a 'array' format
  Serial.println("int IRsignal[] = {");
  Serial.println("// ON, OFF (in 10's of microseconds)");
  for (uint8_t i = 0; i < currentpulse-1; i++) {
    Serial.print("\t"); // tab
    Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
    Serial.print(", ");
    Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
    Serial.println(",");
  }
  Serial.print("\t"); // tab
  Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
  Serial.print(", 0};");
}
And I got this:

int IRsignal[] = {
900, 422,
72, 40,
68, 40,
68, 42,
66, 46,
60, 50,
60, 50,
60, 50,
58, 50,
60, 50,
58, 52,
56, 54,
56, 52,
58, 164,
54, 166,
54, 54,
56, 54,
56, 54,
54, 164,
56, 56,
54, 54,
56, 54,
56, 54,
54, 56,
54, 54,
56, 166,
54, 54,
56, 164,
56, 164,
54, 166,
54, 166,
54, 166,
54, 164,
56, 0};

When I copy this code in this file:

Code: Select all

int IRledPin =  13;
int tipka = 2;

#define NumIRsignals 68
int stanje = 0;

int IRsignal[] = {
  900, 422,
  72, 40,
  68, 40,
  68, 42,
  66, 46,
  60, 50,
  60, 50,
  60, 50,
  58, 50,
  60, 50,
  58, 52,
  56, 54,
  56, 52,
  58, 164,
  54, 166,
  54, 54,
  56, 54,
  56, 54,
  54, 164,
  56, 56,
  54, 54,
  56, 54,
  56, 54,
  54, 56,
  54, 54,
  56, 166,
  54, 54,
  56, 164,
  56, 164,
  54, 166,
  54, 166,
  54, 166,
  54, 164,
  56, 0};

void setup(void) {
  pinMode(IRledPin, OUTPUT);
  pinMode(tipka, INPUT);
  digitalWrite(IRledPin, LOW);  
}

void loop() {
  stanje = digitalRead(tipka);
  delay(1000); 

  if(stanje == 1){              
    for (int i = 0; i < NumIRsignals; i+=2) {        
      pulseIR(IRsignal[i]*10);             
      delayMicroseconds(IRsignal[i+1]*10); 
    }
  }            
}


void pulseIR(long microsecs) {
  while (microsecs > 0) {
    digitalWrite(IRledPin, HIGH); 
    delayMicroseconds(10);        
    digitalWrite(IRledPin, LOW);   
    delayMicroseconds(10);        

    microsecs -= 26;
  }
}
It works good. Problem Is, I want to write that code in mikroC or in PICBasic (as I want, for PIC).
Here is my code in mikroC:

Code: Select all

#include "16f628a.h"

void main(){
CMCON = 0x07;
const int signal[68] = {900, 422,72, 40,68, 40,68, 42,66, 46,60, 50,60, 50,60, 50,58, 50,60, 50,58, 52,56, 54,56, 52,58, 164,54, 166,54, 54,56, 54,56, 54,54, 164,56, 56,54, 54,56, 54,56, 54,54, 56,54, 54,56, 166,54, 54,56, 164,56,164,54, 166,54, 166,54, 166,54, 164,56,0};
float microseconds = 0;
int i = 0;
int j = 0;
int puls = 0;

PORTB=0;
TRISB=0;

delay_ms(1000);

while(1){
for(i = 0;i<68;i+=2){
microseconds = signal[i] * 10;

while(microseconds>0){
PORTB.RB2 = 1;
delay_us(10);
PORTB.RB2 = 0;
delay_us(10);
microseconds -=26;
}

puls = signal[i+1];
for(j=0;j<puls;j++) delay_us(10);
}

PORTB.RB2 = 0;
delay_ms(5000);
}

}

But this doesn't work.
Please help, where is error in my microC code?

Thanks a lot

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino IR problem

Post by adafruit_support_bill »

[Moved from Arduino to Microcontrollers. The Arduino code is working. This is more of a PIC question]

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”