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

Pic18f4550 adc

Discussion in 'Microcontrollers' started by adrianvon, Jan 11, 2012.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Ok adrianvon.... The command IS btfsc however.. The call is a two word instruction so the btfsc doesn't work.. you'll have to branch using BNZ

    The other fault is MOVLW REG1...... this moves the address into W not the value of REG1... You must use MOVF REG1,W
    Code (text):

    STOP
        MOVLW .1                                    
        MOVWF REG1
        MOVF REG1,W
        SUBLW .3
        BNZ  lp1
        CALL LCD_ARROW_3
    lp1 MOVF REG1,W
        SUBLW .2
        BNZ lp2
        CALL LCD_ARROW_2
    lp2 MOVF REG1,W
        SUBLW .1
        BNZ lp3
        CALL LCD_ARROW_1
    lp3 GOTO STOP

     
     
    • Like Like x 1
  2. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks for your help Ian Rogers :) I spent two days trying to make this work :O
     
  3. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi Ian Rogers, can you please explain to me how the bin2bcd routine you gave me (post number 112) works?

    Thanks in advance.
     
  4. dave

    Dave New Member

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


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    I'm not entirely sure.... I have analyzed the code and discovered that the first three shifts, will end up in the tens (our hundreds) by using 0x3 we can identify any digit that may breach the number 9, before shifting, and deal with it... The end result is a packed BCD..... (A packed BCD has thou's, hund's, tens and units packed as nibbles in a two byte number...

    So the number 154 ends up 0x01 and 0x54, Then sending the correct nibble and adding 0x30 we have an ascii character to display.
     
    Last edited: Feb 19, 2012
  6. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi,

    Since i cannot understand what is happening in the routine, i decided to try and make one myself.
    The coding i did is the one below, but for some reason its not working :(

    Can someone help me in trouble-shooting?

    Thanks

    Code (text):
    CLRF tens                   ;clear registers
    CLRF ones

    CONVERT_TENS    ;convert the hundreds into ascii value routine

    MOVF ADRESH                 ;move the a2d result to the working register
    SUBLW .10                      ;subtract by 10
    BTFSS STATUS,C              ;is the result still a positive number
    GOTO CONVERT_ONES    ;if its a negative, go to convert the ones
    MOVWF ADRESH           ;if still positive, move the new subtracted result to the ADRESH register
    INCF tens                        ;and increment the tens register by 1
    BRA CONVERT_TENS       ;keep looping until the result is a negative value


    CONVERT_ONES

    MOVF ADRESH
    SUBLW .1
    BTFSS STATUS,C
    GOTO READY
    MOVWF ADRESH
    INCF ones
    BRA CONVERT_ONES


    READY

    MOVF tens                   ;add 48 to huns and ones to convert them to ASCII
    ADDLW .48
    MOVWF tens

    MOVF ones
    ADDLW .48
    MOVWF ones

     
     
    Last edited: Feb 19, 2012
  7. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Can someone help me with the coding above please?
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Working on it now...... I've been busy.

    Why don't you use the routine I posted..... It works great, up to four digits.
     
    Last edited: Feb 20, 2012
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    OK... firstly SUBLW .10 doesn't work like that.... "Subtract W from Literal" secondly.. BTFSS... You want to skip when clear ( BTFSC )... The carry flag is SET if an overflow occurs..
    Thirdly.. I don't think you can write to the ADRESH register.

    So
    Code (text):

       CLRF ONES          ; ready registers
       CLRF TENS
       MOVF  ADRESH,W   ; Get adc result
       MOVWF TMP          ; store
    CONVERT_TENS
       MOVF TMP,W         ; retrieve here
       SUBLW .10            ; is it bigger than 10
       BC CONVERT_ONES  ; No.
       MOVLW .10           ; Yes load w with 10
       SUBWF TMP          ;  Subtract 10 from temp
       INCF TENS            ;  increase tens
       BRA CONVERT_TENS   ; Again
     
    This code is not useful for numbers over 99
     
    • Like Like x 1
  10. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi,

    Thanks for your help.
     
  11. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi,

    I have a value in REG1 which i want to subtract by 5 without changing the value of the same register. Can someone tell me what instruction set to use please?

    If i use SUBWF, the value in REG1 is changing.

    Thanks
     
    Last edited: Feb 22, 2012
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    MOVLW .5 .........This puts 5 in the W reg
    SUBWF REG1,W ..... This subtracts 5 from REG1 and puts the result back in W..
     
    • Like Like x 1
  13. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks, it worked :)
     

Share This Page