MSP430电池电压采集Proteus仿真程序
[复制链接]
MSP430电池电压采集仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
#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 table_volt[4] = {0}; //数组,存储电压值
uchar Index; //变量
//*******延时函数************//
void Delay_ms(unsigned int t)
{
unsigned int num;
while(t--)
for(num=1330;num>0;num--);
}
//ADC管脚定义
#define ADCST0 P5OUT &= ~BIT4
#define ADCST1 P5OUT |= BIT4
#define ADCALE0 P5OUT &= ~BIT5
#define ADCALE1 P5OUT |= BIT5
#define ADCEOC0 P5OUT &= ~BIT6
#define ADCEOC1 P5OUT |= BIT6
#define ADCOE0 P5OUT &= ~BIT7
#define ADCOE1 P5OUT |= BIT7
#define ADC_Channel0 P5OUT &= ~BIT0
#define ADC_Channel1 P5OUT |= BIT0
unsigned int ADC1 = 0; //变量,采集电池电压值
//*******ADC采集启动函数************//
void ADC_START(void)
{
ADCALE1;
ADCST1;
Delay_ms(1);
ADCALE0;
ADCST0;
Delay_ms(1);
}
//*******ADC采集时序实现函数************//
unsigned int ADC_READ(void)
{
unsigned int data;
data=0x00;
Delay_ms(1);
ADCOE1;
ADC_START();
Delay_ms(5);
data=P4IN; //读取数据
ADCOE0;
return data; //返回数据
}
//*******主函数************//
void main(void)
{
WDTCTL=WDTPW + WDTHOLD; // 关闭看门狗
P2DIR=0xFF; // 设置方向
P2SEL=0; // 设置为普通I/O 口
P3DIR=0xFF; // 设置方向
P3SEL=0; // 设置为普通I/O 口
P2OUT=0x00;
P3OUT=0xFF;
P5SEL = 0x00;
P5DIR |= BIT0+BIT4+BIT5+BIT6; //ADC芯片接口
while(1)
{
P3OUT=0xFF;
if( Index == 0 ) //如果第一个数码管显示,加入小数点
{
P2OUT=table[table_volt[Index]]+0x80;//小数点
}
else //其他不加
{
P2OUT=table[table_volt[Index]]; //显示电压值
}
P3OUT=~(1<<Index);
if(++Index==4) Index=0;
Delay_ms(1); //延时
ADC1 = ADC_READ()*20-80; //ADC电池电压采集,放大
table_volt[0] = ADC1/1000; //千位
table_volt[1] = ADC1%1000/100; //百位
table_volt[2] = ADC1%100/10; //十位
table_volt[3] = ADC1%10; //个位
}
}
|