您的位置 首页 设计

Stm32串口发送字节数据

说明:每发送一次数据,LED等闪烁一下/**说明:*PA0:KEY1;PA1:KEY2;*PA2:LED1;PA3:LED2;*PA9:USART1_TX;PA10:USART1_RX*/#inclu

阐明:每发送一次数据,LED等闪耀一下

/**阐明:*PA0:KEY1;PA1:KEY2;*PA2:LED1;PA3:LED2;*PA9:USART1_TX;PA10:USART1_RX*/#include "stm32f10x.h"#include "stm32f10x_rcc.h"#include "stm32f10x_gpio.h"#include "stm32f10x_usart.h"#include "system_stm32f10x.h"#include "stdio.h"#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) void RCC_Configuration(void);void GPIO_Configuration(void); void delay_ms(u16 time);void USART_Configuration(void);int main(){SystemInit();RCC_Configuration();GPIO_Configuration(); USART_Configuration();while(1)                                           //循环逐字输出,到完毕字\0{printf("\n发送的信息:");GPIO_SetBits(GPIOA,GPIO_Pin_2);USART_SendData(USART1, 0);                              //发送字符或许:USART1->DR = 0;while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){} //等候字符发送完毕//while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){} //等候接纳完毕//变量= (USART_ReceiveData(USART1)); //承受一个字节delay_ms(1000);GPIO_ResetBits(GPIOA,GPIO_Pin_2);}}void RCC_Configuration(void)  {   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);   } void GPIO_Configuration(void)  {  GPIO_InitTypeDef GPIO_InitStructure;  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;             //管脚9GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       //复用推挽输出GPIO_Init(GPIOA, &GPIO_InitStructure);                //TX初始化GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;            //管脚10GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;         //上拉输入GPIO_Init(GPIOA, &GPIO_InitStructure);                //RX初始化GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//LED GPIO_Pin_All  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOA, &GPIO_InitStructure); } void USART_Configuration(void)//串口初始化函数{//串口参数初始化  USART_InitTypeDef USART_InitStructure;               //串口设置康复默许参数//初始化参数设置USART_InitStructure.USART_BaudRate = 9600;                  //波特率9600USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位USART_InitStructure.USART_StopBits = USART_StopBits_1;      //1位中止字节USART_InitStructure.USART_Parity = USART_Parity_No;         //无奇偶校验USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流操控USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//翻开Rx接纳和Tx发送功用USART_Init(USART1,&USART_InitStructure);                   //初始化USART_Cmd(USART1,ENABLE);                                  //发动串口}void delay_ms(u16 time)   {  u16 i=0;  while(time--)   {  i=12000;  while(i--);  }  } int putchar(int c)                                            //putchar函数{if (c == \n){putchar(\r);}                                 //将printf的\n变成\rUSART_SendData(USART1, c);                                   //发送字符while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){} //等候发送完毕return c;                                                      //返回值}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/sheji/277466.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部