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 support@ccsinfo.com

Two questions about 12F75 A/D converters

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







Two questions about 12F75 A/D converters
PostPosted: Tue Feb 25, 2003 10:57 pm     Reply with quote

<font face="Courier New" size=-1>I am having a problem with my 12F675 chips' A/D converters. I'm not trying to do anything particularly complicated, essentially just read in a value. I wrote some test code to make sure the A/D converter was working, which I include below:
<code><font color="#aa0000">
#include <12f675.h>
#use delay (clock = 20000000)
#define LED PIN_A2
void initialize ();
long volt_max;
main ()
{
     initialize ();
      while (true)
      {
      volt_max = read_adc ();
      if (volt_max < 250) led_blink (1);
      if ((volt_max >= 250) & (volt_max < 500)) led_blink (2);
      if ((volt_max >= 500) & (volt_max < 750)) led_blink (3);
      if (volt_max >= 750) led_blink (4);
      delay_ms (2000);
      }
}
void initialize ()
{
      setup_adc (ADC_CLOCK_DIV_2);
      setup_adc_ports (AN1_ANALOG);
      set_adc_channel (1);
      return;
}</font>
</code>
<i>Note: the led_blink(i) function simply blinks an LED i number of times, and I know for sure it works.</i>

First, I am not using a separate voltage reference, just Vdd. When I run this program, which should blink an LED a number of times directly dependent on the voltage on the A/D converter, it will only blink once for voltages of 1V to 4.9V, and then from 4.9V to 5V it blinks twice. For my code, if the LED blinks once, there should be a voltage of 0V to 1.25V on the A/D converter, but that is not the case. If it blinks twice, there should be a voltage of 1.25V to 2.5V on the A/D converter, which is also not happening. The most befuddling aspect of this problem is that if I change <font color="#aa0000"><code>volt_max</code></font> to an <font color="#aa0000"><code>int</code></font> instead of a <font color="#aa0000"><code>long</code></font>, and then adjust the test values in the code accordingly (i.e., 250-500-750 converts to 64-128-192), everything works fine. I thought maybe (a) the A/D converter is not doing full 10-bit conversion, or (b) the format of the output is not being set or read correctly by the C compiler, but it's probably something else. I have tried manually setting the ADFM bit to both states in the ADCON register after the A/D converter setup code, but neither way works. As such, I'm rather out of ideas.<br><br>
My second question is, if I am running the 12F675 off the internal crystal, can I use the option <font color="#aa0000"><code>setup_adc (ADC_CLOCK_INTERNAL);</code></font>? The datasheet for the 12F675 gives the impression that the A/D converter has its own oscillator, but I have not been able to verify if both will work at the same time because I have been testing my chip with an external crystal to eliminate a possible source of error. When I build my final device, however, I will not have enough pins to use an external crystal.

Well, thanks a lot in advance to any help anyone can give.

Regards,
Chris</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 12109
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Two questions about 12F75 A/D converters
PostPosted: Tue Feb 25, 2003 11:54 pm     Reply with quote

<font face="Courier New" size=-1>:=I am having a problem with my 12F675 chips' A/D converters. I'm not trying to do anything particularly complicated, essentially just read in a value. I wrote some test code to make sure the A/D converter was working, which I include below:
:=
:=#include <12f675.h>

To get 10-bit A/D resolution, you need to insert the
following line, at this point:

#device adc = 10

:=#use delay (clock = 20000000)
:=#define LED PIN_A2
:=void initialize ();
:=long volt_max;
:=main ()
:={
:=     initialize ();
:=       while (true)
:=       {
:=       volt_max = read_adc ();
:=       if (volt_max < 250) led_blink (1);

These tests should use the Logical AND operator,
which is "&&".

:=       if ((volt_max >= 250) & (volt_max < 500)) led_blink (2);
:=       if ((volt_max >= 500) & (volt_max < 750)) led_blink (3);
:=       if (volt_max >= 750) led_blink (4);
:=       delay_ms (2000);
:=       }
:=}
:=void initialize ()
:={

If you look at the 12F675 data sheet, in Table 7-1, it gives
the allowable A/D clock divisors for a 20 MHz crystal.
These divisors are 32, 64, or the internal A/D clock.
You shouldn't use a divisor of 2.
The data sheet can be downloaded here:
<a href="http://www.microchip.com/1000/pline/picmicro/category/flash/index.htm" TARGET="_blank"> <a href="http://www.microchip.com/1000/pline/picmicro/category/flash/index.htm" TARGET="_blank">http://www.microchip.com/1000/pline/picmicro/category/flash/index.htm</a></a>

:=      setup_adc (ADC_CLOCK_DIV_2);
:=      setup_adc_ports (AN1_ANALOG);
:=      set_adc_channel (1);
:=      return;
:=}</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 12110
Welshman
Guest







Re: Two questions about 12F75 A/D converters
PostPosted: Wed Feb 26, 2003 12:32 pm     Reply with quote

Thanks a million. My code worked the first time I made the changes.

Regards,
Chris
___________________________
This message was ported from CCS's old forum
Original Post ID: 12130
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