//***************************************************
本文引用地址:http://m.butianyuan.cn/article/201611/321019.htm//功能介紹:對(duì)P6.0口電壓進(jìn)行采集 液晶顯示
//****************************************************
#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轉(zhuǎn)換結(jié)果的數(shù)組 // is not used for anything.
void Trans_val(uint Hex_Val);
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; //關(guān)閉看門狗
BoardConfig(0xb8); //關(guān)閉數(shù)碼管、流水燈、4245
LcdReset(); //復(fù)位1602液晶
DispNChar(2,0,12,tishi); //顯示提示信息
Disp1Char(11,1,V); //顯示電壓?jiǎn)挝?br /> P6SEL |= 0x01; // 使能ADC通道
ADC12CTL0 = ADC12ON+SHT0_8+MSC; // 打開ADC,設(shè)置采樣時(shí)間
ADC12CTL1 = SHP+CONSEQ_2; // 使用采樣定時(shí)器,單路重復(fù)采集
ADC12IE = 0x01; // 使能ADC中斷
ADC12CTL0 |= ENC; // 使能轉(zhuǎn)換
ADC12CTL0 |= ADC12SC; // 開始轉(zhuǎn)換
_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倍原因是大電壓變?yōu)樾‰妷翰杉?br /> 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 擴(kuò)大10倍便于計(jì)算
caltmp = (caltmp << 3) + (caltmp << 1); //caltmp = caltmp * 10 擴(kuò)大10倍便于計(jì)算
Curr_Volt = caltmp >> 12; //Curr_Volt = caltmp / 2^n
ptr[0] = 11; //數(shù)字表中第11位對(duì)應(yīng)符號(hào)"+"
ptr[1] = Curr_Volt / 1000; //相當(dāng)于十位上的數(shù)值
ptr[2] = (Curr_Volt%1000) / 100; //相當(dāng)于個(gè)位上的數(shù)值
t1 = Curr_Volt - (ptr[1] * 1000)-(ptr[2] * 100);
ptr[4] = t1 / 10; //相當(dāng)于小數(shù)點(diǎn)后第一位
ptr[5] = t1 - (ptr[4] * 10); //相當(dāng)于小數(shù)點(diǎn)后第二位
ptr[3] = 10; //數(shù)字表中第10位對(duì)應(yīng)符號(hào)"."
//在液晶上顯示變換后的結(jié)果
for(i = 0;i < 6;i++)
Disp1Char((4 + i),1,shuzi[ptr[i]]);
}
評(píng)論