|
- #ifndef _DEBUGUSART_H_
- #define _DEBUGUSART_H_
- #include <stm32f10x.h>
- #include<stdio.h>
- #define USE_USART 3
- #if(USE_USART==1)
- #define DEBUG_USART USART1
- #define DEBUGRX_PORT GPIOA
- #define DEBUGRX_PIN GPIO_Pin_10//RX
- #define DEBUGTX_PORT GPIOA
- #define DEBUGTX_PIN GPIO_Pin_9//TX
- #define DEBUG_USART_IRQn USART1_IRQn
- #define DEBUG_RCC_APBnPeriphClockCmd() RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- #endif
- #if (USE_USART==3)
- #define DEBUG_USART USART3
- #define DEBUGRX_PORT GPIOD
- #define DEBUGRX_PIN GPIO_Pin_9//RX
- #define DEBUGTX_PORT GPIOD
- #define DEBUGTX_PIN GPIO_Pin_8//TX
- #define DEBUG_USART_IRQn USART3_IRQn
- #define DEBUG_RCC_APBnPeriphClockCmd() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);\
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);\
- GPIO_PinRemapConfig(GPIO_FullRemap_USART3,ENABLE);
- #endif
- void UARTGPIO_config(void);//串口IO口配置
- void USART_config(u32 baudrate);//串口初始化
- void UARTNVIC_config(void);//配置中斷
- int fputc(int ch, FILE *f);//發(fā)送數(shù)據(jù)
- int fgetc(FILE *f); // 接收數(shù)據(jù)
- int USART_Scanf_Name(char * str);
- void DEBUGUSART(u32 baudrate);
- #endif
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include"DEBUG_USART.H"
- void DEBUGUSART(u32 baudrate)
- {
- USART_config(baudrate); //串口初始化19200
- UARTGPIO_config(); //串口IO口配置
- }
- void UARTGPIO_config(void)//串口IO口配置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //GPIO_StructInit(&GPIO_InitStructure);//用默認(rèn)值初始化結(jié)構(gòu)體為所有端口2M浮空輸入
- GPIO_InitStructure.GPIO_Pin = DEBUGTX_PIN;//PA9作為US1的TX端,負(fù)責(zé)發(fā)送數(shù)據(jù)
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//復(fù)用推挽輸出模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//2M
- GPIO_Init(DEBUGTX_PORT,&GPIO_InitStructure);//用以上值初始化PA9口
- GPIO_InitStructure.GPIO_Pin = DEBUGRX_PIN;//PA10作為US1的RX端,負(fù)責(zé)接收數(shù)據(jù)
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//2M
- GPIO_Init(DEBUGRX_PORT, &GPIO_InitStructure);//用以上值初始化PA10口
- }
-
- void USART_config(u32 baudrate)//串口初始化
- {
- USART_InitTypeDef USART_InitStructure;
- DEBUG_RCC_APBnPeriphClockCmd();
- USART_StructInit(&USART_InitStructure);//將結(jié)構(gòu)體設(shè)置為缺省狀態(tài),9600bps,8數(shù)據(jù)位,1停止位,不校驗(yàn),硬件流控制失能
- USART_InitStructure.USART_BaudRate =baudrate;//波特率設(shè)置為19200
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//一幀數(shù)據(jù)的寬度設(shè)置為8bits
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//在幀結(jié)尾傳輸1個(gè)停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;//奇偶失能模式,無奇偶校驗(yàn)
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//發(fā)送/接收使能
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件流控制失能
- USART_Init(DEBUG_USART, &USART_InitStructure);//設(shè)置串口1
- //USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);//打開串口1的接收中斷響應(yīng)函數(shù)
- USART_Cmd(DEBUG_USART, ENABLE);//打開串口1
- }
- void UARTNVIC_config()//配置中斷
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//選擇中斷分組1
- NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQn;//選擇串口1中斷
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//搶占式中斷優(yōu)先級(jí)設(shè)置為0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//響應(yīng)式中斷優(yōu)先級(jí)設(shè)置為3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中斷
- NVIC_Init(&NVIC_InitStructure);
- }
- /*
- void DEBUG_USART_IRQHandler(void)//中斷方式的USART接收
- {
- if (USART_GetITStatus(DEBUG_USART, USART_IT_RXNE) != RESET) //判斷發(fā)生接收中斷
- {//USART_ClearITPendingBit(DEBUG_USART, USART_IT_RXNE); //清除中斷標(biāo)志,讀接收到的數(shù)據(jù)時(shí)自動(dòng)清除
- USART_SendData(DEBUG_USART, USART_ReceiveData(DEBUG_USART));//將數(shù)據(jù)回送至電腦
- while(USART_GetFlagStatus(DEBUG_USART, USART_FLAG_TC) == RESET);//等待數(shù)據(jù)發(fā)送完畢
- }
- }*/
- /*
- if (USART_GetFlagStatus(DEBUG_USART, USART_FLAG_RXNE) != RESET) //判斷接收標(biāo)志;查詢方式的USART接收
- {
- USART_ReceiveData(DEBUG_USART);
- }
- USART_SendData(DEBUG_USART, i);//將數(shù)據(jù)回送至電腦
- while(USART_GetFlagStatus(DEBUG_USART, USART_FLAG_TC) == RESET);//等待數(shù)據(jù)發(fā)送完畢
- */
- int fputc(int ch, FILE *f)//發(fā)送數(shù)據(jù)
- {
- USART_SendData(DEBUG_USART, (unsigned char) ch);//
- while (!(DEBUG_USART->SR & USART_FLAG_TXE));
- return (ch);
- }
-
- int fgetc(FILE *f) // 接收數(shù)據(jù)
- {
- while (!(DEBUG_USART->SR & USART_FLAG_RXNE));
- return ((int)(DEBUG_USART->DR & 0x1FF));
- }
- //如果要使用printf函數(shù)除了#include<stdio.h>和重定義兩個(gè)輸入輸出函數(shù)外還應(yīng)該Target->Code Generation中勾選Use MicroLIB
- //char ch='E',str[10]={"FGHIJKLM"};
- //int intd=12345;
- //printf("The is a example!\n");
- //printf("%c\n%s\n%d\n%c\n%s\n%d\n%d\n",'A',"BCD",0x23,ch,str,ch,intd);
- //The is a example!
- //A
- //BCD
- //35
- //E
- //FGHIJKLM
- //69
- //12345
- /*******************************************************************************
- * Function Name : USART_Scanf_Name
- * Description : Gets Char values from the hyperterminal.
- * Input : None
- * Output : None
- * Return : Length
- *******************************************************************************/
- int USART_Scanf_Name(char * str)
- {
- u16 index = 0;
- USART_ClearFlag(DEBUG_USART,USART_FLAG_RXNE);//清一下接收標(biāo)志位
- while(1)
- {
- /* Loop until RXNE = 1 */
- while(USART_GetFlagStatus(DEBUG_USART, USART_FLAG_RXNE) == RESET){}//等待直到有數(shù)據(jù)的到來
-
- str[index++] = (USART_ReceiveData(DEBUG_USART));
- printf("%c",str[index - 1]);//返回輸入的內(nèi)容
- if(str[index - 1] == 0x0d)//回車鍵\r
- {
- index--;//去掉回車符并以\0結(jié)尾
- str[index] ='\0';//結(jié)束符號(hào)
- return index;
- }
- }
- }
-
復(fù)制代碼
|
|