STM32 串口通信识别程序

转载至:

https://github.com/avislab/STM32F103/blob/master/Example_USART1/main.c

如有侵权,请联系删除!

 

代码现象:从串口发一个数据,串口能够返回相应的值,

 

#include "stm32f10x.h"

#include "stm32f10x_gpio.h"

#include "stm32f10x_rcc.h"

#include "stm32f10x_usart.h"

#include "misc.h"

#include "string.h"



#define RX_BUF_SIZE 80

volatile char RX_FLAG_END_LINE = 0;

volatile char RXi;

volatile char RXc;

char RX_BUF[RX_BUF_SIZE] = {'\0'};    //此处我删去了volatile



void clear_RXBuffer(void) {

	for (RXi=0; RXiSR & USART_FLAG_RXNE) != (u16)RESET)

	{

    		RXc = USART_ReceiveData(USART1);

    		RX_BUF[RXi] = RXc;

    		RXi++;



    		if (RXc != 13) {

    			if (RXi > RX_BUF_SIZE-1) {

    				clear_RXBuffer();

    			}

    		}

    		else {

    			RX_FLAG_END_LINE = 1;

    		}



			//Echo

    		USART_SendData(USART1, RXc);

	}

}



void USARTSend(char *pucBuffer)

{

    while (*pucBuffer)

    {

        USART_SendData(USART1, *pucBuffer++);

        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

        {

        }

    }

}



void SetSysClockTo72(void)

{

	ErrorStatus HSEStartUpStatus;

    /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/

    /* RCC system reset(for debug purpose) */

    RCC_DeInit();



    /* Enable HSE */

    RCC_HSEConfig( RCC_HSE_ON);



    /* Wait till HSE is ready */

    HSEStartUpStatus = RCC_WaitForHSEStartUp();



    if (HSEStartUpStatus == SUCCESS)

    {

        /* Enable Prefetch Buffer */

    	//FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable);



        /* Flash 2 wait state */

        //FLASH_SetLatency( FLASH_Latency_2);



        /* HCLK = SYSCLK */

        RCC_HCLKConfig( RCC_SYSCLK_Div1);



        /* PCLK2 = HCLK */

        RCC_PCLK2Config( RCC_HCLK_Div1);



        /* PCLK1 = HCLK/2 */

        RCC_PCLK1Config( RCC_HCLK_Div2);



        /* PLLCLK = 8MHz * 9 = 72 MHz */

        RCC_PLLConfig(0x00010000, RCC_PLLMul_9);



        /* Enable PLL */

        RCC_PLLCmd( ENABLE);



        /* Wait till PLL is ready */

        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

        {

        }



        /* Select PLL as system clock source */

        RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK);



        /* Wait till PLL is used as system clock source */

        while (RCC_GetSYSCLKSource() != 0x08)

        {

        }

    }

    else

    { /* If HSE fails to start-up, the application will have wrong clock configuration.

     User can add here some code to deal with this error */



        /* Go to infinite loop */

        while (1)

        {

        }

    }

}



int main(void)

{

	// Set System clock

	SetSysClockTo72();



	/* Initialize LED which connected to PC13 */

	GPIO_InitTypeDef  GPIO_InitStructure;

	// Enable PORTC Clock

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

	/* Configure the GPIO_LED pin */

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	GPIO_Init(GPIOC, &GPIO_InitStructure);



	GPIO_ResetBits(GPIOC, GPIO_Pin_13); // Set C13 to Low level ("0")



    // Initialize USART

    usart_init();

    USARTSend(" Hello.\r\nUSART1 is ready.\r\n");



    while (1)

    {

    	if (RX_FLAG_END_LINE == 1) {

    		// Reset END_LINE Flag

    		RX_FLAG_END_LINE = 0;



    		USARTSend("\r\nI has received a line:\r\n");

    		USARTSend(RX_BUF);

    		USARTSend("\r\n");



    		if (strncmp(strupr(RX_BUF), "ON\r", 3) == 0) {       //此处我删去了strupr

    			USARTSend("\r\nTHIS IS A COMMAND \"ON\"!!!\r\n");

    			GPIO_ResetBits(GPIOC, GPIO_Pin_13);

    		}



    		if (strncmp(strupr(RX_BUF), "OFF\r", 4) == 0) {    //此处我删去了strupr


    			USARTSend("\r\nTHIS IS A COMMAND \"OFF\"!!!\r\n");

    			GPIO_SetBits(GPIOC, GPIO_Pin_13);

    		}



    		clear_RXBuffer();

    	}

    }

}

 

你可能感兴趣的:(嵌入式)