#include <hidef.h>
/* common defines and macros */
#include "derivative.h"
/* derivative-specific definitions */
int LED[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void CLK_init(void);
void AD_init(void);
unsigned char GetATD0(unsigned char ch);
void delay(int delaytime)
{//软件延时,24M总线频率时延时时间约为10us
int i;unsigned char j;
for (i=0;i<delaytime;i++)
for (j=0;j<24;j++)
;
}
void longdelay(int delaytime)
{//软件延时,24M总线频率时延时时间约为1ms
int i;
for (i=0;i<delaytime;i++)
delay(1);
}
void CLK_init(void)
{
CLKSEL=0x00; //初始时还没有PLL
PLLCTL=0xe1; //时钟监控使能,PLL允许,VCOCLK调频2%,禁止快速苏醒,禁止自给时钟,伪停止下禁止实时中断和看门狗
SYNR=2;
REFDV=1;
//PLLCLK=2*OSCCLK*(SYNR+1/((REFDV+1))=48M;
asm NOP;asm NOP;asm NOP;//等待锁相环稳定
while(!(CRGFLG&0x08));//锁相环稳定标志位
CLKSEL=0x80;
//时钟选择寄存器,Bus Clock=PLLCLK/2,停止模式下振荡器、系统时钟、锁相环、内核时钟、看门狗继续工作
}
void AD_init(void)
{
ATD0CTL0=0x07;
ATD0CTL1=0x00;
ATD0CTL2=0x40;
ATD0CTL3=0xc0;
ATD0CTL4=0x44;
ATD0CTL5=0x30;
ATD0DIEN=0x00;
/*ATD0CTL0=0x00;
//AD转换序列长度为16
ATD0CTL1=0x00;
//允许指定AD口做外部触发输入口,8位结果,采样前采样电容不放电 1
ATD0CTL2=0x40;
//标志寄存器快速清零,停止模式不运行,禁止外部触发,禁止转换完成中断和比较成功中断
ATD0CTL3=0xc0;
//结果右对齐,非FIFO模式,冻结模式继续转换 转换长度8
ATD0CTL4=0x44;
//第二阶段采用8个AD周期(第一阶段2个AD周期),f=fbus/2*(4+1),4.16ms转换一次
ATD0CTL5=0x38;
//连续多通道转换,从通道8开始
ATD0DIEN=0x00;
//禁用数字输入缓冲区*/
}
void IOinit(void)
{
DDRB=0xff;
PORTB=0x00;
DDR0AD0=0x00;
}
unsigned char GetATD0(unsigned char ch)
{
byte ad=0;
while(!ATD0STAT0_SCF);
switch(ch)
{
case 0:
ad=ATD0DR0L;
break;
case 1:
ad=ATD0DR1L;
break;
case 2:
ad=ATD0DR2L;
break;
case 3:
ad=ATD0DR3L;
break;
case 4:
ad=ATD0DR4L;
break;
case 5:
ad=ATD0DR5L;
break;
case 6:
ad=ATD0DR6L;
break;
case 7:
ad=ATD0DR7L;
break;
}
return ad;
}
void main(void)
{
int ADdata[8];
int i1,i2,i3,i4,i5,i6,i7,i8;
unsigned char i;
CLK_init();
AD_init();
DisableInterrupts;
while(1)
{
/*for(i=0;i<8;i++)
{
PORTB=LED;
ADdata=GetATD0(i);
}
i1= ADdata[0];
i2= ADdata[1];
i3= ADdata[2];
i4= ADdata[3];
i5= ADdata[4];
i6= ADdata[5];
i7= ADdata[6];
i8= ADdata[7];*/
i1=GetATD0(7);
ATD0STAT0_SCF=1;
if(i1)
{
PORTB=0x01;
}
}
}
这是一个关于AD的程序,关于unsigned char GetATD0((unsigned char ch)我没看明白这个函数的作用;求大虾指点迷津
欢迎光临 智能车制作 (http://111.231.132.190/) | Powered by Discuz! X3.2 |