View previous topic :: View next topic |
Author |
Message |
MMurray
Joined: 19 Feb 2011 Posts: 22
|
VREF problem on PIC12F1501 |
Posted: Sat Jun 02, 2012 4:16 pm |
|
|
I use PIC12F1501 and CCS compiler version 4.132.
I want to measure ADC modules using Vref (I use 3.307V), but whatever I do ADC module uses Vdd(5V). Here is some code I use.
Thank you in advance if anyone can help. Code: | setup_adc_ports(sAN3|VSS_VREF);
setup_timer_0(T0_div_8);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
setup_adc(ADC_CLOCK_INTERNAL); |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sat Jun 02, 2012 4:33 pm |
|
|
You should cut a small program with just the ADC setup stuff. Then dump the listing and compare the code the program creates to the register settings that the datasheet for your PIC.
There could be a compiler 'bug' of not setting the correct bits or maybe some code in your real program is trashing the setup. |
|
|
MMurray
Joined: 19 Feb 2011 Posts: 22
|
|
Posted: Sat Jun 02, 2012 4:40 pm |
|
|
OK, Can you see anything?? Code: | #include "12F1501.h"
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,LVP,NOPUT,WDT
#device ADC=10
#use delay(clock=500000,restart_wdt)
#define MAX_TIME 485 //In seconds
#define MIN_TIME 1 //In seconds
#define RELAY pin_a5
#define TEST_PIN pin_a0
#define POT_SET pin_a2
#define sec 15625
#define UL 460
#define LL 450
#define FILTER_LEN (6)
unsigned int16 GetTick(void);
void HandleTimer0(void);
unsigned int16 get_adc(void);
unsigned int i=0;
unsigned int16 tick=0;
unsigned int old_tmr0=0;
unsigned int16 secondstamp=0;
unsigned int16 countseconds=0;
unsigned int32 adc_value=0;
unsigned int16 delay_time=MIN_TIME;
unsigned int filter_init=0;
unsigned int32 filter=0;
#INT_TIMER0
void isr_T0(){}
void main(void)
{
setup_adc_ports(sAN3,VSS_VREF);
//adcon1=0xb2;
setup_timer_0(T0_div_8);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
//setup_vref(vref_on);
//setup_adc_ports(VSS_VREF);
setup_adc(ADC_CLOCK_INTERNAL);
setup_wdt(wdt_16MS);
if(!input(TEST_PIN))
{
delay_ms(1000);
output_high(RELAY);
delay_ms(1000);
output_low(RELAY);
while(TRUE);
}
output_high(RELAY);
secondstamp=GetTick();
while(TRUE)
{
HandleTimer0();
if((unsigned int16)(GetTick()-secondstamp)>=SEC)
{
secondstamp+=SEC;
countseconds++;
}
delay_ms(5);
adc_value=get_adc();
restart_wdt();
delay_time=(((MAX_TIME-MIN_TIME)*adc_value)>>10)+MIN_TIME;
if(delay_time<MIN_TIME)
delay_time=MIN_TIME;
if(delay_time>MAX_TIME)
delay_time=MAX_TIME;
if(countseconds>=delay_time)
output_low(RELAY);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Sun Jun 03, 2012 1:26 am |
|
|
First, you read the ADC, using a function 'get_adc', but there is no declaration for this, except the prototype. Use the CCS read_adc function, or post the definition for 'get_adc'.
Second, a comment. You do understand that the 'Vref' for the ADC has to be fed into pin AN1. Your 3.307v, is connected to this?.
You don't show a set_adc_channel call?.
Are you sure you are using LVP?. 99.9% of systems don't, and this could be causing problems.
Best Wishes |
|
|
MMurray
Joined: 19 Feb 2011 Posts: 22
|
VREF problem on PIC12F1501 |
Posted: Sun Jun 03, 2012 6:01 am |
|
|
Thanks, I turned off the LVP the problem remains. Also re-checked, I am supplying the 3.307V to RA1 (pin 6). The prototype points to this section of the code... Code: | unsigned int16 get_adc(void)
{
unsigned int16 a2d_val=0;
set_adc_channel(3);
delay_us(25);
a2d_val=Read_adc();
return(a2d_val);
} |
|
|
|
MMurray
Joined: 19 Feb 2011 Posts: 22
|
VREF problem on PIC12F1501 |
Posted: Sun Jun 03, 2012 6:40 am |
|
|
Seems to be a compiler issue. If I set the register directly the vref is ok.
Thanks everyone for your help.
Matt |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Sun Jun 03, 2012 7:27 am |
|
|
OK. That would be the next step.
There is a (unfortunate) tendency for CCS to be buggy for a while with new chips....
Best Wishes |
|
|
|