突击蓝桥杯嵌入式(十一)——长短按、EEPROM多字节、多数据类型读写
前言:今天是6.14,作业很多、考试很多,但考虑到6.18比赛,还是象征性的挣扎一下,国赛不考拓展版的话,只能是考一点比较阴间的东西,我感觉可能按键和EEPROM这方面他会作妖,所以今天浅浅的写一篇博文。
一、按键的长短按
首先我们先说一下思想:
大概就是一个延时函数,然后根据按下的时间长短去判断,其实最好是用状态机,但是我水平有限,不会写,所以只能够用最普通的方法去做这个东西,能用,效果也凑合。
声明变量区
__IO uint32_t uwTick_Key_Speed;
uint8_t key_value;
uint8_t key_up;
uint8_t key_old;
uint8_t key_down;
uint8_t count_key[4];
float test_numer;
uint8_t str[21];
来个Scan函数
uint8_t Key_Scan()
{
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) == GPIO_PIN_RESET) return 1;
else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1) == GPIO_PIN_RESET) return 2;
else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2) == GPIO_PIN_RESET) return 3;
else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == GPIO_PIN_RESET) return 4;
else return 0;
}
Key进程
void Key_Proc() //180ms, if count>3 ->long press
{
if(uwTick - uwTick_Key_Speed < 180) return; //if time > 540ms -> long press
else uwTick_Key_Speed = uwTick;
key_value = Key_Scan();
if(key_value != key_old) //感受到按键发生变换,表明松开,count重新计
{
for(int i = 0;i<4;i++)
{
count_key[i] = 0;
}
}
key_old = key_value;
switch (key_value) //仅以按键1举例
{
case 1:
count_key[0]++;
if(count_key[0] > 3)
{
test_numer += 0.3;
}
else if(count_key[0] == 1)
{
test_numer += 0.1;
}
break;
case 2:
test_numer += 0.3;
break;
case 3:
count_key[2]++;
break;
case 4:
count_key[3]++;
break;
}
}
二、EEPROM进阶读写
先说存float,float,其实一般的小数,可以把他扩大100 0000倍这样子,然后拆开存进去,然后取时候在除以100 0000,这样就很方便,问题就转化成了多字节读写。
1.前置知识点
(1)拆分多字节数据
&0xff和移位的作用,如下:
#include <stdio.h>
int x = 45324;
int x_1;
int x_2;
int main(int argc, char** argv)
{
x_1 = x & 0xff;
x_2 = x>>8 & 0xff;
printf("%#x\n", x);
printf("%#x\n",x_1);
}
输出为:
0xb10c
0xc
0xb1
可见我们利用&0xff 和>> 可以拆分字节。
(2)合并多字节数据
第一位 + 第二位<<8 + 第三位 <<16 + 第四位 <<24
#include <stdio.h>
int x = 45324;
int x_1;
int x_2;
int x_sum;
int main(int argc, char** argv)
{
x_1 = x & 0xff;
x_2 = x >> 8 & 0xff;
x_sum = x_1 + (x_2 << 8); //合并
printf("%#x\n", x);
printf("%#x\n",x_1);
printf("%#x\n", x_2);
printf("%#x\n", x_sum);
}
输出为:
0xb10c
0xc
0xb1
0xb10c
2.IIC函数封装
先把最基础的读写整一下
void i2c_write(u8 add ,u8 reg ,u8 data)
{
I2CStart();
I2CSendByte(add);
I2CWaitAck();
I2CSendByte(reg);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
}
uint8_t i2c_read(u8 add ,u8 reg)
{
u8 data ;
I2CStart();
I2CSendByte(add);
I2CWaitAck();
I2CSendByte(reg);
I2CWaitAck();
I2CStart();
I2CSendByte(add+1);
I2CWaitAck();
data =I2CReceiveByte();
I2CWaitAck();
I2CStop();
return data;
}
然后我们来封装多字节读写函数
void iic_write_four_byte(uint8_t add,uint8_t reg,uint32_t data_float) //写
{
static uint8_t data_float_part[4];
data_float_part[0] = data_float & 0xff;
data_float_part[1] = (data_float>>8) & 0xff;
data_float_part[2] = (data_float>>16) & 0xff;
data_float_part[3] = (data_float>>24) & 0xff;
i2c_write(add,reg,data_float_part[0]);
HAL_Delay(5);
i2c_write(add,reg+1,data_float_part[1]);
HAL_Delay(5);
i2c_write(add,reg+2,data_float_part[2]);
HAL_Delay(5);
i2c_write(add,reg+3,data_float_part[3]);
HAL_Delay(5);
}
读
uint32_t iic_read_four_byte(uint8_t add,uint8_t reg)
{
static uint8_t data_float_part[4];
static uint32_t data_sum;
data_float_part[0] = i2c_read(add,reg);
HAL_Delay(5);
data_float_part[1] = i2c_read(add,reg+1);
HAL_Delay(5);
data_float_part[2] = i2c_read(add,reg+2);
HAL_Delay(5);
data_float_part[3] = i2c_read(add,reg+3);
HAL_Delay(5);
data_sum = data_float_part[0] + (data_float_part[1]<<8) + (data_float_part[2]<<16) + (data_float_part[3]<<24);
return data_sum;
}
存取数据我们只需要:
iic_write_four_byte(0xa0,2,(uint32_t)(i2c_number*1000000));
i2c_read_number = iic_read_four_byte(0xa0,2) / 1000000;