//功能:
1、通过串口RS232发送数据到上位机,同时接收上位机的指令
2、发送AD转换的值到上位机,
3、上位机发送DA转换的数字量
---------------------------------*/
#include
//变量定义 反馈字符串
//Br009300380037001200000011ED
//unsigned char TX[28]={0x42,0x72,0x34,0x30,0x39,0x33,0x32,0x30,0x33,0x38,0x32,0x34,0x33,0x37,0x31,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x31,0x45,0x44};
unsigned char TX[42]={0x42,0x72,0x30,0x30,0x39,0x33,0x30,0x30,0x33,0x38,0x30,0x30,0x33,0x37,0x30,0x30,0x31,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x31,0x45,0x44,0x42,0x52,0x30,0x30,0x39,0x33,0x30,0x30,0x33,0x38,0x31,0x31,0x45,0x44};
unsigned char RX[14]={0x42,0x52,0x30,0x30,0x39,0x30,0x30,0x30,0x33,0x38,0x31,0x31,0x45,0x44};
//---设定字符串 -Br0093003811ED
#define Num_of_Results 16
int results0[Num_of_Results],results1[Num_of_Results],results2[Num_of_Results],results3[Num_of_Results],results4[Num_of_Results]; //保存ADC转换结果的数组
int AD_HV,AD_mA,AD_FIL,AD_TEM,AD_PRE;
unsigned int average0,average1,average2,average3,average4; //0~65535
char index0,index1,U0,U1,U2,U3,I0,I1,I2,I3,HV_ON,F_S,F_NEW;
//char index;
char j,k,rxdata0;//0~255
//---------软件延时子程序-------
void DELAY(int time)
{
int x,y;
for(x=0;x<500;x++)
{
for(y=0;y
}
}
/********************主函数********************/
void main(void)
{
//----------初始化---------
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
//----各口初始化--------
P4DIR|=0XC3;//P4为一般的IO口执行简单输入输出功能
//P4.0-1为输出控制口高压开启、快慢速
//P4.2-5为指示信号输入
//P4.6-7为系统运行指示信号输出
P4OUT&=~BIT0;
P4OUT&=~BIT1;
//------串口初始化-----
UCTL0 = 0x11; // 8-bit character,SWRST =1
P3DIR|=0x20; //p3.4-输出,p3.5-输入
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00 = 0x03; // 32k/9600 - 6.83
UBR10 = 0x00; //
UMCTL0 = 0x4A; // Modulation
// UBR00 = 0x1B; // 32k/ 1200 - 13.65
// UBR10 = 0x00; //
// UMCTL0 = 0x03; // Modulation
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
UCTL0 &= ~SWRST; // Initialize USART state machine
ME1 |= URXE0 + UTXE0; // Enable USART0 T/RXD
IE1 |= URXIE0+UTXIE0; // Enable USART0 RX TX interrupt
//--------AD初始化-----
P6DIR|=0XC0; //P6.0-5为输入口(AD模拟量输入端口),P6.6-7为DAC输出口
P6SEL |= 0xFF; // 使能P6口为ADC的模拟量输入,通道0,1或者DAC输出口
ADC12CTL0 = ADC12ON+SHT0_15+MSC; // 打开ADC,使用采样定时器0
//采样时间为4*T*256
//多次采样/转换
//ADC12CTL0 =REFON+REF2_5V;
//ADC12CTL0 |=0X40; //启用内部参考电压2.5V
ADC12CTL1 = SHP+CONSEQ_1; // 采样信号源自采样定时器
//2-单通道多次转换
//3-序列通道多次转化
//1-序列通道单次转换
//ADC12MCTL0=SREF_1;
// ADC12MCTL1=SREF_1;
ADC12MCTL0=INCH_0; //HVFB0
ADC12MCTL1=INCH_1; //mAFB
ADC12MCTL2=INCH_2; //FIL0
ADC12MCTL3=INCH_3; //TEM0
ADC12MCTL4=INCH_4+EOS; //PRE0
ADC12IE = 0x10; // A4使能ADC中断()
ADC12CTL0 |= ENC; // 使能转换
ADC12CTL0 |= ADC12SC; // 开始转换
F_NEW=0XAA;
//-----DA 初始化------
DAC12_0CTL=DAC12AMP_5+DAC12ENC+DAC12IR;
//DAC12_0DAT=0X03FF;
DAC12_0DAT=0X00FF;
DAC12_1CTL=DAC12AMP_5+DAC12ENC+DAC12IR;
//DAC12_1DAT=0X0FFF;
DAC12_1DAT=0X00FF;
_EINT(); //允许全局中断
// LPM1;
while(1)
{
//循环检测输入的故障标识位,以及输出串口是否正常工作标识(S1S2是否闪烁)
if((P4IN&0X04)==0x04) //P4的D2位为1说明高压已开启
{TX[22]=0X01 ;}
else
{TX[22]=0X00 ;}
if((P4IN&0X08)==0x08) //P4的D3位为1说明灯丝已开启
{TX[23]=0X01 ;}
else
{TX[23]=0X00 ;}
if((P4IN&0X10)==0x10) //P4的D4位为1说明电源发生故障1
{TX[24]=0X01 ;}
else
{TX[24]=0X00 ;}
if((P4IN&0X20)==0x20) //P4的D5位为1说明电源发生故障2
{TX[25]=0X01 ;}
else
{TX[25]=0X00 ;}
//点亮指示灯、
P4OUT |= BIT6;
P4OUT |= BIT7;
//更新电源设定
DAC12_0DAT=(U0-0X30)*1000+(U1-0X30)*100+(U2-0X30)*10+(U3-0X30);
DAC12_1DAT=(I0-0X30)*1000+(I1-0X30)*100+(I2-0X30)*10+(I3-0X30);
if(HV_ON==0X31) //1-开启高压
{P4OUT|=BIT0;}
else
{P4OUT&=~BIT0;} //关闭高压
if(F_S==0X31) //快速
{P4OUT|=BIT1;}
else
{P4OUT&=~BIT1;}
//更新上传数据
//ADC12CTL0 &=~ ENC;
if(F_NEW==0X55)
{
ADC12CTL0 &=~ ENC;
if(index0 == Num_of_Results-1)
{
char i;
average0 = 0;
average1 = 0;
average2 = 0;
average3 = 0;
average4 = 0;
for(i = 0; i < Num_of_Results; i++)
{
average0 += results0[i];
average1 += results1[i];
average2 += results2[i];
average3 += results3[i];
average4 += results4[i];
}
AD_HV=average0 >>= 4; //除以16
AD_mA=average1 >>= 4; //除以16
AD_FIL=average2 >>= 4;
AD_TEM=average3 >>= 4;
AD_PRE=average4 >>= 4;
TX[2] = AD_HV / 1000;
TX[3] = (AD_HV - TX[2]*1000)/100;
TX[4] = (AD_HV - TX[2]*1000 - TX[3]*100)/10;
TX[5] = (AD_HV -TX[2]*1000 - TX[3]*100 - TX[4]*10);
TX[6] = AD_mA / 1000;
TX[7] = (AD_mA - TX[6]*1000)/100;
TX[8] = (AD_mA - TX[6]*1000 - TX[7]*100)/10;
TX[9] = (AD_mA - TX[6]*1000 - TX[7]*100 - TX[8]*10);
TX[10] = AD_FIL/ 1000;
TX[11] = (AD_FIL - TX[10]*1000)/100;
TX[12] = (AD_FIL - TX[10]*1000 - TX[11]*100)/10;
TX[13] = (AD_FIL - TX[10]*1000 - TX[11]*100 - TX[12]*10);
TX[14] = AD_TEM / 1000;
TX[15] = ( AD_TEM - TX[14]*1000)/100;
TX[16] = ( AD_TEM - TX[14]*1000 - TX[15]*100)/10;
TX[17] = ( AD_TEM - TX[14]*1000 - TX[15]*100 - TX[16]*10);
TX[18] = AD_PRE / 1000;
TX[19] = (AD_PRE - TX[18]*1000)/100;
TX[20] = (AD_PRE - TX[18]*1000 - TX[19]*100)/10;
TX[21] = (AD_PRE - TX[18]*1000 - TX[19]*100 - TX[20]*10);
}
index0 =(index0 +1)%Num_of_Results;
F_NEW=0XAA;
ADC12CTL0 |= ENC;
ADC12CTL0 |= ADC12SC; // 开始转换
}
// ADC12CTL0 |= ENC;
//DELAY(15);
}
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
//ADC12IE = 0x00;
//DELAY(2);
if(F_NEW==0XAA)
{
results0[index0] = ADC12MEM0; // Move results
results1[index0] = ADC12MEM1; // Move results
results2[index0] = ADC12MEM2;
results3[index0] = ADC12MEM3;
results4[index0] = ADC12MEM4;
F_NEW=0X55;
}
_NOP();
}
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
rxdata0= RXBUF0;
//赋值前检验是否为合法首字母,合法再赋值
RX[k]=rxdata0;
if(RX[0]==0x42)
{
// TX[8+k]=rxdata0;
//输出接收到的值
TX[28+k]=rxdata0;
k++;
if(k==14)
{
k=0;
//验证接收是否有效
if(RX[0]==0x42&&RX[1]==0x52&&RX[12]==0x45&&RX[13]==0x44)//检验数据是否正确
//if(RX[0]==0x42&&RX[1]==0x72)
{
//P4OUT|=BIT0;
U0=RX[2];
U1=RX[3];
U2=RX[4];
U3=RX[5];
I0=RX[6];
I1=RX[7];
I2=RX[8];
I3=RX[9];
HV_ON=RX[10];
F_S=RX[11];
}
}
}
//熄灭指示灯
P4OUT &= ~BIT6;
}
#pragma vector=UART0TX_VECTOR
__interrupt void usart0_tx (void)
{
if(j>1&&j<=25)
{TXBUF0 = TX[j]+0x30;}
else
{TXBUF0 = TX[j];}
// TXBUF0 = TX[j];
j++;
// if(j==28)
if(j==42)
{j=0;}
//熄灭指示灯
P4OUT &= ~BIT7;
}
上一篇:MSP430的高质量代码编写
下一篇:一种基于低功耗单片机SN8P2711A的移动电源设计
推荐阅读最新更新时间:2024-03-16 15:06