//***************************************************
//功能介绍:对P6.0口电压进行采集 液晶显示
//****************************************************
#include msp430x14x.h>
#include "BoardConfig.h"
#include "cry1602.h"
#define Num_of_Results 32
uchar shuzi[] = {"0123456789.+"};
uchar tishi[] = {"The Volt is:"};
static uint results[Num_of_Results]; //保存ADC转换结果的数组 // is not used for anything.
void Trans_val(uint Hex_Val);
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; //关闭看门狗
BoardConfig(0xb8); //关闭数码管、流水灯、4245
LcdReset(); //复位1602液晶
DispNChar(2,0,12,tishi); //显示提示信息
Disp1Char(11,1,V); //显示电压单位
P6SEL |= 0x01; // 使能ADC通道
ADC12CTL0 = ADC12ON+SHT0_8+MSC; // 打开ADC,设置采样时间
ADC12CTL1 = SHP+CONSEQ_2; // 使用采样定时器,单路重复采集
ADC12IE = 0x01; // 使能ADC中断
ADC12CTL0 |= ENC; // 使能转换
ADC12CTL0 |= ADC12SC; // 开始转换
_EINT(); //使能中断
LPM0; //低功耗模式
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
static uint index = 0;
results[index++] = ADC12MEM0; // Move results
if(index == Num_of_Results)
{
uchar i;
unsigned long sum = 0;
index = 0;
for(i = 0; i < Num_of_Results; i++)
{
sum += results[i];
}
sum >>= 5; //除以32
sum=(sum<4)+(sum<2)+sum; //增大21倍原因是大电压变为小电压采集的
Trans_val(sum);
}
}
void Trans_val(uint Hex_Val)
{
unsigned long caltmp;
uint Curr_Volt;
uchar t1,i;
uchar ptr[6];
caltmp = Hex_Val;
caltmp = (caltmp < 5) + Hex_Val; //caltmp = Hex_Val * 33 扩大10倍便于计算
caltmp = (caltmp < 3) + (caltmp < 1); //caltmp = caltmp * 10 扩大10倍便于计算
Curr_Volt = caltmp >> 12; //Curr_Volt = caltmp / 2^n
ptr[0] = 11; //数字表中第11位对应符号"+"
ptr[1] = Curr_Volt / 1000; //相当于十位上的数值
ptr[2] = (Curr_Volt%1000) / 100; //相当于个位上的数值
t1 = Curr_Volt - (ptr[1] * 1000)-(ptr[2] * 100);
ptr[4] = t1 / 10; //相当于小数点后第一位
ptr[5] = t1 - (ptr[4] * 10); //相当于小数点后第二位
ptr[3] = 10; //数字表中第10位对应符号"."
//在液晶上显示变换后的结果
for(i = 0;i < 6;i++)
Disp1Char((4 + i),1,shuzi[ptr[i]]);
}