本帖最后由 小帅哥哥 于 2015-7-26 10:21 编辑
我用定时器输出比较,做了一个5s的延时。想让AD延迟5秒转换,5秒的时候,我关闭了定时器,可是AD转换不了啊,我把定时器打开,AD转换又能用了 这是为什么啊 #include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define ATD_CLOCK 2000000
#define BUS_CLOCK 8000000
#define OSC_CLOCK 16000000
#define baud 9600
#define led1 PORTAB_PB0
#define led2 PORTB_PB1
#define led1_dir DDRAB
#define led2_dir DDRB
#define on 0
unsigned int num = 0;
unsigned char AD0_DATA,AD1_DATA;
void gpio_init(void)
{
PORTB = 0XFF;
led1_dir = 0xff;
led2_dir = 0xff;
}
void ECT_INIT(void)
{
TIOS_IOS0 = 1; //定时器0用作输出比较
TSCR1_TEN = 1; //定时器使能
TSCR1_TFFCA = 1; //快速清除标志位
TCTL2 = 0X00; //定时器与输出引脚断开
TIE = 1; //开定时器0中断
T标志寄存器1 = 0XFF;
T标志寄存器2 = 0XFF; //清除中断标志
TSCR2 = 0X03;
}
void SCI_INIT(void)
{
SCI0BD = BUS_CLOCK/16/baud;
SCI0CR1 = 0X00;
SCI0CR2 = 0X08; //串口使能,发送使能
}
void AD_INIT(void)
{
ATD0CTL1_SRES = 0 ; //8位精度选择
ATD0CTL2 = 0X42; //启动AD 快速清除,转换完成中断
ATD0CTL3 = 0X90; //每次转换两个通道,右对齐模式
ATD0CTL4 = BUS_CLOCK / ATD_CLOCK / 2 - 1; //AD时钟频率2M
}
void AD_START(void)
{
ATD0CTL5 = 0X30; //转换AD00和AD01 单次转换 转换完成,产生中断
}
void sci_send(void)
{
while(!SCI0SR1_TC);
SCI0DRL = AD0_DATA;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void ECT_interrupt(void)
{
num++;
TC0 = TCNT + 1000; //周期1ms
}
interrupt void AD_interrupt(void)
{
AD0_DATA = ATD0DR0L; //"L"表示第八位,,不表示左右对其 “H”表示高八位,
AD1_DATA = ATD0DR1L;
sci_send();
}
#pragma CODE_SEG DEFAULT
void main(void) {
/* put your own code here */
SCI_INIT();
ECT_INIT();
AD_INIT();
gpio_init();
EnableInterrupts;
for(;;) {
if(num ==5000)
{
TSCR1_TEN = 0; //延时五秒,关闭定时器
AD_START(); //开始AD采集
led1 = on;
led2 = on;
}
// _FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
|