蓝桥杯最终模版

min.c

//包含头文件
#include "led.h"
#include "stdio.h"
#include "string.h"
#include "lcd.h"
#include "key.h"
#include "iic.h"

//模版参数定义区域
char lcd_disp[21];
char uart_tx[40];
char uart_rx[40];
uint16_t counter;
float r37_voltage;
char pswd[3]={'0','1','2'};
float fruq_pa15,fruq_pb4,duty_pb4,duty_pa15;
uint16_t arp_pa6,arp_pa7;
float channel2_pb,channel2_pa;
uint8_t eeprom_1[3]={0x00,0x11,0x22},eeprom_2[3]={0};

//函数定义去
void led_proc(void);
void key_proc(void);
void lcd_proc(void);
void uart_proc(void);

//初始化
  LCD_Init();
  LCD_Clear(Black);
  LCD_SetBackColor(Black);
  LCD_SetTextColor(White);
  HAL_TIM_Base_Start_IT(&htim6);
  HAL_ADCEx_Calibration_GetValue(&hadc2,ADC_SINGLE_ENDED);
  HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t*)uart_rx,50);//空闲接收中断
  HAL_TIM_IC_Start(&htim2,TIM_CHANNEL_1);//pa15
  HAL_TIM_IC_Start(&htim2,TIM_CHANNEL_2);//pa15
  HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);//pb4
  HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);//pb4
  HAL_TIM_PWM_Start(&htim16,TIM_CHANNEL_1);//pa6 1kHz,50
  HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);//pa7	2kHz,50
	
	led_disp(0x00);//初始化熄灭所有灯

//主循环
	  r37_voltage = ((float)adc2getvalue()/4096*3.3f);

	  
	  led_proc();
	  key_proc();
	  lcd_proc();
	  uart_proc();


//函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM6)
	{
		key_serv();
	}

}

//main.c中调用
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);	//打开定时器输入捕获(中断启动)
 
float ccrl_val1a,ccrl_val1b = 0;
uint frq1,frq2 = 0;
float duty1,duty2 = 0;
//输入捕获中断回调函数
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)			
{
	if(htim->Instance == TIM3)
	{
		if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)//中断消息来源,选择直接输入通道
		{
			ccrl_val1a = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);	//直接	
			ccrl_val1b = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);	//间接
			__HAL_TIM_SetCounter(htim,0);		    //计数值清零
			frq1 = (80000000/80)/ccrl_val1a;		//计算频率
			duty1 = (ccrl_val1b/ccrl_val1a) * 100;  //计算占空比
			HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);	//重新开启定时器通道一(轮询启动)
			HAL_TIM_IC_Start(htim,TIM_CHANNEL_2);	//重新开启定时器通道二
		}
	}
}


void led_proc(void)
{
	led_chg(1,1);
}
void key_proc(void)
{
	if(keys[1].short_flag==1)//按键一短按
	{
		led_chg(2,1);
		keys[1].short_flag=0;//清除标志位
	}
	if(keys[2].short_flag==1)//按键二短按
	{
		led_chg(2,0);
		keys[2].short_flag=0;//清除标志位
	}
		
	if(keys[1].long_flag==1)
	{
		counter++;
	}
}
void lcd_proc(void)
{
	sprintf(lcd_disp,"%d",counter);
	LCD_DisplayStringLine(Line0,(uint8_t*)lcd_disp);
	sprintf(lcd_disp,"%4.2fV",r37_voltage);
	LCD_DisplayStringLine(Line5,(uint8_t*)lcd_disp);
	
	sprintf(lcd_disp,"%8.3fHz %5.3f%%",fruq_pb4,duty_pb4);
	LCD_DisplayStringLine(Line8,(uint8_t*)lcd_disp);
	sprintf(lcd_disp,"%8.3fHz %5.3f%%",fruq_pa15,duty_pa15);	
	LCD_DisplayStringLine(Line9,(uint8_t*)lcd_disp);
}
void uart_proc(void)
{
	sprintf(uart_tx,"hello\r\n");
	HAL_UART_Transmit(&huart1,(uint8_t*)uart_tx,strlen(uart_tx),40);
	HAL_Delay(1000);
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	char pswd_new[10];
	sscanf(uart_rx,"%3s",pswd_new);
	if(strcmp(pswd_new,pswd)==0)
	sprintf(uart_tx,"pwsd correct\r\n");
	else
	sprintf(uart_tx,"pwsd error\r\n");	
	HAL_UART_Transmit(&huart1,(uint8_t*)uart_tx,strlen(uart_tx),40);
	HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t*)uart_rx,40);
}

led.h

led.c
#include "led.h"
uint8_t led_sta=0x00;
void led_disp(uint8_t ucled)
{
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
                          |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC,ucled<<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_chg(uint8_t num,uint8_t sta)
{
	uint8_t pos=0x01<<(num-1);
	led_sta=(led_sta & (~pos)) | (pos*sta);
	led_disp(led_sta);
}

led.h

#include "main.h"
void led_disp(uint8_t ucled);
void led_chg(uint8_t num,uint8_t sta);
extern uint8_t led_sta;

key,c

#include "key.h"

uint8_t key_sta;
struct mykey keys[5]={0};
uint8_t key_scan(void)
{
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)return 1;
	else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==0)return 2;
	else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==0)return 3;
	else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0)return 4;
	else return 0;
}
void key_serv(void)//长按按下就生效
{
	key_sta = key_scan();
	if(key_sta!=0)//检测有按键按下
	{
		keys[key_sta].age++;
		if(keys[key_sta].age==2)keys[key_sta].press=1;//按下标志位
	}
	else
	{
		for(uint8_t i=0;i<5;i++)
		{
			if(keys[i].press==1&&keys[i].long_flag==0)
			{
				keys[i].short_flag=1;
			}
			keys[i].age=0;
			keys[i].long_flag=0;
			keys[i].press=0;
		}
	}
	if(keys[key_sta].age>199)keys[key_sta].long_flag=1;
}

key.h

#include "main.h"
struct mykey
{
	uint16_t age;
	uint8_t press,short_flag,long_flag;
};
extern struct mykey keys[5];
void key_serv(void);

iic

发送函数先scl延时sdl
void I2CSendByte(unsigned char cSendByte)
{
    unsigned char  i = 8;
    while (i--)
    {
        SCL_Output(0);
        delay1(DELAY_TIME);
        SDA_Output(cSendByte & 0x80);
        delay1(DELAY_TIME);
        cSendByte += cSendByte;
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
}

//
unsigned char I2CReceiveByte(void)
{
    unsigned char i = 8;
    unsigned char cR_Byte = 0;
    SDA_Input_Mode();
    while (i--)
    {
        cR_Byte += cR_Byte;
        SCL_Output(0);
        delay1(DELAY_TIME);
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
        cR_Byte |=  SDA_Input();
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output_Mode();
    return cR_Byte;
}

//
void I2CInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void iic_24c02_write(uint8_t *pucbuf,uint8_t ucAddr,uint8_t num)
{
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(ucAddr);
	I2CWaitAck();
	
	while(num--)
	{
		I2CSendByte(*pucbuf++);
		I2CWaitAck();
	}
	I2CStop();
	delay1(500);
}
void iic_24c02_read(uint8_t *pucbuf,uint8_t ucAddr,uint8_t num)
{
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(ucAddr);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	
	while(num--)
	{
		*pucbuf++ = I2CReceiveByte();
		if(num)
			I2CSendAck();
		else 
			I2CSendNotAck();
	}
	I2CStop();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值