STM32F1串口配置步骤

步骤如下所示:
步骤一:使能串口时钟及GPIO端口时钟
步骤二:GPIO端口模式设置,设置串口对应的引脚为复用功能
步骤三:初始化串口参数,包含波特率、字长、奇偶校验等参数
步骤四:使能串口
步骤五:设置串口中断类型并使能
步骤六:设置串口中断优先级,使能串口中断通道
步骤七:编写串口中断服务函数

#include "sys.h"
#include "usart.h"	  
// 	 
//如果使用ucos,则包括下面的头文件即可.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h"					//ucos 使用	  
#endif 
 

unsigned char ucRxFinish=0;
static unsigned char ucCnt=0,ucLen=0;
unsigned char ucRxData[100];

//
//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  
#if 1
#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
_sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{      
	while((USART1->SR&0X40)==0);//循环发送,直到发送完毕   
    USART1->DR = (u8) ch;      
	return ch;
}
#endif 

/*使用microLib的方法*/
 /* 
int fputc(int ch, FILE *f)
{
	USART_SendData(USART1, (uint8_t) ch);

	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}	
   
    return ch;
}
int GetKey (void)  { 

    while (!(USART1->SR & USART_FLAG_RXNE));

    return ((int)(USART1->DR & 0x1FF));
}
*/
 
#if EN_USART1_RX   //如果使能了接收
//串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误   	
u8 USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15,	接收完成标志
//bit14,	接收到0x0d
//bit13~0,	接收到的有效字节数目
u16 USART_RX_STA=0;       //接收状态标记	  
  
void uart_init(u32 bound)
{
    //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//使能USART1,GPIOA时钟
     //USART1_TX   PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    //USART1_RX	  PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);  

   //Usart1 NVIC 配置

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
  
   //USART 初始化设置

	USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_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;	//收发模式

    USART_Init(USART1, &USART_InitStructure); //初始化串口
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
    USART_Cmd(USART1, ENABLE);                    //使能串口 

}

void USART1_IRQHandler(void)                	//串口1中断服务程序
{
	u8 Res;
#ifdef OS_TICKS_PER_SEC	 	//如果时钟节拍数定义了,说明要使用ucosII了.
	OSIntEnter();    
#endif
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
	{
		Res =USART_ReceiveData(USART1);//(USART1->DR);	//读取接收到的数据
		
		if((USART_RX_STA&0x8000)==0)//接收未完成
		{
			if(USART_RX_STA&0x4000)//接收到了0x0d
			{
				if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
				else USART_RX_STA|=0x8000;	//接收完成了 			//bit31表明是否接收到0x0a(\n)
			}
			else //还没收到0X0D
			{	
				if(Res==0x0d)USART_RX_STA|=0x4000;						//bit30表明是否接收到0x0d(\r)
				else
				{
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收	  
				}		 
			}
		}   		 
   } 
#ifdef OS_TICKS_PER_SEC	 	//如果时钟节拍数定义了,说明要使用ucosII了.
	OSIntExit();  											 
#endif
} 
#endif	






void USART2_Init(u32 bound){
    //GPIO端口设置
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	//使能USART2,GPIOA时钟
 	USART_DeInit(USART2);  //复位串口2
	//USART2_TX   PA.2
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
	GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
   
    //USART2_RX	  PA.3
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//浮空输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA10

   //Usart2 NVIC 配置

	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
  
   //USART 初始化设置
	USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_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;	//收发模式

	USART_Init(USART2, &USART_InitStructure); //初始化串口
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
	USART_Cmd(USART2, ENABLE);                    //使能串口 

}




void USART2_IRQHandler(void)                	//串口1中断服务程序
{
	unsigned char temp=0;
  u16 C=0;

//	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  
//		{
//		  temp=USART_ReceiveData(USART2);
//			
//			if(ucCnt==0)
//					ucRxData[ucCnt++]=temp;
//		  else	if((ucCnt==1)&(temp==0x03))
//					ucRxData[ucCnt++]=temp;
//			else if(ucCnt==2)
//					{ucRxData[ucCnt++]=temp; ucLen=ucRxData[2];}
//			else if((ucCnt>2)&(ucCnt<=(ucLen+4)))
//					ucRxData[ucCnt++]=temp;
//			if(ucCnt==(ucLen+5))
//			{  C=ModbusCRC(ucRxData,ucLen+3);
//				if(C==((ucRxData[ucLen+3]<<8)|ucRxData[ucLen+4]))
//					{ucRxFinish=1;	ucCnt=0;ucLen=0;}			
//				else 
//					{ucCnt=0;ucLen=0;}				
//			}
//			
//		}
	  USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}   		 
 
	

