PDA

View Full Version : Pulsin ir receiver and osc speeds



jimseng
- 23rd March 2012, 15:33
Hello with my pic running at 4 mhz this works. At 16 mhz it doesn't. It is due to the pulsin resolution but I can't work out what I need to change in order to get it working. Can someone explain it to my rather meagre brain?

Thanks

DEFINE PULSIN_MAX 1140 '>1140 RETURNS 0


IR VAR byte[4]
pulse VAR word
i VAR byte
stx VAR word 'start of transmission

init: IR[0]=0:IR[1]=0:IR[2]=0:IR[3]=0:i=0
PulsIn PORTA.4, 0, stx
If (stx<760) Then init

While PORTB.4=1:Wend 'wait space
Repeat
PulsIn PORTA.4, 1, pulse
If (pulse>100) Then
IR.0(i)=1 'set bit
EndIf
i=i+1

Until (i>31)
serout portA.5,6,[#IR[0],10,13,#IR[1],10,13,#IR[2],10,13,#IR[3],10,13]

GoTo init

mackrackit
- 23rd March 2012, 17:46
This may be a stupid question bur are you changing the OSC define to.
DEFINE OSC 16
when you change resonators or crystals

jimseng
- 23rd March 2012, 19:04
Yes. it's actually an internal osc but I understand the pulsin command doesn't care anyway.

mackrackit
- 23rd March 2012, 19:38
Correct, PULSIN works with the actual speed the MCU is running at. Are you sure the internal is setup properly... confirmed the MCU is running at the desired speed?

jimseng
- 23rd March 2012, 20:30
I am pretty sure it is. (I tested it with a blink led set to pause 2000 and it was a 2 second gap...well two Mississippis by my count. I spent a while trying to figure out the OSCCON) My understanding of it is that because the pulsein is "narrower" than when the osc is running at 4mhz the timings for reading IR signals is different.so "if stx<760" actually should be a different value. I am trying to understand the maths of what that value should be.
p.s. I know there is a typo in the code portb.4 is incorrect and I have corrected it in my actual code.

BH_epuk
- 23rd March 2012, 20:40
ok i'm sure youve checked this but have you checked the ports Analog input is turned off.


ANSELA = 0
ANSELB = 0
ANSELC = 0 ' Config A2d All OFF

jimseng
- 23rd March 2012, 21:32
I hadn't but I was using porta.4 which isn't an analogue in. I am fairly sure that my pulse length if statements are wrong because I am defo getting good stuff at 4 mhz. It's just when wang it up to 16 mhz I get nout. I think it is all about pulsin pulse widths changing with different oscillator speeds. So looking for a pulsewidth at 4 mhz is different from a pulsewidth at 16. (I have to admit that I am a bit of a newbie at both pics and Ir protocols.)

mackrackit
- 23rd March 2012, 21:36
Did you mention the part number of you MCU?

BH_epuk
- 23rd March 2012, 21:44
I'm guessing your still using the 18F24K22 ?

Check your OSCCON settings are

OSCCON = %01110010 ' Set internal 16Mhz OSC
OSCCON2 = %00001100

while OSCCON.2 = 0 : WEND ' Wait for stable flag

jimseng
- 24th March 2012, 11:26
I am indeed using the 18f24k22
OSCCON = 10000 is my setting. Hmmn.
But aren't we missing the point? It seems that all the examples I have seen on reading Ir have been at 4 mhz so the pulsin timings are based on that. Because I am trying to run my pic at 16 mhz the pulsin length is different. And further to that, if I set my osc to 4 mhz without changing anything else I get results. It seems to be all about pulsin following the osc frequency.

keithdoxey
- 24th March 2012, 19:12
"The resolution of PULSIN is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the pulse width is returned in 10us increments. If a 20MHz oscillator is used, the pulse width will have a 2us resolution. Defining an OSC value has no effect on PULSIN. The resolution always changes with the actual oscillator speed."

If you are running at 16MHz then you are 4 times faster than if you were running at 4Mhz and the resolution of the pulse will be 2.5us instead of 10us.

If you were trying to detect a pulse of 1000us or 1ms then at 4Mhz you would be looking for a count of 100. At 16Mhz you need to be looking for a count of 400 therefore if you have used an example code as the basis of your project and it is intended for 4Mhz operation you need to increase all the count values by a factor of 4.