Help with setting up ADC and pic12f683 code

Thread Starter

Daemon

Joined Jun 20, 2009
33
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 hereView 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.
 

Markd77

Joined Sep 7, 2009
2,806
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.
 

be80be

Joined Jul 5, 2008
2,395
Here you some thing to play with
Rich (BB code):
    ;;;;;By be80be ;;;;;;;;;;;;;;;
    list      p=12f683      ; list directive to define processor
       #include   <P12F683.inc>      ; processor specific variable definitions
       errorlevel -302 ; Turn off banking message
                                ; known tested (good) code

       __CONFIG    _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
       #define _1VOLT 0x33
       #define _2VOLT 0x66
       #define _2_5VOLT 0x7F
       #define _3VOLT 0x99
       #define _4VOLT 0xCC
       #define _5VOLT 0xff
       CBLOCK       0x020
        ADC_hold
       endc
       
       ORG         0x000      ; processor reset vector
         goto      init      ; go to beginning of program


       ORG         0x004       ; interrupt vector location

    
init:
         bsf       STATUS,RP0     ; select Register Page 1
         movlw     0x72
         movwf       OSCCON
         bsf       STATUS,RP0     ; select Register Page 1
         movlw     0xFF
         movwf     TRISIO          ; Make PortA all input
         movlw     0x71       ;ADC Frc clock,
         iorwf     ANSEL      ; and GP0 as analog
         BANKSEL   ADCON0     ;
         movlw     0x81        ;Right justify,
         movwf     ADCON0     ;Vdd Vref, AN0, On
         bcf       STATUS,RP0     ; back to Register Page 0

         bcf       STATUS,RP0     ; address Register Page 2
         bsf       STATUS,RP1     
         movlw     0x01           ;  Port pins Analog
         movwf     ANSEL
         bcf       STATUS,RP0     ; address Register Page 0
         bcf       STATUS,RP1
        
    
MainLoop:
         call   adcdelay        ;delay to charge cap
         bsf       ADCON0,GO      ; start conversion
         btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
         goto      $-1
       
        movf     ADRESH,w       ; Copy the display to the LEDs
        movlw    _1VOLT
        subwf    ADRESH, w
       BNC    LessThan1V                ; Branch if less than 1V


        movlw    _2VOLT
        subwf    ADRESH, w
       BNC    LessThan2V                ; Branch if Between 1V and 2V
       

        movlw    _2_5VOLT
        subwf    ADRESH, w
       BNC    LessThan2_5V             ; Branch if Between 2V and 2.5V


        movlw    _3VOLT
        subwf    ADRESH, w
       BNC    LessThan3V                ; Branch if Between 2.5V and  3V


        movlw    _4VOLT
        subwf    ADRESH, w
       BNC    LessThan4V                ; Branch if Between 3V and 4V


        movlw    _5VOLT
        subwf    ADRESH, w
       BNC    LessThan5V                ; Branch if Between 3V and 4V
        goto      MainLoop
LessThan1V:
       movlw   b'00000001'
       movwf   ADC_hold
       goto   MainLoop
LessThan2V
       movlw   b'00000010'
       movwf   ADC_hold
       goto   MainLoop

LessThan2_5V:
       movlw   b'00000100'
       movwf   ADC_hold
       goto   MainLoop
LessThan3V:
        movlw   b'00001000'
       movwf   ADC_hold
       goto   MainLoop

LessThan4V:
       movlw   b'00010000'
       movwf   ADC_hold
       goto   MainLoop

LessThan5V:
       movlw   b'00100000'
       movwf   ADC_hold
       goto   MainLoop

adcdelay:
        nop                 ;1 cycle

        return                 ;4 cycles (including call)
       
         end
 
Top