|
求大神指导
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define zk1 PWMDTY0 //左电机前进
#define zk2 PWMDTY1 //左电机后退
#define zk3 PWMDTY2 //右电机前进
#define zk4 PWMDTY3 //右电机后退
#define zk5 PWMDTY4 //杆电机上升
#define zk6 PWMDTY5 //杆电机下降
volatile int count0,count1;
volatile int flag_0=1,flag_1=1;
void delay(int i)
{
int j;
for(;i>0;i--)
for(j=0;j<3200;j++);
}
void PLL_init(void )
{
CLKSEL=0x00;
PLLCTL_PLLON=1;
SYNR=0xc0|0x04; //SYNR=0xc0|0x04;
REFDV=0x80|0x01;
POSTDIV=0x00;
_asm(nop);
_asm(nop);
while(!(CRGFLG_LOCK==1));
CLKSEL_PLLSEL=1;
}
void PIT_init(void) //0.15s
{
PITCFLMT=0x00; //关闭PIT模块
PITCE_PCE0=1; // PIT1通道时能
PITMUX=0x00; //采用微时间基准
PITMTLD0=200-1;
PITLD0=30000-1; //设置16位递减计数器载入初值 Time_out_period=(PITMTLD+1)x(PITLD+1)/fbus 30000
PITINTE_PINTE0=1; //PIT1通道中断使能
// PITCFLMT=0x80; //使能PIT模块
PITCFLMT_PITE=0 ;
}
void MotorPWM0_Init(void)
{
PWME_PWME0=0;
//PWMCTL=0x00; 不级联
PWMCAE_CAE0=0; //0通道左对齐
PWMCNT0= 0; //初始计数值为0
PWMPOL_PPOL0=1; //0通道高电平
PWMPRCLK=0x03; //A时钟8分频,0,1,4,5 A时钟
PWMSCLA=5; //一般10K,=5 , PWMSCLA=30;
PWMCLK_PCLK0= 1;//0通道SA时钟
PWMPER0=100; //0周期寄存器100
PWME_PWME0=1; //打开0通道
}
void MotorPWM1_Init(void)
{
PWME_PWME1=0;
PWMCAE_CAE1=0;
PWMCNT1= 0;
PWMPOL_PPOL1=1;
PWMPRCLK=0x03;
PWMSCLA=5; //PWMSCLA=30;
PWMCLK_PCLK1= 1;
PWMPER1=100;
PWME_PWME1=1;
}
void MotorPWM2_Init(void)
{
PWME_PWME2=0;
// PWMCTL=0x00;
PWMCAE_CAE2=0;
PWMCNT2= 0;
PWMPOL_PPOL2=1;
PWMPRCLK=0x30;//B时钟8分频 =0x30;
PWMSCLB=5;
PWMCLK_PCLK2= 1;
PWMPER2=100;
PWME_PWME2=1;
}
void MotorPWM3_Init(void)
{
PWME_PWME3=0;
PWMCAE_CAE3=0;
PWMCNT3= 0;
PWMPOL_PPOL3=1;
PWMPRCLK=0x30;
PWMSCLB=5;
PWMCLK_PCLK3= 1;
PWMPER3=100;
PWME_PWME3=1;
}
/*----------------------------------------------------------
快速前进(近似直线)zk1=41;zk2=0;
zk3=50;zk4=0;
慢速前进(近似直线)zk1=33;zk2=0;
zk3=40;zk4=0;
后退(近似直线) zk1=0;zk2=41;
zk3=0;zk4=55;
拐弯(90) zk1=0; zk2=40;
zk3=40; zk4=0;
delay(1800);
-----------------------------------------------------------*/
void main(void)
{
PLL_init();
MotorPWM0_Init();
MotorPWM1_Init();
MotorPWM2_Init();
MotorPWM3_Init();
DDRA=0x00;
EnableInterrupts;
PIT_init();
while(1)
{
switch(PORTA)
{
case 0b000:{
PITCFLMT_PITE=1; //打开中断
if(flag_0==1) //0~6s
{
zk1=50;zk2=0;//zk1=50
zk3=50;zk4=0;
}
else if(flag_0==0) //>6s
{
PITCFLMT_PITE=0;//关闭中断
zk1=0;zk2=41;
zk3=0;zk4=55;
delay(600);//后退
}
} break;
case 0b001:{
PITCFLMT_PITE=1; //打开中断
if(flag_1==1) //0~3s
{
zk1=0;zk2=41;
zk3=0;zk4=0; //左轮后退,摆正角度,
}
else if(flag_1==0) //>3s
{
PITCFLMT_PITE=0;//关闭中断
zk1=0;zk2=41;
zk3=0;zk4=55;
delay(200);//后退
zk1=0;zk2=41;
zk3=0;zk4=0; delay(100);//左轮后退,摆正角度
}
} break;
case 0b111:{
zk1=0;zk2=0;zk3=0;zk4=0;
zk5=50; zk6=0; delay(4000+500+200);//提球
zk1=0; zk2=30;
zk3=0; zk4=40; delay(200); //后退,防止挡住
zk1=0; zk2=0;
zk3=0; zk4=0; //停止
zk5=0; zk6=50; delay(3500+500);//下
zk5=0; zk6=0; //提球停止
zk1=0; zk2=41;// 左轮后撤
zk3=0; zk4=55; delay(350);//右轮后撤
zk1=40; zk2=0;
zk3=0; zk4=30; delay(1800);//右轮转弯,右拐
}
case 0b100:{
PITCFLMT_PITE=1; //打开中断
if(flag_1==1) //0~3s
{
zk1=0;zk2=0;
zk3=0;zk4=30; //右轮后退,摆正角度,
}
else if(flag_1==0) //>3s
{
PITCFLMT_PITE=0;//关闭中断
zk1=0;zk2=41;
zk3=0;zk4=55;
delay(200);//后退
zk1=0;zk2=0;
zk3=0;zk4=30; delay(100);//右轮后退,摆正角度?
}
} break;
}
}
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 66 PIT0Interrupt(void)
{
PITTF_PTF0=1;
count0++;
count1++;
if(count0<40) //0~6s
{
flag_0=1; //6s内中断标志位
}
else if(count0>=40)//>6s
{
flag_0=0; //大于6s中断标志位
count0=0; //清零
}
else if(count1<20)//0~3s
{
flag_1=1; // 3s内中断标志位
}
else if(count1>=20)
{
flag_1=0; //大于3s中断标志位
count1=0; //清零
}
}
#pragma CODE_SEG DEFAULT
帮忙看一看有什么问题啊!为什么中断不行啊,进入case后,怎么关闭中断啊???是不是有逻辑问题啊???
|
|