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

12f675 adc problem

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



Joined: 13 Dec 2003
Posts: 7
Location: London

View user's profile Send private message Visit poster's website

12f675 adc problem
PostPosted: Fri Jul 02, 2004 4:35 pm     Reply with quote

Hi there, I've taken some code from the forum to get the ADC on the 12f675 to work, with no success. The following program seems to stall at the line:
Value=read_adc();
If I comment it out (//) it works fine. Any suggestions are greatly appreciated. Thanks, Moritz.
Code:

#include <12F675.h>
#device ADC=10

//////////////////////////////////////////////////
//
// Connections:
//
// PIN 1 to VCC
// PIN 2 to 220 Ohms resistor + LED
// PIN 3 to 220 Ohms resistor + LED
// PIN 4 10K to VCC
// PIN 5 n.c.
// PIN 6 n.c.
// PIN 7 to 3 pins Potentiomenter 10k Ohms ( PIN1 to VCC, PIN 2 to PIC
// PIN7, PIN 3 to GND)
// PIN 8 to GND
//
////////////////////////////////////////////////////
#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,MCLR
//#fuses MCLR,PUT,NOPUT,RC,EC,RC_IO,INTRC_IO,BROWNOUT,NOBROWNOUT
#use delay(clock=4000000)

void main()
{
int16 Value=300; 

set_tris_a(0b00000111);//RA0, RA1, RA2 as Inputs
//RA3, RA4, RA5 as Outputs 
setup_adc_ports( RA0_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0); 
delay_ms(150);                     // Allow the channel to be aquired
 
output_bit(pin_a5,1);
delay_ms (250);
output_bit(pin_a5,0);
delay_ms(10);

while(1)
   {
Value=read_adc();
delay_ms(10);

   if (Value > 512)//If more than 50% Flash fast
      {
      output_high(pin_a4);
      delay_ms (250);
      output_low(pin_a4);
      delay_ms(250);
      }
   else //if (Value < 513) Flash slow
      {
      output_high(pin_a4);
      delay_ms (1500);
      output_low(pin_a4);
      delay_ms(1500);
      }
   }
}
[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 02, 2004 5:02 pm     Reply with quote

What's your version of the compiler ?
moritz



Joined: 13 Dec 2003
Posts: 7
Location: London

View user's profile Send private message Visit poster's website

compiler version 3.1.0.23
PostPosted: Fri Jul 02, 2004 6:16 pm     Reply with quote

Hi, I've got compiler version 3.1.0.23

Cheers, Moritz.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 02, 2004 6:44 pm     Reply with quote

Could you clarify that a bit ? That number is not in
the usual CCS format. See this page to see how
they write their version numbers:
http://www.ccsinfo.com/versions.shtml
moritz



Joined: 13 Dec 2003
Posts: 7
Location: London

View user's profile Send private message Visit poster's website

Where can I find the version number?
PostPosted: Sat Jul 03, 2004 4:06 am     Reply with quote

That number was the version number of the exe file. I just cannot find the real version number, where do they write this?

Thanks for your patience with me...

Moritz.
moritz



Joined: 13 Dec 2003
Posts: 7
Location: London

View user's profile Send private message Visit poster's website

found it: 3.091
PostPosted: Sat Jul 03, 2004 4:50 am     Reply with quote

It's version 3.091
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 03, 2004 12:19 pm     Reply with quote

I installed vs. 3.091 and compiled your program. I looked at the
start-up code inserted by the compiler, and it's incorrect.
Also, for that version, the setup_comparator() function creates
incorrect code.

I have posted part of your program below. You need to add
the lines which are shown in bold. This will fix the problems
listed above.

Also I have one more comment. You are using "standard i/o"
which is the default mode for the compiler. In that mode, if you
use the built-in CCS functions such as output_low(), output_high(),
etc., the compiler will automatically insert code to setup the TRIS
register for an i/o port before it is written to. So you don't need
to use the set_tris_a() function in your program below. If you were
using the "fast_io" mode, then you would need it.

#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,MCLR
#use delay(clock=4000000)

#byte ADCON0 = 0x1F
#byte ANSEL = 0x9F
#byte CMCON = 0x19

void main()
{
int16 Value=300;

ADCON0 = 0; // ADC off
ANSEL = 0; // GPIO pins 0,1,2 and 4 set to all digital
CMCON = 7; // Comparators off

set_tris_a(0b00000111);//RA0, RA1, RA2 as Inputs
moritz



Joined: 13 Dec 2003
Posts: 7
Location: London

View user's profile Send private message Visit poster's website

PostPosted: Sat Jul 03, 2004 6:22 pm     Reply with quote

Thanks a lot for your help so far. I've included the lines in my code as below. Unfortunately the program still hangs up on the line
Value=read_adc();
I'm really totally lost now, any more ideas what might be wrong?
Thanks, Moritz.

Code:

#include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,MCLR

#byte ADCON0 = 0x1F
#byte ANSEL = 0x9F
#byte CMCON = 0x19
#use delay(clock=4000000)

void main()
{
int16 Value=300; 

ADCON0 = 0; // ADC off
ANSEL = 0; // GPIO pins 0,1,2 and 4 set to all digital
CMCON = 7; // Comparators off   

//set_tris_a(0b00000111);//RA0, RA1, RA2 as Inputs RA3, RA4, RA5 as Outputs 

setup_adc_ports( RA0_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0); 
delay_ms(150);                     // Allow the channel to be aquired
 
output_high(pin_a5);
delay_ms (250);
output_low(pin_a5);
delay_ms(10);

while(1)
   {
Value=read_adc();
delay_ms(10);

   if (Value > 512)//If more than 50% Flash fast
      {
      output_high(pin_a4);
      delay_ms (250);
      output_low(pin_a4);
      delay_ms(250);
      }
   else //if (Value < 513) Flash slow
      {
      output_high(pin_a4);
      delay_ms (1500);
      output_low(pin_a4);
      delay_ms(1500);
      }
   }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 03, 2004 11:46 pm     Reply with quote

I looked at it a bit further, and for vs. 3.091 with the 12F675, all
of the following A/D functions are completely screwed up.
They use registers and bit positions that are intended for the 16F877.
It can't possibly work.

setup_adc_ports();
setup_adc();
set_adc_channel();
read_adc();

If I feel energetic tomorrow, I'll work on coming up with some
revised functions that fix it for you.
moritz



Joined: 13 Dec 2003
Posts: 7
Location: London

View user's profile Send private message Visit poster's website

PostPosted: Sun Jul 04, 2004 4:37 am     Reply with quote

Dear PCM programmer,

just to say thank you for your help. I had a look through my disk collection and found another copy of the compiler, which is version 3.180. I installed it , compiled the program and it works.

For anyone who would like to try it out:

Thanks again, Moritz.

Code:
#include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,PUT,MCLR

#byte ADCON0 = 0x1F
#byte ANSEL = 0x9F
#byte CMCON = 0x19
#use delay(clock=4000000)

void main()
{
int16 Value=300; 

ADCON0 = 0; // ADC off
ANSEL = 0; // GPIO pins 0,1,2 and 4 set to all digital
CMCON = 7; // Comparators off   

//set_tris_a(0b00000111);//RA0, RA1, RA2 as Inputs RA3, RA4, RA5 as Outputs 

setup_adc_ports( AN0_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0); 
delay_ms(150);                     // Allow the channel to be aquired
 
output_high(pin_a5);
delay_ms (250);
output_low(pin_a5);
delay_ms(10);

while(1)
   {
Value=read_adc();
delay_ms(10);

   if (Value > 512)//If more than 50% Flash fast
      {
      output_high(pin_a4);
      delay_ms (250);
      output_low(pin_a4);
      delay_ms(250);
      }
   else //if (Value < 513) Flash slow
      {
      output_high(pin_a4);
      delay_ms (1500);
      output_low(pin_a4);
      delay_ms(1500);
      }
   }
}

 
//////////////////////////////////////////////////
//
// Connections:
//
// PIN 1 to VCC
// PIN 2 to 220 Ohms resistor + LED
// PIN 3 to 220 Ohms resistor + LED
// PIN 4 10K to VCC
// PIN 5 n.c.
// PIN 6 n.c.
// PIN 7 to 3 pins Potentiomenter 10k Ohms ( PIN1 to VCC, PIN 2 to PIC
// PIN7, PIN 3 to GND)
// PIN 8 to GND
//
////////////////////////////////////////////////////
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