蓝桥杯
- 1.创建工程
- 2.LED灯点亮
-
- 3.LCD液晶屏显示
-
- 4.定时器按键单机
-
- interrupt.h
- interrupt.c
- man.c
- 5.定时器(长按键)
-
- interrupt.h
- interrupt.c
- main.c
- 6.PWM
-
- 7.定时器-输入捕获(频率,占空比测量)
-
- 8.ADC模数转换
-
- 9.IIC
-
- 10.uart串口通信
-
- 11.printf串口打印
- 12.屏幕反转
- 13.空闲中断DMA
-
- 第一种方法
-
- 1.1main.c
- 1.2main.h
- 1.3stm32g4xx_it.c
- 第二种方法
-
1.创建工程
2.LED灯点亮
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);
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);
}
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_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;
};
#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_Init();
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;
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();
LED_Disp(0x00);
LCD_Init();
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_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
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);
}
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;
}
}
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;
}
}
}
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_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);
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);
__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();
LED_Disp(0x00);
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);
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);
__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();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
dat=I2CReceiveByte();
I2CStop();
return dat;
}
void eeprom_write (uchar addr,uchar dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
HAL_Delay(10);
}
main.c
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
#include "myadc.h"
#include "i2c - hal.h"
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);
void SystemClock_Config(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();
LED_Disp(0x00);
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_PWM_Start (&htim16, TIM_CHANNEL_1);
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);
__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();
LED_Disp(0x00);
LCD_Init();
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);
}
}
11.printf串口打印
int fputc(int ch, FILE * f)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xfff);
return ch;
}
12.屏幕反转
LCD_WriteReg(R1 , 0x0000);
LCD_WriteReg(R96 , 0x2700);
LCD_WriteReg(R1 , 0x0100);
LCD_WriteReg(R96 , 0xA700);
13.空闲中断DMA
- 定义一个接收缓冲的数组,不能低于你的最大传输数据量
- 初始化串口
- 初始化串口DMA(循环)
- 打开IDLE中断
- 开启DMA传输
- 在中断函数中判断IDLE中断是否执行
- 清空IDLE标志位
- 停止DMA传输(处理数据防止干扰)
- 获取数据长度
- 接收缓冲数组数据末尾加上 ‘\0’
- 发送接收缓冲区数据,验证接收
- 重新打开DMA
第一种方法
1.1main.c
int main(void)
{
printf("Please enter arbitrary length characters:\r\n");
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_IDLE);
HAL_UART_Receive_DMA(&huart1,(uint8_t*)Rx_buff,buff_count);
__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);
while(1)
{
}
}
1.2main.h
typedef unsigned char uchar;
typedef unsigned int uint;
#define buff_count 100
extern uint8_t Rx_buff[buff_count];
extern uint32_t Rx_Length;
1.3stm32g4xx_it.c
uint8_t Rx_buff[buff_count];
uint32_t Rx_Length = 0;
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)
{
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_IDLE);
HAL_UART_DMAStop(&huart1);
Rx_Length = buff_count - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
Rx_buff[Rx_Length] = '\0';
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)Rx_buff,Rx_Length);
HAL_UART_Receive_DMA(&huart1,(uint8_t*)Rx_buff,buff_count);
}
}
第二种方法
2.1main.c
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if (huart->Instance == USART1)
{
HAL_UART_Transmit_DMA(&huart1, Rx_buff, Size);
HAL_UARTEx_ReceiveToIdle_DMA(&huart1,Rx_buff,buff_count);
__HAL_DMA_DISABLE_IT(&hdma_usart1_rx,DMA_IT_HT);
}
}
int main(void)
{
printf("Please enter arbitrary length characters:\r\n");
HAL_UARTEx_ReceiveToIdle_DMA(&huart1,Rx_buff,buff_count);
__HAL_DMA_DISABLE_IT(&hdma_usart1_rx,DMA_IT_HT);
while(1)
{
}
}
2.2main.h
typedef unsigned char uchar;
typedef unsigned int uint;
#define buff_count 10
extern uint8_t Rx_buff[buff_count];