PDA

View Full Version : Count?



jderson
- 20th April 2009, 20:38
I obviously don't understand how the COUNT instruction works. The PBP manual says "The resolution of Period is in milliseconds. It tracks the oscillator
frequency based on the DEFINEd OSC." In the program below, the period is 1000msec., or 1 second. When I apply a 66 hz. squarewave to RC5, I the LCD says 66. When I change the period to 2000, it says 33. Shouldn't it say 132? Also, if I change the period to 500, the LCD says 33. I don't get it.

;****16F946:

Pause 10 ;for ICSP
@ __config _WDT_OFF & _CP_OFF & _PWRTE_ON & _MCLRE_ON & _INTRC_OSC_NOCLKOUT

OPTION_REG = %11000000 ;pullups disabled, rising edge, no prescale
ANSEL = 0 ;all pins digital
CMCON0 = 7 ;comparators off
ADCON0 = 0 ;ADC's off
WDTCON = 0 ;WDT off

TRISA = %00000000
TRISB = %11000001 ;switches on RB6,RB7, INT on RB0
TRISC = %00000000 ;LED on RC5, LCD bias on RC3
TRISD = %00000000
TRISE = %00001000 ;MCLR on RE3
TRISF = %00000000
TRISG = %000000

OSCCON = %01100001 ;4mz. mhz. internal

LCD_DB4 VAR PORTB.1
LCD_DB5 VAR PORTB.2
LCD_DB6 VAR PORTA.7
LCD_DB7 VAR PORTA.6
LCD_RS VAR PORTB.3
LCD_E VAR PORTD.2
LCD_Lines CON 1 ' # of Lines on LCD, 1 or 2 (Note: use 2 for 4 lines)
LCD_DATAUS CON 2 ' Data delay time in us
LCD_COMMANDUS CON 20 ' Command delay time in us

INCLUDE "LCD_AnyPin.pbp" ; *** Include MUST be AFTER LCD Pin assignments ****

define OSC 4

W1 var WORD
X1 var word
X1 = 0

PORTC.4 = 0 ;leds off
PORTD.3 = 0

Main:

count PORTC.5, 1000, W1
lcdout $fe,1, dec W1
pause 10
goto Main

Any help will be greatly appreciated.

mister_e
- 20th April 2009, 21:13
Probably you've hit one limitation of COUNT.

Try this one


@wTimer1 = TMR1L
wTimer1 var word EXT
T1CON = %00000111
'xx--x---- Don't care
'--00----- Prescaler 1:1
'-----1--- Do not synchronized external clock input
'------1-- External clock from TOCKI pin (Rising edge)
'-------1- Enable Timer1

Main:
wTimer1=0 ' clear Timer1
pause 1000 ' acquisition time
lcdout $fe,1, dec wTimer1
pause 10
goto Main

mister_e
- 20th April 2009, 21:23
And ho, before trying the above, don't forget to set RC5 as an input first...

mister_e
- 20th April 2009, 21:36
DOH...

Should have wrote T1CKI instead of T0CKI... silly mistake... oh well, it's just comments ;)

HAVING THE EDIT BUTTON BACK WOULD BE REALLY NICE, BUT IT STILL SEEMS TO BE A REALLY TOUGH THING TO IMPLEMENT!!!

Bruce
- 20th April 2009, 22:33
the period is 1000msec., or 1 second. When I apply a 66 hz. squarewave to RC5, I the LCD says 66
Returns the same here.

When I change the period to 2000, it says 33. Shouldn't it say 132?

Yep. Just tested one with with a 66hz signal, and it does indeed return 132.


Also, if I change the period to 500, the LCD says 33.
That's what it should return.

Are you sure you're not changing anything else when you're testing it with 2000 as the
period?

jderson
- 20th April 2009, 23:24
Bruce, I just did it again with exactly the same code as posted. The ckecksum is 3174 with 1000, and 3160 with 2000. Could you check your checksums? I'll check the incoming pulses with a scope later tonight, to make sure nothing's changing there.

jderson
- 21st April 2009, 05:02
After adding the line to enable T1 within the Main loop, Mr. E's program does want I need. Thank you Mr. E!

Main:

wTimer1=0 ' clear Timer1
T1CON = %00000111
pause 1000 ' acquisition time
T1CON = %00000110
lcdout $fe,1, dec wTimer1
pause 100
goto Main

However, the program that uses COUNT does not work! Maybe it's peculiar to the PIC I'm using? I did notice it has something to do with the watchdog timer, but I'm not going to mess with it further right now.

Thank you everyone for the help.

mister_e
- 21st April 2009, 15:41
Great, and it use the internal hardware, use less code space and etc.

Anyways, out of curiosity, which PBP version you're using?

jderson
- 21st April 2009, 16:20
Yes, I think it is a better solution all around! I am using 2.50b, Haven't installed the latest patch yet. Thanks again!

mister_e
- 21st April 2009, 17:26
For the record, I tried COUNT with a 16F917, and it worked regardless of the acquisition time. Hard to tell where the issue come from :confused:

Oh well, it works... if it's not broken, don't fix it :D