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

Adc Pic16f877a

Discussion in 'Microcontrollers' started by derrick826, Oct 5, 2007.

  1. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    thanks eric.... i owe you a lot really! i think i will have to do more research and post my problems later on
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
  3. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    those are the programs (something like GUI) that display on the pc screen using the soundcard. Eric, it seems that the project i'm doing has a bit of limitation which is using either a serial port or a paralel port, my preferably serial port.

    the http://www.oscilloscope-lib.com/ is quite useful, i have also downloaded this morning. thanks a bunch
     
  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,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE

    hi,
    If you have free choice in the method of capturing analog signals, look at the fast, 8 bit or 12 bit parallel ADC convertors.
    These can be connected to the PC's parallel port, many thousands of reading/sec can be taken and displayed.
     
  6. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    hi eric,
    i'm not allowed to use any external ADC , only the internal ADC of the pic, what i had in mind is that maybe when i'm sampling a high freqeuncy sinewave, i will have to store the converted values into a RAM , once it reached to a max amount in the ram, i will send it to the pc. i have calculated my sampling rate and it is more than can be trasmitted using rs232. I haven't checked about the parallel yet.can this eliminate my problem regarding on to low baudrate to send a high sampling data?
     
  7. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    It's still very limited. If you check EPE, they did a PIC 'scope' a few years OK, which used the internal PIC A2D and wrote to a 2K static RAM chip, then this was transferred via the parallel port when full. But it's a fairly low spec.

    More recently, earlier this year, they did a higher spec version. This uses a MAX118 external A2D, writing directly to a static RAM under PIC control. When full the PIC then transfers the contents to the PC via a serial link. This version gives a 40KHz sampling speed!.
     
  8. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    wow, but for now as a learner i think i will just make a low spec frequency scope first... start from a beginner is much better for now.
     
  9. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Those ARE low spec!.
     
  10. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    how low can they be? less than 5khertz?
     
  11. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Assuming you're looking for at least 5 samples per cycle?, then 40KHz sampling gives 8KHz bandwidth.
     
  12. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i nigel, i would like to ask how to create an additional RX pin so that my PIC will have 2 RX pin or more? i followed ur tutorial using the portB 7,6 as a TX and RX .. and by using the simulation of the PIC simulator IDE, i couldn't see any character transmitted on the screen even though the character's has been received. this time i put AN0 as my RX pin and to indicate that there's LED lighted up if the character is being received but the porgramme is not working. below are my codes.. can you help me check if there's anything which i miss out??


    list p=pic16f877, f=inhx8m
    include <P16F877.INC>
    __config _CP_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF

    ERRORLEVEL -302 ;Suppress bank warning




    cblock h'20'
    Xmit_Byte ;Equ 0x20 ;holds byte to xmit
    Rcv_Byte ; Equ 0x21 ;holds received byte
    Bit_Cntr ; Equ 0x22 ;bit counter for RS232
    Delay_Count ; Equ 0x23 ;delay loop counter
    endc

    ORG 0x00
    call RECEIVE ; receive using pin A0

    int ORG 0x04
    goto int

    RECEIVE
    CALL SER_INIT
    CALL Rcv_RS232
    RETURN

    SER_INIT
    BSF STATUS, RP0 ;select bank 1
    ; BCF TRISB, 6 ;set B6 as an output
    CLRF TRISB ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; i set as trisb
    BSF TRISA,0 ;set A0 as an input ;;;;;;;;;;;;;;; i set as trisA
    BCF STATUS, RP0 ;select bank 0
    ; BSF PORTB, 6 ;set B6 high
    RETURN



    Rcv_RS232 BTFSC PORTA, 7 ;wait for start bit
    GOTO Rcv_RS232
    CALL Start_Delay ;do half bit time delay
    BTFSC PORTA, 7 ;check still in start bit
    GOTO Rcv_RS232
    MOVLW 0x08 ;set up to read 8 bits
    MOVWF Bit_Cntr
    CLRF Rcv_Byte
    Next_RcvBit CALL Bit_Delay
    BTFSS PORTA, 7
    BCF STATUS , C
    BTFSC PORTA, 7
    BSF STATUS , C
    RRF Rcv_Byte , f
    DECFSZ Bit_Cntr , f ;test if all done
    GOTO Next_RcvBit
    CALL Bit_Delay
    MOVF Rcv_Byte, W
    MOVWF PORTB ;; display on leds on what data received
    RETURN

    Start_Delay MOVLW 0x3C
    MOVWF Delay_Count
    Start_Wait NOP
    DECFSZ Delay_Count , f
    GOTO Start_Wait
    RETURN

    Bit_Delay MOVLW 0x78
    MOVWF Delay_Count
    Bit_Wait NOP
    DECFSZ Delay_Count , f
    GOTO Bit_Wait
    RETURN

    END
     
  13. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    On a quick glance it looks like you're not setting the pin as a digital one, by default AN0 is an analogue input - you MUST set it to be a digital input if you want to use it as one.
     
  14. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i have already assign into digital by assigning all AN0-AN7 digital I/O by applying h'06' to ADCON1 but it still didn't work.. i used the software UART simulation to input a character into AN0 for testing , one of the simlation tools for PIC simulator IDE.

    list p=pic16f877, f=inhx8m
    include <P16F877.INC>
    __config _CP_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF

    ERRORLEVEL -302 ;Suppress bank warning




    cblock h'20'
    Xmit_Byte ;Equ 0x20 ;holds byte to xmit
    Rcv_Byte ; Equ 0x21 ;holds received byte
    Bit_Cntr ; Equ 0x22 ;bit counter for RS232
    Delay_Count ; Equ 0x23 ;delay loop counter
    endc

    ORG 0x00
    call RECEIVE ; receive using pin A0

    int ORG 0x04
    goto int

    RECEIVE
    CALL SER_INIT
    CALL Rcv_RS232
    RETURN

    SER_INIT
    BSF STATUS, RP0 ;select bank 1
    ; BCF TRISB, 6 ;set B6 as an output
    MOVLW h'06'
    MOVWF ADCON1
    CLRF TRISB ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; i set as trisb
    BSF TRISA,0 ;set A0 as an input ;;;;;;;;;;;;;;; i set as trisA
    BCF STATUS, RP0 ;select bank 0

    ; BSF PORTB, 6 ;set B6 high
    RETURN



    Rcv_RS232 BTFSC PORTA, 0 ;wait for start bit
    GOTO Rcv_RS232
    CALL Start_Delay ;do half bit time delay
    BTFSC PORTA, 0 ;check still in start bit
    GOTO Rcv_RS232
    MOVLW 0x08 ;set up to read 8 bits
    MOVWF Bit_Cntr
    CLRF Rcv_Byte
    Next_RcvBit CALL Bit_Delay
    BTFSS PORTA, 0
    BCF STATUS , C
    BTFSC PORTA, 0
    BSF STATUS , C
    RRF Rcv_Byte , f
    DECFSZ Bit_Cntr , f ;test if all done
    GOTO Next_RcvBit
    CALL Bit_Delay
    MOVF Rcv_Byte, W
    MOVWF PORTB ;; display on leds on what data received
    RETURN

    Start_Delay MOVLW 0x3C
    MOVWF Delay_Count
    Start_Wait NOP
    DECFSZ Delay_Count , f
    GOTO Start_Wait
    RETURN

    Bit_Delay MOVLW 0x78
    MOVWF Delay_Count
    Bit_Wait NOP
    DECFSZ Delay_Count , f
    GOTO Bit_Wait
    RETURN

    END
     
  15. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    You appear to have a confusing number of incorrect calls and returns, with Ser_Init called from within the receive routine - it should only be called once, in the initialisation section of the program. I suggest you check the structure of my tutorials.

    As it stands the program will do nothing but crash as the stack underflows.
     
  16. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    Yeap, had been corrected... i followed the sequence which i think is perfect but still no results displayed on the software UART simulation interface using the PIC simulator IDE program... i wonder why?


    list p=pic16f877, f=inhx8m
    include <P16F877.INC>
    __config _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF

    ERRORLEVEL -302 ;Suppress bank warning




    cblock h'20'
    Xmit_Byte ;Equ 0x20 ;holds byte to xmit
    Rcv_Byte ; Equ 0x21 ;holds received byte
    Bit_Cntr ; Equ 0x22 ;bit counter for RS232
    Delay_Count ; Equ 0x23 ;delay loop counter
    endc

    ORG 0x00

    CALL SER_INIT ; receive using pin A0
    again
    CALL Rcv_RS232
    goto again

    int ORG 0x04
    goto int



    SER_INIT
    BSF STATUS, RP0 ;select bank 1
    ; BCF TRISB, 6 ;set B6 as an output
    MOVLW h'06'
    MOVWF ADCON1
    CLRF TRISB ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; i set as trisb
    BSF TRISA,0 ;set A0 as an input ;;;;;;;;;;;;;;; i set as trisA
    BCF STATUS, RP0 ;select bank 0

    ; BSF PORTB, 6 ;set B6 high
    RETURN



    Rcv_RS232
    BTFSC PORTA, 0 ;wait for start bit
    GOTO Rcv_RS232
    CALL Start_Delay ;do half bit time delay
    BTFSC PORTA, 0 ;check still in start bit
    GOTO Rcv_RS232
    MOVLW 0x08 ;set up to read 8 bits
    MOVWF Bit_Cntr
    CLRF Rcv_Byte
    Next_RcvBit
    CALL Bit_Delay
    BTFSS PORTA, 0
    BCF STATUS , C
    BTFSC PORTA, 0
    BSF STATUS , C
    RRF Rcv_Byte , f
    DECFSZ Bit_Cntr , f ;test if all done
    GOTO Next_RcvBit
    CALL Bit_Delay
    MOVF Rcv_Byte, W
    MOVWF PORTB ;; display on leds on what data received
    RETURN

    Start_Delay MOVLW 0x0C
    MOVWF Delay_Count
    Start_Wait NOP
    DECFSZ Delay_Count , f
    GOTO Start_Wait
    RETURN

    Bit_Delay MOVLW 0x18
    MOVWF Delay_Count
    Bit_Wait NOP
    DECFSZ Delay_Count , f
    GOTO Bit_Wait
    RETURN

    END
     
  17. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Personally I never use simulators, I don't consider them reliable enough - unless they work EXACTLY as the real world, and they don't.

    You should also post code surrounded by the 'code' tags, this keeps the formatting correct, I would also suggest you do what I do at the beginning of the program, it's easer to read and makes problems less likely.


    Code (text):
    list    p=pic16f877, f=inhx8m      
    include <P16F877.INC>
        __config _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF  
               
       ERRORLEVEL      -302    ;Suppress bank warning
               
               


                cblock h'20'
                Xmit_Byte   ;Equ  0x20        ;holds byte to xmit
                Rcv_Byte    ; Equ  0x21        ;holds received byte
                Bit_Cntr    ; Equ  0x22        ;bit counter for RS232
                Delay_Count ; Equ  0x23        ;delay loop counter
                endc
               
               ORG 0x00
               GOTO Main

        int     ORG 0x04
                        RETFI
           
    Main            CALL SER_INIT    ; receive using pin A0


    again      
                CALL Rcv_RS232
                goto  again
               
               



    SER_INIT
                BSF     STATUS, RP0           ;select bank 1
               ; BCF     TRISB, 6              ;set B6 as an output
        MOVLW  h'06'
          MOVWF  ADCON1
        CLRF    TRISB                 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; i set as trisb
                BSF     TRISA,0              ;set A0 as an input ;;;;;;;;;;;;;;; i set as trisA
                BCF     STATUS, RP0           ;select bank 0
           
               ; BSF     PORTB, 6              ;set B6 high
                RETURN
     
  18. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i'll try to burn the codes into the pic to test if it works. in your tutorial, it is possible to assign any pins as a RX or a TX pin except for portc,7 and portc,6 ?
     
  19. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Yes, subject to any individual pin restrictions - such as RA4 that tends to be an open-collector output.
     
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    derrick,
    Ref my PM reply of a few minutes ago.

    You are not transmitting to the software uart within the program.
    Just tested your program on the Sim with the software uart as the transmitter and the transmitted character appears on PORTB as per the program. It works in simulation.

    As a side issue, there is no point in sending members PM's at the same time as you are posting. Its confusing and timewasting;)
     
  21. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    ok eric,
    as in the pm

    what is the possiblities of the codes to be changed if i want to change to a 20hz ocs and a 115200 baudrate?

    i used 0x09 for my delay_count in the start_delay and
    0x05 for my delay_count in my bit_delay. Is it possible?
     

Share This Page