1.部分源程序
#include <msp430.h>
#include<stdio.h>
#include"UCS_25M.h"
#include"4X4keyboard.h"
#include"delay.h"
#include"LCD12864.h"
#include"SPI_SIM.h"
void UCS_INIT(void);
void timerA_initial();
void OVERCURRENT_PROTECT();
int PWM_PID(float sample,float ref_value,float kp);
extern char str0[7];
extern int input_width;
extern int mode_flag; //mode识别
extern int key_flag;
extern int state_flag; //输入识别
float input_duty=20;
float input_current=1;
char str1[12];
extern char Resultf1[13]; //电流显示字符串
extern char Resultf2[13]; //电压1显示字符串
extern char Resultf3[13]; //电压2显示字符串
extern float resultf_current;
extern float resultf_voltage1;
extern float resultf_voltage2;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
unsigned int i;
P2DS|=BIT4; //FULL STRENGTH
P8DIR|=BIT1+BIT2; //shutdown control
P8OUT&=~(BIT1+BIT2);
timerA_initial();
UCS25M_INITIAL(); //clock init
initial_lcd();
clear_screen(); //lcd init
DispStringAt(7,1,"MODE0");
DispStringAt(5,1,"U2:");
DispStringAt(3,1,"U1:");
DispStringAt(1,1,"I1:");
SPI_INITIAL();
KEYPORT_INITIAL();
while(1)
{
for(i=0;i<3;i++)
{
MULTI_READ1118();
}
if(mode_flag==0)
{
MULTI_READ1118();
_DINT();
DispStringAt(3,33,Resultf2);
DispStringAt(5,33,Resultf3);
DispStringAt(7,81,str1);
if(key_flag==0)
{
DispStringAt(1,33,Resultf1);
}
else if(key_flag==1)
{
DispStringAt(1,33,str0);
}
if(state_flag==0)
{
DispStringAt(7,41,":OFF");
}
else if(state_flag==1)
{
OVERCURRENT_PROTECT();
if(resultf_current>=(input_current+0.04))
{
input_width++;
TA2CCR1=input_width;
_delay_ms(1);
}
else if((resultf_current<=(input_current-0.04))&&input_width>0)
{
input_width--;
TA2CCR1=input_width;
_delay_ms(1);
}
}
else if(state_flag==2)
{
}
sprintf(str1,"%4.3fA ",input_current);
DispStringAt(7,81,str1);
_EINT();
_delay_ms(1);
}
else if(mode_flag==1)
{
MULTI_READ1118();
_DINT();
DispStringAt(1,33,Resultf3);
DispStringAt(5,33,str0);
sprintf(str1,"%4.3fA ",input_current);
DispStringAt(3,81,str1);
if(state_flag==0) DispStringAt(3,41,":OFF");
else if(state_flag==1)
{
DispStringAt(3,41,":ON ");
if(resultf_voltage2>=(30+0.1)&&input_width>=0)
{
input_width--;
TA2CCR1=input_width;
_delay_ms(1);
}
else if(resultf_voltage2<=(30-0.1))
{
input_width++;
TA2CCR1=input_width;
_delay_ms(1);
}
}
_EINT();
//_delay_ms(1);
}
else if(mode_flag==2)
{
MULTI_READ1118();
_DINT();
DispStringAt(1,33,Resultf3);
if(state_flag==0) DispStringAt(3,41,":OFF");
else if(state_flag==1)
{
DispStringAt(3,41,":ON ");
if(resultf_voltage2>=(30+0.1)&&input_width>=0)
{
input_width--;
TA2CCR1=input_width;
_delay_ms(1);
}
else if(resultf_voltage2<=(30-0.1))
{
input_width++;
TA2CCR1=input_width;
_delay_ms(1);
}
}
sprintf(str1,"%4.3fA ",input_current);
DispStringAt(3,81,str1);
_EINT();
_delay_ms(1);
}
}
}
void OVERCURRENT_PROTECT()
{
if(resultf_voltage1>=24)
{
P8OUT&=~BIT2;
state_flag=2;
DispStringAt(7,41,"full");
}
else if(resultf_voltage1<24)
{
P8OUT|=BIT2;
state_flag=1;
DispStringAt(7,41,":ON ");
}
}
int PWM_PID(float sample,float ref_value,float kp)
{
unsigned int temp_value;
temp_value=kp*(ref_value-sample);
return temp_value;
}