Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

trouble with ADC on PIC16F873A

Status
Not open for further replies.

junhao_wu

New Member
Hi folks, need advise here. I am trying to do a ADC using pin 7 (AN4) on my PIC16F873A. I want to sense the input voltage. if the volt is less than 2.4V, the PWM function will turned on, if the volt is more than 2.4V then the PWM will turn off. But I keep getting 0 value from my ADC result.
I am using CC5X complier, with 4MHz clock.
below is my code:

#include "math24f.h"
#pragma chip PIC16F873A
#define num1 500

uns16 v_batt;
void int_adc(void)
{
TRISA = 0b.0011.1111; // set RA0, RA1, RA2, RA3, RA4 and RA5 to analog
ADCON1 = 0b.1000.0000;
ADCON0 = 0b.1110.0001;
delay_200ms();
}

void start_conv(void)
{
delay_200ms();
ADCON0.2 = 1; // start A/D conversion
while (ADCON0.2 == 1){}; // test if A/D still converting
delay_100ms();
return;
}
void get_measure_batt(void)
{
v_batt.low8 = ADRESL; //get measurement and store to v_batt
v_batt.mid8 = ADRESH;
}

void measure_batt(void)
{
start_conv();
get_measure_batt();
}
void main(void)
{
while(1)
{
InitFpFlags();
int_adc();
measure_batt();
if(v_batt < num1)
{
delay_200ms();
CCP1CON = 0b.0000.1100;
int_pwm(); // O PWM
T2CON.2 = 1;
}
else
{
int_pwm();
CCP1CON = 0b.0000.0000;
T2CON.2 = 1;
delay_200ms();
}
}
}
 
What is int_pwm?

Edit, ignore this. Should have gone to bed, looked at code instead, missed obvious.
 
Last edited:
int_PWM() is my PWM function. I left it out so that the code won't look so long when i post it here. Sorry.

Thanks
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top