Sunday May 30, 2010

Here is my second program using one of the Pic 16F684's Analog to Digital Converters. I needed a voltage window detector for the simple lamp circuit in the crickets. More or less it is neccesary to send a Digital high if a voltage is greater than voltage 1, but less than voltage 2. Here using Vcc and Ground as reference, I've read a value from the PickIt1's on board potentionmeter, and sent a digital signal based on its position in relation to the window. Once again, not sure this is the best way to go about the process.

#include

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);

int Reading, r1, r2, r3, D1;
int ADCState;

main()
{
/////////// INIT ////////////

PORTA = 0;
PORTC = 0;
TRISA = 0b000001; // All Bits of PORTA are Outputs except RA0
TRISC = 0; // All Bits of PORTC are Outputs

CMCON0 = 7; // Turn off Comparators
ANSEL = 1 << 0; // RA0 is ADC input
ADCON0 = 0b00000001; // Left justify, Use Vdd, Do not start, Turn on
ADCON1 = 0b00010000; // run oscillatr as 8 x prescalar

ADCState = 0;

//// MAIN LOOP ////

while(1)
{
/////////// DISPLAY READOUT ///////////

if(Reading > 100 && Reading < 150){
RA1 = 1; // Value from pot.
RA2 = 0; // Grounded for charlieplex.
}else{
RA1 = 0;
RA2 = 0;
}

switch(ADCState)
{
case 0: //Start ADC operation
GODONE = 1;
ADCState = 1;
for(D1=0;D1<400;D1++);
break;
case 1:
ADCState = 0;
r1 = ADRESH;
r2 = ADRESH >> 4;
r3 = ADRESH & 0x0F;
Reading = r1+r2+r3;
for(D1=0;D1<400;D1++);
break;
}

}
}