您的位置 首页 设计

根据MSP430F169的AD、DA控制程序

/*—————-程序说明——//功能:1、通过串口RS232发送数据到上位机,同时接收上位机的指令2、发送AD转换的值到上位机,3、上位机…

/*—————-程序阐明——

//功用:
1、经过串口RS232发送数据到上位机,一起接纳上位机的指令
2、发送AD转化的值到上位机,
3、上位机发送DA转化的数字量
———————————*/
#include <msp430x16x.h>
//变量界说 反应字符串
//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;

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/sheji/272848.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部