|
|
View previous topic :: View next topic |
Author |
Message |
Joan
Joined: 29 May 2004 Posts: 41 Location: Barcelona, Spain
|
ADC for 12F675 |
Posted: Mon May 31, 2004 12:23 pm |
|
|
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();
}
}
|
|
|
Felix Althaus
Joined: 09 Sep 2003 Posts: 67 Location: Winterthur, Switzerland
|
|
Posted: Mon May 31, 2004 1:01 pm |
|
|
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
|
|
Posted: Mon May 31, 2004 1:24 pm |
|
|
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 ...
Regards.
Joan |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Mon May 31, 2004 2:47 pm |
|
|
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:
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
|
|
Posted: Mon May 31, 2004 2:55 pm |
|
|
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
|
|
Posted: Tue Jun 01, 2004 10:48 am |
|
|
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();
}
} |
|
|
|
|
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
|