当前位置:   article > 正文

STM32F103RCT6学习笔记2:串口通信_stm32f103rct6语音播报全部代码

stm32f103rct6语音播报全部代码

今日开始快速掌握这款STM32F103RCT6芯片的环境与编程开发,有关基础知识的部分不会多唠,直接实践与运用!文章贴出代码测试工程与测试效果图:

 

目录

串口通信实验计划:

串口通信配置代码:

测试效果图:


串口通信实验计划:

配置初始化串口1,使用串口1周期性发送数据给上位机。

 

串口通信配置代码:

  1. #include "stdarg.h" //自定义printf需要使用
  2. #include "stdio.h" //1.61328125kb
  3. #include "USART1.h"
  1. #include "USART1.h"
  2. #define USART_REC_LEN 30
  3. u8 USART_RX_BUF[USART_REC_LEN] __attribute__ ((at(0X20001000)));
  4. //接收缓冲,最大USART_REC_LEN个字节,起始地址为0X20001000.
  5. u16 USART_RX_STA=0; //接收状态标记
  6. u16 USART_RX_CNT=0; //接收的字节数
  7. #if 1
  8. #pragma import(__use_no_semihosting)
  9. //标准库需要的支持函数
  10. struct __FILE
  11. {
  12. int handle;
  13. /* Whatever you require here. If the only file you are using is */
  14. /* standard output using printf() for debugging, no file handling */
  15. /* is required. */
  16. };
  17. /* FILE is typedef’ d in stdio.h. */
  18. FILE __stdout;
  19. //定义_sys_exit()以避免使用半主机模式
  20. void _sys_exit(int x)
  21. {
  22. x = x;
  23. }
  24. //重定向fputc函数
  25. //printf的输出,指向fputc,由fputc输出到串口
  26. //这里使用串口1(USART1)输出printf信息
  27. int fputc(int ch, FILE *f)
  28. {
  29. while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
  30. USART1->DR = (u8) ch;
  31. return ch;
  32. }
  33. #endif
  1. void uart1_init(u32 bound){
  2. //GPIO端口设置(初始化)
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. USART_InitTypeDef USART_InitStructure;
  5. NVIC_InitTypeDef NVIC_InitStructure;
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
  7. //使能USART1,GPIOA时钟
  8. //USART1_TX GPIOA.9
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  12. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  13. //USART1_RX GPIOA.10初始化
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  15. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  16. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  17. //Usart1 NVIC 配置
  18. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  22. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  23. //USART 初始化设置
  24. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  25. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  26. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  27. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  28. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  29. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  30. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  31. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  32. USART_Cmd(USART1, ENABLE); //使能串口1
  33. }
  1. #ifndef _USART1_H_
  2. #define _USART1_H_
  3. #include "headfire.h"
  4. void uart1_init(u32 bound);
  5. #endif
  1. int main(void)
  2. {
  3. uint16_t t;
  4. delay_init();
  5. NVIC_Configuration();
  6. uart1_init(115200);
  7. printf("welcome!\r\n");
  8. while(1)
  9. {
  10. printf("%d\r\n",t);
  11. t++;
  12. delay_ms(500);
  13. }
  14. }

测试效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/836009?site
推荐阅读
相关标签
  

闽ICP备14008679号