VoltVolt
Junior Member level 1
- Joined
- Sep 15, 2013
- Messages
- 15
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 124
Hi,
What i want i actually let PIC to detect the voltage level at RA0(AN0), if the voltage level is from 3-5V, then turn on the LEDs at PORTC...
I have turn on the Analog setting at RA0 but still not working...
Microcontroller: PIC18F45K22
Compiler: MPLABX IDE Hitech C
Thanks for the advises...
What i want i actually let PIC to detect the voltage level at RA0(AN0), if the voltage level is from 3-5V, then turn on the LEDs at PORTC...
I have turn on the Analog setting at RA0 but still not working...
Microcontroller: PIC18F45K22
Compiler: MPLABX IDE Hitech C
Code:
#include <htc.h>
/* Start of configuration fuses*/
#pragma config IESO=OFF, FOSC=INTIO67,PRICLKEN=OFF,FCMEN =OFF,PLLCFG=ON,BOREN=ON,BORV=250
#pragma config WDTEN=OFF
#pragma config P2BMX=PORTC0,CCP2MX=PORTC1,PBADEN=OFF,CCP3MX=PORTE0,MCLRE=INTMCLR,HFOFST=OFF,T3CMX=PORTC0
#pragma config DEBUG=OFF,STVREN=ON,XINST=OFF,LVP=OFF
#pragma config CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF
#pragma config CPB=OFF,CPD=OFF
#pragma config WRT0=ON,WRT1=ON,WRT2=ON,WRT3=ON
#pragma config WRTB=ON,WRTC=ON,WRTD=ON
/* end of configuration fuses */
void init_ADC() {
OSCCON = 0b11110011; //16MHz internal oscillator
OSCCON2 = 0; //off secondary oscillator
ANSELA = 0xFF;
TRISA = 0xFF; //input
LATA = 0;
ANSELC = 0x00;
TRISC = 0x00; //output
ADCON0 = 0x01; //set AN0 to be sampled and the ADC is enabled
ADCON1 = 0x00; //+Vref and -Vref connected to Vdd and Vss respectively
ADCON2 = 0b00110100; //Left justified, 16 TAD, FOSC/64
}
void main() {
init_ADC;
while (1) {
unsigned int adc_eqn;
float voltage;
//ADC start
GO_DONE = 1;
//wait for ADC to complete
while (GO_DONE) {
continue;
}
//retrieve adc equavalent value
adc_eqn = ((ADRESH << 8) | ADRESL);
voltage = (adc_eqn * 5.0 / 1023);
if (voltage > 3 && voltage < 5) {
LATC = 0xFF;
}
}
}
void delay_ms(unsigned char time) //create integral number of milisecond delays, if the clock is 64MHZ
{
char i;
for (i = 0; i < time; i++) _delay(4000); //for 4MIPS operation or 16MHZ XTAL TODO
}
Thanks for the advises...