標(biāo)題: STM32藍(lán)牙控制小車(chē)沒(méi)反應(yīng),求大家?guī)臀铱纯茨挠绣e(cuò)誤 [打印本頁(yè)]

作者: 加油小鴨    時(shí)間: 2022-5-26 14:26
標(biāo)題: STM32藍(lán)牙控制小車(chē)沒(méi)反應(yīng),求大家?guī)臀铱纯茨挠绣e(cuò)誤
#include <stdio.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx.h>
#include "motor.h"
#include "bt.h"
void usart1_init(void)
{
       
        GPIO_InitTypeDef  GPIO_InitStruct;                //        IO 口結(jié)構(gòu)體
        USART_InitTypeDef USART_InitStruct;                //        串口結(jié)構(gòu)體
        NVIC_InitTypeDef NVIC_InitStruct;                //        NVIC 結(jié)構(gòu)體
                RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
       
       

        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
       

        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;                                                //        復(fù)用功能
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA,&GPIO_InitStruct);


        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);                //        使能串口1
        USART_InitStruct.USART_BaudRate = 9600;//        波特率 9600
        USART_InitStruct.USART_WordLength=USART_WordLength_8b; //        數(shù)據(jù)幀 數(shù)據(jù)位        USART_WordLength_8b
        USART_InitStruct.USART_StopBits=USART_StopBits_1;//        指定停止位長(zhǎng)度 USART_StopBits_1
        USART_InitStruct.USART_Parity=USART_Parity_No; //        指定校驗(yàn)方式 USART_Parity_No
        USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //        指定串口模式 USART_Mode_Rx|USART_Mode_Tx 收發(fā)模式
        USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //        指定硬件控制流 USART_HardwareFlowControl
       
        USART_Init(USART1,&USART_InitStruct);
       

                NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn ;                        //        指定中斷通道 USART1_IRQn
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 3;        //        搶占優(yōu)先級(jí) 3
                NVIC_InitStruct.NVIC_IRQChannelSubPriority =4 ;                //        中斷服務(wù)優(yōu)先級(jí) 4
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;                        //        使能中斷
                NVIC_Init(&NVIC_InitStruct);
               

          USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
        USART_Cmd(USART1,ENABLE);
}
u8 Res;
void USART1_IRQHandler(void)
{
        if(USART_GetFlagStatus(USART1,USART_IT_RXNE) != RESET)
        {
                USART_ClearITPendingBit(USART1,  USART_IT_RXNE);
                Res = USART_ReceiveData(USART1);
            if (Res == 1)               
                {
                        move_front();
                }
                else if (Res == 2)               
                {
                        move_back();
       
                }
                else if (Res == 3)               
                {
                        move_left();
               
                }
                else if (Res == 4)               
                {
                        move_back();
       
                }
                else if (Res == 5)               
                {
                        move_stop();
                }
               


        }
}


#include "stm32f4xx.h"
#include "delay.h"
#include "motor.h"
#include "bt.h"

int main()       
       
{
          usart1_init();
          motor_init();
          
        while(1)
                {  
                                                USART1_IRQHandler();
                }
       
}



作者: 不要黑    時(shí)間: 2022-5-27 09:56
debug 模式看一下,
USART1_IRQHandler();不應(yīng)該寫(xiě)到while(1)吧。

作者: qinkaidragon    時(shí)間: 2022-5-27 14:44
USART1_IRQHandler()是STM32中斷入口函數(shù),UART1中斷產(chǎn)生后會(huì)自動(dòng)跳轉(zhuǎn)到中斷函數(shù)入口執(zhí)行。
作者: 加油小鴨    時(shí)間: 2022-5-28 21:41
不要黑 發(fā)表于 2022-5-27 09:56
debug 模式看一下,
USART1_IRQHandler();不應(yīng)該寫(xiě)到while(1)吧。

現(xiàn)在改了還是沒(méi)反應(yīng)
void USART1_IRQHandler(void)
{
        if(USART_GetFlagStatus(USART1,USART_IT_RXNE) != RESET)
        {
               
                Res = USART_ReceiveData(USART1);
                USART_ClearITPendingBit(USART1,  USART_IT_RXNE);
        }
}
void bt_ctl(void)
{
while (1)
        {
               
        switch (Res)
                {
                case 0:led_ctrl(LED1, ON);
                       
                break;
                case 1:led_ctrl(LED2, ON);
               
                break;
                }
               
        }
}
int main()       
       
{
          usart1_init();

          led_GPIO_init();

          
        while(1)
                {  
                       
           bt_ctl();
                       
}
       
}
作者: 加油小鴨    時(shí)間: 2022-5-28 21:42
qinkaidragon 發(fā)表于 2022-5-27 14:44
USART1_IRQHandler()是STM32中斷入口函數(shù),UART1中斷產(chǎn)生后會(huì)自動(dòng)跳轉(zhuǎn)到中斷函數(shù)入口執(zhí)行。

把他改掉了還是沒(méi)反應(yīng)
作者: 不要黑    時(shí)間: 2022-5-30 11:01
加油小鴨 發(fā)表于 2022-5-28 21:42
把他改掉了還是沒(méi)反應(yīng)

復(fù)用時(shí)鐘沒(méi)有開(kāi)吧,引腳的配置呢?




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1