蓝桥杯
- 1.创建工程
- 2.LED灯点亮
- led.c
- 3.LCD液晶屏显示
- lcd.c
- 4.定时器按键单机
- interrupt.h
- interrupt.c
- man.c
- 5.定时器(长按键)
- interrupt.h
- interrupt.c
- main.c
- 6.PWM
- main.c
- 7.定时器-输入捕获(频率,占空比测量)
- interrupt.c
- main.c
- 8.ADC模数转换
- myadc.h
- myadc.c
- main.c
- 9.IIC
- i2c - hal.c
- main.c
- 10.uart串口通信
- interrupter.c
- mian.c
1.创建工程
2.LED灯点亮
- 高电平为默认不点亮
- PD2电平为低不用设置
led.c
#include "main.h"
#include "gpio.h"
#include "led.h"
void LED_Disp(uchar dsLED)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
/*
* GPIO_PIN_RESET: 1有效置0 0无效保持原来状态
* GPIO_PIN_SET : 1有效置1 0无效保持原来状态
* GPIOC 1111 1111 1111 1111
*/
//LED的引脚GPIO的C组的前八位所以左移八位
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);
/*
* dsLED=0x00<<8 0000 0000 0000 0000 GPIOC 1111 1111 1111 1111
* dsLED=0xff<<8 1111 1111 0000 0000 GPIOC 0000 0000 0000 0000
*/
//打开锁存器
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
main()
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
LED_Disp(0x00);//全部熄灭
while (1)
{
LED_Disp(0x00);
HAL_Delay(500);
LED_Disp(0xff);//全点亮
HAL_Delay(500);
}
}
3.LCD液晶屏显示
lcd.c
int main(void)
{
char text[30];
uint i=5;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
sprintf(text," SCH%d ",i);
LCD_DisplayStringLine(Line4, (uint8_t *)text);
while (1)
{
HAL_Delay(500);
}
}
4.定时器按键单机
interrupt.h
#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_
#include "main.h"
#include "stdbool.h"
struct keys
{
uchar judge_sta; //进行到底几步
bool key_sta; //按键的状态
bool single_flag; //当按键按下为1
};
#endif
interrupt.c
#include "interrupt.h"
struct keys key[4]={0,0,0};
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM3)
{
key[0].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
key[1].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
key[2].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
key[3].key_sta=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
for(int i=0;i<4;++i)
{
switch(key[i].judge_sta)
{
case 0:
{
if(key[i].key_sta==0)
key[i].judge_sta=1;
else
key[i].judge_sta=0;
}
break;
case 1:
{
if(key[i].key_sta==0)
{
key[i].judge_sta=2;
key[i].single_flag=1;
}
else
key[i].judge_sta=0;
}
break;
case 2:
{
if(key[i].key_sta==1)
{
key[i].judge_sta=0;
}
}
break;
}
}
}
}
man.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
extern struct keys key[];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim3);//定时器中断
char text[30];
while (1)
{
if(key[0].single_flag==1)
{
key[0].single_flag=0;
sprintf(text," Key0 ");
LCD_DisplayStringLine(Line8, (uint8_t *)text);
}
if(key[1].single_flag==1)
{
key[1].single_flag=0;
sprintf(text," Key1 ");
LCD_DisplayStringLine(Line8, (uint8_t *)text);
}
}
}
5.定时器(长按键)
interrupt.h
#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_
#include "main.h"
#include "stdbool.h"
struct keys
{
uchar judge_sta; //进行到底几步
bool key_sta; //按键的状态
bool single_flag; //当按键按下为1
bool long_flag; //长按键
uint key_time; //按下累计的时间
};
#endif
interrupt.c
#include "interrupt.h"
struct keys key[4]={0,0,0};
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM3)
{
key[0].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
key[1].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
key[2].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
key[3].key_sta=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
for(int i=0;i<4;++i)
{
switch(key[i].judge_sta)
{
case 0:
{
if(key[i].key_sta==0)
{
key[i].judge_sta=1;
key[i].key_time=0;
}
else
key[i].judge_sta=0;
}
break;
case 1:
{
if(key[i].key_sta==0)
{
key[i].judge_sta=2;
}
else
key[i].judge_sta=0;
}
break;
case 2:
{
if(key[i].key_sta==1)
{
key[i].judge_sta=0;
if(key[i].key_time<70)
key[i].single_flag=1;
}
else
{
key[i].key_time++;
if(key[i].key_time>70)
key[i].long_flag=1;
}
}
break;
}
}
}
}
main.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
extern struct keys key[];
uchar view;
void key_pro(void);
void disp_pro(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim3);
while (1)
{
key_pro();
disp_pro();
}
}
void key_pro(void)
{
if(key[0].single_flag==1)
{
view=!view;
key[0].single_flag=0;
}
}
void disp_pro(void)
{
if(view==0)
{
char text[30];
sprintf(text," Data ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
}
else
{
char text[30];
sprintf(text," Para ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
}
}
6.PWM
main.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
extern struct keys key[];
uchar view;
uchar pa6_duty=10;
uchar pa7_duty=10;
void key_pro(void);
void disp_pro(void);
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
MX_TIM16_Init();
MX_TIM17_Init();
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim3);//开启定时器3的中断
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);//PWM开启
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
while (1)
{
key_pro();
disp_pro();
}
}
/* USER CODE BEGIN 4 */
void key_pro(void)
{
if(key[0].single_flag==1)
{
view=!view;
LCD_Clear(Black);
key[0].single_flag=0;
}
if(key[1].single_flag==1)
{
pa6_duty=pa6_duty%90+10;
__HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1,pa6_duty);//控制占空比
key[1].single_flag=0;
}
if(key[2].single_flag==1)
{
pa7_duty=pa7_duty%90+10;
__HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1,pa7_duty);//控制占空比
key[2].single_flag=0;
}
}
void disp_pro(void)
{
if(view==0)
{
char text[30];
sprintf(text," Data ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
}
else
{
char text[30];
sprintf(text," Para ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
sprintf(text," PA6:%d ",pa6_duty);
LCD_DisplayStringLine(Line2, (uint8_t *)text);
sprintf(text," PA7:%d ",pa7_duty);
LCD_DisplayStringLine(Line3, (uint8_t *)text);
}
}
7.定时器-输入捕获(频率,占空比测量)
interrupt.c
#include"interrupt.h"
/测量频率+占空比///
double ccr1_val1a=0,ccr1_val2a=0;
double ccr1_val1b=0,ccr1_val2b=0;
uint frq1=0,frq2=0;//频率
float duty1=0,duty2=0;//占空比
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM2)
{
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)//中断消息来源 选择直接输入的通道
{
ccr1_val1a=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);//直接
ccr1_val1b=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);//间接
__HAL_TIM_SetCounter(htim,0);
frq1=(80000000/80)/ccr1_val1a;
duty1=(ccr1_val1b/ccr1_val1a)*100;
HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);
HAL_TIM_IC_Start(htim,TIM_CHANNEL_2);
}
}
if(htim->Instance==TIM3)
{
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)//中断消息来源 选择直接输入的通道
{
ccr1_val2a=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
ccr1_val2b=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
__HAL_TIM_SetCounter(htim,0);
frq2=(80000000/80)/ccr1_val2a;
duty2=(ccr1_val2b/ccr1_val2a)*100;
HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);
HAL_TIM_IC_Start(htim,TIM_CHANNEL_2);
}
}
}
main.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
extern struct keys key[];
extern uint frq1,frq2;
extern float duty1,duty2;
uchar view;
uchar pa6_duty=10;
uchar pa7_duty=10;
void key_pro(void);
void disp_pro(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
MX_TIM16_Init();
MX_TIM17_Init();
MX_TIM2_Init();
MX_TIM4_Init();
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);//开启定时器4的中断
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);//PWM开启
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);//频率测量捕获定时器开启
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);//频率测量捕获定时器开启 间接通道
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
//
__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,pa6_duty);//设置初始pwm频率
__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,pa7_duty);
while (1)
{
key_pro();
disp_pro();
}
}
void key_pro(void)
{
if(key[0].single_flag==1)
{
view=!view;
LCD_Clear(Black);
key[0].single_flag=0;
}
if(key[1].single_flag==1)
{
pa6_duty=pa6_duty%90+10;
__HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1,pa6_duty);//控制占空比
key[1].single_flag=0;
}
if(key[2].single_flag==1)
{
pa7_duty=pa7_duty%90+10;
__HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1,pa7_duty);//控制占空比
key[2].single_flag=0;
}
}
void disp_pro(void)
{
if(view==0)
{
char text[30];
sprintf(text," Data ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
sprintf(text," FRQ1=%d ",frq1);
LCD_DisplayStringLine(Line2, (uint8_t *)text);
sprintf(text," duty1=%.3f ",duty1);
LCD_DisplayStringLine(Line3, (uint8_t *)text);
sprintf(text," FRQ2=%d ",frq2);
LCD_DisplayStringLine(Line4, (uint8_t *)text);
sprintf(text," duty2=%.3f ",duty2);
LCD_DisplayStringLine(Line5, (uint8_t *)text);
}
else if(view==1)
{
char text[30];
sprintf(text," Para ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
sprintf(text," PA6:%d ",pa6_duty);
LCD_DisplayStringLine(Line2, (uint8_t *)text);
sprintf(text," PA7:%d ",pa7_duty);
LCD_DisplayStringLine(Line4, (uint8_t *)text);
}
}
8.ADC模数转换
myadc.h
#ifndef _MYADC_H_
#define _MYADC_H_
#include "main.h"
double getADC(ADC_HandleTypeDef *pin);
#endif
myadc.c
#include "myadc.h"
double getADC(ADC_HandleTypeDef *pin)
{
uint adc;
HAL_ADC_Start (pin);
adc = HAL_ADC_GetValue (pin);
return adc*3.3/4096;
}
main.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
#include "myadc.h"
extern struct keys key[];
extern uint frq1,frq2;
extern float duty1,duty2;
uchar view;
uchar pa6_duty=10;
uchar pa7_duty=10;
void key_pro(void);
void disp_pro(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
MX_TIM16_Init();
MX_TIM17_Init();
MX_TIM2_Init();
MX_TIM4_Init();
MX_ADC1_Init();
MX_ADC2_Init();
/* USER CODE BEGIN 2 */
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);//开启定时器4的中断
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);//PWM开启
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);//频率测量捕获定时器开启
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);//频率测量捕获定时器开启 间接通道
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,pa6_duty);//设置初始pwm频率
__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,pa7_duty);
while (1)
{
key_pro();
disp_pro();
}
}
void disp_pro(void)
{
if(view==0)
{
char text[30];
sprintf(text," ADC1=%.2f ",getADC(&hadc1));
LCD_DisplayStringLine(Line6, (uint8_t *)text);
sprintf(text," ADC2=%.2f ",getADC(&hadc2));
LCD_DisplayStringLine(Line7, (uint8_t *)text);
}
else if(view==1)
{
}
}
9.IIC
i2c - hal.c
#include "i2c - hal.h"
uchar eeprom_read(uchar addr)
{
uchar dat;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
//I2CStop();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
dat=I2CReceiveByte();
//I2CWaitAck();
I2CStop();
return dat;
}
void eeprom_write (uchar addr,uchar dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
main.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
#include "myadc.h"
#include "i2c - hal.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern struct keys key[];
extern uint frq1,frq2;
extern float duty1,duty2;
uchar view=0;
uchar pa6_duty=10;
uchar pa7_duty=10;
void key_pro(void);
void disp_pro(void);
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM3_Init();
MX_TIM16_Init();
MX_TIM17_Init();
MX_TIM2_Init();
MX_TIM4_Init();
MX_ADC1_Init();
MX_ADC2_Init();
/* USER CODE BEGIN 2 */
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);//开启定时器4的中断
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);//PWM开启
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);//频率测量捕获定时器开启
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);//频率测量捕获定时器开启 间接通道
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,pa6_duty);//设置初始pwm频率
__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,pa7_duty);
while (1)
{
if(key[3].single_flag==1)
{
uchar frq_h=frq1>>8;
uchar frq_l=frq1&0xff;
eeprom_write(1,frq_h);
HAL_Delay(10);
eeprom_write(2,frq_l);
key[3].single_flag=0;
}
uint eeprom_val=(eeprom_read(1)<<8)+eeprom_read(2);
sprintf(text," FRQ_eep=%d ",eeprom_val);
LCD_DisplayStringLine(Line8, (uint8_t *)text);
}
}
10.uart串口通信
interrupter.c
char rxdata[30];
uint8_t rxdat;
uchar rx_pointer;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *hurat)
{
rxdata[rx_pointer++]=rxdat;
HAL_UART_Receive_IT(&huart1,&rxdat,1);
}
mian.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
#include "myadc.h"
#include "i2c - hal.h"
#include "string.h"
extern struct keys key[];
extern char rxdata[30];
extern uint8_t rxdat;
extern uchar rx_pointer;
uchar view=0;
char car_type[5];
char car_data[5];
char car_time[13];
void key_pro(void);
void disp_pro(void);
void uart_rx_proc(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
MX_TIM16_Init();
MX_TIM17_Init();
MX_TIM2_Init();
MX_TIM4_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LED_Disp(0x00);//LCD初始化
LCD_Init();//LCD屏初始化
HAL_UART_Receive_IT(&huart1,&rxdat,1);//打开串口中断
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
while (1)
{
key_pro();
disp_pro();
if(rx_pointer!=0)
{
int temp=rx_pointer;
HAL_Delay(1);
if (temp==rx_pointer)uart_rx_proc();//完成接收
}
}
}
void key_pro(void)
{
if(key[0].single_flag==1)
{
view=0;
LCD_Clear(Black);
key[0].single_flag=0;
}
}
void disp_pro(void)
{
if(view==0)
{
char text[30];
sprintf(text," Car_msg ");
LCD_DisplayStringLine(Line1, (uint8_t *)text);
sprintf(text,"car_type=%s ",car_type);
LCD_DisplayStringLine(Line2, (uint8_t *)text);
sprintf(text,"car_data=%s ",car_data);
LCD_DisplayStringLine(Line3, (uint8_t *)text);
sprintf(text,"cat_time=%s ",car_time);
LCD_DisplayStringLine(Line4, (uint8_t *)text);
}
}
void uart_rx_proc()
{
if(rx_pointer>0)
{
if(rx_pointer==22)
{
sscanf(rxdata,"%4s:%4s:%12s",car_type,car_data,car_time);
}
else
{
char temp[20];
sprintf(temp,"Error");
HAL_UART_Transmit(&huart1,(uint8_t *)temp,strlen(temp),50);
}
rx_pointer=0;memset(rxdata,0,30);
}
}