<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32串口庫函數(shù)版例程

          STM32串口庫函數(shù)版例程

          作者: 時(shí)間:2016-11-09 來源:網(wǎng)絡(luò) 收藏
          定義:
          TXD1----- PA9-US1-TX
          RXD1----- PA10-US1-RX

          速率:115200,n,8,1

          本文引用地址:http://www.ex-cimer.com/article/201611/318038.htm

          /* 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)* 功    能:在指定座標(biāo)范圍顯示一副圖片* 入口參數(shù):StartX     行起始座標(biāo)*           StartY     列起始座標(biāo)*			Dir		   圖像顯示方向       *           pic        圖片頭指針* 出口參數(shù):無* 說    明:圖片取模格式為水平掃描,16位顏色模式  取模軟件img2LCD* 調(diào)用方法: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;		//數(shù)據(jù)位8位USART_InitStructure.USART_StopBits = USART_StopBits_1;			//停止位1位USART_InitStructure.USART_Parity = USART_Parity_No;				//無校驗(yàn)位USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //無硬件流控USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					//收發(fā)模式/* Configure USART1 */USART_Init(USARTx, &USART_InitStructure);							//配置串口參數(shù)函數(shù)/* Enable USART1 Receive and Transmit interrupts */USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //使能接收中斷USART_ITConfig(USART1, USART_IT_TXE, ENABLE);						//使能發(fā)送緩沖空中斷   /* Enable the USART1 */USART_Cmd(USART1, ENABLE);	}/* 名    稱:int main(void)* 功    能:主函數(shù)* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 /int main(void){uint8_t a=0;/* System Clocks Configuration */RCC_Configuration();											  //系統(tǒng)時(shí)鐘設(shè)置/*嵌套向量中斷控制器 說明了USART1搶占優(yōu)先級(jí)級(jí)別0(最多1位) ,和子優(yōu)先級(jí)級(jí)別0(最多7位) */ NVIC_Configuration();											  //中斷源配置/*對(duì)控制LED指示燈的IO口進(jìn)行了初始化,將端口配置為推挽上拉輸出,口線速度為50Mhz。PA9,PA10端口復(fù)用為串口1的TX,RX。在配置某個(gè)口線時(shí),首先應(yīng)對(duì)它所在的端口的時(shí)鐘進(jìn)行使能。否則無法配置成功,由于用到了端口B, 因此要對(duì)這個(gè)端口的時(shí)鐘進(jìn)行使能,同時(shí)由于用到復(fù)用IO口功能用于配置串口。因此還要使能AFIO(復(fù)用功能IO)時(shí)鐘。*/GPIO_Configuration();											  //端口初始化USART_Config(USART1);											  //串口1初始化USART_OUT(USART1,"測(cè)試串口 *rn");    	  //向串口1發(fā)送開機(jī)字符。USART_OUT(USART1,"*rn"); USART_OUT(USART1,"rn"); USART_OUT(USART1,"rn");    	while (1){if(rec_f==1){												  //判斷是否收到一幀有效數(shù)據(jù)rec_f=0;USART_OUT(USART1,"rn您發(fā)送的信息為: rn");    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)* 功    能:延時(shí)函數(shù)* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 /void Delay(__IO uint32_t nCount){for(; nCount != 0; nCount--);}/* 名    稱:void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...)* 功    能:格式化串口輸出函數(shù)* 入口參數(shù):USARTx:  指定串口Data:   發(fā)送數(shù)組...:     不定參數(shù)* 出口參數(shù):無* 說    明:格式化串口輸出函數(shù)"r"	回車符	   USART_OUT(USART1, "abcdefgr")   "n"	換行符	   USART_OUT(USART1, "abcdefgrn")"%s"	字符串	   USART_OUT(USART1, "字符串是:%s","abcdefg")"%d"	十進(jìn)制	   USART_OUT(USART1, "a=%d",10)* 調(diào)用方法:無 /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){				                          //判斷是否到達(dá)字符串結(jié)束符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:										  //十進(jìn)制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);}}/整形數(shù)據(jù)轉(zhuǎn)字符串函數(shù)char *itoa(int value, char *string, int radix)radix=10 標(biāo)示是10進(jìn)制	非十進(jìn)制,轉(zhuǎn)換結(jié)果為0;  例:d=-379;執(zhí)行	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)* 功    能:系統(tǒng)時(shí)鐘配置為72MHZ, 外設(shè)時(shí)鐘配置* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 / 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口配置* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:/  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;    		 //復(fù)用推挽輸出GPIO_Init(GPIOA, &GPIO_InitStructure);		    		 //A端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;	         	 //USART1 RXGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   	 //復(fù)用開漏輸入GPIO_Init(GPIOA, &GPIO_InitStructure);		         	 //A端口 }/* 名    稱:void NVIC_Configuration(void)* 功    能:中斷源配置* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 /void NVIC_Configuration(void){/*  結(jié)構(gòu)聲明*/NVIC_InitTypeDef NVIC_InitStructure;/* Configure the NVIC Preemption Priority Bits */  /* Configure one bit for preemption priority *//* 優(yōu)先級(jí)組 說明了搶占優(yōu)先級(jí)所用的位數(shù),和子優(yōu)先級(jí)所用的位數(shù)   在這里是1, 7 */    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);	  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;			     	//設(shè)置串口1中斷NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;	     	//搶占優(yōu)先級(jí) 0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;				//子優(yōu)先級(jí)為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 中斷服務(wù)程序{unsigned int i;if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)	   //判斷讀寄存器是否非空{(diào)	RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);   //將讀寄存器的數(shù)據(jù)緩存到接收緩沖區(qū)里if(RxBuffer1[RxCounter1-2]==0x0d&&RxBuffer1[RxCounter1-1]==0x0a)     //判斷結(jié)束標(biāo)志是否是0x0d 0x0a{for(i=0; i< RxCounter1; i++) TxBuffer1[i]	= RxBuffer1[i]; 	     //將接收緩沖器的數(shù)據(jù)轉(zhuǎn)到發(fā)送緩沖區(qū),準(zhǔn)備轉(zhuǎn)發(fā)rec_f=1;															 //接收成功標(biāo)志TxBuffer1[RxCounter1]=0;		                                     //發(fā)送緩沖區(qū)結(jié)束符    RxCounter1=0;}}if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)                   //這段是為了避免STM32 USART 第一個(gè)字節(jié)發(fā)不出去的BUG { USART_ITConfig(USART1, USART_IT_TXE, DISABLE);					     //禁止發(fā)緩沖器空中斷, }	}


          關(guān)鍵詞: STM32串口庫函

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();