STM32实战 4.利用定时器与串口接收指令控制LED亮度

#include "sys.h"
#include "led.h"
#include "delay.h"
#include "string.h"
#include "USART3_TIM2.h"


char *str_open = "open";
char *str_close = "close";

int main(void)
{
	delay_init();
	Led_Init();
	USART3_TIM2_Init(115200);
	
	

	while(1)
	{
		if(RX_stat == 1)//表示接受完成
	 {
		
			if(strcmp(RX_buf,str_open) == 0)
			{
				LED = 0;
			}
			if(strcmp(RX_buf,str_close) == 0)
			{
				LED = 1;
			}

			RX_stat = 0;
			count = 0;
			memset(RX_buf,0,strlen(RX_buf));
		}	
  }
	

}


#include "led.h"

void Led_Init(void)
{
	//结构体的声明,必须放在最前面,不能有任何
		GPIO_InitTypeDef GPIO_InitStructure;
	
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_Init(GPIOC, &GPIO_InitStructure);//区地址符
}

#include "USART3_TIM2.h"


u8 count = 0;
u8 RX_stat = 0;//0表示接收未完成,1表示接收完成
char RX_buf[10] = {0};

void USART3_TIM2_Init(u32 bound)
{
	
		GPIO_InitTypeDef GPIO_InitStructure;
		USART_InitTypeDef USART_InitStructure;
		NVIC_InitTypeDef NVIC_InitStructure;
		TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	
			//开启系统和复用功能时钟
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
			//PB10配置为复用推挽式输出
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_Init(GPIOB, &GPIO_InitStructure);
		//PB11配置为浮空输入
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOB, &GPIO_InitStructure);


		USART_InitStructure.USART_BaudRate = bound;
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;
		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_Tx | USART_Mode_Rx;
		USART_Init(USART3, &USART_InitStructure);
		
		USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//配置串口接收为中断模式
		
		NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);

		USART_Cmd(USART3,ENABLE);
		
		USART_ClearFlag(USART3,USART_FLAG_TC);
		
		//开启TIM2的使能定时器
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
		

		
		TIM_TimeBaseStructure.TIM_Period = (100 - 1);//ARR
		TIM_TimeBaseStructure.TIM_Prescaler = (7200 - 1);//PSC
		TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟不分割
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
		TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);	
			
		//使能定时器2的中断
		TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
		
		//配置定时器2的中断优先级
		NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);

		//使能定时器2
		TIM_Cmd(TIM2,DISABLE);
}

void USART3_IRQHandler(void)
{
	USART_ClearFlag(USART3,USART_FLAG_RXNE);
	
	RX_buf[count] =  USART_ReceiveData(USART3);
	count++;
	TIM_SetCounter(TIM2,0);
	TIM_Cmd(TIM2,ENABLE);

}

void TIM2_IRQHandler(void)
{
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);
	TIM_Cmd(TIM2,DISABLE);
	RX_stat = 1;
}


你可能感兴趣的:(STM32实战 4.利用定时器与串口接收指令控制LED亮度)