蓝桥杯嵌入式一晚速通

一、基础外设

串口中断+DMA接收不定长数据

先在cubemx中配置并打开中断与dma

uint8_t buffer_uart[100];
//初始化
__HAL_UART_ENABLE_IT(&huart1,UART_FLAG_IDLE);
  HAL_UART_Receive_DMA(&huart1,buffer_uart,100);
//修改中断服务函数
if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)!=RESET)
	{
	__HAL_UART_CLEAR_IDLEFLAG(&huart1);
			HAL_UART_DMAStop(&huart1);
		uint8_t len = 100 - __HAL_DMA_GET_COUNTER(huart1.hdmarx);
		HAL_UART_Receive_DMA(&huart1,buffer_uart,100);
		
	
	}

翻转模式使用定时器输出频率占空比可调PWM

cubemx中选择toggle on match 计数值为65535

//初始化
	HAL_TIM_OC_Start_IT(&htim4,TIM_CHANNEL_1);
//写回调函数
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
	uint16_t tim_count=0;
    pwm_arr = 1000000/fre;
	pwm_crr = pwm_arr * palus;
	tim_count = htim4.Instance->CCR1;
	if(htim->Instance==TIM4)
	{
		if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
		{
			if(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_11))
			{
				__HAL_TIM_SetCompare(&htim4,TIM_CHANNEL_1,tim_count+pwm_crr);
			
			}
			else
			{
				__HAL_TIM_SetCompare(&htim4,TIM_CHANNEL_1,tim_count+pwm_arr-pwm_crr);
			}
		}
	}
	
}

LED与按键

void led_init()
{
	uint32_t LED_MODE=0xff00;
	
	GPIOC->ODR=LED_MODE;
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);

}
void led_light(unsigned char dsled)
{
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC,dsled<<8,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);

}


void LED_Proc(void)
{
	static unsigned char led = 0;
	
	if(VR37 > Number)
	{
		if(usLED == 100)//在systic中计时
		{	
			led ^=4;//闪烁
			usLED = 0;
		}
	}
	else
	{
		led &= ~4;	//灭
	}
	if(Ui == 0)
	{
		led &= ~2;//灭
		led |= 1;//亮
	}else{
		led &= ~1;
		led |= 2;
	}
	
	led_light(led);
}

按键

struct KEY KEY[4];
void key_init()
{
 KEY[0].count=0;
 KEY[0].status=0;
 KEY[1].count=0;
 KEY[1].status=0;
 KEY[2].count=0;
 KEY[2].status=0; 
 KEY[3].count=0;
 KEY[3].status=0;

}
void key_scan()
{
	if(task_key_flag)//定时器计时标志
	{
		task_key_flag=0;
		if(B1==0)
		{
			KEY[0].count+=10;
		}
		if(B2==0)
		{
			KEY[1].count+=10;
		}
		if(B3==0)
		{
			KEY[2].count+=10;
		}
		if(B4==0)
		{
			KEY[3].count+=10;
		}
		
		
	}
}
void key_cheak()
{
	if(KEY[0].count>50)
	{
		while(B1==0);
		KEY[0].status=~KEY[0].status;
		lcd_page=~lcd_page;
		KEY[0].count=0;
	}
	if(KEY[1].count>50&&lcd_page!=0)
	{
		while(B2==0);
		KEY[1].status=~KEY[1].status;
		vp1=vp1+0.3;
		if(vp1>3.3)
			vp1=0;
		KEY[1].count=0;
	}
	if(KEY[2].count>50&&lcd_page==0)
	{
		while(B3==0);
		KEY[2].status=~KEY[2].status;
		fre = fre+1000;
		if(fre>10000)
			fre=1000;
		KEY[2].count=0;
	}
	if(KEY[3].count>50)
	{
		KEY[3].status=~KEY[3].status;
		KEY[3].count=0;
	}
	
}

ADC轮询

uint16_t get_adc()
{
	uint16_t adc_v =0;
	HAL_ADC_Start(&hadc2);
	adc_v = HAL_ADC_GetValue(&hadc2);
	return adc_v;
}

adc_value = get_adc()*3.3/4095;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值