注册会员
- 积分
- 61
- 威望
- 45
- 贡献
- 16
- 兑换币
- 23
- 注册时间
- 2014-3-17
- 在线时间
- 0 小时
- 毕业学校
- 西北工业大学
|
5贡献
很简单的一段程序,但就是找不到错误……
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
/*电机控制IO口*/
#define STOP(x) (PTH_PTH5=(~x))
#define BREAK(x) (PTH_PTH7=(~x))
#define DIR(x) (PTH_PTH6=(x))
unsigned char i;
void DelayMs(int time)
{
int x,y;
for(x=0;x<4106;x++)
for(y=time;y>0;y--);
}
void PLL_Init() //16M
{
CLKSEL=0x00;
PLLCTL_PLLON=1;
SYNR=0x00 | 0x01;
REFDV=0x80 | 0x01;
POSTDIV=0x00;
_asm(nop);
_asm(nop);
while(!(CRGFLG_LOCK==1));
CLKSEL_PLLSEL =1;
}
void PWM_Init(){
PWMPRCLK=0x00; //clockA 不分频,clockA=busclock=16MHz;clockB 不分频 clockA=busclock=16MHz;
PWMSCLA = 8; //对clock SA 16分频, pwm clock=clockA/16=1MHz;
PWMSCLB = 8; //对clock SB 16分频,pwm clock=clockB/16=1MHz;
}
/*舵机测试*/
void SERVO_test(){
PWMCTL_CON45=1; //0和1联合成16位PWM;
PWMCAE_CAE5=0; //选择输出模式为左对齐输出模式
PWMCNT45 = 0; //计数器清零;
PWMPOL_PPOL5=1; //先输出高电平,计数到DTY时,反转电平
PWMCLK_PCLK5 = 1; //选择clock SA做时钟源
PWMPER45 = 20000; //周期20ms; 50Hz;
PWMDTY45= 1490; //高电平时间为1.5ms;居中
PWME_PWME5 = 1;
//下面这三个数值需视情况修改,每个车都不太一样
DelayMs(1000);
PWMDTY45=1620; //极左
DelayMs(1000);
PWMDTY45=1350; //极右
DelayMs(1000);
PWMDTY45=1490; //居中
}
/*电机测试*/
void MOTOR_test(){
PWMCTL_CON67=1; //2和3联合成16位PWM;
PWMCAE_CAE7=0; //选择输出模式为左对齐输出模式
PWMCNT67 = 0; //计数器清零;
PWMCNT7=0;
PWMPOL_PPOL7=1; //先输出高电平,计数到DTY时,反转电平
PWMCLK_PCLK7 = 1; //选择clock SB做时钟源
PWMPER67 = 20000; //周期20ms; 50Hz;
PWMPER7 = 100; //频率10kHz
PWMDTY67= 470;
PWMDTY7=0;
PWME_PWME7 = 1;
DDRH_DDRH5=1;
DDRH_DDRH6=1;
DDRH_DDRH7=1;
STOP(0);
BREAK(0);
DIR(1); //正转
for(i=0;i<=70;i++){
PWMDTY7=i;
DelayMs(20);
}
DelayMs(1000);
for(i=70;i>0;i--){
PWMDTY7=i;
DelayMs(20);
}
DIR(0); //反转
for(i=0;i<=70;i++){
PWMDTY7=i;
DelayMs(50);
}
DelayMs(1000);
for(i=70;i>0;i--){
PWMDTY7=i;
DelayMs(50);
}
PWMDTY7=0;
}
void main(void) {
/* put your own code here */
DDRB=0xFF;
PORTB=0xFF;
PWM_Init();
PLL_Init();
SERVO_test();
MOTOR_test();
//CCD_light_test();
EnableInterrupts;
PWMDTY7=50;
for(;;) {
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
} |
|