Help with setting up ADC and pic12f683 code

Discussion in 'Embedded Systems and Microcontrollers' started by Daemon, Jun 17, 2011.

  1. Daemon

    Thread Starter Active Member

    Jun 20, 2009
    Hi all

    I have some code I need to test and would like a little help.
    It does not entirely function as I originally intended and I need to make sure I understand the flow of the data as I had much help with it.

    The code is here View attachment SimpleTimer-2.asm.txt

    The psuedo code originally was
    Is swB held while device powered on
    if so do adc conversion and store result in EEPROM
    if not read EEPROM for lvc setting and monitor voltage. Cutoff circuit below this value. Also read EEPROM for timer setting and led flashes and flash led to indicate timer setting.
    if swA pressed increment timer by 5 minutes and flash led a number of times to represent the time interval set. Copy both timer setting and led flashes to EEPROM and save for future use next time device powered on.
    if swA incremented past 6 go back to 1
    if swB pressed then start 1 minute timer to allow moving device into water then start timer interval for relay afterwards
    if swB held again turn device off

    and as it has evolved it has become

    Read EEPROM for flashes, time on and time off and put display results on led
    If swA pressed increment flashes and use same value to increment timer and store in EEPROM then flash led corresponding number of times and debounce button
    If swB pressed then setup 6 minute timer? and copy to EEPROM and then flash led again?

    There seem to be a few issues as I see it
    Can someone confirm, all the EEPROM locations read at the beginning seem to be the same address. How does the code differentiate between reading flashes, offtimer and on timer?
    What is the purpose of the ontimer and how long is it set for?
    I have built the circuit and tested it and it seems to increment the timer and flashes each time I power on and off.
    If it does the adc conversion at the start of the code as it presently does then it will keep setting the current battery voltage as the low voltage cutoff point and never run the timer wont it?

    I need to sort this out and also get help setting up the first low voltage cutoff setting. I have a dc power pack that outputs 8.9V so I was going to use that as the first setting point for the low voltage cutoff.

    Any help with this would be appreciated.
  2. Markd77

    Senior Member

    Sep 7, 2009
    Just spotted that timeon will never end unless a button is pressed. The goto $-.11 goes to the point that sets loops to 58. Should be goto $-.09.
    Time to start drinking now, but will have another look tomorrow.
  3. Daemon

    Thread Starter Active Member

    Jun 20, 2009
    Nice comment. Not because of me I hope?
    Thanks and happy drinking
  4. Markd77

    Senior Member

    Sep 7, 2009
    It's just that time of night.
  5. be80be

    AAC Fanatic!

    Jul 5, 2008
    Here you some thing to play with
    Code ( (Unknown Language)):
    2.     ;;;;;By be80be ;;;;;;;;;;;;;;;
    3.     list      p=12f683      ; list directive to define processor
    4.        #include   <>      ; processor specific variable definitions
    5.        errorlevel -302 ; Turn off banking message
    6.                                 ; known tested (good) code
    9.        #define _1VOLT 0x33
    10.        #define _2VOLT 0x66
    11.        #define _2_5VOLT 0x7F
    12.        #define _3VOLT 0x99
    13.        #define _4VOLT 0xCC
    14.        #define _5VOLT 0xff
    15.        CBLOCK       0x020
    16.         ADC_hold
    17.        endc
    19.        ORG         0x000      ; processor reset vector
    20.          goto      init      ; go to beginning of program
    23.        ORG         0x004       ; interrupt vector location
    26. init:
    27.          bsf       STATUS,RP0     ; select Register Page 1
    28.          movlw     0x72
    29.          movwf       OSCCON
    30.          bsf       STATUS,RP0     ; select Register Page 1
    31.          movlw     0xFF
    32.          movwf     TRISIO          ; Make PortA all input
    33.          movlw     0x71       ;ADC Frc clock,
    34.          iorwf     ANSEL      ; and GP0 as analog
    35.          BANKSEL   ADCON0     ;
    36.          movlw     0x81        ;Right justify,
    37.          movwf     ADCON0     ;Vdd Vref, AN0, On
    38.          bcf       STATUS,RP0     ; back to Register Page 0
    40.          bcf       STATUS,RP0     ; address Register Page 2
    41.          bsf       STATUS,RP1    
    42.          movlw     0x01           ;  Port pins Analog
    43.          movwf     ANSEL
    44.          bcf       STATUS,RP0     ; address Register Page 0
    45.          bcf       STATUS,RP1
    48. MainLoop:
    49.          call   adcdelay        ;delay to charge cap
    50.          bsf       ADCON0,GO      ; start conversion
    51.          btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
    52.          goto      $-1
    54.         movf     ADRESH,w       ; Copy the display to the LEDs
    55.         movlw    _1VOLT
    56.         subwf    ADRESH, w
    57.        BNC    LessThan1V                ; Branch if less than 1V
    60.         movlw    _2VOLT
    61.         subwf    ADRESH, w
    62.        BNC    LessThan2V                ; Branch if Between 1V and 2V
    65.         movlw    _2_5VOLT
    66.         subwf    ADRESH, w
    67.        BNC    LessThan2_5V             ; Branch if Between 2V and 2.5V
    70.         movlw    _3VOLT
    71.         subwf    ADRESH, w
    72.        BNC    LessThan3V                ; Branch if Between 2.5V and  3V
    75.         movlw    _4VOLT
    76.         subwf    ADRESH, w
    77.        BNC    LessThan4V                ; Branch if Between 3V and 4V
    80.         movlw    _5VOLT
    81.         subwf    ADRESH, w
    82.        BNC    LessThan5V                ; Branch if Between 3V and 4V
    83.         goto      MainLoop
    84. LessThan1V:
    85.        movlw   b'00000001'
    86.        movwf   ADC_hold
    87.        goto   MainLoop
    88. LessThan2V
    89.        movlw   b'00000010'
    90.        movwf   ADC_hold
    91.        goto   MainLoop
    93. LessThan2_5V:
    94.        movlw   b'00000100'
    95.        movwf   ADC_hold
    96.        goto   MainLoop
    97. LessThan3V:
    98.         movlw   b'00001000'
    99.        movwf   ADC_hold
    100.        goto   MainLoop
    102. LessThan4V:
    103.        movlw   b'00010000'
    104.        movwf   ADC_hold
    105.        goto   MainLoop
    107. LessThan5V:
    108.        movlw   b'00100000'
    109.        movwf   ADC_hold
    110.        goto   MainLoop
    112. adcdelay:
    113.         nop                 ;1 cycle
    115.         return                 ;4 cycles (including call)
    117.          end
  6. Daemon

    Thread Starter Active Member

    Jun 20, 2009
    Thanks for that