|
|
View previous topic :: View next topic |
Author |
Message |
chrissh
Joined: 01 Jan 2016 Posts: 2
|
PIC12F675's ADC |
Posted: Fri Jan 01, 2016 4:35 am |
|
|
Hello CCS Forum,
This is my first topic and I never had to post something in a forum but this deal is a really annoying thing, just because it's something so small and can't move on with the entire project.
I got problems with the 12F675's ADC, with the following code:
Code: | #include <12f675.h>
#fuses INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOBROWNOUT
#use delay(clock = 4M)
void main()
{
disable_interrupts(GLOBAL);
SETUP_TIMER_0(T0_INTERNAL|T0_DIV_64|T0_8_BIT);
setup_comparator(NC_NC);
set_tris_a(0x10); // only GP4 as input
port_a_pullups(0x0);
SETUP_ADC(ADC_CLOCK_INTERNAL);
SETUP_ADC_PORTS(sAN3|VSS_VDD);
output_a(0x00);
while (1)
{
set_adc_channel(PIN_A4);
unsigned int16 adc;
adc = read_adc(7);
output_low(PIN_A0);
output_low(PIN_A1);
if (adc > 500) output_high(PIN_A1);
else output_high(PIN_A0);
delay_ms(500);
}
} |
As you can see it's not a complicated code...
The idea is that the green LED turns on (PIN_A1) when the ADC reads more than ~2.5V and the red LED turns on (PIN_A0) when otherwise (< ~2.5V).
But when I put two 1K resistors as a votage divider it always marks around 0.11V in the ADC reference resistor as the following picture shows:
According to Ohm's Law (pretty root stuff) I should get 2.5V, ADC reads 511 in binary, 511 > 500 and the green LED turns on but nope. So I assume it's something in the PIC right in the ADC section or in the input buffer that won't let it work.
Knowing that the max impedance Microchip tells you this PIC can hold on is 10K, i tested it with 10K, 1K, 220 (as voltage divider) getting always the same result.
So I just don't know where to look know, maybe I'm missing something.
If anyone can show me I've got it wrong in someplace please reply.
Thank you for your time. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Jan 01, 2016 4:49 am |
|
|
Your 'output_a(0)' instruction, is setting the pin to an output, and pulling it low.....
This drives all pins (except GP3) low.
First add a capacitor right adjacent to the PIC between pins 1 and 8. Get in the habit of this. PIC's (and most digital IC's), _require_ smoothing right by the chip, if you are not to get problems later...
Then change your code to:
Code: |
#include <12f675.h>
#device ADC=10 //configures the ADC to return 10bits
#fuses INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOBROWNOUT
#use delay(clock = 4M)
void main()
{
//declare your variables at the start of code sections
unsigned int16 adc;
//disable_interrupts(GLOBAL); //not needed interrupts are disabled
SETUP_TIMER_0(T0_INTERNAL|T0_DIV_64|T0_8_BIT);
setup_comparator(NC_NC);
output_a(0x00); //set all pins low
set_tris_a(0x10); //and now enable GP4 as input
port_a_pullups(0x0);
SETUP_ADC(ADC_CLOCK_DIV_8); //'Internal' is not recommended above
//1Mhz, unless you are putting the chip to sleep for the conversion...
//read the data sheet.
SETUP_ADC_PORTS(sAN3|VSS_VDD);
set_adc_channel(3); //this takes a channel number, not a PIN
while (TRUE)
{
delay_ms(500); //putting the delay at the start ensures the
//adc has time to acquire before you start to read it.
adc = read_adc(); //no value needed here
if (adc > 500)
{
output_low(PIN_A0);
output_high(PIN_A1);
}
else
{
output_low(PIN_A1);
output_high(PIN_A0);
}
}
}
|
Lots of comments inline.
Putting the 'output_low' inside the condition, stops the LED's both being momentarily turned off. |
|
|
chrissh
Joined: 01 Jan 2016 Posts: 2
|
|
Posted: Fri Jan 01, 2016 5:35 am |
|
|
Thank you for your help. There were some details I didn't know. _________________ UTN FRBA - IngenierÃa Electrónica |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|