⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 34

📁 电脑鼠走迷宫程序
💻
📖 第 1 页 / 共 4 页
字号:
    if (iL >= 500) {
        GPIOPinWrite(GPIO_PORTD_BASE,
                     __PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
                     0x00);
    }
    /*
     *  如果右电机长时间停止,则断电
     */
    if (__GmRight.cState == __MOTORSTOP) {
        iR++;
    } else {
        iR = 0;
    }
    if (iR >= 500) {
        GPIOPinWrite(GPIO_PORTD_BASE,
                     __PHRA1 | __PHRA2 | __PHRB1 | __PHRB2,
                     0x00);
    }
    /*
     *  红外线检测
     */
    __irCheck();
}


/*********************************************************************************************************
** 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 {
            __GucDistance[__FRONTR]  = 0x01;
        }
        if (ucIRCheck & __FRONTSIDE_L) {
            __GucDistance[__FRONTL]  = 0x00;
        } else {
            __GucDistance[__FRONTL]  = 0x01;
        }
        break;

    case 2:
        __irSendFreq(36000, 2);                                         /*  驱动检测左前右三个方向远距  */
        break;
        
    case 3:
        ucIRCheck = GPIOPinRead(GPIO_PORTB_BASE, 0x2a);                 /*  读取传感器状态              */
        PWMGenDisable(PWM_BASE, PWM_GEN_2);                             /*  禁止PWM发生器2              */
        break;

    case 4:
        __irSendFreq(36000, 2);                                         /*  重复检测左前右三个方向远距  */
        break;
        
    case 5:
        ucIRCheck &= GPIOPinRead(GPIO_PORTB_BASE, 0x2a);                /*  读取传感器状态              */
        PWMGenDisable(PWM_BASE, PWM_GEN_2);                             /*  禁止PWM发生器2              */
        if (ucIRCheck & __RIGHTSIDE) {
            __GucDistance[__RIGHT] &= 0xfe;
        } else {
            __GucDistance[__RIGHT] |= 0x01;
        }
        if (ucIRCheck & __LEFTSIDE) {
            __GucDistance[__LEFT]  &= 0xfe;
        } else {
            __GucDistance[__LEFT]  |= 0x01;
        }
        if (ucIRCheck & __FRONTSIDE) {
            __GucDistance[__FRONT] &= 0xfe;
        } else {
            __GucDistance[__FRONT] |= 0x01;
        }
        break;

    default:
        break;
    }
    ucState = (ucState + 1) % 6;                                        /*  循环检测                    */
}


/*********************************************************************************************************
** Function name:       mouseTurnright
** Descriptions:        右转
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseTurnright(void)
{
    while (__GmLeft.cState  != __MOTORSTOP);
    while (__GmRight.cState != __MOTORSTOP);
    /*
     *  开始右转
     */
     __delay(100);
    __GucMouseState   = __TURNRIGHT;
    __GmRight.cDir    = __MOTORGOBACK;
    __GmRight.uiPulse = 53;
    __GmRight.iSpeed = 65;
    
    __GmLeft.cDir     = __MOTORGOAHEAD;
    __GmLeft.uiPulse  = 54;
    __GmLeft.iSpeed = 65;
    __GmRight.cState  = __MOTORRUN;
    __GmLeft.cState   = __MOTORRUN;
    GucMouseDir     = (GucMouseDir + 1) % 4;                            /*  方向标记                    */
    while (__GmLeft.cState  != __MOTORSTOP);
    while (__GmRight.cState != __MOTORSTOP);
    __mazeInfDebug();
    __delay(110000);
}


/*********************************************************************************************************
** Function name:       mouseTurnleft
** Descriptions:        左转
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseTurnleft(void)
{
  __delay(100);
    while (__GmLeft.cState  != __MOTORSTOP);
    while (__GmRight.cState != __MOTORSTOP);
    /*
     *  开始左转
     */
    __GucMouseState   = __TURNLEFT;
    __GmRight.cDir    = __MOTORGOAHEAD;
    __GmRight.uiPulse = 53;
    __GmRight.iSpeed = 65;
    
    __GmLeft.cDir     = __MOTORGOBACK;
    __GmLeft.uiPulse  = 54;
     __GmLeft.iSpeed = 65;
    
    __GmRight.cState  = __MOTORRUN;
    __GmLeft.cState   = __MOTORRUN;
    GucMouseDir     = (GucMouseDir + 3) % 4;                            /*  方向标记                    */
    while (__GmLeft.cState  != __MOTORSTOP);
    while (__GmRight.cState != __MOTORSTOP);
    __mazeInfDebug();
    __delay(110000);
}


/*********************************************************************************************************
** Function name:       mouseTurnback
** Descriptions:        后转
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseTurnback(void)
{
    /*
     *  等待停止
     */
    while (__GmLeft.cState  != __MOTORSTOP);
    while (__GmRight.cState != __MOTORSTOP);
    /*
     *  开始后转
     */
    __GucMouseState   = __TURNBACK;
    __GmRight.cDir    = __MOTORGOBACK;
    __GmRight.uiPulse = 112;//162 * 10;
     __GmRight.iSpeed = 100;
    
    __GmLeft.cDir     = __MOTORGOAHEAD;
    __GmLeft.uiPulse  = 111;//162 * 10;
     __GmLeft.iSpeed = 100;
    __GmLeft.cState   = __MOTORRUN;
    __GmRight.cState  = __MOTORRUN;
    GucMouseDir = (GucMouseDir + 2) % 4;                                /*  方向标记                    */
    while (__GmLeft.cState  != __MOTORSTOP);
    while (__GmRight.cState != __MOTORSTOP);
    __mazeInfDebug();
    __delay(110000);
}


/*********************************************************************************************************
** Function name:       __mouseCoorUpdate
** Descriptions:        根据当前方向更新坐标值
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void __mouseCoorUpdate (void)
{
    switch (GucMouseDir) {

    case 0:
        GmcMouse.cY++;
        break;

    case 1:
        GmcMouse.cX++;
        break;

    case 2:
        GmcMouse.cY--;
        break;

    case 3:
        GmcMouse.cX--;
        break;

    default:
        break;
    }
    __mazeInfDebug();
    __wallCheck();
}


/*********************************************************************************************************
** Function name:       __wallCheck
** Descriptions:        根据传感器检测结果判断是否存在墙壁
** input parameters:    无
** output parameters:   无
** Returned value:      cValue: 低三位从左到右一次代表左前右。1为有墙,0为没墙。
*********************************************************************************************************/
void __wallCheck (void)
{
    uint8 ucMap = 0;
    ucMap |= MOUSEWAY_B;
    
    if (__GucDistance[__LEFT]  & 0x01) {
        ucMap &= ~MOUSEWAY_L;
    }else {
        ucMap |=  MOUSEWAY_L;
    }
    if (__GucDistance[__FRONT] & 0x01) {
        ucMap &= ~MOUSEWAY_F;
    }else {
        ucMap |=  MOUSEWAY_F;
    }
    if (__GucDistance[__RIGHT] & 0x01) {
        ucMap &= ~MOUSEWAY_R;
    }else {
        ucMap |=  MOUSEWAY_R;
    }
    if (GucMapBlock[GmcMouse.cX][GmcMouse.cY] == 0x00) {
        GucMapBlock[GmcMouse.cX][GmcMouse.cY] = ucMap;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -