Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

IR in C

Status
Not open for further replies.
"However, doing short delays in C is tricky as you don't know how long a call or a for next loop will take."
pommie ... i had a feeling about that but was not sure. BTW your code works great.

Could you explain the delay in more detail? Thanks
 
I hope this isnt too "low tech" a solution, but if Atomsoft is ready to chuck in the towel perhaps he might consider an astable 555 circuit to provide the 38khz then AND it with his SIRC output pin via a transistor?

Mark
 
I hope this isnt too "low tech" a solution, but if Atomsoft is ready to chuck in the towel perhaps he might consider an astable 555 circuit to provide the 38khz then AND it with his SIRC output pin via a transistor?

It's a really poor and pointless solution :D

It's extremely trivial, and more accurate, to do it in the PIC.

Pommies solution, using a small assembler routine to easily give the accurate timing, is a simple and effective one.
 
Could you explain the delay in more detail? Thanks

The delay is just about the shortest delay you could do in C18 as a function. When you call a function in C, where the parameter that is passed is an unsigned char, it is placed in the W register. Because it's in C and W was given a name (even though it was W) it (the compiler) places the value in memory incase you want to access it multiple times. This and the call/return takes up the 13 clock cycles.

Anyway, the easiest way to think of it is that if you pass it a value (say 23), then it will delay for value*3+13 (23*3+13=82) cycles.

Microchip do provide a delay that is in clock cycles but it just inserts lots of "nop" instructions and so eats memory. I thought this was slightly neater.

BTW, what are you intending to do with your project or is it a learing exersize?

Mike.
P.S. if anyone knows how to tell the compiler to place this code inline and therefore get rid of the 13 cycle overhead, please let me know.
 
"However, doing short delays in C is tricky as you don't know how long a call or a for next loop will take."

When working with any code to generate a timed bit stream
the simulator is a very good tool for getting the times right. Open the
LA window. What you see is what you get :)

Also: Look at the dissassembly window in MPLAB
or use the stopwatch in the MPLAB simulator.
 
P.S. if anyone knows how to tell the compiler to place this code inline and therefore get rid of the 13 cycle overhead, please let me know.

I looked at inline code a while back. C18 does not have it. BoostC does.

But you still have to deal with overhead of parameter passing. I did not check to see how the compiler worked with call by referance, that could work.

Maybe a write a delay macro?
 
Pommie i finally understand your code. I used the stop watch and i even altered it for use on a 20 MHz clock. Here it is:

Code:
void Pulse(unsigned char Bit){
unsigned char i;
    if(Bit==0){                 //send 24 blips for a zero
        for(i=0;i<24;i++){      //12 cycles
            ir=1;    //1
            Delay13p3W(7);      //1*3+13=16
            ir=0;    //1
            Delay13p3W(24);      //3*3+13=22      
        }
    }else if(Bit==1){           //48 for a 1
        for(i=0;i<48;i++){
            ir=1;
            Delay13p3W(7);
            ir=0;
            Delay13p3W(24);
        }
    }else{                      //must be a start pulse
        for(i=0;i<96;i++){      //so do 96 blips
            ir=1;
            Delay13p3W(7);
            ir=0;
            Delay13p3W(24);
        }
    }
    Delay13p3W(250);            //Do 600uS pause. 196*3+13 = 601
    Delay13p3W(250);            //do it again as we are at 8MHz
    Delay13p3W(250);
    Delay13p3W(236);
}

I only altered the Pulse code and works like a charm. I get almost on the DOT results(output)
 
Actually Im making like a touch screen Remote controll system with some added coNtrol features like my pc and maybe my lights and some other things and of course my tv, dvd, surround sound
 
Reading through the posts of this thread.

Actually Im making like a touch screen Remote controll system with some added coNtrol features like my pc and maybe my lights and some other things and of course my tv, dvd, surround sound

Reading through the posts of this thread. I've been wondering about why I should learn C. Practical examples like these are how I gain the why's of stuff. With the (why) then comes the motivation to do it.

If it were not for Pommie's insistence.

Makes me re-establish why I came here in the first place. :D


Thanks Everyone.

kv
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top