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 pulse measuring problems, need help?

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



Joined: 12 Jul 2007
Posts: 15

View user's profile Send private message

IR remote pulse measuring problems, need help?
PostPosted: Mon Aug 10, 2009 2:25 pm     Reply with quote

Hello to everyone!
I try to write some code for IR remote control decoding. After a lot of coding I think that I locate the problem which I can demonstrate with following short code snippet:
Code:

#include "D:\Projekti_CCS\LED_16F88\led16F88.h"

int8 counter=0;
int8 flag=0;

#int_EXT
void isr_EXT(void) {
   output_toggle(PIN_A0);
   if(counter==32) {
      disable_interrupts(INT_EXT);
      //counter=0;
      flag=1;
      output_toggle(PIN_A1);
   }
   counter++;
   clear_interrupt(INT_EXT);
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   ext_int_edge(0,H_TO_L);
   clear_interrupt(INT_EXT);
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);

   while(TRUE) {
      if(flag==1) {
         disable_interrupts(INT_EXT);
         flag=0;
         counter=0;
         output_toggle(PIN_A2);
         clear_interrupt(INT_EXT);
         enable_interrupts(INT_EXT);
      }
   }

}


I'm not able to count number of falling edges with RB0 interrupt correctly. You can see screenshots of different counter variable values. The code is working only for counter==2,3 and 5. But for counter==32 I always lose synchronism and debugging pins A1 and A2 are not toggling after 32 pulses. Am I missing something important?
Thanks for any help!
http://rapidshare.com/files/265933456/counter0.jpg.html
http://rapidshare.com/files/265933457/counter1.jpg.html
http://rapidshare.com/files/265933458/counter2.jpg.html
http://rapidshare.com/files/265933459/counter5.jpg.html
http://rapidshare.com/files/265933460/counter32.jpg.html
Ttelmah
Guest







PostPosted: Mon Aug 10, 2009 2:54 pm     Reply with quote

Obvious comment.
With a test for 'counter==32', counter starting at zero, and incremented after the test, you will need 33 edges for the 'end' to be reached, not 32....

Then some minor comments.
You don't need to clear the interrupt at the end of the handler. The compiler does this for you, unless told not to.
You don't need to disable the interrupt in the 'main', when flag==1. It has already been disabled, if flag is 1.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 10, 2009 4:44 pm     Reply with quote

Code:
 setup_spi(SPI_SS_DISABLED);
This is an error in the CCS project wizard and might have some strange side effects. To disable the SPI hardware change to:
Code:
 setup_spi(FALSE);
tomcuga



Joined: 12 Jul 2007
Posts: 15

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 4:05 am     Reply with quote

to Ttelmah:
You are right, but it's not important to me right now. What ever number I use for counter I can't get precise number of edges. PIN_A0 is toggling on every falling edge of signal on RB0 which is OK but PIN_A1 is toggling very caotic.
I'm expecting PIN_A1 to toggle after counter+1 falling edges. But it;s true only for small values of counter (1,2,3,...). For bigger values of counter behaviour is very odd - not in synchronism with signal on RB0 and different after each reset.
I'm completely confused. ISR routine is very short and fast. my shortest time between two edges is about 0.6ms which is enough "long" I think.
My PIC is 16F88 on 20MHz so instruction cycle is 0.2us and interrupt latency is about 30 instructions which is about 6us plus time in isr routine is still small enough compare to 0.6ms.
I disabled interrupt in the main because I'm calling decoding function in between which I omitted because it is not important now for describing my problem.
to Ckielstra:
I'll try this today I hope.

I'm very sad now and starting to be angry. I'm working with PICs about some 9 years now and I'm not able to count number of pulses with RB0 interrupt?!
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 7:13 am     Reply with quote

I've decoded IR pulses like this before and you should have plenty of time.

I don't see anything in your code that should change results based on the
test value of counter. But, also, your screenshot of 32 doesn't show any
problem, it is not a long enough capture. Perhaps with a large number of
(33) cycles you would see a pattern of the error that will point to the real
problem.
tomcuga



Joined: 12 Jul 2007
Posts: 15

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 8:59 am     Reply with quote

but screenshot for counter value of 32 shows that PIN_A2 is toggled after 28 edges which is wrong. The following screenshots shows only first press of remote button is OK (this was immediately after PIC reset) but every following button press toggles A2 earlier and earlier in time - and completely random?!

http://rapidshare.com/files/266207288/counter33_first_press.jpg.html
http://rapidshare.com/files/266207289/counter33_second_press.jpg.html
http://rapidshare.com/files/266207290/counter33_third_press.jpg.html
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 10:31 am     Reply with quote

Notice how first press you have that strange IR repeat of 2 pulse length, then the second press you are off by 2 pulses? Third one is not off by 4, but I think that you are just getting extra IR information that is unexpected. Also, most any other remote with the same modulation nearby could add pulse count for you.

Your software seems to be working properly. Probably the stuff you took out is the timer information that times the wide pulse to let you know that the transmission is starting. You will need that or real-world will always get you out-of-sync.
tomcuga



Joined: 12 Jul 2007
Posts: 15

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 1:58 pm     Reply with quote

This is my complete code for now. It is very ugly at some places and functionality is very poor unfortunately. Sometimes I need to press button 5 to 10 times and nothing, sometimes every press is decoded OK (rarely, Crying or Very sad ). So I'm still very unhappy. There are a lot of place for improvements so every ideas, suggestions are very very well come!
Code:

#include "D:\Projekti_CCS\LED_16F88\led16F88.h"

#byte T1CON=0x10
#bit TMR1ON=T1CON.0

int8 counter=0;
int8 flag=0;
int16 rx[35];
int16 timer;
int8 overflow=0;

#int_EXT
void isr_EXT(void) {
   TMR1ON=0;
   timer=get_timer1();        //first timer data rx[0] will be garbage
   //output_toggle(PIN_A0);
   rx[counter]=timer;
   set_timer1(0);
   TMR1ON=1;
   counter++;
   if(counter==2) {           //testing lead-in time
      if((rx[counter-1]<8166) || (rx[counter-1]>8563))
         counter=0;           //reset if not in some limits
   }
   if(counter==36) {
      disable_interrupts(INT_EXT);
      //counter=0;
      flag=1;
      //output_toggle(PIN_A1);
   }
}

void decode() {
  int32 command=0x00000000;
  int32 kurva=0x80000000;
  int32 mask=0x00000000;
  int8 k;
  int16 temp;
  for(k=2;k<=33;k++) {
      mask=kurva>>(k-2);
      if(rx[k]>1300) {    //"1"
         command=command|mask;
      }
   }
  //printf(" %Lx ",command);
  //SmartVision
  if(command==0x00FFF807)  //"1"
      output_toggle(PIN_B1);
  if(command==0x00FFD827)  //"2"
      output_toggle(PIN_B2);
  if(command==0x00FFE817)  //"3"
      output_toggle(PIN_B3);
  if(command==0x00FF3AC5)  //"4"
      output_toggle(PIN_B4);
  if(command==0x00FFFA05) //"OSD"
  {
      output_toggle(PIN_B1);
      output_toggle(PIN_B2);
      output_toggle(PIN_B3);
      output_toggle(PIN_B4);
  }
}

void main()
{
   int8 k;
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   for(k=0;k<=34;k++) {
      rx[k]=11;
   }
   
   ext_int_edge(0,H_TO_L);
   clear_interrupt(INT_EXT);
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);

   while(TRUE) {
      if(flag==1) {
         disable_interrupts(INT_EXT);
         //for(k=0;k<=34;k++) {
         //   printf(" %Lu ",rx[k]);
         //}
         decode();
         flag=0;
         counter=0;
         output_toggle(PIN_A2);
         clear_interrupt(INT_EXT);
         enable_interrupts(INT_EXT);
      }
  }

}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 3:08 pm     Reply with quote

There is probably code on the forum or on the net for this.
Tell us what manufacturer, model number and IR protocol used by
your transmitter.
tomcuga



Joined: 12 Jul 2007
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 11:19 am     Reply with quote

Yes, I found very similar code from author newguy here:

http://www.ccsinfo.com/forum/viewtopic.php?t=21290&highlight=leadin+sequence

This author decodes each bit immediately after reception. Maybe this is better approach I don't know.

I don't know exactly the type of remote - it's SmartVision's DVD remote. Protocol is similar to Sony as far as I can see on my logic analyzer device.
Lead-in sequence is about 13.4ms (about 9ms low, rest high). Logic low is 0.6ms low and 0.5ms high and logic one is 0.6ms low and 1.6ms high approximately.

Still no improvements with my code. Crying or Very sad
tomcuga



Joined: 12 Jul 2007
Posts: 15

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 1:36 pm     Reply with quote

I tried the code of Newguy which I referenced in last mail. The symptoms are very similar: Sometimes (often) it is necessary to press button more times to decode successfully.
I also noticed some sporadic pulses on TSOP1738 data pin which makes false RB0 interrupts. I can see that pulses on scope. I also toggle a LED immediately after entering ISR routine. And I noticed that when LED is in one state the decoding is working OK, each time when LED changes state I have problem with decoding and must press button a lot of times to decode it.
So I think problem is hardware - noise on TSOP sensor but I don't know how to solve this. I put 100ohm in power supply line and 4.7uF parallel to sensor power pins as in datasheet but still have those pulses. I also turn off my desk light. Sad
alfonsol
Guest







PostPosted: Thu Aug 27, 2009 3:39 pm     Reply with quote

Can you show your schematic?

Try putting 0,1uf caps between the supply pins of the microcontroller and ground. Do the same with the tsop1738. It is very important that the caps are as close as you can to the pin.

Also configure all the pins of the microcontroller that you are not using as inputs and tie them to ground with a resistor.

good luck
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