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

Setting up 16F627/628 voltage reference

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







Setting up 16F627/628 voltage reference
PostPosted: Tue Feb 25, 2003 5:00 am     Reply with quote

I have tried for too long to get the internal voltage reference enabled. I use the setup_vref(VREF_LOW|15|VREF_A2) but no voltage appears at pin A2. Looking at assembler code VRCON gets value 0xEF, which is correct. I also made sure the A2 is in input mode according to 16F62x manuals.

What is the trick ?

I tried looking for the ex_comp.c example file but it is for some reason (murphy's law?) not part of my example files.

Grateful for some hints.

Michel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12077
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Setting up 16F627/628 voltage reference
PostPosted: Tue Feb 25, 2003 7:36 am     Reply with quote

Vref is an input voltage. The micro will not output a voltage here. You must supply a voltage here. This voltage is what the A/D conversions are based upon. Putting 4V here will cause the A/D module to give a value of 0xFF when 4V is applied to another analog input. When 2V is applied to an anlog input, it would give you a reading of 0x7F.

:=I have tried for too long to get the internal voltage reference enabled. I use the setup_vref(VREF_LOW|15|VREF_A2) but no voltage appears at pin A2. Looking at assembler code VRCON gets value 0xEF, which is correct. I also made sure the A2 is in input mode according to 16F62x manuals.
:=
:=What is the trick ?
:=
:=I tried looking for the ex_comp.c example file but it is for some reason (murphy's law?) not part of my example files.
:=
:=Grateful for some hints.
:=
:=Michel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12080
mnoxfeld
Guest







Re: Setting up 16F627/628 voltage reference
PostPosted: Tue Feb 25, 2003 8:34 am     Reply with quote

Well, in the user manual for 16F62x, it is clearly written that the Vref can internally be output at pin A2, thus also providing the reference voltage to external components, or creating a very simple D/A converter.
So I'm afraid, you're wrong here.

However, I have continued to trace the error(s).

The statement setup_vref(VREF_LOW|15|VREF_A2) should work but it doesn't. Whether that is a compiler bug or a silicon bug I can't tell.

Thanks for your input.

Michel



:=Vref is an input voltage. The micro will not output a voltage here. You must supply a voltage here. This voltage is what the A/D conversions are based upon. Putting 4V here will cause the A/D module to give a value of 0xFF when 4V is applied to another analog input. When 2V is applied to an anlog input, it would give you a reading of 0x7F.
:=
:=:=I have tried for too long to get the internal voltage reference enabled. I use the setup_vref(VREF_LOW|15|VREF_A2) but no voltage appears at pin A2. Looking at assembler code VRCON gets value 0xEF, which is correct. I also made sure the A2 is in input mode according to 16F62x manuals.
:=:=
:=:=What is the trick ?
:=:=
:=:=I tried looking for the ex_comp.c example file but it is for some reason (murphy's law?) not part of my example files.
:=:=
:=:=Grateful for some hints.
:=:=
:=:=Michel
___________________________
This message was ported from CCS's old forum
Original Post ID: 12082
Bruce R. Knox
Guest







Re: Setting up 16F627/628 voltage reference
PostPosted: Tue Feb 25, 2003 10:13 am     Reply with quote

:=I have tried for too long to get the internal voltage reference enabled. I use the setup_vref(VREF_LOW|15|VREF_A2) but no voltage appears at pin A2. Looking at assembler code VRCON gets value 0xEF, which is correct. I also made sure the A2 is in input mode according to 16F62x manuals.
:=
:=What is the trick ?
:=
:=I tried looking for the ex_comp.c example file but it is for some reason (murphy's law?) not part of my example files.
:=
:=Grateful for some hints.
:=
:=Michel

Michel:

Two things that may help:

1. Have you actually looked at the VRCON and TRISA registers after you have run your program? Maybe some other code is wiping out the 0xef in VRCON or the 1 in TRISA.2 ....

2. The docs say you need to use "high impedance" loads on VREF when connected to RA2 - I assume you're using a scope or other hig-impedance meter to measure the pin....

Happy hunting....

Bruce
___________________________
This message was ported from CCS's old forum
Original Post ID: 12085
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Setting up 16F627/628 voltage reference
PostPosted: Tue Feb 25, 2003 11:00 am     Reply with quote

Quote:

The statement setup_vref(VREF_LOW|15|VREF_A2) should work but it
doesn't. Whether that is a compiler bug or a silicon bug I can't tell.

I did this in a recent project. Depending on what version
of the compiler you have, there may be bugs in setup_vref().
Also, it's possible that the bug may have been re-introduced
in a later version of the compiler.

Here's a work-around routine:

Code:
void power_up_init(void)
{

// *** I snipped everything but the comparator and vref
// *** setup in this routine, for this post.

// Comparator pins:  The two comparator inputs (A1 and A2) are
// set to floating so they won't interfere with comparator or Vref
// operation.

// Configure Pin A1 as an Input -- It's the comparator Vin- input.
output_float(BAT_LEVEL_PIN_A1);

// Configure Pin A2 as an Input  --  It's the comparator Vin+ input,
// and Vref output
output_float(VREF_PIN_A2);

// Leave comparator #2 turned on all the time.
setup_comparator(NC_NC_A1_A2);

// Turn on the Vref generator.   Vref goes to the Vin+
// comparator input on pin A2.
configure_vref(VREF_LOW | VREF_A2 | LOBAT_VREF_LOW_THRESHOLD);
}

//--------------------------------------------------------------
// This function is used instead of the CCS function,
// setup_vref(), because setup_vref() has two bugs, in
// PCM vs. 3.127.  It writes to SPBRG instead of VRCON
// and it turns PIN_A2 into an output, instead of an
// input.  The function below has neither of those bugs.
//
// Note:  The bug described above was fixed in vs. 3.130
// but I'm going to keep using the function below, because
// it works, and who knows if CCS might re-introduce the
// bug at some future date.
//
// Constants used in configure_vref() are the
// same as for setup_vref().  Check the
// 16f628.h file for the values.

void configure_vref(char value)
{
// If the bit for VREF on pin A2 is set, then
// turn A2 into an input.
if(value & VREF_A2)
  {
   output_float(PIN_A2);
  }

VRCON_REG = value;  // Write the value to the VRCON register

delay_us(20);  // Wait for 2x the settling time specified in data sheet.
}

//--------------------------------------------------------------
// Returns True if the battery voltage is low, and False if not.
//
// The comparator was already turned on in the power_up_init() function.

char is_battery_low(void)
{
char comp2_output;

// Turn on the FET which puts applies the battery voltage
// to the top of a voltage divider.  The output of the voltage
// divider then goes to pin A1 which is a comparator input.
output_low(BATTERY_TEST_FET_ON_PIN_B5);

// Wait for a settling time after turning on the FET.
delay_us(20);

// Use "low res" mode for the Vref generator.  This means
// that Vref = Vdd * n/24  where n is from 0 to 15.  Put the
// Vref voltage out on pin A2.  If the "LOBAT" indicator is off,
// then check if the battery voltage is less than 6.5 volts.
// If so, return TRUE.   If the LOBAT indicator is already on,
// then check if the battery voltage is less than 6.6 volts.
// If so, return TRUE.  This provides some hysteresis, so that
// we don't turn on "LOBAT" at 6.49 volts, and then turn it
// off a few seconds later if we read 6.51 volts.

if(gc_low_battery_flag == FALSE)
   configure_vref(VREF_LOW | VREF_A2 | LOBAT_VREF_LOW_THRESHOLD);
else
   configure_vref(VREF_LOW | VREF_A2 | LOBAT_VREF_HIGH_THRESHOLD);

// After Vref is stable, wait for 2x the spec'ed amount of setup
// time before reading the comparator output.  Note:  This is in
// addition to the Vref settling time.
delay_us(20);

// Read the comparator output.
comp2_output = COMP2_OUTPUT_BIT;

// Turn off the battery test FET, to conserve the battery.
output_high(BATTERY_TEST_FET_ON_PIN_B5);

// If the battery voltage is below 6.5 volts, C2OUT will be = 1.
// Return True in that case.
if(comp2_output)
   return(TRUE);
else
   return(FALSE);

}

// Here are some of the definitions necessary for the code above:

#byte VRCON_REG = 0x9F
#bit COMP2_OUTPUT_BIT = 0x1F.7  // C2OUT bit = CMCON reg, bit 7
#define LOBAT_VREF_LOW_THRESHOLD   10     
#define LOBAT_VREF_HIGH_THRESHOLD  11
#define BAT_LEVEL_PIN_A1            PIN_A1
#define BATTERY_TEST_FET_ON_PIN_B5  PIN_B5     


------------------------------
Edited on Feb. 10, 2004 to remove the double-spacing that was
somehow added when this post was ported to the new forum.
Also put it in a Code format block so it would look better.

___________________________
This message was ported from CCS's old forum
Original Post ID: 12086


Last edited by PCM programmer on Thu Feb 10, 2005 8:31 pm; edited 1 time in total
mnoxfeld
Guest







Re: Setting up 16F627/628 voltage reference
PostPosted: Wed Feb 26, 2003 10:14 am     Reply with quote

Hi.
A download of the latest version did not solve the problem since the setup_vref() function now will not accept input parameters VREF_A2, VREF_LOW and VREF_HIGH.

Maybe this is a way for CCS to say "do not try to use this function because it is malfunctioning all the time" :-)

Anyway I defined #BYTE VREF = 0x9F

then VREF=0xEF will work as I expect.
It is also possible to drive the vref on the comparator and also put it out at Pin A2 at the same time, in case anyone want to know.


Best Regards and thank you for sharing your experiences.
Michel

:=The statement setup_vref(VREF_LOW|15|VREF_A2) should work but it doesn't. Whether that is a compiler bug or a silicon bug I can't tell.
:=-------------------------------------------------------
:=
:=I did this in a recent project. Depending on what version
:=of the compiler you have, there may be bugs in setup_vref().
:=Also, it's possible that the bug may have been re-introduced
:=in a later version of the compiler.
:=
:=Here's a work-around routine:
___________________________
This message was ported from CCS's old forum
Original Post ID: 12122
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Setting up 16F627/628 voltage reference
PostPosted: Wed Feb 26, 2003 3:01 pm     Reply with quote

:=Hi.
:=A download of the latest version did not solve the problem since the setup_vref() function now will not accept input parameters VREF_A2, VREF_LOW and VREF_HIGH.

That's because they've accidently left the VREF definitions
out of the 16F628.H file.

#define VREF_LOW 0xa0
#define VREF_HIGH 0x80
#define VREF_A2 0x40

:=
:=Maybe this is a way for CCS to say "do not try to use this function becaus it is malfunctioning all the time" :-)

It makes you wonder...

:=
:=Anyway I defined #BYTE VREF = 0x9F
:=
:=then VREF=0xEF will work as I expect.
:=It is also possible to drive the vref on the comparator and also put it out at Pin A2 at the same tim, in case anyone want to know.
:=

Yes, my example program does that. Pin A2 must be
defined as an input, so it doesn't interfere with
the Vref voltage.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12134
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