基于MSP430F169的AD、DA控制程序
//功能:
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);
MSP430F169ADDA控制程 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)