1
\$\begingroup\$

So currently mucking around with the ADC on my PIC16F917. I got single button functions going and all works well.

What I'd really love help with, is how to make the MCU accept 2 inputs, without the second being an ISR.

How can I modify this code, to make it so when, say AN1 goes > 512, PORTDbits.RD1 goes high, alongside AN0 and RD0. Acting like two separate systems, but working simultaneously. Is it actually even possible?

Thanks again, Ezra

#include <xc.h>
#include "config-bits.h"

#define _XTAL_FREQ 4000000

void delay()
{
    int i;

    for(i=0; i<5000; i++)
    {
        /*Timer Stuff*/
    }
}

int main()
{
    TRISD=1;                    //Set all D pins to input

    TRISDbits.TRISD0 = 0;       //LED output
    ANSELbits.ANS0 = 1;         //Select ADC input

    ADCON0bits.ADFM = 1;        //ADC result is right justified
    ADCON0bits.VCFG = 0;        //Vdd is the +ve reference
    ADCON1bits.ADCS = 0b001;    //Fosc/8 is the conversion clock
                                //This is selected because the conversion
                                //clock period (Tad) must be greater than 1.5us.
                                //With a Fosc of 4MHz, Fosc/8 results in a Tad
                                //of 2us.
    ADCON0bits.CHS =  0;        //select analog input, AN2
    ADCON0bits.ADON = 1;        //Turn on the ADC

    int result;

    while(1)
    {
        __delay_us(5);                  //Wait the acquisition time (about 5us).

        ADCON0bits.GO = 1;              //start the conversion
        while(ADCON0bits.GO==1){};      //wait for the conversion to end

        result = (ADRESH<<8)+ADRESL;    //combine the 10 bits of the conversion

        if(result > 512)
        {
            PORTDbits.RD0 = 1;
            delay();
            PORTDbits.RD0 = 0;
            delay();
        }
        else
        {
            PORTDbits.RD0 = 0;
        }
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

With this PIC true simultaneity is not possible, as there is only one sample and hold and one converter. If nearly is good enough, then adjusting your code to simply switch between inputs would work. If not, then a PIC with more than one ADC/Sample and Holds would be the way to go, e.g. a dsPIC such as the dsPIC33FJ128GP802. Or of course you could add an external ADC.

\$\endgroup\$
4
  • \$\begingroup\$ That's great! I didn't think of that, so you'd suggest a multiplexing like function to check the inputs and run them through the ADC? Are the ADC accessible ports only limited from AN0 through to AN7 or can you direct any input to the ADC? \$\endgroup\$
    – ezra_vdj
    Commented Sep 7, 2015 at 9:45
  • 1
    \$\begingroup\$ Yes, that kind of thing - simply toggle a variable and check the value in code then switch to the appropriate input (or for more inputs increment a value then do a switch case) You can only use the analogue inputs (AN*) \$\endgroup\$
    – Oli Glaser
    Commented Sep 7, 2015 at 9:50
  • \$\begingroup\$ So say for 3 inputs, a counter that only counts to three, and along the way checking inputs 1, 2 and three 3 at the corresponding number? And the frequency of the checking is limited to the A/D conversion time of 5us right? \$\endgroup\$
    – ezra_vdj
    Commented Sep 7, 2015 at 9:56
  • \$\begingroup\$ Yes, that's the kind of thing I mean. \$\endgroup\$
    – Oli Glaser
    Commented Sep 7, 2015 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.