Hi,
I have the chip in my board all works fine exept the VeREF+ issue. I have 3V3 on my board to supply the most peripheral and the MCU and 5V0 to supply one sensor. I use VeREF+(P1.4) made from the 5V0 devided by two (simple resistor devider 10K/10K). And signal from the sensor devided by two too (another simple resistor devider 10k/10k). All goes fine several minutes, but after i measure VeREF+ from the devider and got values from 1V to 0.5V or even 0.3V, as the 5V0 is stable.
Below is full code with ADC init, measurement and CPU frequency init, I feel that i do something wrong during initialisation, please help:
#include "msp430g2553.h"
//////////////////////////////////////////////////////////////////////
// Definitions
//////////////////////////////////////////////////////////////////////
//
// Constants
//
static const int Nmax = 800; // RH = 100%
static const int Nmin = 164; // RH = 0%
// Define P1 pins
// P1.4 Vcc of RH sensor from devider (use as Vref for ADC)
// P1.3 V from RH sensor from devider
#define rhVccPin (BIT4)
#define rhSencePin (BIT3)
// functions
void clockInit(void); // system clock(s) init
void gpioInit(void); // initialize ports
void ADC10_Init(void);
unsigned int RHMeasurement(void); // max - 1023 (Vcc),
// min - 0 (Vss)
void sleep_msec(unsigned int);//delay
//////////////////////////////////////////////////////////////////////
// Main
//////////////////////////////////////////////////////////////////////
unsigned int rhN;
unsigned int RH;
float calc_;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
// Inits
gpioInit();
clockInit();
ADC10_Init();
// Main forever loop
for(;;){
// 1. measure
rhN = RHMeasurement(); // max - 1023 (Vcc), min - 0 (Vss)
// 2. calculate
if (rhN>=Nmax){
// 100%
RH = 100;
}else if(rhN<=Nmin){
// 0%
RH = 0;
}else{
// 0%..100%
calc_ = (rhN/6.35)-25;
RH = (rhN/6.35)-25;
}
// 3. sleep
sleep_msec(20);
}
return 0;
}
//////////////////////////////////////////////////////////////////////
// Functions
//////////////////////////////////////////////////////////////////////
void sleep_msec(unsigned int t){
TACCR1 = t*2000; // ms when 2MHz sub frequency
TACCTL1 = CCIE; // Compare mode, interrupt
TACTL = TASSEL_2 + TACLR + ID_0 + MC_2; // SMCLK, TA clear, continuous mode
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, wait for CCR1 int
}
void gpioInit(void){
// Initialize I/O pins. First clear all and make them output
// P1.x
P1OUT = 0x00;
P1DIR = 0xFF;
P1DIR &= ~rhSencePin ;
P1DIR &= ~rhVccPin;
// P2.x
P2OUT = 0x00;
P2DIR = 0xFF ;
}
void clockInit(void){
BCSCTL2 = SELM_0 | DIVM_0 | DIVS_3;
if (CALBC1_16MHZ != 0xFF) {
/* Adjust this accordingly to your VCC rise time */
__delay_cycles(100000);
/* Follow recommended flow. First, clear all DCOx and MODx bits. Then
* apply new RSELx values. Finally, apply new DCOx and MODx bit values.
*/
DCOCTL = 0x00;
BCSCTL1 = CALBC1_16MHZ; /* Set DCO to 16MHz */
DCOCTL = CALDCO_16MHZ;
}
/*
* Basic Clock System Control 1
*
* XT2OFF -- Disable XT2CLK
* ~XTS -- Low Frequency
* DIVA_0 -- Divide by 1
*
* Note: ~XTS indicates that XTS has value zero
*/
BCSCTL1 |= XT2OFF | DIVA_0;
/*
* Basic Clock System Control 3
*
* XT2S_0 -- 0.4 - 1 MHz
* LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator
* XCAP_1 -- ~6 pF
*/
BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;
}
// Measure count value for light: max - 1023 (Vcc), min - 0 (Vss)
unsigned int RHMeasurement(void)
{
unsigned int result;
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC10_ISR will force exit
result = ADC10MEM;
return(result);
}
void ADC10_Init(void)
{
ADC10CTL0 &= ~ENC;
/*
* Control Register 0
*
* ~ADC10SC -- No conversion
* ~ENC -- Disable ADC
* ~ADC10IFG -- Clear ADC interrupt flag
* ADC10IE -- Enable ADC interrupt
* ADC10ON -- Switch On ADC10
* ~REFON -- Disable ADC reference generator
* ~REF2_5V -- Set reference voltage generator = 1.5V
* ~MSC -- Disable multiple sample and conversion
* ~REFBURST -- Reference buffer on continuously
* ~REFOUT -- Reference output off
* ADC10SR -- Reference buffer supports up to ~50 ksps
* ADC10SHT_0 -- 4 x ADC10CLKs
* SREF_3 -- VR+ = Buffered VeREF+ and VR- = VSS
*
* Note: ~<BIT> indicates that <BIT> has value zero
*/
ADC10CTL0 = ADC10IE | ADC10ON | ADC10SHT_0 | SREF_3;
/*
* Control Register 1
*
* ~ADC10BUSY -- No operation is active
* CONSEQ_0 -- Single channel single conversion
* ADC10SSEL_3 -- SMCLK
* ADC10DIV_7 -- Divide by 8
* ~ISSH -- Input signal not inverted
* ~ADC10DF -- ADC10 Data Format as binary
* SHS_0 -- ADC10SC
* INCH_3 -- ADC Channel 3
*
* Note: ~<BIT> indicates that <BIT> has value zero
*/
ADC10CTL1 = CONSEQ_0 | ADC10SSEL_3 | ADC10DIV_7 | SHS_0 | INCH_3;
/* Analog (Input) Enable Control Register 0 */
ADC10AE0 = 0x18;
/* enable ADC10 */
ADC10CTL0 |= ENC;
}
// Timer A1 interrupt service routine
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR_HOOK(void)
{
__bic_SR_register_on_exit(LPM0_bits + GIE);
CCTL1 &= ~CCIFG;
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR_HOOK(void)
{
__bic_SR_register_on_exit(LPM0_bits + GIE); // Clear CPUOFF bit from 0(SR)
}