CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADC test code not working as expected

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AArdvark



Joined: 09 Mar 2015
Posts: 10

View user's profile Send private message

ADC test code not working as expected
PostPosted: Fri Aug 14, 2015 2:21 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 14, 2015 3:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 14, 2015 3:43 pm     Reply with quote

thanks Jay,

I knew it would be a simple silly mistake so obvious when someone points it out! Embarassed

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

View user's profile Send private message AIM Address

PostPosted: Fri Aug 14, 2015 6:42 pm     Reply with quote

is this hardware or
Proteus/Isis/MPlab?
AArdvark



Joined: 09 Mar 2015
Posts: 10

View user's profile Send private message

PostPosted: Sat Aug 15, 2015 3:10 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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