查看: 805|回复: 0
打印 上一主题 下一主题

STM32 F103基础学习笔记06(串口通讯 RS232 485)

[复制链接]
跳转到指定楼层
沙发
发表于 2016-5-7 22:08:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
********************
众想STM32 串口通讯 RS232

********************




入门还是靠 众想科技 的STM32 视频。再看了 其他视频之后(质量普遍低,基本没看完),可以更加确认,众想STM32 视频 更适合初学者,但是 开发板 外观看起来 一般。

最近又回头复习了一下 C51,发现同样的外设,达到同样理解程度,用 C51 学习 至少省 一半的精力。 换句话话,学 STM32 同时,同时学习外设, 属于 难难联合。

*********************************

不过, uart 在 C51 通过寄存器操作,而 在 STM32 中通过 库函数 则 好像 更简单。

比如 96 N 8 1 设置,
  • 通过函数配置很简单
  •         USART_InitStructure.USART_BaudRate=9600;
  •         USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  •         USART_InitStructure.USART_StopBits=USART_StopBits_1;
  •         USART_InitStructure.USART_Parity=USART_Parity_No;
  •         USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;

[color=rgb(51, 102, 153) !important]复制代码


核心内容是在 串口中断 中配置

  • void USART1_IRQHandler(void)
  • {
  •    if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
  •    {
  •                 USART_SendData(USART1,USART_ReceiveData(USART1));
  •                 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
  •    }
  • }

[color=rgb(51, 102, 153) !important]复制代码


这个例子还是非常 简单 清晰的

  • int main(void)
  • {
  •    RCC_Configuration();        //系统时钟初始化
  •    GPIO_Configuration();//端口初始化
  •    USART_Configuration();
  •    NVIC_Configuration();
  •    while(1);
  • }

[color=rgb(51, 102, 153) !important]复制代码


也可以借助 STM32CUBEMX 来辅助生产代码



  • void MX_GPIO_Init(void)
  • {
  •   GPIO_InitTypeDef GPIO_InitStruct;
  •   /** USART1 GPIO Configuration
  •   PA9   ------> USART1_TX
  •   PA10   ------> USART1_RX
  •   */
  •   /*Enable or disable APB2 peripheral clock */
  •   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  •   /*Configure GPIO pin : PA */
  •   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
  •   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  •   GPIO_Init(GPIOA, &GPIO_InitStruct);
  • }

[color=rgb(51, 102, 153) !important]复制代码


代码没有 设置 输出 和输入 模式,

TX 复用 推挽输出,RX 悬空输入。
  •         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
  •         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  •         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  •         GPIO_Init(GPIOA,&GPIO_InitStructure);
  •         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
  •         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  •         GPIO_Init(GPIOA,&GPIO_InitStructure);

[color=rgb(51, 102, 153) !important]复制代码


*****************************

在 uart 设置上, STM32 相对还是容易的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表