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;
}
}
}