MSP430 battery voltage acquisition Proteus simulation program
[Copy link]
The MSP430 battery voltage acquisition simulation schematic is as follows (the proteus simulation project file can be downloaded from the attachment of this post)
The microcontroller source program is as follows:
#include <MSP430x24x.h>
#define uchar unsigned char
#define uint unsigned int
uchar const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, //Common cathode digital tube segment selection table, no decimal point
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar table_volt[4] = {0}; //array, stores voltage value
uchar Index; //variable
//*******Delay function************//
void Delay_ms(unsigned int t)
{
unsigned int num;
while(t--)
for(num=1330;num>0;num--);
}
//ADC pin definition
#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; //Variable, collect battery voltage value
//*******ADC acquisition start function************//
void ADC_START(void)
{
ADCALE1;
ADCST1;
Delay_ms(1);
ADCALE0;
ADCST0;
Delay_ms(1);
}
//*******ADC acquisition timing implementation function************//
unsigned int ADC_READ(void)
{
unsigned int data;
data=0x00;
Delay_ms(1);
ADCOE1;
ADC_START();
Delay_ms(5);
data=P4IN; //Read data
ADCOE0;
return data; //Return data
}
//*******Main function****************//
void main(void)
{
WDTCTL=WDTPW + WDTHOLD; //Turn off the watchdog
P2DIR=0xFF; //Set direction
P2SEL=0; //Set to normal I/O port
P3DIR=0xFF; //Set direction
P3SEL=0; //Set to normal I/O port
P2OUT=0x00;
P3OUT=0xFF;
P5SEL = 0x00;
P5DIR |= BIT0+BIT4+BIT5+BIT6; //ADC chip interface
while(1)
{
P3OUT=0xFF;
if( Index == 0 ) //If the first digital tube displays, add a decimal point
{
P2OUT=table[table_volt[Index]]+0x80;//decimal point
}
else //Others do not add
{
P2OUT=table[table_volt[Index]]; //Display voltage value
}
P3OUT=~(1<<Index);
if(++Index==4) Index=0;
Delay_ms(1); //Delay
ADC1 = ADC_READ()*20-80; //ADC battery voltage acquisition, amplification
table_volt[0] = ADC1/1000; //Thousands
table_volt[1] = ADC1%1000/100; //Hundreds
table_volt[2] = ADC1%100/10; //Tens
table_volt[3] = ADC1%10; //Ones
}
}
|