今天简单的学习了一下串口通信原理,先简单的写了程序代码,规范不是很好,通过串口工具方便调试程序

#include "usart.h"
#include "stm32f4xx.h"
#include "led.h"
#include "stdio.h"

void usart1_init(uint32_t baud)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
	

	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9 | GPIO_Pin_10; 
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF; //复用模式
	GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //上拉模式
	GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //推挽输出
	GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed; //引脚响应速度
	GPIO_Init(GPIOA ,&GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = baud;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
	USART_InitStructure.USART_Parity = USART_Parity_No;  //无鸡偶校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位
	USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
	USART_Init(USART1, &USART_InitStructure);
	
	
	NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x1;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	
	USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
	
	USART_Cmd(USART1, ENABLE);

}


//发送一个字符
void Usart_SendByte(USART_TypeDef * pUSARTx , uint16_t ch)
{
	//发送一个字节数据到USARTx
	USART_SendData(pUSARTx,ch);
	
	//等待发送数据寄存器为空,TXE 位由硬件置 1,
	//它表示:USART_DR (数据寄存器)中可写入下一个数据,而不会覆盖前一个数据。
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

}



//调用 Usart_SendByte 函数发送字符串到数据寄存器
void Usart_SendString(USART_TypeDef * pUSARTx , char* str)
{
	uint16_t k = 0;
	
	do
	{
		Usart_SendByte(pUSARTx,*(str+k));
		k++;
	}while(*(str+k)!='\0');
	
}


//重定向c库函数printf到串口1,这样我们就可以使用printf函数打印输出信息到串口1了
int fputc(int ch, FILE *stream)
{
	//发送一个字节数据到串口1
	USART_SendData(USART1,(uint16_t)ch);
	
	//等待发送数据寄存器为空
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

	return ch;
}


// USART1中断服务函数
void USART1_IRQHandler()
{
	uint16_t recieve;
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		recieve = USART_ReceiveData(USART1);
		printf("我已经进中断了\r\n");
		if(recieve == '1')
		{
			//亮灯
            printf("我接收到数字1,进行亮灯\r\n");
            GPIO_ResetBits(GPIOF,GPIO_Pin_9);
            GPIO_ResetBits(GPIOF,GPIO_Pin_10);
		}
        if(recieve == '2')
		{
			//灭灯
            printf("我接收到数字2,进行灭灯\r\n");
            GPIO_SetBits(GPIOF,GPIO_Pin_9);
            GPIO_SetBits(GPIOF,GPIO_Pin_10);
		}
		
		
		
	}
	USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.