Hi,
I use HVI0 to measure 0~30VDC. But I can not measure 22.2~30V.
The ADC result:
8bit ADC
5V ==> 43
10V ==> 86
15V ==> 129
20V ==> 172
22.1V ==> 190
25V ==> 190 (I got wrong value, It should be around 215)
30V ==> 190 (I got wrong value, It should be around 255)
10bit ADC
5V ==> 172
10V ==> 343
15V ==> 515
20V ==> 687
22.2V ==> 762
25V ==> 762 (I got wrong value, It should be around 859)
30V ==> 762 (I got wrong value, It should be around 1023)
What problem in my code?
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "ADC.h"
#define HVI0 0x0D //Internal_5 - High voltage input port L0
unsigned int ADC_Vin_VALUE=0;
void main(void) {
unsigned int result=0;
unsigned char digit[4];
DIENL_DIENL0= 0; // PL0: DISABLE Digital Input
PTAENL_PTAENL0=1; // PL0: ENABLE ADC
PTADIRL_PTADIRL0= 0; // PL0: DISABLE Direct-Mode
PIRL_PIRL0=0; // PL0: H_HVI (Ratio 6:1)
ADC_init();
for(;;){
ADC_Vin_VALUE = ADC_read(HVI0);
result = ADC_Vin_VALUE;
digit[0] = result % 10;
digit[1] = ((result - digit[0]) / 10) % 10;
digit[2] = ((result - digit[1]*10 - digit[0]) /100) % 10;
digit[3] = ((result - digit[2]*100 - digit[1]*10 - digit[0]) /1000) % 10;
}
}
/*
* ADC.c
*
* Created on: Dec 1, 2015
* Author: B55840
*/
#include "derivative.h"
#include "ADC.h"
#include "GPIO.h"
/* ADC list directions value*/
volatile unsigned char adc0_cmdlist[1][4] = {0xC0,0xD0,0xA0,0x00};
volatile unsigned int adc0_results[1];
void ADC_init(void){
ADC0CTL_0 = 0x0D; /*Dual Access mode and trigger mode selected */
ADC0CTL_1 = 0x00; /* Single command and result value list */
ADC0TIM = 0x0F; /*No prescale selected */
ADC0FMT = 0x80; /* Left justified and 8 bit resolution */
// ADC0FMT = 0x82; /* Left justified and 10 bit resolution */
// ADC0FMT = 0x84; /* Left justified and 12 bit resolution */
/* ADC0 Command & Result Base Pointers */
ADC0CBP_0 = (unsigned char)(((long)adc0_cmdlist) >> 16);
ADC0CBP_1 = (unsigned char)(((long)adc0_cmdlist) >> 8);
ADC0CBP_2 = (unsigned char)((long)adc0_cmdlist);
// ADC0 Result Base Pointer
ADC0RBP_0 = (unsigned char)(((long)adc0_results) >> 16);
ADC0RBP_1 = (unsigned char)(((long)adc0_results) >> 8);
ADC0RBP_2 = (unsigned char)((long)adc0_results);
ADC0CTL_0_ADC_EN= 1; /*Enables the ADC */
}
unsigned int ADC_read(unsigned int channel){
adc0_cmdlist[0][1] = 0xC0|channel;
ADC0FLWCTL_RSTA = 1; // Restart Event
while(0x00 == ADC0CONIF){} //wait until conversion is complete
ADC0CONIF = ADC0CONIF;//clear flag
return adc0_results[0];
}