|
求助:帮忙看看自己写的MSP430f149红外避障的程序,哪里错误了
[复制链接]
帮忙看下写的430f149的红外避障程序哪里错误了,电机不能够转。但是自己也不到哪里错了。希望有经验的大神帮帮忙
#include "msp430x14x.h"
#define uint unsigned int
#define uchar unsigned char
/*****************时钟初始化**********/
void int_clk (void)
{
uchar i;
BCSCTL1 &=~XT2OFF; //打开XT振荡器
BCSCTL2 |=SELM1+SELS; //MCLK 8M and SMCLK 1M
do
{
IFG1 &= ~OFIFG; //清除振荡错误标志
for(i = 0; i < 100; i++)
_NOP(); //延时等待
}
while (IFG1 & OFIFG) ; //如果标志为1继续循环等待
IFG1 &=~OFIFG;
}
/********定时器初始化********/
void init_timer (void)
{
TBCTL = TBSSEL0+TBCLR; // ACLK, 清除 TAR
TBCTL =MC_1 + TBIE; // 系统辅助时钟ACLK增计数模式不分频中断
TBCCR0 = 400; // PWM周期约12ms
}
/**********************延时nus**************/
void Delay_nus(unsigned char n) //延时n个us
{
while(n--)
{
_NOP();
}
}
/*******************延时nms****************/
void Delay_nms(unsigned char n) //延时n个ms
{
unsigned char i;
while(n--)
{
for(i = 0; i< 250; i++)
{
Delay_nus(4);
}
}
}
/********************车轮转向************************************/
void Forward(void) //前进
{
TBCCR0=400;
TBCCTL1|= OUTMOD_7;
TBCCR1 = 200;
TBCCTL2|= OUTMOD_7;
TBCCR2 = 0;
TBCCTL3|= OUTMOD_7;
TBCCR3 = 200;
TBCCTL4|= OUTMOD_7;
TBCCR4 = 0;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
void Back(void) //后退
{ TBCTL = TBSSEL0 + TBCLR;
TBCCR0=400;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 0;
TBCCTL2 = OUTMOD_7;
TBCCR2 =200;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 0;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 200;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
void Left(void) //左转
{ TBCTL = TBSSEL0 + TBCLR;
TBCCR0=400;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 100;
TBCCTL2 = OUTMOD_7;
TBCCR2 = 0;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 300;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 0;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
void Right(void) //右转
{ TBCTL = TBSSEL0 + TBCLR;
TBCCR0=400;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 300;
TBCCTL2 = OUTMOD_7;
TBCCR2 = 0;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 100;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 0;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
void Left_Back(void) //左后转
{ TBCTL = TBSSEL0 + TBCLR;
TBCCR0=400;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 0;
TBCCTL2 = OUTMOD_7;
TBCCR2 = 100;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 0;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 300;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
void Right_Back(void) //右后转
{ TBCTL = TBSSEL0 + TBCLR;
TBCCR0=400;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 0;
TBCCTL2 = OUTMOD_7;
TBCCR2 = 300;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 0;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 100;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
void Stop(void) //停止
{ TBCTL = TBSSEL0 + TBCLR;
TBCCR0=400;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 0;
TBCCTL2 = OUTMOD_7;
TBCCR2 = 0;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 0;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 0;
P4DIR |= 0x1e; // P4.1- P4.4 输出
P4SEL |= 0x1e; // P4.1- P4.4 TBx
}
/*****************主函数********************/
void main( void )
{
WDTCTL=WDTPW+WDTHOLD;
int_clk();
init_timer();
_EINT();
while(1)
{
P2SEL=0x00; //作为中断
P2DIR=0xDC; //P2.0 P2.1 P2.5输入
P2IE|=0xff; //P2中断允许
P2IES|=0xff; //下降沿触发
if(P2IFG==0x23)
{
Back(); //检测到三边都有障碍物,首先小车倒退一段距离
P2IFG=0x00;
}
else
if (P2IFG==0x03)
{
Forward(); //检测到检测到右边和左边都有障碍物,小车前进
P2IFG=0x00;
}
else
if(P2IFG==0x21)
{
Right_Back(); //检测到左边和前边都有障碍物,小车右后退
P2IFG=0x00;
}
else
if(P2IFG==0x01)
{
Right(); //检测到左边有障碍物,小车右转
P2IFG=0x00;
}
else
if(P2IFG==0x22)
{
Left_Back(); //检测到右边和前边都有障碍物,小车左后转
P2IFG=0x00;
}
else
if(P2IFG==0x02)
{
Left(); //检测到右边有障碍物,小车左转
P2IFG=0x00;
}
else
if(P2IFG==0x20)
{
Back(); //检测到前边有障碍物,小车后腿
P2IFG=0x00;
}
else
Forward(); //没有检测到障碍物前进。
}
}
|
|