中级会员
- 积分
- 434
- 威望
- 255
- 贡献
- 81
- 兑换币
- 24
- 注册时间
- 2010-10-23
- 在线时间
- 49 小时
|
本帖最后由 50572209 于 2011-5-30 20:21 编辑
#include <hidef.h> /* common defines and macros */
#include "derivative.h"
signed char a[14];
signed char max,i;
//-----时钟初始化程序--------//
void PLL_Init(void)
{
REFDV=3; //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
SYNR=7; //锁相环时钟=2*16*(7+1)/(3+1)=64MHz
while(!(CRGFLG&0x08));//busclock=64/2=32MHz
CLKSEL=0x80; //选定锁相环时钟
}
void AD_Init(void)
{
ATD0CTL0=0x0e;
ATD0CTL1=0x00; //选择AD通道为外部触发,8位精度,采样前不放电
ATD0CTL2=0x40; //标志位自动清零,禁止外部触发, 禁止中断
ATD0CTL3=0x80; //右对齐无符号,每次转换16个序列, No FIFO, Freeze模式下继续转
ATD0CTL4=0x01; //采样时间为4个AD时钟周期,PRS=1,ATDClock=6MHz
ATD0CTL5=0x31; //特殊通道禁止,连续转换 ,多通道转换,起始通道为0转换4个通道
ATD0DIEN=0x00; //禁止数字输入
}
void IO_Init(void) //IO PORT INITILIZATION
{
DDRB = 0XFF;
DDRA = 0XFF;
PORTA=0Xff;
PORTB=0Xff;
}
void AD_GetValue(void)
{
while(ATD0STAT2_CCF0==0);a[0]=ATD0DR0L;
while(ATD0STAT2_CCF1==0);a[1]=ATD0DR1L;
while(ATD0STAT2_CCF2==0);a[2]=ATD0DR2L;
while(ATD0STAT2_CCF3==0); a[3]=ATD0DR3L;
while(ATD0STAT2_CCF4==0); a[4]=ATD0DR4L;
while(ATD0STAT2_CCF5==0); a[5]=ATD0DR5L;
while(ATD0STAT2_CCF6==0); a[6]=ATD0DR6L;
while(ATD0STAT2_CCF8==0);a[7]=ATD0DR8L;
while(ATD0STAT2_CCF9==0);a[8]=ATD0DR9L;
while(ATD0STAT2_CCF10==0);a[9]=ATD0DR10L;
while(ATD0STAT2_CCF11==0);a[10]=ATD0DR11L;
while(ATD0STAT2_CCF12==0);a[11]=ATD0DR12L;
while(ATD0STAT2_CCF13==0);a[12]=ATD0DR13L;
while(ATD0STAT2_CCF14==0);a[13]=ATD0DR14L;
}
//-----PWM初始化程序------//
void PWM_Init(void)
{
PWME = 0x00; //pwm关闭
PWMCTL_CON01=1;//通道01结连
PWMPRCLK = 0x22; // clockA=clockB=busclock/4=32/4=8MHz
PWMSCLA = 0x04; //clockSA=clockA/(2*PWMSCLA)=8/8=1MHz
PWMSCLB = 0x04; //clockSA=clockA/(2*PWMSCLA)=8/8=1MHz
PWMCLK = 0xff; //时钟源选择SA,SB
PWMPOL = 0xff; //正极性脉冲
PWMCAE = 0x00; // 左对齐模式
PWMPER01 = 20000;//舵机计数周期寄存器(20ms)
PWMDTY01 = 1500; //输出高电平时间1.4ms
PWME= 0x07; //输出PWM
}
//-----主程序------//
void main(void)
{
PLL_Init();
IO_Init();
AD_Init();
PWM_Init();
EnableInterrupts;
for(;;)
{
AD_GetValue();
max=a[0];
for(i=0;i<14;i++)
{
if(max<a)
max=a;
}
switch (max)
{
case 0: PWMDTY01=1300; ;break;
case 1: PWMDTY01=1350; ;break;
case 2: PWMDTY01=1380; ;break;
case 3: PWMDTY01=1400; ;break;
case 4: PWMDTY01=1420; ;break;
case 5: PWMDTY01=1470; ;break;
case 6: PWMDTY01=1500; ;break;
case 7: PWMDTY01=1500; ;break;
case 8: PWMDTY01=1530; ;break;
case 9: PWMDTY01=1580; ;break;
case 10: PWMDTY01=1600; ;break;
case 11: PWMDTY01=1620; ;break;
case 12: PWMDTY01=1650; ;break;
case 13: PWMDTY01=1700; ;break;
default:break;
}
_FEED_COP();
}
} |
|