1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Program to read A/D on firefly...

Discussion in 'Microcontrollers' started by RobertD, Jul 1, 2008.

  1. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    Hi folks, I wrote this program to read the AD on pin 3 of the firefly, and I can't seem to get any results in the ADRESH/L registers. What am I doing wrong?

     
  2. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    First thing I do in a case like this is clean up the code so it's readable. :p
    Code (text):
        list    p=16F88
        include <p16f88.inc>
        __config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

        cblock  0x20
        count
        endc

        banksel TRISA       ;bank 1
        movlw   B'00001000' ;MAKE RA3 INPUT
        movwf   TRISA
        movwf   ANSEL       ;MAKE RA3 ANALOG INPUT
        movlw   B'10000000'
        movwf   ADCON1      ;set right justified
        banksel ADCON0      ;bank 0
        movlw   B'01011001'
        movwf   ADCON0      ;enable A/D
        movlw   0x06
        movwf   count       ;set count register

    loop    decfsz  count,F     ;count 6 tocks
        goto    loop
        bsf ADCON0,GO   ;capture A/D
        btfsc   ADCON0,GO
        goto    $-1     ;waiting for A/D to finish

        end
    Then I look for trouble. Found some. Your code has nothing to catch it from falling out the bottom and blasting through empty memory (all NOPs) and restarting over and over. END does not stop code execution - it tells the assembler to stop assembling there. Put a continuous loop there to stop the program. Or jump back to loop to read A/D again. Read your ADRESH/L now.
     
    Last edited: Jul 1, 2008
  3. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    OK did that, how can I read the memory of ADRESH/L while it's in the firefly? I put the switch off, and now the program is running, but I don't get any reading when I go into MPLAB view special function registers section.

    And I modifed the code to get some LEDs to light up when I get something in the ADRESH register.


    Code (text):
        list    p=16F88
        include <p16f88.inc>
        __config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

        cblock  0x20
        count
        endc

        banksel TRISA       ;bank 1
        movlw   B'11111111'
        movwf   TRISB       ;make PORTB output
        movlw   B'00001000' ;MAKE RA3 INPUT
        movwf   TRISA
        movwf   ANSEL       ;MAKE RA3 ANALOG INPUT
        movlw   B'10000000'
        movwf   ADCON1      ;set right justified
        banksel ADCON0      ;bank 0
        movlw   B'01011001'
        movwf   ADCON0      ;enable A/D
        movlw   B'00000110'
        movwf   count       ;set count register

    loop
        decfsz  count,F     ;count 6 tocks
        goto    loop
        bsf     ADCON0,GO   ;capture A/D
        btfsc   ADCON0,GO
        goto    $-1     ;waiting for A/D to finish
       
        movfw   ADRESH
        movwf   PORTB

    HANG
        GOTO HANG

        end
     
    Last edited: Jul 1, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.

    Use the debugger, since you don't have any display attached.

    There's always something in ADRESH/L, even if it's zero. You could code it to light up when ADRESH/L changes though.

    Gotta take a shower. I'll hook up my Firefly when I get back and do some checking.
     
  6. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    When I step through the debugger, it works OK, goes through the six tocks, then through the GO bit wait time, then goes to hang after about another six tocks. I did set the trimpot dip switch 2 on.

    Shower time for me too... Canada day.
     
  7. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Heh. :D Nice lazy day. Need another coffee... EDIT: Silvia and Rocky have done their job. Nice espresso to drink. Yum! :p

    This code:
    Code (text):
        list    p=16F88
        include <p16f88.inc>
        __config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF
        ERRORLEVEL 0, -302

        cblock  0x20
        count
        endc

        banksel TRISA       ;bank 1
        movlw   B'00001000' ;MAKE RA3 INPUT
        movwf   TRISA
        movwf   ANSEL       ;MAKE RA3 ANALOG INPUT
        movlw   B'10000000'
        movwf   ADCON1      ;set right justified
        banksel ADCON0      ;bank 0
        movlw   B'01011001'
        movwf   ADCON0      ;enable A/D
        movlw   0x06
        movwf   count       ;set count register

    loop    decfsz  count,F     ;count 6 tocks
        goto    loop
        bsf ADCON0,GO   ;capture A/D
        btfsc   ADCON0,GO
        goto    $-1     ;waiting for A/D to finish
        goto    loop

        end
    works just fine. I use "run to cursor" in the debugger on the "goto loop" line over and over while adjusting VR2 and watching (in the Watch list) ADRESH and ADRESL. The numbers vary from $0000 to $03ff (0 to 1023) just as they should.

    You DO have the VR2 dip switch in the ON position, don't you? My dip switch is set to 010111.
     
    Last edited: Jul 1, 2008
  8. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    Thank you, I'm relieved to know my coding is adequate... :) I could not find anything wrong with it, so that's why I asked. Usually , since I'm very green I do errors all over the place, like maybe not reading the registers correctly or something like that. My dip switch setting is 010100, the 28/18 both off.

    I don't have "run to cursor" and how do you read the registers?

    When I run the debugger, I click "watch" and add the two AD registers, and they're flat line... :(

    the ADCON0 does change when I watch special function registers. The GO pin changes form 0 to 1, so I should have a reading in AD regs...
     
    Last edited: Jul 1, 2008
  9. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Your code was basically fine. Just some nitpicks to clean up.

    I don't think those two matter, since you're not using the board as a ZIF programmer, but as a 16F88 tutor instead.
     
  10. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    I'll try running the prog with these on or off, tutor has to be off, when the prog is running, I just tested the trimpot, and there is power to RA3 2.4v, same as the trimpot output, though the trimpot seems erratic, I do get a voltage to work with.

    I changed the 88 and now my leds light up... :) But the AD registers still read 00
     
    Last edited: Jul 1, 2008
  11. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    NO! Tutor has to be on all the time when you're using the tutor (everything on the board except the ZIF socket area).

    Trimpots aren't the best quality pots usually. The readings do jump around some.
     
  12. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Yes you do. Put your cursor on the line you want to run to and right-click the mouse. Select Run To Cursor.

    Open a Watch window. In the left list at the top select ADRESH and hit Add SFR. Do the same for ADRESL. Then step through the code or use run to cursor and you'll see those SFR's change. They'll go red whenever a change happens.

    Probably because you're switching your tutor section off with the dip switch (see other post about this).
     
  13. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    I did that, I changed the code to see if I would get any reading on ADRESH and toggle a value (FF) on and off. It changed on and off, but not with the input, with the software, when I tried to do the same thing with ADRESL I still got ADRESH to toggle, and can't get ADRESL to toggle.... ?!?!

    Code (text):
        list    p=16F88
        include <p16f88.inc>
        __config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

        cblock  0x20
        count
        endc

    start
        banksel TRISA       ;bank 1
        movlw   B'11111111'
        movwf   TRISB       ;make PORTB output
        movlw   B'00001000' ;MAKE RA3 INPUT
        movwf   TRISA
        movwf   ANSEL       ;MAKE RA3 ANALOG INPUT
        movlw   B'10000000'
        movwf   ADCON1      ;set right justified
        banksel ADCON0      ;bank 0
        movlw   B'01011001'
        movwf   ADCON0      ;enable A/D

        clrw

        call delay

        bsf     ADCON0,GO   ;capture A/D
        btfsc   ADCON0,GO
        goto    $-1     ;waiting for A/D to finish
       


        movlw   B'11111111'
        movwf   ADRESL
       
        call delay
       
        movlw   B'00000000'
        movwf   ADRESL
        goto    start  


    delay
        movlw   B'00000110'
        movwf   count       ;set count register
    loop
        decfsz  count,F     ;count 6 tocks
        goto    loop
        return

        end
     
    Last edited: Jul 1, 2008
  14. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    Aha... I found a problem.

    I isolated the register ADRESL for testing and found I still toggle ADRESH instead of ADRESL, so there is definitely a problem there..



    Code (text):
        list    p=16F88
        include <p16f88.inc>
        __config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

    cblock  0x20
        count
        endc

    start
        movlw   B'11111111'
        movwf   ADRESL
       
        call delay
       
        movlw   B'00000000'
        movwf   ADRESL
       
        call    delay  
        goto    start

    delay
        movlw   B'00000110'
        movwf   count       ;set count register
    loop
        decfsz  count,F     ;count 6 tocks
        goto    loop
        return

        end
     
  15. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    I don't have a clue what you're trying to accomplish with that. Try my (your :p) tested and working code above. If that won't work for you, then your hardware is set wrong (tutor dip switch) or you're using the debugger incorrectly.
     
    Last edited: Jul 1, 2008
  16. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    Can't seem to be able to write anything in any registry...
    This code won't toggle PORTB, is there anything wrong with this?

    Code (text):
        list    p=16F88
        include <p16f88.inc>
        __config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

    cblock  0x20
        count
        endc

        banksel TRISB
        movlw   0x00
        movwf   TRISB
        banksel PORTB

    start
        movlw   B'11111111'
        movwf   PORTB
       
        call delay
       
        movlw   B'00000000'
        movwf   PORTB
       
        call    delay  
        goto    start

    delay
        movlw   B'00000110'
        movwf   count       ;set count register
    loop
        decfsz  count,F     ;count 6 tocks
        goto    loop
        return

        end
     
    Last edited: Jul 1, 2008
  17. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Umm... Your PortB is set to all inputs. That could be the problem. :p
     
  18. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    Yes I noticed the error and changed it, still no response...

    I'm beginning to think the compiler has a bug...
     
    Last edited: Jul 1, 2008
  19. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    The assembler is fine. You can't write to inputs and expect them to hold the values you wrote. :D
     
  20. RobertD

    RobertD New Member

    Joined:
    May 20, 2008
    Messages:
    216
    Likes:
    1
    Location:
    Brampton Ontario
    Thanks... :)

    But I did change the trisb to output... and still.
     
  21. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    I'll go test it.
     

Share This Page