void USART2_Send(unsigned char *data,uint8_t ucLen)
{
	uint8_t i;
	USART_ClearFlag(USART2,USART_FLAG_TC);
	for(i=0;i<ucLen;i++)
	{
		USART_SendData(USART2,*(data+i));
		while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET);
	}
}

uint16_t ModbusCRC(uint8_t *ptr,uint16_t ucLen)//CRC校验
{
	uint8_t i;
	uint16_t j,crc=0xffff;
	uint16_t n;
	i=i;	
	
	for(n=0;n<ucLen;n++)
	{
		crc=ptr[n]^crc;
		for(i=0;i<8;i++)
		if(crc&0x01)
		{
			crc=crc>>1;
			crc=crc^0xa001;
		}
		else
		{
			crc=crc>>1;	
		}		
	}

	j=crc>>8;
	j=j|(crc<<8);
	return j;

}







void usart3_init(u32 bound)          //串口初始化函数
{  

	 GPIO_InitTypeDef GPIO_InitStructure;
	 USART_InitTypeDef USART_InitStructure;
	 NVIC_InitTypeDef NVIC_InitStructure;
// 串口时钟使能    
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // GPIOB时钟
         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口3时钟使能
//串口复位
         USART_DeInit(USART3);  //复位串口3
//USART3_TX    GPIOB10
	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    //PB10
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
	  GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
 //USART3_RX   GPIOB11
	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;   //PB11
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
	  GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
 //USART 初始化设置
	  USART_InitStructure.USART_BaudRate = bound;//串口波特率
	  USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	  USART_InitStructure.USART_StopBits = USART_StopBits_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; //收发模式
   	  USART_Init(USART3, &USART_InitStructure); //初始化串口3
//Usart3  NVIC 配置
	 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;       //串口3的中断
	 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
	 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;     //子优先级3
	 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;       //IRQ通道使能
	 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//开启中断	 
         USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
//使能串口    
      	  USART_Cmd(USART3, ENABLE);                    //使能串口3 
  }

  void USART3_IRQHandler(void)                 //串口3中断服务程序
 {
 	u8 ReceiveData ;
 	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //接收中断
  	{
		  ReceiveData =USART_ReceiveData(USART3); //读取接收到的数据
	}
}
 

int main(void)
{		
    uint16_t c=14;
//	Init_HX711pin();
	delay_init();
	
	NVIC_Configuration(); 	 //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
//    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
	uart_init(9600);	 //串口初始化
    USART2_Init(9600);
	usart3_init(9600);    //波特率设置为9600
//	Get_Maopi();				//称毛皮重量
	delay_ms(1000);
	delay_ms(1000);
//	Get_Maopi();				//重新获取毛皮重量
	
	while(1)
	{
//		Get_Weight();

//		printf("净重量 = %d g\r\n",Weight_Shiwu); //打印 
//		delay_ms(1000);
//        
        USART2_Send("9999",4);
        delay_ms(1000);

        USART_SendData(USART3,68);    //串口发送数据

	}
}

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
配置 STM32F1串口1 时,你需要在 main 函数中进行以下步骤: 1. 首先,确保已经打开了 USART1 的时钟。你可以使用 `RCC_APB2PeriphClockCmd()` 函数来实现,例如:`RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);`。 2. 配置 USART1 的引脚。根据你的需求,选择相应的引脚作为 USART1 的 TX 和 RX 引脚,并将它们配置为复用功能。你可以使用 `GPIO_Init()` 函数来实现,例如: ```c GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // USART1 TX 引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // USART1 RX 引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); ``` 3. 配置 USART1 的参数,例如波特率、数据位、停止位和校验位等。你可以使用 `USART_Init()` 函数来实现,例如: ```c USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; // 波特率 USART_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; // 同时接收和发送 USART_Init(USART1, &USART_InitStructure); ``` 4. 启用 USART1,你可以使用 `USART_Cmd()` 函数来实现,例如:`USART_Cmd(USART1, ENABLE);`。 至此,你已经完成了串口1的配置。接下来,你可以使用 `USART_SendData()` 和 `USART_ReceiveData()` 函数来发送和接收数据。 注意:以上代码片段仅供参考,具体的配置参数和引脚设置需要根据你的实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值