|
|
View previous topic :: View next topic |
Author |
Message |
moritz
Joined: 13 Dec 2003 Posts: 7 Location: London
|
12f675 adc problem |
Posted: Fri Jul 02, 2004 4:35 pm |
|
|
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
|
|
Posted: Fri Jul 02, 2004 5:02 pm |
|
|
What's your version of the compiler ? |
|
|
moritz
Joined: 13 Dec 2003 Posts: 7 Location: London
|
compiler version 3.1.0.23 |
Posted: Fri Jul 02, 2004 6:16 pm |
|
|
Hi, I've got compiler version 3.1.0.23
Cheers, Moritz. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 02, 2004 6:44 pm |
|
|
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
|
Where can I find the version number? |
Posted: Sat Jul 03, 2004 4:06 am |
|
|
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
|
found it: 3.091 |
Posted: Sat Jul 03, 2004 4:50 am |
|
|
It's version 3.091 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 03, 2004 12:19 pm |
|
|
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
|
|
Posted: Sat Jul 03, 2004 6:22 pm |
|
|
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
|
|
Posted: Sat Jul 03, 2004 11:46 pm |
|
|
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
|
|
Posted: Sun Jul 04, 2004 4:37 am |
|
|
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
//
////////////////////////////////////////////////////
|
|
|
|
|
|
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
|