STM32外设————USART

今天简单记录一下串口,其中原理不多赘述直接开始配置

1、开启GPIO和USART1的时钟

2、GPIO和USART1的结构体配置

3、NVIC 中断配置

4、串口接收中断开启

5、NVIC中断分组

6、USART1开启

#define	BaudRate	9600

/**
  * 函    数:初始化函数
  * 参    数:none
  * 返 回 值:none
  */
void usart1_init()
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
	
	GPIO_InitTypeDef	GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitTypeDef	USART_InitStructure;
	USART_InitStructure.USART_BaudRate = BaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Tx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructure);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef	NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1,ENABLE);
	
}

/**
  * 函    数:发送一个字节
  * 参    数:data
  * 返 回 值:none
  */
void usart_SendBits(uint8_t data)
{
	USART_SendData(USART1,data);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}

/**
  * 函    数:发送一个数组
  * 参    数:待发送数组首地址
  * 参    数:数组长度
  * 返 回 值:none
  */
void usart_SendArry(uint8_t *arry, uint8_t length)
{
	uint16_t i = 0;
	for (i=0;i

你可能感兴趣的:(stm32,单片机,嵌入式硬件)