📄 main.c
字号:
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
PHLA2);
break;
case 4: /* A1B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
PHLA2 | PHLB2);
break;
case 5: /* B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
PHLB2);
break;
case 6: /* A2B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
PHLA1 | PHLA2 | PHLB2);
break;
case 7: /* A2 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
PHLA1 | PHLA2);
break;
default:
break;
}
}
/*********************************************************************************************************
** Function name: speedContrR
** Descriptions: 右电机速度调节
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void speedContrR (void)
{
int32 iDPusle;
iDPusle = GmRight.uiPulse - GmRight.uiPulseCtr; /* 统计电机还剩余的步数 */
if (iDPusle <= GmRight.iSpeed) {
GmRight.iSpeed--;
} else { /* 非减速区间,则加速到最大值 */
if (GmRight.iSpeed < GiMaxSpeed) {
GmRight.iSpeed++;
} else {
GmRight.iSpeed--;
}
}
if (GmRight.iSpeed < 0) { /* 设置速度下限 */
GmRight.iSpeed = 0;
}
TimerLoadSet(TIMER0_BASE, TIMER_A, GuiAccelTable[GmRight.iSpeed]); /* 设置定时时间 */
}
/*********************************************************************************************************
** Function name: speedContrL
** Descriptions: 左电机速度调节
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void speedContrL (void)
{
int32 iDPusle;
iDPusle = GmLeft.uiPulse - GmLeft.uiPulseCtr; /* 统计电机还剩余的步数 */
if (iDPusle <= GmLeft.iSpeed) {
GmLeft.iSpeed--;
} else { /* 非减速区间,则加速到最大值 */
if (GmLeft.iSpeed < GiMaxSpeed) {
GmLeft.iSpeed++;
}
}
if (GmLeft.iSpeed < 0) { /* 设置速度下限 */
GmLeft.iSpeed = 0;
}
TimerLoadSet(TIMER1_BASE,TIMER_A,GuiAccelTable[GmLeft.iSpeed]); /* 设置定时时间 */
}
/*********************************************************************************************************
** Function name: Timer0A_ISR
** Descriptions: Timer0中断服务函数
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void Timer0A_ISR(void)
{
static int8 n = 0,m = 0;
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 清除定时器0中断。 */
switch (GmRight.cState) {
case MOTORSTOP: /* 停止,同时清零速度和脉冲值 */
GmRight.iSpeed = 0;
GmRight.uiPulse = 0;
GmRight.uiPulseCtr = 0;
break;
case WAITONESTEP: /* 暂停一步 */
GmRight.cState = MOTORRUN;
break;
case MOTORRUN: /* 电机运行 */
if (GucMouseState == GOAHEAD) { /* 根据传感器状态微调电机位置 */
if (GucDistance[FRONTL] && (GucDistance[FRONTR] == 0)) { /* 左前方有障碍,且前方无挡板 */
if (n == 1) {
GmRight.cState = WAITONESTEP; /* 下一次电机暂停 */
}
n++;
n %= 2;
} else {
n = 0;
}
if ((GucDistance[RIGHT] == 1) && (GucDistance[LEFT] == 0)) {/* 离右挡板太远,且左方无挡板 */
if(m == 3) {
GmRight.cState = WAITONESTEP; /* 下一次电机暂停 */
}
m++;
m %= 6;
} else {
m = 0;
}
}
rightMotorContr(); /* 推动电机转动一步 */
break;
default:
break;
}
/*
* 是否完成任务判断
*/
if (GmRight.cState != MOTORSTOP) {
GmRight.uiPulseCtr++; /* 运行脉冲计数 */
speedContrR(); /* 速度调节 */
if (GmRight.uiPulseCtr >= GmRight.uiPulse) {
GmRight.cState = MOTORSTOP;
GmRight.uiPulseCtr = 0;
GmRight.uiPulse = 0;
GmRight.iSpeed = 0;
}
}
}
/*********************************************************************************************************
** Function name: Timer1A_ISR
** Descriptions: Timer1中断服务函数
** input parameters: GmLeft.cState :驱动步进电机的时序状态
** GmLeft.cDir :步进电机运动的方向
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void Timer1A_ISR(void)
{
static int8 n = 0, m = 0;
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT); /* 清除定时器1中断。 */
switch (GmLeft.cState) {
case MOTORSTOP: /* 停止,同时清零速度和脉冲值 */
GmLeft.iSpeed = 0;
GmLeft.uiPulse = 0;
GmLeft.uiPulseCtr = 0;
break;
case WAITONESTEP: /* 暂停一步 */
GmLeft.cState = MOTORRUN;
break;
case MOTORRUN: /* 电机运行 */
if (GucMouseState == GOAHEAD) { /* 根据传感器状态微调电机位置 */
if (GucDistance[FRONTR] &&(GucDistance[FRONTL]==0)) {
if (n == 1) {
GmLeft.cState = WAITONESTEP;
}
n++;
n %= 2;
} else {
n = 0;
}
if ((GucDistance[LEFT] == 1) && (GucDistance[RIGHT] == 0)) {
if(m == 3) {
GmLeft.cState = WAITONESTEP;
}
m++;
m %= 6;
} else {
m = 0;
}
}
leftMotorContr(); /* 电机驱动程序 */
break;
default:
break;
}
/*
* 是否完成任务判断
*/
if (GmLeft.cState != MOTORSTOP) {
GmLeft.uiPulseCtr++; /* 运行脉冲计数 */
speedContrL(); /* 速度调节 */
if (GmLeft.uiPulseCtr >= GmLeft.uiPulse) {
GmLeft.cState = MOTORSTOP;
GmLeft.uiPulseCtr = 0;
GmLeft.uiPulse = 0;
GmLeft.iSpeed = 0;
}
}
}
/*********************************************************************************************************
** Function name: irSendFreq
** Descriptions: 发送红外线。
** input parameters: uiFreq: 红外线调制频率
** cNumber: 选择需要设置的PWM模块
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void irSendFreq (uint32 uiFreq, int8 cNumber)
{
uiFreq = SysCtlClockGet() / uiFreq;
switch (cNumber) {
case 1:
PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, uiFreq); /* 设置PWM发生器1的周期 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, uiFreq / 2); /* 设置PWM2输出的脉冲宽度 */
PWMGenEnable(PWM_BASE, PWM_GEN_1); /* 使能PWM发生器1 */
break;
case 2:
PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, uiFreq); /* 设置PWM发生器2的周期 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_4, uiFreq / 2); /* 设置PWM4输出的脉冲宽度 */
PWMGenEnable(PWM_BASE, PWM_GEN_2); /* 使能PWM发生器2 */
break;
default:
break;
}
}
/*********************************************************************************************************
** Function name: irCheck
** Descriptions: 红外线传感器检测。
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void irCheck (void)
{
static uint8 ucState = 0;
static uint8 ucIRCheck;
switch (ucState) {
case 0:
irSendFreq(32200, 2); /* 探测左右两侧近距 */
irSendFreq(35000, 1); /* 驱动斜角上的传感器检测 */
break;
case 1:
ucIRCheck = GPIOPinRead(GPIO_PORTB_BASE, 0x3e); /* 读取传感器状态 */
PWMGenDisable(PWM_BASE, PWM_GEN_2); /* 禁止PWM发生器2 */
PWMGenDisable(PWM_BASE, PWM_GEN_1); /* 禁止PWM发生器1 */
if (ucIRCheck & RIGHTSIDE) {
GucDistance[RIGHT] &= 0xfd;
} else {
GucDistance[RIGHT] |= 0x02;
}
if (ucIRCheck & LEFTSIDE) {
GucDistance[LEFT] &= 0xfd;
} else {
GucDistance[LEFT] |= 0x02;
}
if (ucIRCheck & FRONTSIDE_R) {
GucDistance[FRONTR] = 0x00;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -