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 support@ccsinfo.com

IR decoding question

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



Joined: 16 Sep 2008
Posts: 51

View user's profile Send private message Send e-mail

IR decoding question
PostPosted: Sun Oct 26, 2008 5:17 am     Reply with quote

Hello 2 all !

I'm trying to decode the "header" part of a REC80 IR protocol.
This header contains a 9 ms low portion and a 4,5 ms high portion.
An example is illustrated in the code below.
My problem is that although I am able to succesfully recognize the first 9 ms low portion of the transmission, I can't recognize the second part (the 4,5 ms high).

I am using a pic18f452, with a 4.4 MHZ oscillator and using CCS C version 4.057.

I think that somewhere is a trivial mistake but I can't figure it out !! Crying or Very sad

P.S. The example code was provided by this post :http://www.ccsinfo.com/forum/viewtopic.php?t=36124&highlight=wireless

THANK YOU !!

Code:


//This is a test program for REC80 IR protocol.
//Trying to determine the "header" part of a IR transmission.
//Transmision example:
//------\____________/------------\__/--\__/--\__/--\__/--\__/--\__
//(idle)| header low  header high | the rest of the transmission |
//         (9 ms)       (4,5 ms)
//Hardware -
// TSOP1738 on Pin B0
// TSOP data is inverted; it idles high.
// [--\_ : negative going edge; _/-- : positive going edge]
//
// Original author :Rohit de Sa
// Modifyed by : Andrew83


#include <18F452.h>
#fuses HS,NOPROTECT,NOOSCSEN,BROWNOUT,BORV45,NOWDT,WDT128,PUT,NOSTVREN,NODEBUG,NOLVP,WRT,NOCPB,WRTB,WRTC,NOCPD,NOWRTD,NOEBTR,NOEBTRB
#use delay(clock=4.4MHZ) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use fast_io(b)

#define start0_min        8000      //no of counts to safely detect a "0" start
#define start0_max        12000     //optimal @1,1 MIPS is 9900
#define start1_min        3000      //no of counts to safely detect a "1" start
#define start1_max        6000      //optimal @1,1 MIPS is 4950
#define tsop             pin_b0


int16 start_address=0;              //ROM address of 'Start' label

#int_timer1                         //if timeout error
void timer1_isr()
{
        *0xffd=start_address;      //modify return address in TOS
        *0xffe=start_address/256;
}
       

void main()
{
       
        int16 timer_value=0;
        //int1 irdone=false;
       
        delay_ms(100);                                                //setting up PIC
        setup_adc_ports(no_analogs);
        setup_adc(adc_off);
        set_tris_b(0b00000001); 
       
        start_address=label_address(start_ir);                  //ROM address of IR detection routine
       
        delay_ms(100);

        setup_timer_1(t1_internal|t1_div_by_1);
        disable_interrupts(int_timer1);
        enable_interrupts(global);
       
       
     while(1)
        {
start_ir:
                disable_interrupts(int_timer1);
                clear_interrupt(int_timer1);
               
                while(1)
                {       
                        while(input(tsop));                                               //polling the input RB0
                        set_timer1(0);                                                    //set timer1 to zero
                        enable_interrupts(int_timer1);                                    //enable timer1 interrupt     
                        delay_us(10);                                                     //settling delay
                        while(!(input(tsop)));                                            //fall of signal detected
                        timer_value = get_timer1();                                       //get timer1 value
                        set_timer1(0);                                                    //reset timer1 to zero
                        if (!((start0_min < timer_value) && (timer_value < start0_max)))  //compare values to see if it's a valid 9 ms "zero" duration
                        {
                         printf("\nNo first part of signal header !");                    //first part of header not detected
                                    //break;
                                    }
                         else
                        { printf("\nDetected a 0 pulse value:%lu",timer_value); }         // print value for "0" duration
                         
                        delay_us(10);                                                     //settling delay
                        while(input(tsop));                                               // if the input RB0 is high again
                        timer_value = get_timer1();                                       //get timer1 value
                        set_timer1(0);                                                    //reset timer1 to zero
                        if (!((start1_min < timer_value) && (timer_value < start1_max)))  //compare values to see if it's a valid 4,5 ms "one" duration
                        {
                          printf("\nNo second part of signal header !");                  //second part of header not detected
                                  //break;
                                  }
                         else
                        { printf("\nDetected a 1 pulse value:%lu",timer_value); }        // print value for "1" duration
                       
                                           
                        }
                     
                }
        }



 
Andrew83



Joined: 16 Sep 2008
Posts: 51

View user's profile Send private message Send e-mail

PostPosted: Sun Oct 26, 2008 5:38 am     Reply with quote

Just a little add-on to what i've stated.

The results i'm getting from my test are :

- for a low duration (9 ms) a counter value of 10057 -> almost spot on
- for a high duration (4,5 ms) a counter value of about 33681 ->this is where the problem lies : it shold be much much less.

According to my calculations, the counter value should be around 4950 or so. Rolling Eyes
ckielstra



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

View user's profile Send private message

PostPosted: Sun Oct 26, 2008 7:18 am     Reply with quote

Code:
{ printf("\nDetected a 0 pulse value:%lu",timer_value); }
At 9600 baud it takes about 1ms to transmit each character.
+/- 30 characters = 30ms = 33000 timer1 ticks, very close to your measured value.

Instead of sending a text try setting one or more LEDs for debugging info.
Andrew83



Joined: 16 Sep 2008
Posts: 51

View user's profile Send private message Send e-mail

PostPosted: Mon Oct 27, 2008 1:20 am     Reply with quote

Thank you ckielstra Wink ! ..I will try this solution and let you know !
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Oct 27, 2008 1:37 am     Reply with quote

As pointed out, directly sending text through RS232 during time critical program section can't work.
If the required information is mainly in the measured time value, you can also store it in a variable and
print it after finishing the timing measurement.

Another general solution is by operating RS232 output buffered and interrupt driven through a FIFO.
Andrew83



Joined: 16 Sep 2008
Posts: 51

View user's profile Send private message Send e-mail

PostPosted: Mon Oct 27, 2008 3:14 am     Reply with quote

Works flawless ! Thak you again for your quick answers ! This field is very new to me, but I am willing to learn. Wink
Quote:
Another general solution is by operating RS232 output buffered and interrupt driven through a FIFO.

I would like to know more about how this method could be implemented.
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