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

ADC for 12F675

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



Joined: 29 May 2004
Posts: 41
Location: Barcelona, Spain

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

ADC for 12F675
PostPosted: Mon May 31, 2004 12:23 pm     Reply with quote

I had found this code on a previous message but doesn't works, why?

#device PIC12F675
#device ADC=10

//////// Fuses: LP,XT,INTRC,HS,NOWDT,WDT,CPD,NOCPD,PROTECT,NOPROTECT,NOMCLR
//////// Fuses: MCLR,PUT,NOPUT,RC,EC,RC_IO,INTRC_IO,BROWNOUT,NOBROWNOUT
#use delay(clock=4000000)

#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45

////////////////////////////////////////////////////////////////// Useful defines
#define FALSE 0
#define TRUE 1

////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used in SETUP_ADC_PORTS() are: (These may be ORed together)

#define NO_ANALOGS 0
#define ALL_ANALOG 15 //| GP0 GP1 GP2 GP4
#define AN0_ANALOG 1 //| GP0
#define AN1_ANALOG 2 //| GP1
#define AN2_ANALOG 4 //| GP2
#define AN3_ANALOG 8 //| GP4
//#define VREF 0x4002
#define VSS_VDD 0x0000
#define VSS_VREF 0x4002 //| Range 0-Vref
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 1
#define ADC_CLOCK_DIV_4 0x41
#define ADC_CLOCK_DIV_8 0x11
#define ADC_CLOCK_DIV_16 0x51
#define ADC_CLOCK_DIV_32 0x21
#define ADC_CLOCK_DIV_64 0x61
#define ADC_CLOCK_INTERNAL 0x31 // Internal 2-6us

// Constants used in READ_ADC() are:
#define ADC_START_AND_READ 7 // This is the default if nothing is specified
#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6

#define RED PIN_A4
#define GREEN PIN_A5

//Value to save ADC reading

long Value;

void ReadMyAdc()
{
Value=read_adc(adc_read_only);
//If more then 50%
if (Value > 512)//Flash Green
{
output_bit(GREEN,1);
output_bit(RED,0);
delay_ms (250);
output_bit(GREEN,0);
output_bit(RED,0);
delay_ms(250);
}
if (Value < 513)//Flash Red
{
output_bit(GREEN,0);
output_bit(RED,1);
delay_ms (250);
output_bit(GREEN,0);
output_bit(RED,0);
delay_ms(250);
}

}


void main()
{

set_tris_a(0xf);//RA0, RA1, RA2, RA3 as Inputs
//RA4, RA5 as Outputs
setup_adc_ports( AN0_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0);

while(1)
{
ReadMyAdc();
}
}

Rolling Eyes
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

PostPosted: Mon May 31, 2004 1:01 pm     Reply with quote

Hello

What do you mean with "it doesn't work"? Does it compile? What exactly doesn't work, what results do you get?

Felix
Joan



Joined: 29 May 2004
Posts: 41
Location: Barcelona, Spain

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

PostPosted: Mon May 31, 2004 1:24 pm     Reply with quote

I don't have any error when compiling.

It must change the blinking LED (GREEN or RED) when Vadc (AN0_Channel) is great or less 2.5V, respectively. But only blinks the GREEN everytime, like ADC where not runnig well. I don't have any idea ... Sad

Regards.

Joan
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Mon May 31, 2004 2:47 pm     Reply with quote

I haven't gone through your entire program, but I did notice that you are using the "read_only" parameter in your read_adc() function. You haven't started the adc anywhere, so the reading you get is the initialization value of the adc result register.

I would suggest leaving out the parameter in the read_adc() function altogether, and letting the compiler do the work. It will start the adc and wait for it to be done and then continue on with your code.

Change:

Code:

Value=read_adc(adc_read_only);


to:

Code:

Value=read_adc();


Once this is working, you can start to work with the adc_start_only and adc_read_only parameters. If you use these, you must start the adc with the start_adc_only parameter, and then either perform other instructions or wait for the adc to complete. Then you can read the result with read_adc(adc_read_only) function call.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Mon May 31, 2004 2:55 pm     Reply with quote

I would use the output_toggle() function too.

Optimize more like:

if(Value>512) {
do something;
} else do other thing;
Joan



Joined: 29 May 2004
Posts: 41
Location: Barcelona, Spain

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

PostPosted: Tue Jun 01, 2004 10:48 am     Reply with quote

Hello Guys (Charlie U & future & CO.):

I've rewited the code with the lines that you suggestet. Now works fine. Thx very much for your help. Here it is the code for who wants to test:

#device PIC12F675
#device ADC=10

//////////////////////////////////////////////////
//
// Connections:
//
// PIN 1 to VCC
// PIN 2 to 220 Ohms resistor + LED (GREEN)
// PIN 3 to 220 Ohms resistor + LED (RED)
// PIN 4 n.c.
// 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: LP,XT,INTRC,HS,NOWDT,WDT,CPD,NOCPD,PROTECT,NOPROTECT,NOMCLR
//////// Fuses: MCLR,PUT,NOPUT,RC,EC,RC_IO,INTRC_IO,BROWNOUT,NOBROWNOUT
#use delay(clock=4000000)

#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45

////////////////////////////////////////////////////////////////// Useful defines
#define FALSE 0
#define TRUE 1

////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used in SETUP_ADC_PORTS() are: (These may be ORed together)

#define NO_ANALOGS 0
#define ALL_ANALOG 15 //| GP0 GP1 GP2 GP4
#define AN0_ANALOG 1 //| GP0
#define AN1_ANALOG 2 //| GP1
#define AN2_ANALOG 4 //| GP2
#define AN3_ANALOG 8 //| GP4
//#define VREF 0x4002
#define VSS_VDD 0x0000
#define VSS_VREF 0x4002 //| Range 0-Vref
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 1
#define ADC_CLOCK_DIV_4 0x41
#define ADC_CLOCK_DIV_8 0x11
#define ADC_CLOCK_DIV_16 0x51
#define ADC_CLOCK_DIV_32 0x21
#define ADC_CLOCK_DIV_64 0x61
#define ADC_CLOCK_INTERNAL 0x31 // Internal 2-6us

// Constants used in READ_ADC() are:
#define ADC_START_AND_READ 7 // This is the default if nothing is specified
#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6

#define RED PIN_A4
#define GREEN PIN_A5

//Value to save ADC reading


void ReadMyAdc()
{
long Value;
Value=read_adc();
//Value=read_adc(adc_read_only);

//If more then 50%
if (Value > 512)//Flash Green
{
output_bit(GREEN,1);
output_bit(RED,0);
delay_ms (250);
output_bit(GREEN,0);
output_bit(RED,0);
delay_ms(250);
}
else //if (Value < 513) Flash Red
{
output_bit(GREEN,0);
output_bit(RED,1);
delay_ms (250);
output_bit(GREEN,0);
output_bit(RED,0);
delay_ms(250);
}

}


void main()
{

set_tris_a(0xf);//RA0, RA1, RA2, RA3 as Inputs
//RA4, RA5 as Outputs
setup_adc_ports( AN0_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0);

while(1)
{
ReadMyAdc();
}
}
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