高校网站建设花费,管理课程培训视频教程全集,2018江苏省海门市建设局网站,一般网站建设多少钱USART 介绍串口发送使用工具初始化发送数据接收数据 介绍 电平标准是数据1和数据0的表达方式#xff0c;是传输线缆中人为规定的电压与数据的对应关系#xff0c;串口常用的电平标准有如下三种#xff1a; 
TTL电平#xff1a;3.3V或5V表示1#xff0c;0V表示0 RS232电平是传输线缆中人为规定的电压与数据的对应关系串口常用的电平标准有如下三种 
TTL电平3.3V或5V表示10V表示0 RS232电平-3-15V表示1315V表示0 RS485电平两线压差26V表示1-2-6V表示0差分信号 串口参数 波特率串口通信的速率 起始位标志一个数据帧的开始固定为低电平 数据位数据帧的有效载荷1为高电平0为低电平低位先行 校验位用于数据验证根据数据位计算得来 停止位用于数据帧间隔固定为高电平 USARTUniversal Synchronous/Asynchronous Receiver/Transmitter通用同步/异步收发器 USART是STM32内部集成的硬件外设可根据数据寄存器的一个字节数据自动生成数据帧时序从TX引脚发送出去也可自动接收RX引脚的数据帧时序拼接为一个字节数据存放在数据寄存器里 自带波特率发生器最高达4.5Mbits/s 可配置数据位长度8/9、停止位长度0.5/1/1.5/2 可选校验位无校验/奇校验/偶校验 支持同步模式、硬件流控制、DMA、智能卡、IrDA、LIN STM32F103C8T6 USART资源 USART1、 USART2、 USART3  串口发送 
使用工具 
USB TO TTL 
初始化 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //初始化USART外设
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //由于USART外设在GPIOA 所以初始化GPIOGPIO_InitTypeDef GPIO_InitStructure;		GPIO_InitStructure.GPIO_Mode  GPIO_Mode_AF_PP;  // TX 输出设置为复用推挽输出  这里只用到了发送 GPIO_InitStructure.GPIO_Pin  GPIO_Pin_9;GPIO_InitStructure.GPIO_Speed  GPIO_Speed_50MHz;GPIO_Init(GPIOA, GPIO_InitStructure);///usart初始化USART_InitTypeDef USART_InitStructure;USART_InitStructure.USART_BaudRate  9600;  // 波特率USART_InitStructure.USART_HardwareFlowControl  USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode  USART_Mode_Tx; //需要发送还是接收  都可以选择USART_InitStructure.USART_Parity  USART_Parity_No;  //校验位  不选择USART_InitStructure.USART_StopBits  USART_StopBits_1;  //停止位 1USART_InitStructure.USART_WordLength  USART_WordLength_8b; //不需要校验 所以字长选择8USART_Init(USART1, USART_InitStructure);//开启usartUSART_Cmd(USART1, ENABLE); 
发送数据 
void Serial_SendByte(uint8_t Byte)
{USART_SendData(USART1, Byte);  //发送数据byte 到 USARTDR   然后再发送给移位寄存器 最后一位一位的移出TX引脚while (USART_GetFlagStatus(USART1, USART_FLAG_TXE)  RESET);  //等待传送到移位寄存器   //不需要清零  在send data时自清零
} 
重定向printf 使得printf输出到串口 
int fputc(int ch, FILE *f)  //由于 fputc是printf的底层 所以修改foutc函数
{Serial_SendByte(ch);return ch;
} 
发送数组跟字符串 
void Serial_SendArray(uint8_t *Array, uint16_t Length)  //
{uint16_t i;for (i  0; i  Length; i )  {Serial_SendByte(Array[i]);}
}void Serial_SendString(char *String)
{uint8_t i;for (i  0; String[i] ! \0; i ){Serial_SendByte(String[i]);}
}接收数据 
初始化输入RX GPIO_InitStructure.GPIO_Mode  GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin  GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed  GPIO_Speed_50MHz;GPIO_Init(GPIOA, GPIO_InitStructure); 
接收 while (1){if (USART_GetITStatus(USART1, USART_IT_RXNE)  SET){RxData  USART_ReceiveData(USART1); OLED_ShowHexNum(2, 1, RxData,2);}
}