基于MSP430F169的AD、DA控制程序
//功能:
1、通過串口RS232發(fā)送數(shù)據(jù)到上位機(jī),同時接收上位機(jī)的指令
2、發(fā)送AD轉(zhuǎn)換的值到上位機(jī),
3、上位機(jī)發(fā)送DA轉(zhuǎn)換的數(shù)字量
---------------------------------*/
#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};
//---設(shè)定字符串 -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轉(zhuǎn)換結(jié)果的數(shù)組
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
}
}
/********************主函數(shù)********************/
void main(void)
{
//----------初始化---------
WDTCTL = WDTPW + WDTHOLD; //關(guān)閉看門狗
//----各口初始化--------
P4DIR|=0XC3;//P4為一般的IO口執(zhí)行簡單輸入輸出功能
//P4.0-1為輸出控制口高壓開啟、快慢速
//P4.2-5為指示信號輸入
//P4.6-7為系統(tǒng)運(yùn)行指示信號輸出
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
//多次采樣/轉(zhuǎn)換
//ADC12CTL0 =REFON+REF2_5V;
//ADC12CTL0 |=0X40; //啟用內(nèi)部參考電壓2.5V
ADC12CTL1 = SHP+CONSEQ_1; // 采樣信號源自采樣定時器
//2-單通道多次轉(zhuǎn)換
//3-序列通道多次轉(zhuǎn)化
//1-序列通道單次轉(zhuǎn)換
//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; // 使能轉(zhuǎn)換
ADC12CTL0 |= ADC12SC; // 開始轉(zhuǎn)換
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)
{
//循環(huán)檢測輸入的故障標(biāo)識位,以及輸出串口是否正常工作標(biāo)識(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說明電源發(fā)生故障1
{TX[24]=0X01 ;}
else
{TX[24]=0X00 ;}
if((P4IN&0X20)==0x20) //P4的D5位為1說明電源發(fā)生故障2
{TX[25]=0X01 ;}
else
{TX[25]=0X00 ;}
//點亮指示燈、
P4OUT |= BIT6;
P4OUT |= BIT7;
//更新電源設(shè)定
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;} //關(guān)閉高壓
if(F_S==0X31) //快速
{P4OUT|=BIT1;}
else
{P4OUT&=~BIT1;}
//更新上傳數(shù)據(jù)
//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; // 開始轉(zhuǎn)換
}
// 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)//檢驗數(shù)據(jù)是否正確
//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;
}
評論