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 CCS Technical Support

VREF problem on PIC12F1501

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



Joined: 19 Feb 2011
Posts: 22

View user's profile Send private message

VREF problem on PIC12F1501
PostPosted: Sat Jun 02, 2012 4:16 pm     Reply with quote

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: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 02, 2012 4:33 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 02, 2012 4:40 pm     Reply with quote

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: 19504

View user's profile Send private message

PostPosted: Sun Jun 03, 2012 1:26 am     Reply with quote

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

View user's profile Send private message

VREF problem on PIC12F1501
PostPosted: Sun Jun 03, 2012 6:01 am     Reply with quote

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

View user's profile Send private message

VREF problem on PIC12F1501
PostPosted: Sun Jun 03, 2012 6:40 am     Reply with quote

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: 19504

View user's profile Send private message

PostPosted: Sun Jun 03, 2012 7:27 am     Reply with quote

OK. That would be the next step. Smile
There is a (unfortunate) tendency for CCS to be buggy for a while with new chips....

Best Wishes
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