PDA

View Full Version : Pulsin



jmbanales21485
- 10th June 2007, 20:49
Hello all, I am trying to count some pulses in the 12f683 pic using pulsin command. is an external oscillator necessary to utilize the pulsin command in PBP? if not what defines are necessary to make it work because so far i can't make the pic read pulses. i keep reading "0" for the pulse width. thank you all for your help.

Jose
Define Osc 4 '4mhz

TRISIO = %000100
LCD var GPIO.0
T2400 con 396 '2400 baud

lowP VAR WORD 'falling edge
hiP VAR WORD 'rising edge
widthP VAR WORD 'pulse width
freqIn VAR GPIO.2 'input pulse

loop:
pulsin freqIN, 0 , lowP 'low pulse width
pulsin freqIN, 1 , hiP 'get high pulse width

widthP = lowP + hiP
pause 5:serout2 LCD, T2400, ["start", 1, DEC (lowP), ",", DEC (hiP), ",", DEC (widthP), 0]

pause 500
goto loop

'===================
'===pulse being sent====
Define Osc 4 '4mhz

TRISIO = %000000
pulse VAR GPIO.0

loop:
HIGH pulse
pause 1
LOW pulse
pause 1
goto loop

jmbanales21485
- 10th June 2007, 21:02
by the way i RTFM but didn't understand if it was necessary or not. if i can avoid having to purchase an external oscillator it would be better.
"Defining an OSC value has no effect on PULSIN. The resolution always
changes with the actual oscillator speed." thanks

skimask
- 10th June 2007, 21:52
"Defining an OSC value has no effect on PULSIN. The resolution always changes with the actual oscillator speed." thanks

All this means is that pulsin will count as fast as the PIC is running.
An example...
Your statement is: Pause 1 and you've got DEFINE OSC 20 and a 20Mhz crystal. PBP adds in a delay loop, a few NOPs, etc, and you'll get a 1 millisecond pause.
Now, without changing the program, you switch over to a 4Mhz crystal and run the same program. The pause will be 5ms long because PBP figured you wanted a 1ms pause @ 20Mhz.
Same thing with SERIN/SEROUT, and a few other timing sensitive commands.

Pulsin is different...It runs just as fast as it can in a nice tight loop checking the pins as fast as it can. You can 'DEFINE OSC xx' all day and not change how fast pulsin runs. The only way you can change it is by changing the oscillator.

So, yes, for the best resolution with pulsin, you have to go with a faster clock speed.

jmbanales21485
- 10th June 2007, 21:59
got it! i'll get a crystal as soon as i can to get pulsin to work. thanks again skimask.

jose

skimask
- 10th June 2007, 22:06
got it! i'll get a crystal as soon as i can to get pulsin to work. thanks again skimask.

jose

The only problem with going with a faster oscillator and using pulsin is that you might end up counting too fast and max out the pulsin statement before you actually finish counting.
If you've got an idea of the maximum pulse width that you'd ever be sampling, you should base your oscillator choice on that.
Once you've got that down and working, there are other ways to count pulse widths, using a timer, a capture, and so on...

jmbanales21485
- 11th June 2007, 00:54
I got the ccp interupt working on the 12f683 and i am able to count the pulse width, period, etc with it. but i need to count the pulse on 6 different pins and i could only use the ccp interupt on the ccp pin. therefore i though i should use the "pulsin" to count the pulses so i can use multiple pins.

i could use "pulsin" on any pin and as many pins i want right? the minimum pulse width is 25us and the maximum pulse is 16.5ms on the system that im trying to interface. do you have any suggestions as to what size crystal i should get? i think im gonna stick with the pic16f84A for this project. it has 13 different I/Os 6 of which are going to be input from the pulses.

skimask
- 11th June 2007, 03:12
I got the ccp interupt working on the 12f683 and i am able to count the pulse width, period, etc with it. but i need to count the pulse on 6 different pins and i could only use the ccp interupt on the ccp pin. therefore i though i should use the "pulsin" to count the pulses so i can use multiple pins. i could use "pulsin" on any pin and as many pins i want right? the minimum pulse width is 25us and the maximum pulse is 16.5ms on the system that im trying to interface. do you have any suggestions as to what size crystal i should get? i think im gonna stick with the pic16f84A for this project. it has 13 different I/Os 6 of which are going to be input from the pulses.

You could 'steer' the different pulses to the ccp pin using a simple logic gate, something like a 2 input AND gate, one input tied to an 'enable' pin, the other input tied to the actual input pulse.

- Pulsin can be used on as many pins as can be set to inputs.

- If you used a 20Mhz oscillator, the pulsin resolution is 2us... Therefore, at 25us, you'll get either 12 (24us) or 13 (26us) counts; at 'exactly' 16.5ms (16,500 us), you'll get 8,250 counts.
(Theoretically, and if the PIC supported it, you should be able to go all the way up to 80Mhz...25us=100 counts, 16,500=33,000 counts).

dhouston
- 11th June 2007, 14:25
PulsIn ties up the processor for the duration of the pulse. Without knowing the details of the application it's hard to say for certain but it's not a good choice if you need to count pulses on multiple pins simultaneously.