中级会员
- 积分
- 278
- 威望
- 189
- 贡献
- 51
- 兑换币
- 0
- 注册时间
- 2010-12-22
- 在线时间
- 19 小时
|
我通过采集8路AD的值来改变电机的转速,可是速度根本就改变不了。。。麻烦大家帮我看看程序哪里有问题?
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
uint ad0_dat[8];
#define Minspeed 35
#define Lowspeed 50
#define Midspeed 60
#define Highspeed 70
#define Maxspeed 90
#define Stopspeed 25
#define PIT0TIME 20
uint bianhua;
uint a=0;
uint p,pt,i;
void init_PLL()
{
REFDV=1; //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
SYNR=2; //锁相环时钟=2*16*(2+1)/(1+1)=48MHz
while(!CRGFLG_LOCK); //总线时钟=48/2=24MHz
CLKSEL=0X80; //选定锁相环时钟
}
void ATD_Init() //AD初始化
{
ATD0CTL1 = 0x00; //8位精度,不放电
ATD0CTL2 = 0x40; //快速清除标志位,禁止外部触发,bu使能中断
ATD0CTL3 = 0xC4; //右对齐,每序列8次转换,用FIFO,//进入FreezeMode/冻结模式完成当前转换
ATD0CTL4 = 0x05; //采样用4个ATD周期,F(ATD)=2MHz[bus clock为24MHz]
ATD0CTL5 = 0x30; //scan模式 duo通道
ATD0DIEN = 0x00; //禁止数字输入
}
void init_PWM()
{
PWME_PWME5=0;
PWMPRCLK=0X03;//分频为24M/8=3M
PWMCLK_PCLK5=1; //设SA为时钟源
PWMSCLA=10;//SA=3M/2/10=150K
PWMPOL_PPOL5=1; //上升沿翻转
PWMCAE_CAE5=0; //左对齐输出
PWMPER5=150; // 150K/150=1K
}
/*void PIT0_init() //PIT定时器初始化
{
PITCFLMT_PITE=1; //PIT使能
PITCE_PCE0=1; // 通道设置
PITMTLD0=120-1; //给PIT中的8位计数器赋初值 0~255 120分频 0.2MHZ 5US
PITLD0=PIT0TIME-1; //给PIT中的16位计数器赋初值 0~65535 0.1ms
PITINTE_PINTE0=1; //定时器0中断通道中断使能
PITCFLMT_PITE=1; //定时器0通道使能
} */
void pwm5_in(uchar pwm5)
{
PWMCNT5=0X00;//计数器清零
PWMDTY5=pwm5; //
PWME_PWME5=1; //使能
/*PWMDTY3=pwm3; //
PWMCNT3=0X00; //计数器清零
PWME_PWME3=1; //使能 */
}
void Port_init()
{
DDRE_DDRE2=1;
DDRE_DDRE3=1;
PORTE_PE2=1;
PORTE_PE3=0;
}
void read_AD()
{
// uchar i;
ad0_dat[0]=ATD0DR0L;
ad0_dat[1]=ATD0DR1L;
ad0_dat[2]=ATD0DR2L;
ad0_dat[3]=ATD0DR3L;
ad0_dat[4]=ATD0DR4L;
ad0_dat[5]=ATD0DR5L;
ad0_dat[6]=ATD0DR6L;
ad0_dat[7]=ATD0DR7L;
for(i=0;i<8;i++)
{
if(ad0_dat[i]>0X80)
{
ad0_dat[i]=1; //baiban
}
else ad0_dat[i]=0; //黑线
}
}
uint Get_position()
{
// uint i;
//if(Examine_cross()==1)
// return 7;
//if(zhixian()==1)
// return 7;
// else
//{
for(i=0;i<8;i++)
{
if(ad0_dat[i]==0)
{
p=p+2*i;
a++;
}
}
if(a>0)
{
return (p/a);
}
else return 14;
//}
}
void Change_direction(uint position)
{
switch(position)
{
case 0: pwm5_in(Minspeed);
break;
case 1:
pwm5_in(Minspeed);
break;
case 2:
pwm5_in(Minspeed);
break;
case 3:
pwm5_in(Minspeed);
break;
case 4:
pwm5_in(Lowspeed);
break;
case 5:
pwm5_in(Lowspeed);
break;
case 6:
pwm5_in(Highspeed);
break;
case 7:
pwm5_in(Maxspeed);
break;
case 8:
pwm5_in(Highspeed);
break;
case 9:
pwm5_in(Midspeed);
break;
case 10:
pwm5_in(Lowspeed);
break;
case 11:
pwm5_in(Lowspeed);
break;
case 12:
pwm5_in(Minspeed);
break;
case 13:
pwm5_in(Minspeed);
break;
case 14:
pwm5_in(Minspeed);
break;
default: pwm5_in(Minspeed);
}
}
void main(void)
{
init_PLL();
init_PWM();
//PIT0_init();
//pwm3_in(Stopspeed);
Port_init();
ATD_Init();
EnableInterrupts;
for(;;)
{
while(!(ATD0STAT0_SCF));
read_AD();
bianhua=Get_position();
//Change_direction(bianhua);
if((bianhua<=pt-2)||(bianhua>=pt+2)) //position>=3&&position<=11
{
Change_direction(bianhua); //zhuanxiang
if(bianhua!=14)
pt=bianhua ; //保留这一次的值跟下一次对比
}
else
Change_direction(pt);
}
}
/*pragma CODE_SEG __NEAR_SEG NON_BANKED //指示该程序在不分页区
void interrupt 66 PIT0(void) //中断服务程序 0.1ms进入一次中断
{
/////////////////////turnangel//////////////////////////////////////
while(!(ATD0STAT0_SCF));
read_AD();
bianhua=Get_position();
if((bianhua>=pt-3)&&(bianhua+3))
{
Change_direction(bianhua);
if(bianhua!=14)
pt=bianhua;
}
else
Change_direction(pt);
PITTF_PTF0=1; //请中断标志位
}
*/ |
|