通过学习USART1深入STM32F107VCT6的串口通信
例:
#include "stm32f10x.h"
#include "stm32_eval.h"
#include USART_InitTypeDef USART_InitStructure; //定义结构体类型变量 void GPIO_Configuration(void); //声明GPIO配置函数 #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) //此处定义为putchar应用 #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif { SystemInit(); //配置系统时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE); //打开APB2功能时钟(UART1为连接在APB2上的高速外设)开启了串口时钟和复用功能时钟 GPIO_Configuration(); //调用GPIO配置函数 USART_InitStructure.USART_BaudRate = 115200; //设置USART传输波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置USART传输数据位一帧为8位 USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置USART传输每帧一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No; //设置USART无奇偶校验 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //设置USART无硬件流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//开启USART发送和接受功能 USART_Init(USART1, &USART_InitStructure); //初始化USART1设置 USART_Cmd(USART1, ENABLE); //开启USART1 printf(" 李继超是个好人吗? "); printf(" 回答:李继超是个大好人! "); printf(" 菏泽是个是个美丽的地方! "); printf(" 发生了什么?你是猴子请来的救兵吗? "); printf(" 嗯!李继超的确是个大好人!!!你才魔道呢!哼!!! "); //配置输出数据 while (1) { } } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; //定义结构体变量类型 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开GPIOA的功能时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //选择GPIO引脚GPIO_Pin_9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置GPIO速率 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO_pin_9为 复用功能 推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO_Pin_9设置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选择GPIO引脚GPIO_Pin_10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置GPIO_Pin_10浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO_Pin_10设置 } PUTCHAR_PROTOTYPE //重定义printf函数 { USART_SendData(USART1, (uint8_t) ch); //发送字符串 while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)// 检测是否发送完成 {} return ch; } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t* file, uint32_t line) { while (1) {} } #endif
int main(void)
评论