Saving COUNTs?


Closed Thread
Results 1 to 11 of 11

Thread: Saving COUNTs?

  1. #1

    Default Saving COUNTs?

    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.

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Code:
    count PORTC.5, 1000, W1
    write 1,W1
    read 1,X1
    write 2,X1
    read 2,X2
    Should be

    Code:
    count PORTC.5, 1000, W1
    write 1,W1.byte0
    write 2,W1.byte1
    read 1,X1
    read 2,X2
    Al.
    All progress began with an idea

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    You're only saving the latest count.

    Try adding the new count to another var on each pass;
    Code:
    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
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Correction. Should have been W1 is greater than the constant.

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    At least, do you have some readings?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Yes, sorry. The last program displays 15, which is W1-60. But it does not become 30,45,60,75, etc.

  9. #9
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Try this code

    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.
    All progress began with an idea

  10. #10


    Did you find this post helpful? Yes | No

    Default

    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.

  11. #11


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. ADC Max Impendance and Power saving
    By Michael Wakileh in forum Schematics
    Replies: 3
    Last Post: - 28th June 2009, 19:29
  2. 16F to 18F context saving differences
    By brittons in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 1st April 2007, 03:40
  3. FYI Daylight Saving Time in the U.S.
    By ccsparky in forum General
    Replies: 1
    Last Post: - 7th November 2005, 15:05
  4. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10
  5. I think my interrupt is not saving context properly?
    By royly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th December 2004, 17:21

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts