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. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    Presumably you're still talking about a low spec scope connected via serial?, you really need to use the hardware USART - doing it in software is just further crippling your sample rate.
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    As we keep pointing out, if this is a 'real' application other than a learning exercise,
    you are not going to get the results you expect from the project.

    If it is a learning exercise, thats fine by me.

    If I was trying to write a 'software uart' routine for the 16F877 I would use the internal 'hardware uart' iof the 16F877.
    Use a proven RS232 routine and get the 16F877 transmitting/receiving from a PC.

    Then, write the software 'receive' routine, [the one you are working on], send characters from the PC to the S/W uart pin
    of the 16F877and get your 16F877 H/W routine to send it back to the PC,,, so that you check and verify the S/W routine.

    Is that clear?

    Although I havn't had a reason to use a PIC S/W uart, I would feel as you are talking 20MHz xtal and a baud rate 115,000,
    that the pulse timing is going to be 'tight' and critical.

    I am sure other members who have gone this route will be able to give a more meaningful response.
     
  3. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    ok eric, i'll try it first .. i just want to learn how to have an additional RX pin besides using the original RX build in pin in the PIC so that i can have more than one RX pin that might be useful and act as a RX multiplexer and TX one pin to the UART .
     
  4. dave

    Dave New Member

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


     
  5. Edy_Tse88

    Edy_Tse88 New Member

    Joined:
    Nov 6, 2007
    Messages:
    8
    Likes:
    0

    Hi Everyone,
    I'm new to PIC. I'm trying to do the ADC using PIC16F877A. For example if input ADC RA0 more than 2.5 V then the LED at RD0 is ON and if input ADC RA0 less than 2.5 then LED at RD1 is ON. I need a help. Can i get the assembly code. I'm confused what are the register i have to enable. Please help me. if everyone who know the code in assembly please give know or e-mail me at edy_tse88@yahoo.com. Thank's.
     
  6. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    Check my tutorials for how to use the A2D.
     
  7. sohaib_a

    sohaib_a New Member

    Joined:
    Nov 27, 2007
    Messages:
    14
    Likes:
    0
    I am am using the 877A to digitise an audio signal,i want to sample it at 8khz and the then send it over a serial SPI interface to a DAC.I am having major issues with the timing,If i use a 1Mhz crystal and Tad of 4us (which is the max I think one can take)...ill get a rate of about 14k..is it ok?
    Following is the code...

    void main ()
    {
    Spi_Init(); // initilalise SPI (built in function) clock is Fosc/4
    TRISA =0xFF; // Configure RA0 as analog input
    TRISC=0;
    ADCON1=0x4F; //configure analog channel An0 and left justified
    ADCON0=0x01; // configure a/d clock=fosc/4,powerup a/d
    do{
    delay_us(20); // acquistion delay
    ADCON0.GO_DONE =1; // start conversion;
    while (ADCON0.GO_DONE == 1);
    Spi_Write(ADRESH);
    }
    while(1); // infinite loop
    }


    I simulated it in Proteus ISIS Professional (i haven yet implemented it on hardwre) and during simulation there was excessive load on the CPU,ie; it wasnt able to perform in real time.What could be the reason..need some help urgently..
     
  8. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    For a start don't cripple it with a 1MHz crystal!. It would also be much faster to use an R2R ladder D2A, you're wasting time sending it as SPI - not writing it using C would probably help as well?.

    Are you low pass filtering the input and output?, good sharp filters, at less than 4KHz, to avoid antialiasing.
     
  9. sohaib_a

    sohaib_a New Member

    Joined:
    Nov 27, 2007
    Messages:
    14
    Likes:
    0
    Well actually i have to use SPI as i need to send the a/d result to an ISM transceiver MC13192 from freescale..i was just using the DAC to see if the SPI works..(they dont have transciever on ISIS).Would a 4Mhz crystal be fine...what rate should i set for the SPI clock if i wish to sample at 8Khz?
    In some examples I have seen them use the Timer0 to generate an interrupt every 125us(8Khz) for A/d sampling....is this a good method?
     
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    4MHz would be better, but why not just use 20MHz?, gives you more headroom, particularly as you're using a high level compiler.

    I don't see as your SPI clock rate has any bearing on the sample rate?, and you could certainly use timer interrupts to time the sampling if you wanted? - but bear in mind TMR0 is the poorest of the timers, TMR2 would be a lot better.
     
  11. sohaib_a

    sohaib_a New Member

    Joined:
    Nov 27, 2007
    Messages:
    14
    Likes:
    0
    Need some help with the timer settings...I know that it increments every instruction cycle (Tosc x 4) but I cant figure out what the pre-scaler and post scaler do...what do the ratios mean?
     
  12. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    OK, here goes... If anyone sees errors in my explanation, feel free to jump in.

    Fosc is your clock, or crystal speed. The timer gets that clock divided by four. So if you have a 20MHz crystal, then Fosc/4 = 5MHz, or 5,000,000 cycles per second.

    With a 1:1 prescale, the timer increments once per Fosc/4. We'll call Fosc/4 "clock" from now on.

    With a 1:16 prescale, the timer increments once every 16 clocks. With a 1:256 prescale, the timer increments once every 256 clocks. You get the idea, I'm sure. Those bigger prescales give longer timer periods between interrupts.

    Then with a 1:1 postscale, every time the timer rolls over it triggers an interrupt.

    With a 1:16 postscale, an interrupt isn't triggered until 16 rollovers. This again makes the timer period longer between interrupts.

    Just in case you don't understand what I mean by rollover, here it is: The timer is an 8 bit register or a pair of 8 bit registers forming a 16 bit register. An 8 bit timer counts up to 255 ($ff), rolls over to 0 and triggers an interrupt as it rolls over. Then it starts counting up again. A 16 bit timer counts up to 65535 ($ffff) and rolls over to 0 as it triggers an interrupt.

    With some scratch paper and a calculator you can calculate the timer periods very accurately. At first it'll seem very difficult, but you'll soon catch on to how to figure it.

    Then you can do stuff like set the timer value to the number that gives you the timer period you need. In your interrupt service routine you just reset it to that value every time it rolls over. Or you can have the value vary according to the time period you need, like when using a timer to generate a variable pulse train for a servo.

    Oh ya, some timers can use multiple different clock sources too. You have to choose how you want your timer clocked when you set up.
     
    Last edited: Nov 30, 2007
  13. sohaib_a

    sohaib_a New Member

    Joined:
    Nov 27, 2007
    Messages:
    14
    Likes:
    0
    thanks alot futz...that was very helpful...now i got some calculations to do..:)
     

Share This Page