|
|
View previous topic :: View next topic |
Author |
Message |
john cutler
Joined: 06 Sep 2003 Posts: 82 Location: Hot Tub, California
|
12F675 - Can't get AD working at all. |
Posted: Tue Sep 03, 2002 12:53 pm |
|
|
After 2 days of hair pulling, I'm exasperated. I downloaded 3.110 CCS PCWH and now (at least)the 675.h file seems more complete. Here's my code:
#include <12F675.h>
#use delay(clock=4000000)
#fuses INTRC_IO,PUT,MCLR
void main() {
setup_adc_ports(AN0_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
while(TRUE){
int value;
int i;
delay_us(200);
value = read_adc();
for(i=0; i<8; ++i) {
output_bit(PIN_A2,shift_right(&value,1,0));
}
}
}
Dead in the water. If I assign a hex number to "value" and comment out the read_adc() statement, I et I nice repeatin pulse train on AN_2.
Can anyone shed light on what I'm doing wrong?
The PIC12F675 manual is very confusing. For ANSEL register (9fh) it says (and I quote)
"Between analog or digital function on pins AN<3:0>,repectively.)
0 = Digital I/O; pin is assigned to port or special function
1 = Analog input; pin is assigned to analog input"
However, when I run MPSIM, it seems the CCS functions assign a 0 to whichever port pins I ask to be analog inputs in ANSEL bits 3-0.
HELP!!!
Thanks
John
___________________________
This message was ported from CCS's old forum
Original Post ID: 6839 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: 12F675 - Can't get AD working at all. |
Posted: Tue Sep 03, 2002 3:27 pm |
|
|
:=After 2 days of hair pulling, I'm exasperated. I downloaded 3.110 CCS PCWH and now (at least)the 675.h file seems more complete.
----------------------------------------------
I compiled your program with PCM 3.110, and upon examining
the .LST file, I would say that CCS has screwed up these
three functions:
setup_adc_ports(AN0_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
They appear to be using masks that are for the 16F877.
The 12F675 has the A/D control bits in slightly different
locations than the 16F877, so of course the code doesn't work.
For example, in setup_adc(), they're doing "ANDLW 38",
which fits the bitmap of ADCON0 for the 16F877, but not
the 12F675. There are similar problems with the other
functions.
The data sheet also says to be sure to set the TRISIO
register bit to input mode. I don't see them doing that.
It probably works out OK because those bits are set
as inputs upon power-up reset.
My suggestion is to setup the A/D registers yourself, by
defining the registers with the #byte command, and then
manually stuff values into them. Then at least you know
it's done right.
___________________________
This message was ported from CCS's old forum
Original Post ID: 6842 |
|
|
john cutler
Joined: 06 Sep 2003 Posts: 82 Location: Hot Tub, California
|
Re: 12F675 - Can't get AD working at all. |
Posted: Tue Sep 03, 2002 3:34 pm |
|
|
:=:=After 2 days of hair pulling, I'm exasperated. I downloaded 3.110 CCS PCWH and now (at least)the 675.h file seems more complete.
:=----------------------------------------------
:=
:=I compiled your program with PCM 3.110, and upon examining
:=the .LST file, I would say that CCS has screwed up these
:=three functions:
:=
:=setup_adc_ports(AN0_ANALOG);
:=setup_adc(ADC_CLOCK_DIV_8);
:=set_adc_channel(0);
:=
:=They appear to be using masks that are for the 16F877.
:=The 12F675 has the A/D control bits in slightly different
:=locations than the 16F877, so of course the code doesn't work.
:=
:=For example, in setup_adc(), they're doing "ANDLW 38",
:=which fits the bitmap of ADCON0 for the 16F877, but not
:=the 12F675. There are similar problems with the other
:=functions.
:=
:=The data sheet also says to be sure to set the TRISIO
:=register bit to input mode. I don't see them doing that.
:=It probably works out OK because those bits are set
:=as inputs upon power-up reset.
:=
:=My suggestion is to setup the A/D registers yourself, by
:=defining the registers with the #byte command, and then
:=manually stuff values into them. Then at least you know
:=it's done right.
:=
THANK YOU!!! That's what I thought but I always suspect myself and assume the "big boys" have it right. What a relief to have confirmation that I'm not losing my mind.
I will try coding it myself and will let you know if I achieve success.
Now let's see, 3 days of my life at $10/day - CCS owes me $30!!!
___________________________
This message was ported from CCS's old forum
Original Post ID: 6843 |
|
|
john cutler
Joined: 06 Sep 2003 Posts: 82 Location: Hot Tub, California
|
Re: 12F675 - Can't get AD working at all. |
Posted: Tue Sep 03, 2002 6:07 pm |
|
|
:=:=:=After 2 days of hair pulling, I'm exasperated. I downloaded 3.110 CCS PCWH and now (at least)the 675.h file seems more complete.
:=:=----------------------------------------------
:=:=
:=:=I compiled your program with PCM 3.110, and upon examining
:=:=the .LST file, I would say that CCS has screwed up these
:=:=three functions:
:=:=
:=:=setup_adc_ports(AN0_ANALOG);
:=:=setup_adc(ADC_CLOCK_DIV_8);
:=:=set_adc_channel(0);
:=:=
:=:=They appear to be using masks that are for the 16F877.
:=:=The 12F675 has the A/D control bits in slightly different
:=:=locations than the 16F877, so of course the code doesn't work.
:=:=
:=:=For example, in setup_adc(), they're doing "ANDLW 38",
:=:=which fits the bitmap of ADCON0 for the 16F877, but not
:=:=the 12F675. There are similar problems with the other
:=:=functions.
:=:=
:=:=The data sheet also says to be sure to set the TRISIO
:=:=register bit to input mode. I don't see them doing that.
:=:=It probably works out OK because those bits are set
:=:=as inputs upon power-up reset.
:=:=
:=:=My suggestion is to setup the A/D registers yourself, by
:=:=defining the registers with the #byte command, and then
:=:=manually stuff values into them. Then at least you know
:=:=it's done right.
:=:=
:=
:=THANK YOU!!! That's what I thought but I always suspect myself and assume the "big boys" have it right. What a relief to have confirmation that I'm not losing my mind.
:=I will try coding it myself and will let you know if I achieve success.
:=
:=Now let's see, 3 days of my life at $10/day - CCS owes me $30!!!
Well mr PCM Porgrammer - not three but all 4 functions are wrong! The read_adc() was offset as well and kept trying to set bit 2 instead of bit 1 of the ADCON0 in the 12F675. I finally saw the mistake and rewrote assembly code for the read_adc function. wow - 3 DAYS OF MY LIFE - Thanks so much for your help. I think I feel some of my hair growing back :)
John
___________________________
This message was ported from CCS's old forum
Original Post ID: 6847 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: 12F675 - Can't get AD working at all. |
Posted: Tue Sep 03, 2002 6:13 pm |
|
|
:=Well mr PCM Porgrammer - not three but all 4 functions are wrong! The read_adc() was offset as well and kept trying to set bit 2 instead of bit 1 of the ADCON0 in the 12F675. I finally saw the mistake and rewrote assembly code for the read_adc function. wow - 3 DAYS OF MY LIFE - Thanks so much for your help. I think I feel some of my hair growing back <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0">
:=
:=
---------------------------------------------------------
Put together an email to CCS support with all your fixes.
Hopefully they'll implement it in a future release.
___________________________
This message was ported from CCS's old forum
Original Post ID: 6848 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: 12F675 - Can't get AD working at all. |
Posted: Tue Sep 03, 2002 6:39 pm |
|
|
This is a good example of why not to use the built in functions. I try to always read the datasheet and use the register method. It also makes your code more portable.
Regards,
Mark
:=:=:=:=After 2 days of hair pulling, I'm exasperated. I downloaded 3.110 CCS PCWH and now (at least)the 675.h file seems more complete.
:=:=:=----------------------------------------------
:=:=:=
:=:=:=I compiled your program with PCM 3.110, and upon examining
:=:=:=the .LST file, I would say that CCS has screwed up these
:=:=:=three functions:
:=:=:=
:=:=:=setup_adc_ports(AN0_ANALOG);
:=:=:=setup_adc(ADC_CLOCK_DIV_8);
:=:=:=set_adc_channel(0);
:=:=:=
:=:=:=They appear to be using masks that are for the 16F877.
:=:=:=The 12F675 has the A/D control bits in slightly different
:=:=:=locations than the 16F877, so of course the code doesn't work.
:=:=:=
:=:=:=For example, in setup_adc(), they're doing "ANDLW 38",
:=:=:=which fits the bitmap of ADCON0 for the 16F877, but not
:=:=:=the 12F675. There are similar problems with the other
:=:=:=functions.
:=:=:=
:=:=:=The data sheet also says to be sure to set the TRISIO
:=:=:=register bit to input mode. I don't see them doing that.
:=:=:=It probably works out OK because those bits are set
:=:=:=as inputs upon power-up reset.
:=:=:=
:=:=:=My suggestion is to setup the A/D registers yourself, by
:=:=:=defining the registers with the #byte command, and then
:=:=:=manually stuff values into them. Then at least you know
:=:=:=it's done right.
:=:=:=
:=:=
:=:=THANK YOU!!! That's what I thought but I always suspect myself and assume the "big boys" have it right. What a relief to have confirmation that I'm not losing my mind.
:=:=I will try coding it myself and will let you know if I achieve success.
:=:=
:=:=Now let's see, 3 days of my life at $10/day - CCS owes me $30!!!
:=
:=Well mr PCM Porgrammer - not three but all 4 functions are wrong! The read_adc() was offset as well and kept trying to set bit 2 instead of bit 1 of the ADCON0 in the 12F675. I finally saw the mistake and rewrote assembly code for the read_adc function. wow - 3 DAYS OF MY LIFE - Thanks so much for your help. I think I feel some of my hair growing back <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0">
:=
:=
:=John
___________________________
This message was ported from CCS's old forum
Original Post ID: 6850 |
|
|
maria Guest
|
CA/D PIC12F675 problem |
Posted: Wed Nov 03, 2004 11:23 am |
|
|
Hi
I am new to the PIC12F675 but not to pics.
I have been fighting with getting the ADC working.....but it doesn't work.
I have used the ADC before for the PIC16F871 and I didn't have any problems.
I wrote a "dummy"program in order to find out why my program stucks at the point where a I use the ADC and it doesn't work neither. Here it is.
I am using the PCM version 3.099.
// Pre-processor directives.
#include <12F675.H>
#device adc=8 // The number of bits that read_adc() is going to return.
#use fast_io(A)
#use delay(clock=4000000) // 4 Mhz PIC.
#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,MCLR,NOCPD
#id CHECKSUM
// Output pins.
#define BAT_FAULT_COMMS PIN_A1
// voltage parameters.
byte bat_level;
// Function prototypes.
void read_battery_level(void);
//----------------------------------------------------------------------------------------------------------------
void read_battery_level(void)
{
set_adc_channel(0);
delay_ms(100);
bat_level=read_adc();
if(bat_level<=0xAF) /*Vbatt >=3.42V--->9.45V at battery*/
{
//battery ok
Output_high(BAT_FAULT_COMMS);
delay_ms(1000);
Output_low(BAT_FAULT_COMMS);
delay_ms(1000);
}
else /*otherwise, don't do anything*/
{
Output_low(BAT_FAULT_COMMS);
delay_ms(1000);
}
}
//------------------------------------------------------------------------------------------------------------------
main()
{
disable_interrupts(GLOBAL);
// Set RA0 pin as analogue and Vref=Vdd.
setup_adc_ports(RA0_ANALOG);
// Set the ADC frequency.
setup_adc(ADC_CLOCK_DIV_8);
// Set RA0 and RA3 as inputs. RA3 is the MCLR
set_tris_a(0x09);
setup_comparator(NC_NC_NC_NC);
// Clear outputs. Some of them are low-active.
Output_bit(PIN_A1, 0);
do
{
read_battery_level();
}while(TRUE);
}
I have been trying different things but nothing seems to work.
Can somebody give me any clue??
Thanks
Maria |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 03, 2004 11:42 am |
|
|
The thread you have posted onto the tail of, explains why you cannot get it working. The functions hndling the ADC, on the 12F675, are faulty on the older compilers. This has been fixed for several months now, but 3.099, is definately back in the faulty versions.
You can simply 'hand code', instad of using the inbuilt functions, to make it work with your compiler version, or you need to update to a newer release.
Best Wishes |
|
|
maria Guest
|
CAD PIC12F675 |
Posted: Fri Nov 05, 2004 3:38 am |
|
|
Hi
Can somebody send me code for doing the conversion?
I have trying to do it in assembler(copying and pasting from the Embedded Control handbook from Microchip)and the compiler gives me an error that I am not able to get rid of.
I have tried to do it in 'C' and it does something but it looks like that it nevers stop of converting.
Thanks |
|
|
maria Guest
|
CAD PIC12F675 |
Posted: Fri Nov 05, 2004 3:54 am |
|
|
Hi
What I am doing is defining the CAD registers using the directive
#byte ADCON0=0x1F
# bit GO_DONE=0x1F.1
and so on...(write 0x11 on ANSEL to get the CAD speed and define the use of RA0 as analogue).
and following the idea of the handbook start a conversion writing '1' at the bit GO_DONE.
Using the debugger I have just realised that if I try to write a value on other bit of ADCON0 register...no problem...but, it doesn't let me do it on the GO_DONE bit.However, the datasheet says is a R/W bit.
Any ideas?
Thanks |
|
|
maria Guest
|
ADC PIC12F675 |
Posted: Mon Nov 08, 2004 4:10 am |
|
|
Hi Folks..!!
I have got it working....!!!
Thanks |
|
|
|
|
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
|