举酒邀月影先醉
2022-06-30 16:13
采纳率: 50%
浏览 40

串口中断接收后发送的数据不一样

问题遇到的现象和发生背景

stm32学习串口中断接收,自己写的程序总是接收后返回的其他值,后来在CSDN上找到一个程序,稍作修改后该程序是完美运行的,但是把代码移植到我建立的cubeMX的工程文件里就还是返回其他值,大概率是配置哪里出了问题,有没有人可以帮我看看

运行结果及报错内容

img

问题相关代码,请勿粘贴截图

主函数的代码如下:
uint8_t uart1RxState;
uint8_t uart1RxBu[100]={0};
uint8_t usart_chen[]="C++,stm32,java";
uint8_t *chen=usart_chen;
void SystemClock_Config(void);

int main(void)
{
uart1RxState=UART_RX_STATE_READY;//准备
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Transmit(&huart1,usart_chen,sizeof(usart_chen),0xff);
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);//开接收完成中断
while (1)
{
if(uart1RxState==UART_RX_STATE_DEAL)
{
HAL_UART_Transmit(&huart1 ,uart1RxBu,uart1RxCounter,0xff);
memset(uart1RxBuf,0,sizeof(uart1RxBu));
uart1RxState=UART_RX_STATE_READY;
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);//开接收完成中断
}

}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error




```_Handler();
  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

void Error_Handler(void)
{
  __disable_irq();
  while (1)
  {
  }
}
void assert_failed(uint8_t *file, uint32_t line)
{
}
USART的代码如下
#include "usart.h"
extern uint8_t uart1RxState;
extern uint8_t uart1RxBuf[100];
uint8_t uart1RxCounter=0;
UART_HandleTypeDef huart1;
void MX_USART1_UART_Init(void)
{
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
    __HAL_RCC_USART1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  }
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
    __HAL_RCC_USART1_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  }
}

void userUart1Handler()
{
    if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE)!=RESET))
    {
            if(uart1RxState==UART_RX_STATE_READY)   //接收到一帧中的第一个字节
            {
                   uart1RxState=UART_RX_STATE_START;
                  __HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);   //打开空闲中断
                  uart1RxCounter=0;                             //计数清零
                  uart1RxBu[uart1RxCounter]=(uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF);
                  uart1RxCounter++;
            }
            else if(uart1RxState==UART_RX_STATE_START)  
            {
                  uart1RxBuf[uart1RxCounter]=(uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF);
                  uart1RxCounter++;
            }
            __HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_RXNE);
          
    }
    if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)!=RESET))  //进入空闲中断
    {          
          __HAL_UART_DISABLE_IT(&huart1,UART_IT_IDLE);    //关了空闲中断
          __HAL_UART_DISABLE_IT(&huart1,UART_IT_RXNE);    //关了接收完成中断
          uart1RxState=UART_RX_STATE_DEAL;               
    }
}
其中userUart1Handler()是自己设定的中断回调,这个函数被放在了USART1_IRQHandler()里,同时屏蔽了userUart1Handler()。

其他的一些参数定义
#define UART_RX_STATE_READY 0  //准备
#define UART_RX_STATE_START 1  //开始
#define UART_RX_STATE_DEAL  2  //完成    


###### 我的解答思路和尝试过的方法 
最开始我是用回调函数写的,主函数while上写的receive_it来使能接收中断,回调函数直接判断是否是这个usart然后直接transmit,但是也是返回的不正确。
###### 我想要达到的结果
请求看看问题出在哪里,肯定是我哪里出问题了


1条回答 默认 最新

相关推荐 更多相似问题