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. adrianvon

    adrianvon Member

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

    The ADC that i made increment the binary value by 1 bit for every degree (i.e. 0degree =00000000B ... 1degree= 00000001B etc.). Now i want to display the Temperature on an LCD which means that i have to convert the digital value from the ADC (which is set to 7 bit) to ASCII. To do so, i am adding 48 decimal to the binary value.

    The problem is that this method is only working fine for a single character (i.e. from 1 to 9). When the temperature gets higher (from 10 onwards), the LCD will display other characters instead of numbers. I know that this is caused since the ASCII table does not represent more that one character with 8 bits.

    Is there a way that the values from the ADC which are above 9 be converted to ASCII ??

    Thanks in advance,
    Adrian
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,242
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi adrian,
    This is a simple demo, write the ascbfrs to the LCD as ASCII

    Code (text):

    ;Byte to BCD or ASCII into ascbfr, from Byte in binvall

        list    p=16f876a
        #include <p16f876A.inc>

        errorlevel -302, -207

        __config _CP_OFF & _XT_OSC & _PWRTE_ON  & _WDT_OFF & _LVP_OFF



    temp1   equ     0x21
    temp2   equ     0x22
    binvall equ     0x23
    binvalm equ 0x24

     
    ascbfr2 equ     0x40
    ascbfr1 equ     0x41
    ascbfr0 equ     0x42



        org 0x0000
        goto test
        org 0x0004
        retfie
       
    test:
        movlw   .140
        movwf   temp1
        addwf   temp1,w
       
    trap:   goto trap


        movlw   .123
        movwf   binvall
        call    bin2asc
       


    ;convert 8 bit bin to 3 asci in ascbfr0
    bin2asc:
        clrf    ascbfr2
        clrf    ascbfr1
        clrf    ascbfr0;lsd
             
            movlw   .8
        movwf   temp1

    bitloop:;shift msb into carry
            rlf     binvall,F
           
            movlw   ascbfr0 ;0=lsd 1st
            movwf   FSR ;fsr=pointer to digits
            movlw   0X03    ;digits to do
            movwf   temp2
    adjloop:
            rlf     INDF,F  ;shift digit 1 bit left
            movlw   0X0A
            subwf   INDF,W  ;check and adjust for decimal overflow
            btfsc   STATUS,C
            movwf   INDF

            decf    FSR,F;incf=msd 1st next digit
            decfsz  temp2,F
            goto    adjloop
            decfsz  temp1,F;next bit
            goto    bitloop

        ;return from here if BCD only
       
            movlw   0X30;make asci
        iorwf   ascbfr2,F
        iorwf   ascbfr1,F
        iorwf   ascbfr0,F
        return

        end
       
     
  3. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi,
    Thanks for the reply.

    Why is .140 moved to register temp1 and .123 moved to register binvall ?? to be honest i cannot understand the coding very well since i never programmed a 16F.
     
  4. dave

    Dave New Member

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


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,242
    Likes:
    645
    Location:
    Ex Yorks' Hants UK

    hi,
    As I said, its a demo, this code is just dummy data, remove following fragment from the routine.
    frag:
    test:
    movlw .140
    movwf temp1
    addwf temp1,w
    trap: goto trap
    movlw .123



    You move the 7bit value you want to convert to ASCII into the binval register and then call bin2asc.

    After the conversion is complete you will have 3 ASCII characters in these registers
    ascbfr2 equ 0x40
    ascbfr1 equ 0x41
    ascbfr0 equ 0x42
    Send them one at a time to the LCD character write subroutine
    If you want to insert a decimal point in the displayed value do this.

    Send ascbfr2, then ascbfr1, then decimal point "." , then ascbfr0

    OK.?
     
    Last edited: Jan 25, 2012
    • Like Like x 1
  6. adrianvon

    adrianvon Member

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

    Thanks for explaining. I will try to do it later today.
     
  7. adrianvon

    adrianvon Member

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

    The program is giving errors.

    Code (text):
    RLF     BINVALL,F
    rlf is not being recognized as an instruction, and are FSR and INDF registers ??

    Thanks
     
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,242
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    As you are using an 18F4550, just change the instructions from the 16F series to the equivalent 18F instruction, do you know how to do that.??
     
  9. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi,
    Unfortunately i dont know how to do that. Is this done form the software itself ? or find the instructions from the datasheet ?

    Thanks
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,242
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    Its in the datasheets, look at the Instruction Set section, if you get stuck , please ask.
     
    • Like Like x 1
  11. adrianvon

    adrianvon Member

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

    I changed rlf with rlcf, FSR with FSR0 and INDF with INDF0. The program is recognizing them as instructions but its still not working, but i will try again tomorrow.

    Thanks
     
  12. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi, i managed to do it. I used the coding in the link below:

    http://www.piclist.com/techref/microchip/math/radix/b2a-8b3d-ab.htm

    The only problem is that i cannot understand how the conversion is being done. I would appreciate if someone can explain with comments how the conversion is being done.

    Thanks in advance for your help.
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Thats a confusing function.... Its named BIN2BCD.... when of course it isn't coding a decimal... Its just separating the binary number into hundreds, tens and units and adding 48 to each..

    Ascii '0' is equal to 48 decimal ( as I'm sure you know ).
     
  14. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,242
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi adrian.
    I have tidied the subr, it can be either a 16 or 8 bit conversion, see the comments.
    Code (text):
    ;8 or 16 Bit Binary to 3 or 5 Ascii Routine for 18F4550   24May08    
    ;tested works OK.  redone 26/01/2012 OK


        list    p=18f4550,f=inhX32
        #include <p18f4550.inc>

        errorlevel -302, -207

     CONFIG PLLDIV=5,CPUDIV=OSC1_PLL2,USBDIV=2,FOSC=HSPLL_HS,IESO=OFF
     CONFIG VREGEN=ON,CCP2MX=ON,WDT=OFF,WDTPS=32768,PBADEN=OFF ,PWRT=OFF,MCLRE=ON
     CONFIG LPT1OSC=OFF,BOR=ON,BORV=2,STVREN=ON,LVP=OFF,ICPRT= OFF,XINST=OFF,DEBUG=OFF
     CONFIG WRTB=ON

    Xtal SET 4

    ;AscBfrs for convert subr    
        AscBfr4 equ 0x060;; not needed for 8bit
        AscBfr3 equ 0x061;; not needed for 8bit
        AscBfr2 equ 0x062
        AscBfr1 equ 0x063
        AscBfr0 equ 0x064
             
        BinValL equ 0x068
        BinValM equ 0x069;  not needed for 8bit

        No_Bits equ 0x06B
        Digit_Cnt equ 0x06C


    ;-------------------
        org 0x0000
               
    Main:
        movlw   .123;39h ;just for testing preload value
        movwf   BinValL
        ;;movlw 0;30h
        ;;movwf BinValM

        call    Bin2Asc
    done:  
        goto    done;  Main


    ;Binary to Ascii conversion Subr  
    ;enter with BinValH and BinValL holding the [8] 16bit binary value to be converted.    
    ;exit with  [3] 5 Ascii value,,,, the BinValH/L is all '0' on exit    
    ;NOTE: to expand to a 16bit WORD and 5 ASCII character output, remove the ';;' before the
    ;intructions....
    Bin2Asc:    
        ;;clrf  AscBfr4     ;clear buffers
        ;;clrf  AscBfr3
        clrf    AscBfr2
        clrf    AscBfr1
        clrf    AscBfr0
        movlw   .8;;.16
        movwf   No_Bits
    BitLoop:    
        rlcf    BinValL,F
        ;;rlcf  BinValM,F   ;carry = msb < 1

        LFSR    FSR0,AscBfr0    ;lsd 1st
        movwf   FSR0        ;pointer to ascbfr
        movlw   0X03;;05        ;digits to do
        movwf   Digit_Cnt
    AdjLoop:    
        rlcf    INDF0,F     ;shift ASCII byte in [INDF0] 1 bit left
        movlw   0X0A        ;decimal 10
        subwf   INDF0,W     ;W=[INDF0]-10

        btfsc   STATUS,C    ;skip if no carry,  its a negative result
        movwf   INDF0       ;only save if postive result  

        movf    STATUS,W    ;NB. the decf FSR0L sets Carry!
        decf    FSR0L,F     ;point to next msd Ascbfr  
        movwf   STATUS      ;recall Status from before the decf FSR0L

        decfsz  Digit_Cnt,F ;dec AscBfr counter
        goto    AdjLoop     ;go next ascii

        decfsz  No_Bits,F
        goto    BitLoop     ;go next bit shift left for BinVal

        movlw   0X30        ;make asci
        ;;iorwf AscBfr4,F
        ;;iorwf AscBfr3,F
        iorwf   AscBfr2,F
        iorwf   AscBfr1,F
        iorwf   AscBfr0,F
        return

         
        end
     
     
    • Like Like x 1
  15. adrianvon

    adrianvon Member

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

    Thanks a lot for explaining the code :)
     
  16. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi, i am including two interrupt rourines to my coding, one high priority and a low priority interrupt. Now my question is, can i use goto instead of RETFIE ?? I want to use goto instruction since when the low priority interrupt is active, and the high priority interrupts it, when ready it will return to the low priority ISR. I dont want this to happen, instead i want it to go back to the MAIN label.

    Thanks.

    EDIT: I already tried using the GOTO instruction, but its not working.
     
    Last edited: Jan 28, 2012
  17. adrianvon

    adrianvon Member

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

    As i said in previous posts, i want to display the temperate on an LCD. The LCD coding that is was using previously wasn't mine and it was to complex to understand, so i decided to do a new coding.

    The tutorial im using is attached below and the coding used is of experiment 8.

    When building the program (on MPLAB) it give no errors, but when programmed to the PIC, its not working. Can someone help me trouble-shooting the problem please. (attached is my coding)

    Thanks in advance.
     

    Attached Files:

    • lcd2.pdf
      File size:
      121.5 KB
      Views:
      172
    • LCD.asm
      File size:
      0 bytes
      Views:
      5
  18. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,242
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi adrian,
    The LCD is not being initialised correctly.

    From your earlier posts I thought you had decided on a 4 bit LCD for Data.??

    The pdf you posted shows a different connections for the LCD control.
     
  19. adrianvon

    adrianvon Member

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

    The problem was that the starting delay was too long, but now i have another problem. Instead of 'HELLO' the LCD is displaying 'HHHHH' (i.e. the first letter of HELLO for five times). Is this caused by 'ADDWF STATUS,F' instruction ??

    Thanks

    EDIT: Hi ericgibbs. I set the LCD to 8 bit mode since i have pins which are not being used.
     
    Last edited: Feb 5, 2012
  20. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    and by the way, what is the difference between BRA and GOTO instructions? Is there a limit on how much the GOTO instruction can be used? and the CALL instruction, is there a limit on how much it can be used?
     
    Last edited: Feb 5, 2012
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Branch and goto are immediate jumps... The PC is changed and that't that... you can do this as often as you like... Call however has a return address.... This has to be saved so the PC can continue after the call.... there are a few times this happens.... Call's... interrupt's.. Lookup's.... Interrupts require two stack positions... On a pic14 there are 8 positions in the stack... On the pic16 there are 16.

    That governs the amount of nested calls you can make... I think you are using pic16 range.... pic18fxxxx, If so I would limit the nested calls to 14 (always keep two for an interrupt)
     
    • Like Like x 1

Share This Page