View previous topic :: View next topic |
Author |
Message |
AArdvark
Joined: 09 Mar 2015 Posts: 10
|
ADC test code not working as expected |
Posted: Fri Aug 14, 2015 2:21 pm |
|
|
Hi,
I am trying to write some test code for the ADC reading to control the blink rate of an led. See below for the code, this seems to work although a bit flaky.
Code: |
#include <main.h>
void main()
{
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(0);
int x;
Output_low(PIN_C7);
while (TRUE)
{
x = read_adc();
Output_high(PIN_C7);
Delay_ms(x);
output_low(PIN_C7);
Delay_ms(x);
}
}
|
and main.h contains
Code: |
#include <16F690.h>
#device ADC=8
#use delay(internal=8000000)
|
I am using a pic 16f690 and i have a 10K pot across a 5V supply feeding into AN0 or pin 19.
What i am trying to do is have some code that has a maximum output of 166.66hz (a 50% mark space ratio of 6ms) on pin C7.
I was going to use the value from the ADC to be calculated with a constant so when the pot is fully turned the output will be 166.66hz and turned the other way will be 0hz. Please see the code below.
Code: |
#include <main.h>
void main()
{
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(0);
int y;
float x;
Output_low(PIN_C7);
while (TRUE)
{
y = read_adc();
x =y*0.00234;
Output_high(PIN_C7);
Delay_ms(x);
output_low(PIN_C7);
Delay_ms(x);
}
}
|
But this code does not work. I am aware that this once working will be the inverse to what i want as the minimum will be at 6ms not the maximum which will be a lot quicker, the idea was to get the adc reading code working first.
Can anyone see what silly mistakes i have made?
thanks in advance |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Aug 14, 2015 3:23 pm |
|
|
First HUGE mistake is the ADC_clock selection !
Please read note 4 of table 9-1 of the datasheet ( page 109). Though I've only got 20 years of PICs under my belt I've never, ever used the 'internal' ADC clock......
2nd mistake is that delay_ms(x) takes an INTEGER( 0 - 65535) NOT a floating point. Play 'computer' and see what values YOU get for the ADC result being 0 and then 255.
that's a start....
recode,recompile,reload,retest,report back your progress..
Jay |
|
|
AArdvark
Joined: 09 Mar 2015 Posts: 10
|
|
Posted: Fri Aug 14, 2015 3:43 pm |
|
|
thanks Jay,
I knew it would be a simple silly mistake so obvious when someone points it out!
as for the 'ADC_clock selection' i am not going against your 20 years experience but the first code i posted only worked when this was added, i will double check this though. note taken about the datasheet. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Aug 14, 2015 6:42 pm |
|
|
is this hardware or
Proteus/Isis/MPlab? |
|
|
AArdvark
Joined: 09 Mar 2015 Posts: 10
|
|
Posted: Sat Aug 15, 2015 3:10 am |
|
|
Yes this is hardware it is being using on a demo board that I built years ago. Where all the ports breakout to headers and on port C I have a jumper to switch in leds as well as what is connected to the headers. And the center pin of a 10K pot going to AN0 or pin 19 with the other 2 pins on the pot going across 5 rail. |
|
|
|