PDA

View Full Version : pulsein question (units of measure?)



nomad
- 24th October 2007, 23:21
I know the resolution for pulsein is determined upon the oscillator frequency, but I thought the unit of measure was in microseconds. yet my code produces 2 different results, leading me to think that resolution is actually the unit of measure. (ie: a pulsein measurement of 250 with a 20mhz crystal is actually 500us?)



@ DEVICE pic16F88, HS_OSC ' osc setting
@ DEVICE pic16F88, WDT_ON ' Watchdog Timer
@ DEVICE pic16F88, PWRT_ON ' Power-On Timer
@ DEVICE pic16F88, MCLR_OFF ' Master Clear Options (Internal)
@ DEVICE pic16F88, BOD_ON ' Brown-Out Detect
@ DEVICE pic16F88, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F88, CPD_OFF ' data protect
@ DEVICE pic16F88, PROTECT_OFF ' code protect

define osc 20

PORTB = 0
PORTA = 0
TRISB = 0
TRISA = 255
CMCON = 7
ADCON0 = 0
ADCON1 = 0
ansel = 0
x var WORD
FREQ VAR WORD
w1 var word
w2 var word
TEMP VAR WORD

' Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 4
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 2
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 0
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50

LCDOUT $fe,1
Pause 500 ' Wait for LCD to startup
Lcdout $fe,1, "Speed Adjuster" ' Clear LCD screen start line 1
LCDOUT $FE,$C0," GIDDYUPGO.NET" ' goto start line 2
pause 5000
LCDOUT $FE,1

Start:
PULSIN PORTA.1,1,W1
PULSIN PORTA.1,0,W2
X=W1+W2 'period = high + low
TEMP=1000
TEMP=TEMP*TEMP 'get 1000000 for div32
FREQ = DIV32 X 'freq = 1000000 / period(in .us)
lcdout $fe,$80,"FRQ pulse ",#freq
PAUSE 100
count PORTA.1,250,w1
FREQ = W1*4
lcdout $fe,$c0,"FRQ count ",#FREQ
PAUSE 1000
goto start

dhouston
- 25th October 2007, 00:37
The manual gives the units. It varies with the oscillator frequency.

4MHz = 10µS
20MHz = 2µS

which works out to 40/fOsc.

nomad
- 25th October 2007, 00:59
Actually the manual gives the resolution, but does not specify the units of measure.
(I've read the manual)



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.


they probably could clarify that part a bit.
Thanks for your reply.

Edit: that 40/fOsc should come in handy when I cannot find a standard crystal. ( Have lots of 10.24Mhz etc lying about) Thanks