View previous topic :: View next topic |
Author |
Message |
sandyw
Joined: 30 Jun 2023 Posts: 3
|
Cant read PIC16F15345 ADC |
Posted: Fri Jun 30, 2023 4:12 am |
|
|
Hello,
Can anybody help me with this problem? I am trying to read a DC voltage going from 0.5 to 3.5V on pin A2 of a pic16F15345. For some reason all I can read is the max value of 1023 or thereabouts. I can watch the voltage change on pin A2 on the scope.
I've chopped the code back to what I think should be the bare minimum.
I tried setting the tris and all the other (probably) stupid other thing I can think of.
Any comments..
Compiler Version 5.115
Code: |
#include <16F15345.h>
#device ADC=10 // 10 bit ADC values
#use delay(internal=16000000)
#fuses NOWDT
void main()
{
int16 reading = 0;
#pin_select NULL=PIN_A2
// Tries setting TRIS. Didn't work.'
set_tris_a(0b110100);
// Set pins A2 as analogue input.
set_analog_pins (pin_a2);
setup_adc_ports(sAN2|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
// This is to control a fet to let DC voltage through to A2
output_high(pin_c3);
while(TRUE)
{
reading = 0;
set_adc_channel(sAN2);
delay_us(100);
reading = read_adc();
delay_ms(1); // Just setting a point to stop code at and read whats in reading
reading = 0;
//TODO: User Code
}
}
|
Any help appreciated.
SandyW |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19503
|
|
Posted: Fri Jun 30, 2023 6:57 am |
|
|
One key thing wrong:
set_adc_channel(sAN2);
set_adc_channel, does not take sAN2, it just takes a channel number.
So:
set_adc_channel(2);
sAN2 is defined as 0x04000000, so this will be trying to select channel
67million!.... Not going to work....
Also better generally to always use the clock derived from Fosc, not
Frc. So ADC_CLOCK_DIV_16. This will always give slightly more accurate
results than the RC oscillator. |
|
|
sandyw
Joined: 30 Jun 2023 Posts: 3
|
|
Posted: Fri Jun 30, 2023 8:08 am |
|
|
Thanks Ttelmah,
You are a gental man and a scholar.
Problem solved. when pointed out it is obvious.
Sandyw |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 01, 2023 11:50 am |
|
|
One more small bug. You have two params joined with an OR symbol:
Quote: | setup_adc_ports(sAN2 | VSS_VDD); |
But the ccs manual and the .h file show the params separated by a comma.
This is the correct way to do it:
Code: | void setup_adc_ports(int32 pins, int32 reference); |
Your method doesn't cause harm because VSS_VDD is defined as 0.
But the correct method is the 2nd one. |
|
|
sandyw
Joined: 30 Jun 2023 Posts: 3
|
Cant read PIC16F15345 ADC - Solved |
Posted: Mon Jul 03, 2023 2:04 am |
|
|
Thanks for this PCMProgrammer. It is wrong on the posted code. A typo on my part due to cut and paste etc.
It is right in the main code that I am using but thank you, it made me go and check.
- SandyW |
|
|
|