您的位置 首页 数字

STM32串口库函数版例程

定义:TXD1—–PA9-US1-TXRXD1—–PA10-US1-RX速率:115200,n,8,1/*Includes———————————

界说:
TXD1—– PA9-US1-TX
RXD1—– PA10-US1-RX

速率:115200,n,8,1

/* Includes ------------------------------------------------------------------*/#include "stm32f10x.h"#include "platform_config.h"#include "stm32f10x_usart.h"#include "misc.h"#include "stdarg.h"/* Private variables ---------------------------------------------------------*/USART_InitTypeDef USART_InitStructure;uint8_t TxBuffer1[] = "USART Interrupt Example: This is USART1 DEMO";  uint8_t RxBuffer1[],rec_f,tx_flag;__IO uint8_t TxCounter1 = 0x00;__IO uint8_t RxCounter1 = 0x00; uint32_t Rec_Len;/* Private function prototypes -----------------------------------------------*/void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void Delay(__IO uint32_t nCount);void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...);char *itoa(int value, char *string, int radix);void USART_Config(USART_TypeDef* USARTx);GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStruct;USART_ClockInitTypeDef USART_ClockInitStruct;/* 名    称:void ili9325_DrawPicture(u16 StartX,u16 StartY, u8 Dir,u8 *pic)* 功    能:在指定座标规模显现一副图片* 进口参数:StartX     行开始座标*           StartY     列开始座标*			Dir		   图画显现方向       *           pic        图片头指针* 出口参数:无* 说    明:图片取模格局为水平扫描,16位色彩形式  取模软件img2LCD* 调用办法:ili9325_DrawPicture(0,0,0,(u16*)demo);/void USART_Config(USART_TypeDef* USARTx){USART_InitStructure.USART_BaudRate = 115200;						//速率115200bpsUSART_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;					//收发形式/* Configure USART1 */USART_Init(USARTx, &USART_InitStructure);							//装备串口参数函数/* Enable USART1 Receive and Transmit interrupts */USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //使能接纳中止USART_ITConfig(USART1, USART_IT_TXE, ENABLE);						//使能发送缓冲空中止   /* Enable the USART1 */USART_Cmd(USART1, ENABLE);	}/* 名    称:int main(void)* 功    能:主函数* 进口参数:无* 出口参数:无* 说    明:* 调用办法:无 /int main(void){uint8_t a=0;/* System Clocks Configuration */RCC_Configuration();											  //体系时钟设置/*嵌套向量中止操控器 说明晰USART1抢占优先级等级0(最多1位) ,和子优先级等级0(最多7位) */ NVIC_Configuration();											  //中止源装备/*对操控LED指示灯的IO口进行了初始化,将端口装备为推挽上拉输出,口线速度为50Mhz。PA9,PA10端口复用为串口1的TX,RX。在装备某个口线时,首要应对它地点的端口的时钟进行使能。不然无法装备成功,因为用到了端口B, 因而要对这个端口的时钟进行使能,一起因为用到复用IO口功用用于装备串口。因而还要使能AFIO(复用功用IO)时钟。*/GPIO_Configuration();											  //端口初始化USART_Config(USART1);											  //串口1初始化USART_OUT(USART1,"测验串口 *\r\n");    	  //向串口1发送开机字符。USART_OUT(USART1,"*\r\n"); USART_OUT(USART1,"\r\n"); USART_OUT(USART1,"\r\n");    	while (1){if(rec_f==1){												  //判别是否收到一帧有用数据rec_f=0;USART_OUT(USART1,"\r\n您发送的信息为: \r\n");    USART_OUT(USART1,&TxBuffer1[0]);if(a==0) {GPIO_SetBits(GPIOB, GPIO_Pin_5); a=1;}          //LED1  V6(V3板) V2(MINI板) 明暗闪耀                else {GPIO_ResetBits(GPIOB, GPIO_Pin_5);a=0;  }}}}/* 名    称:void Delay(__IO uint32_t nCount)* 功    能:延时函数* 进口参数:无* 出口参数:无* 说    明:* 调用办法:无 /void Delay(__IO uint32_t nCount){for(; nCount != 0; nCount--);}/* 名    称:void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...)* 功    能:格局化串口输出函数* 进口参数:USARTx:  指定串口Data:   发送数组...:     不定参数* 出口参数:无* 说    明:格局化串口输出函数"\r"	回车符	   USART_OUT(USART1, "abcdefg\r")   "\n"	换行符	   USART_OUT(USART1, "abcdefg\r\n")"%s"	字符串	   USART_OUT(USART1, "字符串是:%s","abcdefg")"%d"	十进制	   USART_OUT(USART1, "a=%d",10)* 调用办法:无 /void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...){ const char *s;int d;char buf[16];va_list ap;va_start(ap, Data);while(*Data!=0){				                          //判别是否抵达字符串完毕符if(*Data==0x5c){									  //\switch (*++Data){case r:							          //回车符USART_SendData(USARTx, 0x0d);	   Data++;break;case n:							          //换行符USART_SendData(USARTx, 0x0a);	Data++;break;default:Data++;break;}}else if(*Data==%){									  //switch (*++Data){				case s:										  //字符串s = va_arg(ap, const char *);for ( ; *s; s++) {USART_SendData(USARTx,*s);while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);}Data++;break;case d:										  //十进制d = va_arg(ap, int);itoa(d, buf, 10);for (s = buf; *s; s++) {USART_SendData(USARTx,*s);while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);}Data++;break;default:Data++;break;}		 }else USART_SendData(USARTx, *Data++);while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);}}/整形数据转字符串函数char *itoa(int value, char *string, int radix)radix=10 标明是10进制	非十进制,转化成果为0;  例:d=-379;履行	itoa(d, buf, 10); 后buf="-379"							   			  /char *itoa(int value, char *string, int radix){int     i, d;int     flag = 0;char    *ptr = string;/* This implementation only works for decimal numbers. */if (radix != 10){*ptr = 0;return string;}if (!value){*ptr++ = 0x30;*ptr = 0;return string;}/* if this is a negative value insert the minus sign. */if (value < 0){*ptr++ = -;/* Make the value positive. */value *= -1;}for (i = 10000; i > 0; i /= 10){d = value / i;if (d || flag){*ptr++ = (char)(d + 0x30);value -= (d * i);flag = 1;}}/* Null terminate the string. */*ptr = 0;return string;} /* NCL_Itoa *//* 名    称:void RCC_Configuration(void)* 功    能:体系时钟装备为72MHZ, 外设时钟装备* 进口参数:无* 出口参数:无* 说    明:* 调用办法:无 / void RCC_Configuration(void){SystemInit(); RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 |RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO  , ENABLE);  }/* 名    称:void GPIO_Configuration(void)* 功    能:通用IO口装备* 进口参数:无* 出口参数:无* 说    明:* 调用办法:/  void GPIO_Configuration(void){GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				     //LED1操控--PB5GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;			 //推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);					 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;	         		 //USART1 TXGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    		 //复用推挽输出GPIO_Init(GPIOA, &GPIO_InitStructure);		    		 //A端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;	         	 //USART1 RXGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   	 //复用开漏输入GPIO_Init(GPIOA, &GPIO_InitStructure);		         	 //A端口 }/* 名    称:void NVIC_Configuration(void)* 功    能:中止源装备* 进口参数:无* 出口参数:无* 说    明:* 调用办法:无 /void NVIC_Configuration(void){/*  结构声明*/NVIC_InitTypeDef NVIC_InitStructure;/* Configure the NVIC Preemption Priority Bits */  /* Configure one bit for preemption priority *//* 优先级组 说明晰抢占优先级所用的位数,和子优先级所用的位数   在这里是1, 7 */    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);	  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;			     	//设置串口1中止NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;	     	//抢占优先级 0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;				//子优先级为0NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;					//使能NVIC_Init(&NVIC_InitStructure);}
///*            STM32F10x Peripherals Interrupt Handlers                        *////* @brief  This function handles USART1 global interrupt request.* @param  None* @retval : None*/void USART1_IRQHandler(void)      //串口1 中止服务程序{unsigned int i;if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)	   //判别读寄存器是否非空{	RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);   //将读寄存器的数据缓存到接纳缓冲区里if(RxBuffer1[RxCounter1-2]==0x0d&&RxBuffer1[RxCounter1-1]==0x0a)     //判别完毕标志是否是0x0d 0x0a{for(i=0; i< RxCounter1; i++) TxBuffer1[i]	= RxBuffer1[i]; 	     //将接纳缓冲器的数据转到发送缓冲区,预备转发rec_f=1;															 //接纳成功标志TxBuffer1[RxCounter1]=0;		                                     //发送缓冲区完毕符    RxCounter1=0;}}if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)                   //这段是为了防止STM32 USART 第一个字节发不出去的BUG { USART_ITConfig(USART1, USART_IT_TXE, DISABLE);					     //制止发缓冲器空中止, }	}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部