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
    Alright nigel, thanks

    Nigel i want to clarify somethings which i particularly kinda blur. how do i maintain at least 100 samples/ cycle for a specific frequency of sinewave (ie 50k hertz sinewave) besides increasing my baudrate?
     
  2. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK
    For a 100 samples of a 50KHz sinewave you would need 5,000,000 samples a second, and to transfer 10,000,000 bytes per second.

    But you seem to have suddenly jumped from 50Hz to 50KHz, which makes everything VERY, VERY different.

    Perhaps you might try mentioning what you're actually trying to do, rather than keep randomly changing the rules?.
     
  3. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    oops that will be a typo , ok let's say its 50khz, and i need 10,000,000 bytes per second... that's mean i need a really really large baud rate right?

    if for a really high baud rate, what PIC would you recommend that can sample at least 100 samples/second and TX the datas to the pc via the serial cable throguh rs232?
     
    Last edited: Oct 18, 2007
  4. dave

    Dave New Member

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


     
  5. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK

    Like I said above, 9600 baud will send 480 ten bit samples per second, so that easily meets your requirement. Any PIC with A2D will do it, although one with a hardware USART would make it slightly simpler, but even a software UART would work fine.

    Check my tutorial 11.4 at http://www.winpicprog.co.uk/pic_tutorial11.htm which does pretty well what you're looking for?, although it will require speeding up for your use.
     
  6. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    Hi nigel,

    i found out that when i input a 190hertz frequency of the sinewave, it seems that the data displayed in the hyper terminal is very limited, only 4 samples/cycle. it's worst when i input 5k hertz sinewave

    Is there a way i can have more samples on the higher frequency of sinewave of about 5k hertz. My fixed maximum sinewave frequency that i have to plot is 5k hertz
     
  7. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK
    I've explained above how to calculate how fast you need to send the data, to sample 5KHz you need more than 10,000 samples per second, and you MUST, MUST, have a sharp 5KHz low-pass filter before the A2D. Transferring 10,000 two byte samples would require a 200,000 baud serial link. Dropping to 8 bit samples would only require 100,000 baud so would be possible at 115Kbaud.
     
  8. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i was quite confused with these calculations as well:

    200,000 baud rate = 200,000 bits/sec

    since i'm sending (1 sample= 2 bytes), 1 byte got 10bits, 2 bytes = 20bits

    200,000bits/sec divide by 20 bits = 10,000 of 2 bytes samples,

    since i'm sampling 5khz sinewave ,

    so 10,000 divided by 5khz = 2 samples/cycle ?

    kinda confused cause eric taught me this calculations
     
  9. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK
    Yes, just as I said.

    In what way are you confused? - the minimum sampling rate is double the highest frequency, and preferably more (CD is just over 2.2 times maximum frequency) - and like I said, it's essential to lowpass filter the input.
     
  10. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    oh i mean about the 10,000 divided bu 5khz = 2 samples/cycle

    meaning in one cycle , there is only 2 samples? meaning in one cycle, 2 point is being sampled from the 5khz sinewave?
     
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,235
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Hi derrick,
    Thats not quite accurate. :(
    I explained in my PM,the baud rates needed for ASCII transfer, assuming that you had 4 ascii data bytes and 2 for CRLF.

    Also we were discussing a 50HZ sinewave.

    If you can accept an 8 bit ADC value and transmit as a binary byte then you can increase your data transfer rate to the PC.
    The ADC conversion time is minute compared to the RS232 transmit time.

    You said you wanted to use your PC to display the waveforms, not audio reproduction.

    If you want to faithfully reproduce the waveforms as a trace on your PC then the sampling rate must be about 5 times the frequency of the waveform you are sampling. If you are talking audio reproduction a much lower sampling rate will be acceptable.
     
  12. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i'm really trying hard to understand , have to forgive me.. sorry..

    currently i'm trasmitting the 4 ascii data bytes and 2 for CRLF but from the calculation it seems that the more character i send, the less accurate my sampling data getting lesser.

    anyway this is my current coding which can work to trasmit the converted ADC values into ASC11 and display in hyperterminal. the only thing is that a few modifications have to be made so that i can send up to 5k hertz waveform and i kinda lost.

    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'
    thirty
    delaytime
    digit0
    digit1
    digit2
    digit3
    resultlbyte
    resulthbyte
    count
    temp
    H_byte
    L_byte
    R0 ; RAM Assignments
    R1
    R2
    endc



    org 0x000
    goto start

    int org 0x004
    goto int


    start
    call initial
    CALL INITUSART
    again
    call delay
    call startconv
    call check
    call result
    call B2_BCD
    call bcdsplit
    call send
    goto again

    initial
    MOVLW h'30'
    MOVWF thirty
    BCF STATUS,RP1
    BSF STATUS,RP0
    MOVLW b'00000001'
    MOVWF TRISA
    MOVLW b'00000000'
    MOVWF TRISB
    MOVLW b'00000000'
    MOVWF TRISD
    MOVLW b'10001110'
    MOVWF ADCON1
    BCF STATUS,RP1
    BCF STATUS,RP0
    MOVLW b'01000001'
    MOVWF ADCON0
    RETURN
    INITUSART:
    BSF STATUS,RP0
    BCF TRISC,6 ; make rc6 an output
    BSF TRISC,7 ; make rc7 as input
    MOVLW .25 ; 9600,n,8,1
    MOVWF SPBRG

    BSF TXSTA,TXEN ; enable transmit
    BSF TXSTA,BRGH ; select high baud rate

    BCF STATUS,RP0
    BSF RCSTA,SPEN ; enable serial port
    BSF RCSTA,CREN ; enable continuous reception
    BCF PIR1,RCIF ; clear rcif interrupt flag

    BSF STATUS,RP0
    MOVLW b'00000000'
    MOVWF TRISB
    BCF STATUS,RP0


    RETURN

    delay
    MOVLW h'07'
    MOVWF delaytime
    run
    DECFSZ delaytime,f ; 21micro second
    goto run

    startconv
    BSF ADCON0,GO
    RETURN


    check
    BTFSS PIR1,ADIF
    goto check
    ; MOVLW b'00000001'
    ; MOVWF PORTC
    BCF PIR1,ADIF
    return

    result
    BCF STATUS,RP1
    BSF STATUS,RP0
    MOVF ADRESL,W
    BCF STATUS,RP0
    movwf PORTB
    movwf L_byte
    BCF STATUS,RP1
    BCF STATUS,RP0
    MOVF ADRESH,W
    MOVWF PORTD
    movwf H_byte
    RETURN

    B2_BCD
    bcf STATUS,0 ; clear the carry bit
    movlw .16
    movwf count
    clrf R0
    clrf R1
    clrf R2
    loop16 rlf L_byte, f
    rlf H_byte, f
    rlf R2, f
    rlf R1, f
    rlf R0, f
    ;
    decfsz count, f ; routine for bcd conversion
    goto adjDEC
    retlw 0
    ;
    adjDEC movlw R2
    movwf FSR
    call adjBCD ; no bank switching, always
    ; ; indirect access for RAM
    movlw R1
    movwf FSR
    call adjBCD
    ;
    movlw R0
    movwf FSR
    call adjBCD
    ;
    goto loop16
    ;
    adjBCD movlw 3
    addwf 0,W
    movwf temp
    btfsc temp,3 ; test if result > 7
    movwf INDF
    movlw 30
    addwf 0,W
    movwf temp
    btfsc temp,7 ; test if result > 7
    movwf INDF ; save as MSD
    retlw 0

    bcdsplit
    movf R1,w ; bring the second digit!
    andlw 0F0h ; mask the lower nibble.
    movwf digit0 ; send to display routine variable.
    swapf digit0,f ; after swaping!


    movf R1,w ; bring first nibble
    andlw 0Fh ; mask the upper nibble
    movwf digit1 ; send to display routine variable.

    movf R2,w ; bring the second digit!
    andlw 0F0h ; mask the lower nibble.
    movwf digit2 ; send to display routine variable.
    swapf digit2,f ; after swaping!

    movf R2,w ; Again bring the lowbyte
    andlw 0Fh ; mask the upper nibble
    movwf digit3 ; send it to display routine variable
    return

    send
    MOVFW thirty
    ADDWF digit0,w
    CALL TXRDY
    MOVFW thirty
    ADDWF digit1,w
    CALL TXRDY
    MOVFW thirty
    ADDWF digit2,w
    CALL TXRDY
    MOVFW thirty
    ADDWF digit3,w
    CALL TXRDY
    MOVLW .13 ;crlf
    CALL TXRDY
    ; MOVLW .10
    ; CALL TXRDY

    TXRDY:
    BTFSS PIR1,TXIF
    GOTO TXRDY;busy,so loop
    XMIT1:
    MOVWF TXREG;send
    RETURN


    END
     
  13. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK
    If you're sending six bytes per sample then you need 600,000 baud serial transfer rate. You are quite right, and it's blindingly obvious, that the more data you send the longer it takes and the lower your sample rate needs to be.
     
  14. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    yea that's why i change my mind into sending 2 bytes instead. cause i have a 10-bit converted ADC values, and let the pc to do the decoding of the 2 bytes.

    about the previous calculation regarding on


    10,000 divided by 5khz = 2 samples/cycle

    meaning in one cycle , there is only 2 samples? meaning in one cycle, 2 point is being sampled from the 5khz sinewave?

    my sampling rate have to divided by the amount of the frequency of the sinewave in order for me to tell how many samples percycle are being indicated on the hyperterminal ?

    May i know for the internal ADc of my PIC16F877A , what is the maximum sampling rate of the internal ADC?

    Tacq+ 12Tad = 38.92us is my maximum sampling rate= 1/ 38.92us = 25.69khz ?
    not sure about this calculation either
     
    Last edited: Oct 19, 2007
  15. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK
    Perhaps you might try telling us exactly what you're trying to do?.
     
  16. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i just want to know if my calculations above as well as my clarification regarding on the 10,000 divided by 5khz = 2 samples/cycle. is correct or not
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,235
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi derrick,
    So that we are all discussing the same problem.

    Are you talking about a PC oscilloscope that can display a 5KHz waveform
    accurately or audio reproduction?:)
     
  18. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    Yea close to PC oscilloscope that can display a 5khz waveform using a PIC, not only 5khz but less than that too... i'm not to worry about displaying but i want to get the samples correct for any high or low frequency of sinewave input. with these correct samples, i will get to display. but now my samples are inaccurate with high frequencies, hopefully with the correct samples which i acquired i'm able to TX to the pc with these correct datas.. something like a real time situation. that's all, no more other problems beside this.


    not audio reproduction ..
     
  19. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,235
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    In that case, you have to sample at least 5 times the maximum frequency of the waveform you are sampling. Its that simple.
    So for a 5KHz waveform you should sample at 5000 * 5 = 25000 samples/sec!

    IF you can sample at this frequency, it would mean for a 2.5KHz waveform you would be sampling at 10 samples/waveform cycle, and so on to lower frequencies. At the lower waveform frequencies you would be taking far more samples than required, eg; for say for 50HZ the sampling would be 500/cycle.
    The lower frequencies are not the problem

    So assume 25000smp/sec and two binary bytes/sample = 50000 bytes/sec!
    As you have 10bits/byte of RS232 transmission thats 500,000 bits/sec!!!

    As I have suggested before, there is software on the web, that uses the sound card in your PC. Also you could use a 8/12 bit parallel ADC connected to the PC's printer port, I have seen 10KHz PC scopes made this way.
     
  20. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    can you tell me more about the sound card and the software thingy? how fast can the soundcard receive data i send from my pic? i was not aware about using the soundcard to do PC scopes.. is there any links to that particular information?
     
  21. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,235
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    Look here.
    http://www.picotech.com/picoscope-oscilloscope-software.html

    Google for 'PC scope software'
     

Share This Page