蓝桥杯嵌入式uart,iic,adc_scan模版
本次用到的是ttl电平
1.波特率配置
2.中断使能
为什么会乱码
//uartmy_main.h
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
char uart_tx[50];
char uart_rx[50];
extern struct Bkeys bkey[];
char passwd[3]={'1','2','3'};
void LED_Disp(uint8_t dsLED)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
float adc_read(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start(hadc);
adc_val=HAL_ADC_GetValue(hadc);
adc_f=adc_val*3.3f/4096.0f;
return adc_f;
}
float adc_read_os(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start(hadc);
adc_val=HAL_ADC_GetValue(hadc);
adc_f=adc_val*3.3f/65536.0f;
return adc_f;
}
void setup()
{
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
//TIM15_CH1输入演示
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);
//TIM3_CH2输入演示
HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);
HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);
LED_Disp(0x00);//关掉所有LED
LCD_Init();//LCD初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
}
void loop()
{
static uint16_t temp=0;
float frq1,frq2=0;
float duty1,duty2=0;
float adc_data_PB15;
RTC_TimeTypeDef time;
RTC_DateTypeDef date;
HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
sprintf(text,"time:%d:%d:%d ",time.Hours,time.Minutes,time.Seconds);
LCD_DisplayStringLine(Line9,(uint8_t *)text);//局部刷新
frq1=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);
duty1=((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1)));
sprintf(text,"FRQ:%.2fHz ",frq1);
LCD_DisplayStringLine(Line1,(uint8_t *)text);//局部刷新
sprintf(text,"DUTY:%.2f%% ",duty1);
LCD_DisplayStringLine(Line2,(uint8_t *)text);//局部刷新
frq2=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);
duty2=((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)));
sprintf(text,"FRQ2:%.2fHz ",frq2);
LCD_DisplayStringLine(Line3,(uint8_t *)text);//局部刷新
sprintf(text,"DUTY2:%.2f%% ",duty2);
LCD_DisplayStringLine(Line4,(uint8_t *)text);//局部刷新
adc_data_PB15=adc_read_os(&hadc2);
sprintf(text,"PB15:%.4fV ",adc_data_PB15);
LCD_DisplayStringLine(Line5,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"PB15:%.4fV \r\n",adc_data_PB15);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
if(bkey[1].short_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 down! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
__HAL_TIM_SET_AUTORELOAD(&htim2,250-1);
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);
bkey[1].short_flag=0;
}
if(bkey[2].short_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_2 down! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
bkey[2].short_flag=0;
}
/*长按快速增加功能*/
if(bkey[1].long_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 long!,%d ",temp++);
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
}
if(bkey[1].double_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 double! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
bkey[1].double_flag=0;
}
/*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM6)
key_serv_double();
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
char old_pswd[10];
char new_pswd[10];
sscanf(uart_rx,"%3s-%3s",old_pswd,new_pswd);
if(strcmp(old_pswd,passwd)==0)
{
for(int i=0;i<3;i++)passwd[i]=new_pswd[i];
sprintf(text,"new:%s ",passwd);
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"new:%s \r\n",passwd);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
}
else
{
sprintf(text,"Fail ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"old:%s new:%s \r\n",old_pswd,new_pswd);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
}
HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
}
#include "key.h"
struct Bkeys bkey[5]={0,0,0,0};
uint8_t key_read()
{
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()
{
uint8_t key_sta=key_read();
if(key_sta!=0)
{
bkey[key_sta].age++;
if(bkey[key_sta].age>1)bkey[key_sta].short_flag=1;
}
else
{
for(int i=0;i<5;i++) bkey[i].age=0;
}
}
/*长按快速增加功能*/
void key_serv_long()
{
uint8_t key_sta=key_read();
if(key_sta!=0)
{
bkey[key_sta].age++;
if(bkey[key_sta].age>1) bkey[key_sta].press=1;
}
else
{
for(int i=0;i<5;i++)
{
if(bkey[i].press==1&&bkey[i].long_flag==0)
bkey[i].short_flag=1;
bkey[i].age=0;
bkey[i].press=0;
bkey[i].long_flag=0;
}
}
if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
/*长按后抬起执行*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1) bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].age>69) bkey[i].long_flag=1;
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
// bkey[i].age=0;
// bkey[i].press=0;
// }
// }
//}
void key_serv_double()
{
uint8_t key_sta=key_read();
if(key_sta!=0)
{
bkey[key_sta].age++;
if(bkey[key_sta].age>1) bkey[key_sta].press=1;
}
else
{
for(int i=0;i<5;i++)
{
if(bkey[i].double_ageEN==1&&bkey[i].press==1)
{
bkey[i].double_flag=1;
bkey[i].double_ageEN=0;
bkey[i].press=0;
}
if(bkey[i].press&&bkey[i].long_flag==0)
{
bkey[i].double_ageEN=1;
}
if(bkey[i].double_ageEN==1)bkey[i].double_age++;
if(bkey[i].double_ageEN==1&&bkey[i].double_age>20)
{
bkey[i].short_flag=1;
bkey[i].double_ageEN=0;
bkey[i].double_age=0;
}
bkey[i].age=0;
bkey[i].press=0;
bkey[i].long_flag=0;
}
}
if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
IIC_EEPROM
原理图
24c02
引脚配置
为什么要配置成output模式因为官方会提供一个i2c的库,可以直接用用的是软件IIC
不可用状态,用的软件IIC
直接生成就可
将这两个文件复制到bsp文件下面
At24c02手册
1k,2k是容量
一个总线上可以挂很多很多芯片
真题案例
实验现象
问题 ---按键没有用只显示1038
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
char uart_tx[50];
char uart_rx[50];
extern struct Bkeys bkey[];
char passwd[3]={'1','2','3'};
uint8_t eep_num;
uint16_t eep_frq;
float eep_adc;
void LED_Disp(uint8_t dsLED)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
float adc_read(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start(hadc);
adc_val=HAL_ADC_GetValue(hadc);
adc_f=adc_val*3.3f/4096.0f;
return adc_f;
}
float adc_read_os(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start(hadc);
adc_val=HAL_ADC_GetValue(hadc);
adc_f=adc_val*3.3f/65536.0f;
return adc_f;
}
void setup()
{
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
//TIM15_CH1输入演示
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);
//TIM3_CH2输入演示
HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);
HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);
LED_Disp(0x00);//关掉所有LED
LCD_Init();//LCD初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
I2CInit();
mcp_write(10);
}
void loop()
{
static uint16_t temp=0;
float frq1,frq2=0;
float duty1,duty2=0;
float adc_data_PB15;
RTC_TimeTypeDef time;
RTC_DateTypeDef date;
HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
sprintf(text,"time:%d:%d:%d ",time.Hours,time.Minutes,time.Seconds);
LCD_DisplayStringLine(Line9,(uint8_t *)text);//局部刷新
frq1=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);
duty1=((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1)));
sprintf(text,"FRQ:%.2fHz ",frq1);
LCD_DisplayStringLine(Line1,(uint8_t *)text);//局部刷新
sprintf(text,"DUTY:%.2f%% ",duty1);
LCD_DisplayStringLine(Line2,(uint8_t *)text);//局部刷新
frq2=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);
duty2=((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)));
sprintf(text,"FRQ2:%.2fHz ",frq2);
LCD_DisplayStringLine(Line3,(uint8_t *)text);//局部刷新
sprintf(text,"DUTY2:%.2f%% ",duty2);
LCD_DisplayStringLine(Line4,(uint8_t *)text);//局部刷新
adc_data_PB15=adc_read_os(&hadc2);
sprintf(text,"PB15:%.4fV ",adc_data_PB15);
LCD_DisplayStringLine(Line5,(uint8_t *)text);//局部刷新
eep_num=eeprom_read(8);
eep_frq=eeprom_read16(10);
eep_adc=eeprom_read_f(12);
sprintf(text,"EEP:%d,%d,%.2f ",eep_num,eep_frq,eep_adc);
LCD_DisplayStringLine(Line6,(uint8_t *)text);//局部刷新
// sprintf(uart_tx,"PB15:%.4fV \r\n",adc_data_PB15);
// HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
if(bkey[1].short_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 down! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
__HAL_TIM_SET_AUTORELOAD(&htim2,250-1);
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);
bkey[1].short_flag=0;
}
if(bkey[2].short_flag==1)
{
/* LCD演示 */
eep_num+=1;
eeprom_write(8,eep_num);
HAL_Delay(10);
eeprom_write16(10,(uint16_t)frq1);
eeprom_write_f(12,adc_data_PB15);
sprintf(text," KEY_2 down! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
bkey[2].short_flag=0;
}
/*长按快速增加功能*/
if(bkey[1].long_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 long!,%d ",temp++);
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
}
if(bkey[1].double_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 double! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
bkey[1].double_flag=0;
}
/*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM6)
key_serv_double();
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
char old_pswd[10];
char new_pswd[10];
sscanf(uart_rx,"%3s-%3s",old_pswd,new_pswd);
if(strcmp(old_pswd,passwd)==0)
{
for(int i=0;i<3;i++)passwd[i]=new_pswd[i];
sprintf(text,"new:%s ",passwd);
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"new:%s \r\n",passwd);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
}
else
{
sprintf(text,"Fail ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"old:%s new:%s \r\n",old_pswd,new_pswd);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
}
HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
}
i2c_hal.c
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 eeprom_write(uint8_t addr,uint8_t dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
uint8_t eeprom_read(uint8_t addr)
{
uint8_t rec;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
rec=I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return rec;
}
void eeprom_write16(uint8_t addr,uint16_t dat)
{
uint8_t dat_h=dat>>8;
uint8_t dat_l=dat;
eeprom_write(addr,dat_h);
HAL_Delay(10);
eeprom_write(addr+1,dat_l);
HAL_Delay(10);
}
uint16_t eeprom_read16(uint8_t addr)
{
uint8_t dat_h=eeprom_read(addr);
uint8_t dat_l=eeprom_read(addr+1);
return (dat_h<<8)+dat_l;
}
void eeprom_write_f(uint8_t addr,float dat)
{
uint16_t dat_f=dat*100;
uint8_t dat_h=dat_f>>8;
uint8_t dat_l=dat_f;
eeprom_write(addr,dat_h);
HAL_Delay(10);
eeprom_write(addr+1,dat_l);
HAL_Delay(10);
}
float eeprom_read_f(uint8_t addr)
{
uint8_t dat_h=eeprom_read(addr);
uint8_t dat_l=eeprom_read(addr+1);
return ((dat_h<<8)+dat_l)/100.0f;
}
void mcp_write(uint8_t dat)
{
I2CStart();
I2CSendByte(0x5e);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
ADC_scan_DMA
原理图
引脚配置
Adc是将一个模拟的值转化为一个电压的值
Adc通道使能配置
之后点击生成即可
实例测试
显示在第三行
my_main.h
#include "my_main.h"
uint8_t led_sta=0x10;
char text[30];
char uart_tx[50];
char uart_rx[50];
extern struct Bkeys bkey[];
uint16_t adc_Buf[2];
char passwd[3]={'1','2','3'};
uint8_t eep_num;
uint16_t eep_frq;
float eep_adc;
void LED_Disp(uint8_t dsLED)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//所有LED熄灭(l小写)
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);//左移8位:控制C8-15引脚,值为1的点亮
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//开锁存器
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关锁存器
}
float adc_read(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start(hadc);
adc_val=HAL_ADC_GetValue(hadc);
adc_f=adc_val*3.3f/4096.0f;
return adc_f;
}
float adc_read_os(ADC_HandleTypeDef *hadc)
{
uint16_t adc_val;
float adc_f;
HAL_ADC_Start(hadc);
adc_val=HAL_ADC_GetValue(hadc);
adc_f=adc_val*3.3f/65536.0f;
return adc_f;
}
void setup()
{
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
//TIM15_CH1输入演示
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim15,TIM_CHANNEL_2);
//TIM3_CH2输入演示
HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);
HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
HAL_ADC_Start_DMA(&hadc1,(uint32_t *)adc_Buf,2);
LED_Disp(0x00);//关掉所有LED
LCD_Init();//LCD初始化
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
I2CInit();
mcp_write(20);
}
void loop()
{
static uint16_t temp=0;
float frq1,frq2=0;
float duty1,duty2=0;
float adc_data_PB15;
RTC_TimeTypeDef time;
RTC_DateTypeDef date;
HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
sprintf(text,"time:%d:%d:%d ",time.Hours,time.Minutes,time.Seconds);
LCD_DisplayStringLine(Line9,(uint8_t *)text);//局部刷新
frq1=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1);
duty1=((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1)+1)));
sprintf(text,"FRQ:%.2fHz ",frq1);
LCD_DisplayStringLine(Line1,(uint8_t *)text);//局部刷新
sprintf(text,"DUTY:%.2f%% ",duty1);
LCD_DisplayStringLine(Line2,(uint8_t *)text);//局部刷新
frq2=1000000.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);
duty2=((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/((HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)));
sprintf(text,"FRQ2:%.2fHz ",frq2);
LCD_DisplayStringLine(Line3,(uint8_t *)text);//局部刷新
sprintf(text,"DUTY2:%.2f%% ",duty2);
LCD_DisplayStringLine(Line4,(uint8_t *)text);//局部刷新
adc_data_PB15=adc_read_os(&hadc2);
sprintf(text,"PB15:%.4fV ",adc_data_PB15);
LCD_DisplayStringLine(Line5,(uint8_t *)text);//局部刷新
eep_num=eeprom_read(8);
eep_frq=eeprom_read16(10);
eep_adc=eeprom_read_f(12);
sprintf(text,"EEP:%d,%d,%.2f ",eep_num,eep_frq,eep_adc);
LCD_DisplayStringLine(Line6,(uint8_t *)text);//局部刷新
sprintf(text,"ADC1:%.3f,2:%.3f ",adc_Buf[0]*3.3f/4096.0f,adc_Buf[1]*3.3f/4096.0f);
LCD_DisplayStringLine(Line7,(uint8_t *)text);//局部刷新
// sprintf(uart_tx,"PB15:%.4fV \r\n",adc_data_PB15);
// HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
if(bkey[1].short_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 down! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
__HAL_TIM_SET_AUTORELOAD(&htim2,250-1);
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,125);
bkey[1].short_flag=0;
}
if(bkey[2].short_flag==1)
{
/* LCD演示 */
eep_num+=1;
eeprom_write(8,eep_num);
HAL_Delay(10);
eeprom_write16(10,(uint16_t)frq1);
eeprom_write_f(12,adc_data_PB15);
sprintf(text," KEY_2 down! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
bkey[2].short_flag=0;
}
/*长按快速增加功能*/
if(bkey[1].long_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 long!,%d ",temp++);
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
}
if(bkey[1].double_flag==1)
{
/* LCD演示 */
sprintf(text," KEY_1 double! ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
bkey[1].double_flag=0;
}
/*长按后抬起执行*/
// if(bkey[1].long_flag==1)
// {
// /* LCD演示 */
// sprintf(text," KEY_1 long! ");
// LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
// bkey[1].long_flag=0;
// }
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM6)
key_serv_double();
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
char old_pswd[10];
char new_pswd[10];
sscanf(uart_rx,"%3s-%3s",old_pswd,new_pswd);
if(strcmp(old_pswd,passwd)==0)
{
for(int i=0;i<3;i++)passwd[i]=new_pswd[i];
sprintf(text,"new:%s ",passwd);
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"new:%s \r\n",passwd);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
}
else
{
sprintf(text,"Fail ");
LCD_DisplayStringLine(Line8,(uint8_t *)text);//局部刷新
sprintf(uart_tx,"old:%s new:%s \r\n",old_pswd,new_pswd);
HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
}
HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
}
#include "key.h"
struct Bkeys bkey[5]={0,0,0,0};
uint8_t key_read()
{
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()
{
uint8_t key_sta=key_read();
if(key_sta!=0)
{
bkey[key_sta].age++;
if(bkey[key_sta].age>1)bkey[key_sta].short_flag=1;
}
else
{
for(int i=0;i<5;i++) bkey[i].age=0;
}
}
/*长按快速增加功能*/
void key_serv_long()
{
uint8_t key_sta=key_read();
if(key_sta!=0)
{
bkey[key_sta].age++;
if(bkey[key_sta].age>1) bkey[key_sta].press=1;
}
else
{
for(int i=0;i<5;i++)
{
if(bkey[i].press==1&&bkey[i].long_flag==0)
bkey[i].short_flag=1;
bkey[i].age=0;
bkey[i].press=0;
bkey[i].long_flag=0;
}
}
if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
/*长按后抬起执行*/
//void key_serv_long()
//{
// uint8_t key_sta=key_read();
// if(key_sta!=0)
// {
// bkey[key_sta].age++;
// if(bkey[key_sta].age>1) bkey[key_sta].press=1;
// }
// else
// {
// for(int i=0;i<5;i++)
// {
// if(bkey[i].age>69) bkey[i].long_flag=1;
// if(bkey[i].press==1&&bkey[i].long_flag==0)
// bkey[i].short_flag=1;
// bkey[i].age=0;
// bkey[i].press=0;
// }
// }
//}
void key_serv_double()
{
uint8_t key_sta=key_read();
if(key_sta!=0)
{
bkey[key_sta].age++;
if(bkey[key_sta].age>1) bkey[key_sta].press=1;
}
else
{
for(int i=0;i<5;i++)
{
if(bkey[i].double_ageEN==1&&bkey[i].press==1)
{
bkey[i].double_flag=1;
bkey[i].double_ageEN=0;
bkey[i].press=0;
}
if(bkey[i].press&&bkey[i].long_flag==0)
{
bkey[i].double_ageEN=1;
}
if(bkey[i].double_ageEN==1)bkey[i].double_age++;
if(bkey[i].double_ageEN==1&&bkey[i].double_age>20)
{
bkey[i].short_flag=1;
bkey[i].double_ageEN=0;
bkey[i].double_age=0;
}
bkey[i].age=0;
bkey[i].press=0;
bkey[i].long_flag=0;
}
}
if(bkey[key_sta].age>69) bkey[key_sta].long_flag=1;
}
i2c_hal.c
//
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 eeprom_write(uint8_t addr,uint8_t dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
uint8_t eeprom_read(uint8_t addr)
{
uint8_t rec;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
rec=I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return rec;
}
void eeprom_write16(uint8_t addr,uint16_t dat)
{
uint8_t dat_h=dat>>8;
uint8_t dat_l=dat;
eeprom_write(addr,dat_h);
HAL_Delay(10);
eeprom_write(addr+1,dat_l);
HAL_Delay(10);
}
uint16_t eeprom_read16(uint8_t addr)
{
uint8_t dat_h=eeprom_read(addr);
uint8_t dat_l=eeprom_read(addr+1);
return (dat_h<<8)+dat_l;
}
void eeprom_write_f(uint8_t addr,float dat)
{
uint16_t dat_f=dat*100;
uint8_t dat_h=dat_f>>8;
uint8_t dat_l=dat_f;
eeprom_write(addr,dat_h);
HAL_Delay(10);
eeprom_write(addr+1,dat_l);
HAL_Delay(10);
}
float eeprom_read_f(uint8_t addr)
{
uint8_t dat_h=eeprom_read(addr);
uint8_t dat_l=eeprom_read(addr+1);
return ((dat_h<<8)+dat_l)/100.0f;
}
void mcp_write(uint8_t dat)
{
I2CStart();
I2CSendByte(0x5e);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}