我想做一个通过MSP430F169片上ADC采样,并通过片上DAC输出的程序,参考了一些书,但是现在情况是DAC可以正常输出,但ADC那里无法确定是没有采集到信号,还是没有进入中断把程序输出出去。代码如下:
#include "io430.h"
#include "dac12.h"
typedef unsigned char uchar;
typedef unsigned int uint;
//typedef unsigned int result0;
#define Num_of_Results 15
static uint results[Num_of_Results]; //保存ADC转换结果的数组
void ADC_Init(void);
/************************主函数****************************/
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; //关闭看门狗
/*下面六行程序关闭所有的IO口*/
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0XFF;
P5DIR = 0XFF;P5OUT = 0XFF;
P6DIR = 0XFF;P6OUT = 0X00;
ADC_Init();
InitDac12();
GoDac12(100,0); //打开DAC12通道0
GoDac12(100,1); //打开DAC12通道1
Dac12Write(0xFFF,1);
//Dac12Write(0x0FF,0);
while (1)
{
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
ADC12CTL0 |= ENC;
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
__no_operation(); // For debugger
}
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
static uint index = 0;
results[index++] = ADC12MEM0; // Move results
if(index == Num_of_Results)
{
uchar i;
unsigned long sum = 0;
index = 0;
for(i = 0; i < Num_of_Results; i++)
{
sum += results[i];
}
sum =sum/Num_of_Results; //除以15
Dac12Write(sum,0);
}
__bic_SR_register_on_exit(LPM0_bits);
}
/*******************************************
函数名称:ADC_Init
********************************************/
void ADC_Init(void)
{
ADC12CTL0 &= ~ENC;
P6SEL |= 0x01;
ADC12CTL0 = ADC12ON+SHT0_4+MSC+REFON+REF2_5V;
ADC12CTL1 = SHP+CONSEQ_2;
ADC12MCTL0 = INCH_0;
ADC12IE = 0x01;
}
/*******************************配置DAC********************/
#include "io430.h"
#include "dac12.h"
void InitDac12()
{
//ADC12CTL0 = REF2_5V + REFON; //打开内部参考电压源,参考电压为2.5V
//设置DAC12通道0
//满量程为参考电压,中速度/电流输出,12位分辨率,Vref为参考电压,立即刷新输出
DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC + DAC12LSEL_1;
DAC12_0CTL |= DAC12CALON; //自动校准DA输出
//设置DAC12通道1
//满量程为参考电压的3倍,中速度/电流输出,12位分辨率,Vref为参考电压,立即刷新输出
DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC +DAC12LSEL_1;
DAC12_1CTL |= DAC12CALON; //自动校准DA输出
//等待校准结束
while(DAC12_0CTL & DAC12CALON == DAC12CALON);
while(DAC12_1CTL & DAC12CALON == DAC12CALON);
}
/**********************************************
打开或关闭DAC12
doit:0--关闭; 100:运行
which:打开或关闭。 0--DAC0; 1--DAC1;
**********************************************/
void GoDac12(unsigned char doit, unsigned char which)
{
if(doit == 0) //关闭DAC12
{
if(which == 0)
DAC12_0CTL &= ~DAC12ENC;
else
DAC12_1CTL &= ~DAC12ENC;
}
else if(doit == 100) //打开DAC12
{
if(which == 0)
DAC12_0CTL |= DAC12ENC;
else
DAC12_1CTL |= DAC12ENC;
}
}
/**********************************************
写输出值
dat:要输出的值,准备写入DA数据寄存器
which:写入哪个通道。 0--DAC0; 1--DAC1;
**********************************************/
void Dac12Write(unsigned int dat, unsigned char which)
{
if(which == 0)
DAC12_0DAT = dat;
else
DAC12_1DAT = dat;
}