Thoughts on why my included PIC asm code isn't doing as it should?

Discussion in 'Programmer's Corner' started by Subterranean, Aug 29, 2011.

  1. Subterranean

    Thread Starter New Member

    Jul 27, 2010
    Hi, I've got quite a simple circuit to build around a PIC10F222, but having written the program and made up a test ciruit board it's not behaving as I was hoping and I was hoping I could get some thoughts from this forum. I'll check over my program first.

    The idea is very simple; the uC starts with 2 of its I/O pins in output mode set high. When the ADC (GP0) reads a voltage above a certain limit, these 2 I/O pins (GP1 and GP2) switch low and never switch back high, no matter what the ADC measures until the uC is reset.

    I have included my test code. Apart from plenty of optimisations that could be made, should this code do as I've outlined above? The board I have made up does not seem to do this And I'm struggling to figure out what's the problem, but the software should be easiest to check first.

    It seems to work as expected, except that it switches off the I/O pins at a VERY low voltage. In the code I have it set to switch at around half the working voltage (around 2.5V running the chip at 5V) but it actually switches at around 0.007V (consistantly, I'm using a Fluke DMM to measure this). I'm using a 1k pot to set the ADC voltage so I think this should be fine.

    If the code is fine, I at least know its something wrong with my test board.
    Code ( (Unknown Language)):
    1.             LIST   P=PIC10F222
    2.             #include
    4.             ERRORLEVEL    -302        ; Supress bank switching messages
    6.             __config    _MCLRE_ON & _CP_OFF & _WDT_OFF & _MCPU_OFF & _IOFSCS_8MHZ
    8.             org 0
    9.             limit     equ    0x10            ;Location to store voltage limit.
    10.             movlw    d'128'                ;Conversion: 1 = 0.01953125V
    11.             movwf    limit                ;Move into F
    13.             movlw    b'11011111'            ;Set up option bits
    14.             OPTION    w                    ;Set Option bits
    16.             movlw    b'01000001'            ;Set up ADC to use AN0/GP0
    17.             movwf    ADCON0                ;and move into ADC register
    18.             movlw    b'00001001'            ;Set up GP1 and GP2 as outputs            
    19.             TRIS     0x06                ;and move into TRIS    
    22.             bsf        GPIO,GP1            ;Set output High
    23.             bsf        GPIO,GP2            ;Set output High
    26. start        clrwdt                        ;Clear watchdog timer
    27.             bsf        GPIO,GP1            ;Make sure output is high (not needed?)
    28.             bsf        GPIO,GP2            ;Make sure output is high (not needed?)
    29.             bsf        ADCON0,GO            ;Start ADC cycle
    30. ADCBegin    btfsc    ADCON0,NOT_DONE        ;Evaluate if ADC is finished
    31.             goto    ADCBegin            ;Not done so keep checking
    32.             movfw    ADRES                ;ADC finished. Move result into W
    33.             subwf    limit                ;Subtract result from limit
    34.             btfsc    STATUS,C            ;Evaluate if it went less than 0
    35.             goto    start                ;Voltage less than limit, retest.
    36. warning        bcf        GPIO,GP1            ;Voltage is over limit, set warning pin
    37.             bcf        GPIO,GP2            ;Voltage is over limit, set warning pin
    38.             clrwdt                        ;Keep watchdog timer clear
    39.             goto    warning                ;Stay in warning loop
    41.             end
  2. Markd77

    Senior Member

    Sep 7, 2009
    You want to use "subwf limit, W" because the default is to put the result in "limit", so by the time it's been through the loop a few times any ADRES apart from 0 will cause a carry.
  3. Subterranean

    Thread Starter New Member

    Jul 27, 2010
    That was it, so simple. Thanks for the help, that piece of information will be ingrained in my memory for next time I need to do something similar.