Esempio n. 1
0
/* Initialize stuff:
*  - Start Vref module
*  - Clear all fail flags
*  - Internal reference (default: external vcc)
*  - Mux between a and b channels (b channels)
*  - Calibrate with 32 averages and low speed
*  - When first calibration is done it sets:
*     - Resolution (default: 10 bits)
*     - Conversion speed and sampling time (both set to medium speed)
*     - Averaging (set to 4)
*/
void ADC_Module::analog_init() {

    // default settings:
    /*
        - 10 bits resolution
        - 4 averages
        - vcc reference
        - no interrupts
        - pga gain=1
        - conversion speed = medium
        - sampling speed = medium
    initiate to 0 (or 1) so the corresponding functions change it to the correct value
    */
    analog_res_bits = 0;
    analog_max_val = 0;
    analog_num_average = 0;
    analog_reference_internal = 1;
    var_enableInterrupts = 0;
    pga_value = 1;

    conversion_speed = 0;
    sampling_speed =  0;

    // the first calibration will use 32 averages and lowest speed,
    // when this calibration is over the averages and speed will be set to default by wait_for_cal and init_calib will be cleared.
    init_calib = 1;

    fail_flag = ADC_ERROR_CLEAR; // clear all errors

    // Internal reference initialization
    VREF_TRM = VREF_TRM_CHOPEN | 0x20; // enable module and set the trimmer to medium (max=0x3F=63)
	VREF_SC = VREF_SC_VREFEN | VREF_SC_REGEN | VREF_SC_ICOMPEN | VREF_SC_MODE_LV(1); // (=0xE1) enable 1.2 volt ref with all compensations

	// select b channels
	*ADC_CFG2_muxsel = 1;

    // set reference to vcc/external
	setReference(ADC_REF_EXTERNAL);

    // set resolution to 10
    setResolution(10);

    // calibrate at low speed and max repetitions
    setAveraging(32);
    setConversionSpeed(ADC_LOW_SPEED);
    setSamplingSpeed(ADC_LOW_SPEED);
    // begin init calibration
	calibrate();
}
Esempio n. 2
0
/* Initialize stuff:
*  - Start Vref module
*  - Clear all fail flags
*  - Internal reference (default: external vcc)
*  - Mux between a and b channels (b channels)
*  - Calibrate with 32 averages and low speed
*  - When first calibration is done it sets:
*     - Resolution (default: 10 bits)
*     - Conversion speed and sampling time (both set to medium speed)
*     - Averaging (set to 4)
*/
void ADC_Module::analog_init() {

    // default settings:
    /*
        - 10 bits resolution
        - 4 averages
        - vcc reference
        - no interrupts
        - pga gain=1
        - conversion speed = medium
        - sampling speed = medium
    initiate to 0 (or 1) so the corresponding functions change it to the correct value
    */
    analog_res_bits = 0;
    analog_max_val = 0;
    analog_num_average = 0;
    analog_reference_internal = 2;
    var_enableInterrupts = 0;
    pga_value = 1;

    conversion_speed = 0;
    sampling_speed =  0;

    calibrating = 0;

    fail_flag = ADC_ERROR_CLEAR; // clear all errors

    num_measurements = 0;

    // select b channels
    // *ADC_CFG2_muxsel = 1;
    setBit(ADC_CFG2, ADC_CFG2_MUXSEL_BIT);

    // set reference to vcc
    setReference(ADC_REF_3V3);

    // set resolution to 10
    setResolution(10);

    // the first calibration will use 32 averages and lowest speed,
    // when this calibration is over the averages and speed will be set to default by wait_for_cal and init_calib will be cleared.
    init_calib = 1;
    setAveraging(32);
    setConversionSpeed(ADC_LOW_SPEED);
    setSamplingSpeed(ADC_LOW_SPEED);

    // begin init calibration
    calibrate();
}
Esempio n. 3
0
/* Waits until calibration is finished and writes the corresponding registers
*
*/
void ADC_Module::wait_for_cal(void)
{
	uint16_t sum;

	while(*ADC_SC3_cal) { // Bit ADC_SC3_CAL in register ADC0_SC3 cleared when calib. finishes.

	}
	if(*ADC_SC3_calf) { // calibration failed
        fail_flag |= ADC_ERROR_CALIB; // the user should know and recalibrate manually
	}

	__disable_irq();
	if (calibrating) {
		sum = *ADC_CLPS + *ADC_CLP4 + *ADC_CLP3 + *ADC_CLP2 + *ADC_CLP1 + *ADC_CLP0;
		sum = (sum / 2) | 0x8000;
		*ADC_PG = sum;

		sum = *ADC_CLMS + *ADC_CLM4 + *ADC_CLM3 + *ADC_CLM2 + *ADC_CLM1 + *ADC_CLM0;
		sum = (sum / 2) | 0x8000;
		*ADC_MG = sum;

		calibrating = 0;
	}
	__enable_irq();

	// the first calibration uses 32 averages and lowest speed,
    // when this calibration is over, set the averages and speed to default.
	if(init_calib) {

	    // set conversion speed to medium
        setConversionSpeed(ADC_MED_SPEED);

        // set sampling speed to medium
        setSamplingSpeed(ADC_MED_SPEED);

        // number of averages to 4
        setAveraging(4);

        init_calib = 0; // clear
	}

}