程序如下:
#include <msp430x24x.h>
#define uchar unsigned char
#define uint unsigned int
uchar const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, //共阴数码管段选码表,无小数点
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar Index,flag,Disbuf[8];
void delayms(uint t)
{
uint i;
while(t--)
for(i=1000;i>0;i--);//进过参数的调整1330
}
void VoltDataProcess(uint Volt)
{
float temp;
uchar i;
unsigned long t;
temp=((float)(Volt))*5/4096;
t=(unsigned long)(temp*1000);
while(t)
{
Disbuf[7-i++]=table[t%10];
t=t/10;
}
for(;i<8;i++)
{
if(i<=3)
{
if(i==3)
{
Disbuf[7-i]=table[0];
continue;
}
Disbuf[7-i]=table[0];
continue;
}
Disbuf[7-i]=0x00;
}
Disbuf[4]|=0x80;
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
ADC12CTL0 &= ~(ENC);
ADC12CTL0 = SHT0_2 + ADC12ON; // 打开ADC12模块、设置采样时间
ADC12CTL1 = CSTARTADD_1+SHP; // 选择信号源自采样定时器,设置为单通道转换
ADC12MCTL1 |= INCH_1;
ADC12IE = 0x02; // 使能中断
ADC12CTL0 |= ENC; // 使能转化
P6DIR = 0xFD; // P6.1, i/p
P6SEL|= 0x02; // 使能ADC12模块的A1
P1DIR=0xFF; // 设置方向
P1SEL=0; // 设置为普通I/O 口
P2DIR=0xFF;
P2SEL=0;
P2OUT=0x00;
P3DIR=0xFF; // 设置方向
P3SEL=0; // 设置为普通I/O 口
P1OUT=0x00;
P3OUT=0xFF;
ADC12CTL0 |= ADC12SC; // 开始转换
//_BIS_SR(CPUOFF + GIE); // LPM0, ADC12_ISR will force exit
_BIS_SR(LPM0_bits+ GIE);//进入低功耗睡眠模式
while(1)
{
ADC12IE=0x00;
P3OUT=0xFF;
P1OUT=Disbuf[Index];
P3OUT=~(1<<Index);
ADC12IE=0x02;
if(++Index==8)
{
Index=0;
ADC12CTL0 |= ADC12SC;
}
delayms(1);
}
}
// ADC12 interrupt service routine
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR (void)
{
VoltDataProcess(ADC12MEM0);
LPM0_EXIT;
} |