this is a microprocessing question write a c program for the PIC 16F 18857, that
ID: 3786824 • Letter: T
Question
this is a microprocessing question
write a c program for the PIC 16F 18857, that takes an ADC input from a voltages divider, and turns on a Led based on the input. input is atached to a light sensor, if light is present LED in off, if dark LED is on. this program uses only two ports RA0 for input and R05 for output. Your PIC based light sensor device must: 1. Convert the light intensity information to a variable voltage (0-3V). 2. Obtain an analog input signal (sensory data) via an analog pin of the PIC (e.g. AN0) 3. Use the internal ADC of PIC to convert analog signals to digital values 4. Turn on a LED if the sensor is encapsulated in a dark box or put in a dark environment. 5. Turn off the LED if the sensor is placed in a well-lighted environment.
Explanation / Answer
program for the PIC 16F 18857, that takes an ADC input from a voltages divider, and turns on a Led based on the input. input is atached to a light sensor, if light is present LED in off, if dark LED is on.
#include <htc.h>
#include "lcd.h"
//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);
//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}
//Function to Initialise the ADC Module
void ADCInit()
{
//We use default value for +/- Vref
//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN
//Port Configuration
//We also use default value here too
//All ANx channels are Analog
/*
ADCON2
*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/
ADCON2=0b10001010;
}
//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
if(ch>13) return 0; //Invalid Channel
ADCON0=0x00;
ADCON0=(ch<<2); //Select ADC Channel
ADON=1; //switch on the adc module
GODONE=1;//Start conversion
while(GODONE); //wait for the conversion to finish
ADON=0; //switch off adc
return ADRES;
}
void main()
{
//Let the LCD Module start up
Wait(100);
//Initialize the LCD Module
LCDInit(LS_BLINK);
//Initialize the ADC Module
ADCInit();
//Clear the Module
LCDClear();
//Write a string at current cursor pos
LCDWriteString("ADC Test");
while(1)
{
unsigned int val; //ADC Value
val=ADCRead(0); //Read Channel 0
LCDWriteIntXY(0,1,val,4);
Wait(1000);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.