青与鱼博客

@钦与鱼

January 14th 2019 / 玩玩编程

stm32重定向printf到串口

首先需要包含#include
然后初始化以下代码

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//使用串口发送出去

  return ch;
}

阅读更多......

January 8th 2019 / 玩玩编程

stm32使用esp8266实现远端控制

多说无益:看看代码吧
链接:https://pan.baidu.com/s/11_-cmupXYedzom0d7212Ew
提取码:ab86
代码逻辑也很简单

  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
    if(hasData=='t')//
    {
        dealData();//处理数据
    }
    char requestL[20]="AT+CIPSEND=";//

    openTCP(&huart3);//打开连接
    HAL_Delay(1000);
    char *strl;
    strl=itoa(strlen(request),10);
    strcat(requestL,strl);
    strcat(requestL,"\r\n");

    sendCmd(&huart3,requestL);
  HAL_Delay(500);
    sendCmd(&huart3,request);//发送请求内容
    HAL_Delay(500);
        if(hasData=='t')
    {
        dealData();
    }
    //closeTCP(&huart3);//
    HAL_Delay(1000);
  }

  /* USER CODE END 3 */

}

January 7th 2019 / 玩玩编程

cubemx配置uart透传

配置完成后,生成的代码如图

其中需要修改的是用户代码
首先msp和it已经配置好了,不需要操作。
main.c里
先添加两个buff

char u1rxbuff[1],u3rxbuff[1];

然后开始接收数据,这个代码是不会帮你生成的

    HAL_UART_Receive_IT(&huart1, (uint8_t *)u1rxbuff, 1);
    HAL_UART_Receive_IT(&huart3, (uint8_t *)u3rxbuff, 1);

再加一个就收完成的代码就行了

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//
{
    if(huart->Instance==USART1)//
    {
            HAL_UART_Transmit(&huart3,(uint8_t *)u1rxbuff,1,0xffff);//将数据发出去
      HAL_UART_Receive_IT(&huart1, (uint8_t *)u1rxbuff, 1);//重新开始接收
    }
    else if(huart->Instance==USART3)//
    {
       HAL_UART_Transmit(&huart1,(uint8_t *)u3rxbuff,1,0xffff);
       HAL_UART_Receive_IT(&huart3, (uint8_t *)u3rxbuff, 1);
    }

}
x