Hi there,
I am trying to program a microprocessor in C to do a simple test for a sensor system. The chip takes in 3 values from three different pins and then measures them through the use of the A/D converter. These stored values are then compared to preset values and certain pins are turned high or low as a result. These high or low pins will light LEDs which ultimately are the output and tell the user what the reading is.
The specific model of the chip is the PIC16F684 and the specific pins layout (such as RA0) can be found online.This is a school project and is due in less than a week.
I am very new to programming and created this with the help of the book that came with the micon ("123 PIC microcontroller programs for the Evil Genius"), so peculiar expressions are not my idea. It compiles fine, however when we test it in modeled circuit it does nothing.
Can someone tell me what is wrong here or how I could do this another way??
The program is found below.
Thank you very much..
#include <pic.h>
/* APSC 230 program
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
int ADCValue0 = 0;
int ADCValue1 = 0;
int ADCValue2 = 0;
int HIGHPH = 11010010; //4210 in binary = high voltage threshold for sensor
int LOWPH = 1000110; //70 in binary = low voltage threshold for sensor
int HIGHCl = 10010110; //150 in binary = high voltage threshold for FCL and TCL
main()
{
PORTC = 0;
CMCON0 = 7; //Comparators off
ANSEL = 0b00001111;// enable RA0 RA1 RA2 and RA3 analogue inputs
ADCON0 = 0b000000001; //Turn on the ADC
// bit 7 – left justified sample
// bit 6 – Use VDD
// bit 4:2- Channel 0, i.e. pin RA0
// bit 1 – Do not Start
// bit 0 – Turn on ADC, see pg 94..
ADCON1 = 0b001110000; // Select the internal RC Clock
ADCON0 = ADCON0 | (1 << 1); // Could also be "GODONE = 1;"
while(!GODONE)// wait for ADC to complete
ADCValue0 = ADRESH; //Save sample value after operation
while (1 == 1)
{
if (ADCValue0 > HIGHPH)
{
RA5 = 1;
RC5 = 0;
RA4 = 0;//High pH LED turned on
}
else if (ADCValue1 < LOWPH)
{
RA5 = 0;
RC5 = 1;
RA4 = 0;
// Low pH LED turned on
}
else
{
RA5 = 1;
RC5 = 0;
RA4 = 1; // Safe pH LED Turned on
}
}
ADCON0 = 0b000001001; //Turn on the ADC
// bit 7 – left justified sample
// bit 6 – Use VDD <<<<<<<< or could i use RA1 as vref???? if so use bit 6 = 1 for RA1 as vref
// bit 4:2- Channel 1, i.e. pin RA2
// bit 1 – Do not Start
// bit 0 – Turn on ADC, see pg 94..
ADCON1 = 0b001110000; // Select the internal RC Clock
ADCON0 = ADCON0 | (1 << 1); // Could also be "GODONE = 1;"
while(!GODONE)// wait for ADC to complete
ADCValue1 = ADRESH; //Save sample value after operation
while (1 == 1)
{
if ((ADCValue1 > HIGHCl) )
{
RC0 = 1;//High FCl LED turned on
RC1 = 0;
}
else
{
RC0 = 0;
RC1 = 1; // Safe pH LED Turned on
}
}
ADCON0 = 0b000010001; //Turn on the ADC
// bit 7 – left justified sample
// bit 6 – Use VDD
// bit 4:2- Channel 2, i.e. pin RA3
// bit 1 – Do not Start
// bit 0 – Turn on ADC, see pg 94..
ADCON1 = 0b001110000; // Select the internal RC Clock
ADCON0 = ADCON0 | (1 << 1); // Could also be "GODONE = 1;"
while(!GODONE)// wait for ADC to complete
ADCValue2 = ADRESH; //Save sample value after operation
while (1 == 1)
{
if ((ADCValue2 > HIGHCl) )
{
RC4 = 1;
RC3 = 0;//High TCl LED turned on
}
else
{
RC4 = 0;
RC3 = 1; // Safe TCl LED turned on
}
}
}
Technorati Tags: adc, analogue inputs, comparators, evil genius, fcl, high voltage, leds, low voltage, lt 1, micon, microprocessor, pic microcontroller, pic16f684, pins, preset values, ra1 ra2, ra3, sensor system, simple test, threshold