Software for the initial measurement
To make things easy the measurements are based on 1 bit equals 100 mA, this is equal to measure 1 mV over the 10 mOhm measurent resistor. The AD converter is run in a one shot mode, this might be improved with a interrupt version which probably will decrease the average current consumption slightly.
After the conversion is finished the result is scaled to acomplish the 1bit/100mA approach. The scaled sample is added to a sum for later calculation of the amp hours, there is also a value saved to show the max current.
void ad_3(void){
int status = 0;
int i; CCTL0 = OUT; // TXD Idle as Mark
TACTL = TASSEL_1 + MC_2; // ACLK, Continuous mode
BCSCTL2 |= DIVS_3; // SMCLK/8
SD16CTL = SD16REFON + SD16SSEL_1; // Use 1.2V ref,
SMCLK SD16CCTL0 = SD16DF; // twos complement
SD16INCTL0 = SD16INCH_2; // SD16 A2+/- pin 6 & 7
SD16AE = SD16AE4 + SD16AE5; // A2+ and A2-
for(i=0; i<3600; i++);
SD16CCTL0 |= SD16SNGL; // Single conversion
SD16CCTL0 |= SD16SC; // Start conversion
while((status & SD16IFG) == 0)
status = SD16CCTL0; // Wait until ready
sampleData = SD16MEM0; // Read the sample
if(sampleData < 0)
sampleData = -sampleData;
SD16CTL = 0; // set reference off
SD16AE = 0; // disable inputs
scale();
sum_sample();
if(sample_scaled > sample_max)
sample_max = sample_scaled;
}
void scale(void){
unsigned int sample;
sample = sampleData;
sample = sample * scale_mult; // be careful to multiply first not to lose small values
sample = sample / scale_div;
sample_scaled = sample;
}
void sum_sample(void){
sum += sample_scaled;
}
After the conversion is finished the result is scaled to acomplish the 1bit/100mA approach. The scaled sample is added to a sum for later calculation of the amp hours, there is also a value saved to show the max current.
void ad_3(void){
int status = 0;
int i; CCTL0 = OUT; // TXD Idle as Mark
TACTL = TASSEL_1 + MC_2; // ACLK, Continuous mode
BCSCTL2 |= DIVS_3; // SMCLK/8
SD16CTL = SD16REFON + SD16SSEL_1; // Use 1.2V ref,
SMCLK SD16CCTL0 = SD16DF; // twos complement
SD16INCTL0 = SD16INCH_2; // SD16 A2+/- pin 6 & 7
SD16AE = SD16AE4 + SD16AE5; // A2+ and A2-
for(i=0; i<3600; i++);
SD16CCTL0 |= SD16SNGL; // Single conversion
SD16CCTL0 |= SD16SC; // Start conversion
while((status & SD16IFG) == 0)
status = SD16CCTL0; // Wait until ready
sampleData = SD16MEM0; // Read the sample
if(sampleData < 0)
sampleData = -sampleData;
SD16CTL = 0; // set reference off
SD16AE = 0; // disable inputs
scale();
sum_sample();
if(sample_scaled > sample_max)
sample_max = sample_scaled;
}
void scale(void){
unsigned int sample;
sample = sampleData;
sample = sample * scale_mult; // be careful to multiply first not to lose small values
sample = sample / scale_div;
sample_scaled = sample;
}
void sum_sample(void){
sum += sample_scaled;
}
Kommentarer
Trackback