当前位置:网站首页>Blue Bridge Cup embedded learning summary (new version)

Blue Bridge Cup embedded learning summary (new version)

2022-06-26 07:13:00 Yinzz2

1.LED

GPIO_output level Set to high

void led_show(uint16_t led,uint8_t state)
{
	if(state == 1)
	{
   HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOC,led<<8,GPIO_PIN_RESET);
	   HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
  }
	else
	{
	  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOC,led<<8,GPIO_PIN_SET);
	   HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
	}
}

2.KEY

GPIO Input is set to pull up , Three line key anti shake is specifically known from other blogs

_Bool key_flag=0;

unsigned char Trg;
unsigned char Cont;
void key_read(void)
{
  unsigned char ReadData = (KEYPORT)^0xff;
  Trg = ReadData & ( ReadData ^ Cont );
  Cont = ReadData;
}

// Specific usage: search three lines to eliminate trembling 
//inter.h

#ifndef  _INTER_H_
#define _INTER_H_

#include "main.h"

#define KB1 	HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)
#define KB2 	HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)
#define KB3 	HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)
#define KB4 	HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)

#define KEYPORT KB1 | (KB2 << 1) | (KB3 << 2) | (KB4 << 3) | 0xf0
void key_read(void);
	
extern unsigned char Trg;
extern unsigned char Cont;
extern _Bool key_flag;
#endif
// Remember to add... To the main function 
HAL_TIM_Base_Start_IT(&htim3);


void    HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)
{
	static uint16_t key_count =0;
      if(htim->Instance == TIM3)
			{
				  key_count++;
				if(key_count==10)
				{
					key_count=0;
          key_flag=1;
			}
}
}

// Timer 1ms Trigger once ,key_count by 10 when , amount to 0.1s Make key_flag Set one 

 3.PWM Output

Mainly in the Cubemx Open selection   PWM Generation CH1  Set it up according to your needs Period,Prescaler and Pulse

// Add... To the main function 	HAL_TIM_PWM_Start(&htim16,TIM_CHANNEL_1);

// Set duty cycle      __HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,pa6duty);
// It is equivalent to setting... In one cycle pa6duty Number of pulses 


// Set the frequency       __HAL_TIM_SetAutoreload(&htim16,2000-1);
// It is equivalent to setting a cycle with 2000 Pulse 

原网站

版权声明
本文为[Yinzz2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260711274755.html