View previous topic :: View next topic |
Author |
Message |
matthewmilford
Joined: 09 Feb 2021 Posts: 23
|
ADC unexpected results |
Posted: Mon Jun 28, 2021 8:07 am |
|
|
So, I was working on some code and needed to get an analog input signal. I have done this several times, with no issues.... however this time it seems to be a bit different. What am I missing? I am using a pic 18f26k42 with mplabx v 5.35. See code.
Code: |
#include <18f26k42.H>
#device ADC = 10
#include <BMLCD420.c>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#fuses HS, NOPROTECT, NOPUT, NOLVP
#use delay(internal = 4MHz)
#define inputsignal PIN_B0
#define gate2 PIN_B2
#define light PIN_B4
#define involt PIN_A7
void main()
{
lcd_init();
setup_adc(ADC_CLOCK_INTERNAL); // set ADC clock source
setup_adc_ports(sAN7 | VSS_VDD);
set_analog_pins(PIN_A7);
set_adc_channel(7);
while(TRUE)
{
delay_ms(500);
output_toggle(light);
int32 readout = read_adc(ADC_READ_ONLY);
lcd_gotoxy(1,3);
printf(lcd_putc," ");
lcd_gotoxy(1,3);
printf(lcd_putc,"%lu",readout);
}
|
I was looking at the datasheet for this pic
[url]
http://ww1.microchip.com/downloads/en/DeviceDoc/Microchip%208bit%20mcu%20%20PIC18%20L_F26_27_45_46_47_55_56_57K42%20low-power%20high-performance%20mcu%20with%20xlp%20tech%2040001919B.pdf
[/url]
On page 9 it references pin A7 where I am trying to read the signal from as being referenced as 9 or 6 (with my spdip pin set I believe its 9) should this replace the 7 in set_adc_channel command?
Or am I missing something else? I know this has to be something really simple but I just can't see what right now.
Oh also, I am currently just using a pot as a test to emulate the analog signal and I have a meter on it and can watch the voltage range from 0-5V going into pin A7, so no hardware issues.
In terms of output, I am getting 1023(ish) regardless of if the signal is connected or not so it obviously is not actually reading it. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Jun 28, 2021 10:35 am |
|
|
No, the problem is that you are never starting an actual ADC conversion.
Simply get rid of the ADC_READ_ONLY setting in your read_adc.
The default is START_AND_READ, and this starts a conversion, waits
for it to complete, and then reads the results. Currently you are repeatedly
reading the result from the ADC, without an actual conversion ever being
started....
int32 readout = read_adc();
You use 'ADC_READ_ONLY', when you use something like a timer to
start the ADC conversion. |
|
|
matthewmilford
Joined: 09 Feb 2021 Posts: 23
|
|
Posted: Mon Jun 28, 2021 11:30 am |
|
|
changed that... still no luck. It has changed however, now it is hovering at zero as its readout. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Mon Jun 28, 2021 1:24 pm |
|
|
sounds like you might have your signal on a different pin than you're reading with the adc.
Any chance your using an xtal? That seems to be using the AN6 & AN7 pins.
Maybe your on Pin 7 (an5) by mistake? |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Mon Jun 28, 2021 1:30 pm |
|
|
I think you set the fuses for an external xtal. Shouldn't it be HFINTOSC or something like that instead of HS?
(don't have time to look it up, sorry) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 28, 2021 1:34 pm |
|
|
Quote: | #fuses HS, NOPROTECT, NOPUT, NOLVP
#use delay(internal = 4MHz) |
He is using the internal 4 MHz oscillator but mistakenly has the HS
fuse in there. He needs to delete the HS fuse. |
|
|
matthewmilford
Joined: 09 Feb 2021 Posts: 23
|
|
Posted: Mon Jun 28, 2021 1:41 pm |
|
|
I don't think its as simple as a pinout issue. I have tried changing the pinout between 6, 7, and 9 ( 7 being the logical one and 6 and 9 coming from the data sheet).
It's entirely possible that I am inadvertantly using an xtal... perhaps this is why my "normal" adc code isn't working as I need a different fuse declaration. I will do some research on this and post again if I find anything that seems promising or fixes it. Thanks for the idea! |
|
|
matthewmilford
Joined: 09 Feb 2021 Posts: 23
|
|
Posted: Mon Jun 28, 2021 2:33 pm |
|
|
I needed to add in the NOEXTOSC fuse, that seems to have fixed the issue. Thanks to those of you that mentioned fuses you were totally on the right track. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 28, 2021 4:09 pm |
|
|
Nope. You needed to pull the HS fuse. If you do that, the compiler
automatically puts in NOEXTOSC. You also need to learn your fuses.
HS is one of the most basic. It's for a crystal (usually > 4 MHz).
This:
Code: | #include <18F26K42.h>
#fuses NOPROTECT, NOPUT, NOLVP // Note: HS removed
#use delay(internal = 4MHz)
|
Gives this:
Code: |
Configuration Fuses:
Word 1: FFEC NOEXTOSC RSTOSC_HFINTRC_1MHZ NOCLKOUT PRLOCK1WAY CKS FCMEN
Word 2: FFF7 MCLR NOPUT NOMVECEN IVT1WAY NOLPBOR BROWNOUT BORV24 ZCDDIS PPS1WAY STVREN NODEBUG NOXINST
Word 3: FF9F WDTSW NOWDT WDTWIN_SW WDTCLK_SW
Word 4: DFFF BBSIZ512 NOBOOTBLOCK NOSAF NOWRT NOWRTB NOWRTC NOWRTD NOWRTSAF NOLVP
Word 5: FFFF NOPROTECT |
|
|
|
|