PDA

View Full Version : Saving COUNTs?



jderson
- 18th April 2009, 17:49
I'm trying to accumulate the number of counts that the COUNT command returns on each pass though Main. I feel really stupid, but this code always saves and displays the most recent count:

;****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 ****


W1 var BYTE
X1 var byte
X2 var byte

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

Main:

count PORTC.5, 1000, W1
write 1,W1
read 1,X1
write 2,X1
read 2,X2
lcdout $fe,1, dec W1
lcdout $fe,$c0, dec X2
pause 10
goto Main


Can someone help, please?

Thank you.

aratti
- 18th April 2009, 18:08
count PORTC.5, 1000, W1
write 1,W1
read 1,X1
write 2,X1
read 2,X2

Should be



count PORTC.5, 1000, W1
write 1,W1.byte0
write 2,W1.byte1
read 1,X1
read 2,X2


Al.

Bruce
- 18th April 2009, 18:11
You're only saving the latest count.

Try adding the new count to another var on each pass;


W1 var BYTE
X1 var byte
X1=0
PORTC.4 = 0 ;leds off
PORTD.3 = 0

Main:

count PORTC.5, 1000, W1
X1=X1+W1 ' accumulate counts
lcdout $fe,1, dec W1
lcdout $fe,$c0, dec X1
pause 10
goto Main

Acetronics2
- 18th April 2009, 18:18
Hi,

Nothing surprising :

1) you Write W1 in EEProm @1
2) you read @1 an call it X1
3) you Write X1 @2
4) you read @2 and call it X2


from that ... X2 = W1 ...

The good question is :

What did you intend to do ??? Store Odd passes result in W1 and even passes result in X2 ???

Alain

jderson
- 18th April 2009, 21:49
Thanks for the help. I'm beginning to see the light, but my head's not completely pulled out yet! I know that on each pass, X1 will be greater than a constant(60 for example). I would like to total all of these difference numbers over a period of time. I know I may have to use WORD variables, but I just want to get the BYTEs working first! This code doesn't do it:

Main:

count PORTC.5, 1000, W1
X1 = W1 - 60
lcdout $fe,1, dec W1
lcdout $fe,$c0, dec X1
pause 10
goto Main

jderson
- 18th April 2009, 21:51
Correction. Should have been W1 is greater than the constant.

mister_e
- 18th April 2009, 22:21
At least, do you have some readings?

jderson
- 18th April 2009, 22:57
Yes, sorry. The last program displays 15, which is W1-60. But it does not become 30,45,60,75, etc.

aratti
- 19th April 2009, 00:48
Try this code


count PORTC.5, 1000, W1
X1 = X1+(W1 - 60)
lcdout $fe,1, dec W1
lcdout $fe,$c0, dec X1
pause 10
goto Main

Al.

jderson
- 19th April 2009, 15:25
Arrati-
Thank you for your help. Although it looks good, your program does strange things! With no input on RC.5(LOW), X1 is all over the place, 126,76,20,216, etc., while W1 remains zero.

jderson
- 19th April 2009, 17:55
Adding the IF statement fixed it, and all is good! Thank you, Arrati.

Main:

count PORTC.5, 1000, W1
if W1 > 60 then
X1 = X1+(w1-60)
endif
lcdout $fe,1, dec W1
lcdout $fe,$c0, dec X1
pause 10
goto Main