写一个stm32f1 HAL库 stemwin制作一个0-9键盘,具备小数及删除功能, 键盘输入数值后,按确定键后改变fm25cl64地址0x100数据
时间: 2024-04-30 17:20:06 浏览: 195
基于stm32f1键盘设计
以下是一个基于STM32F1 HAL库和STemWin的0-9数字键盘的示例代码,包括小数点和删除功能,以及将输入的值保存在FM25CL64存储器中的功能。
```
#include "main.h"
#include "GUI.h"
#include "DIALOG.h"
#include "stm32f1xx_hal.h"
#define EEPROM_ADDR 0xA0
static WM_HWIN hWin;
static int16_t value = 0;
static uint8_t decimal = 0;
static void update_display(void)
{
char buf[16];
sprintf(buf, "%d.%d", value, decimal);
EDIT_SetText(WM_GetDialogItem(hWin, GUI_ID_EDIT0), buf);
}
static void store_value(void)
{
uint8_t data[2];
data[0] = (uint8_t)value;
data[1] = decimal;
HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDR, 0x100, I2C_MEMADD_SIZE_16BIT, data, 2, HAL_MAX_DELAY);
}
static void load_value(void)
{
uint8_t data[2];
HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDR, 0x100, I2C_MEMADD_SIZE_16BIT, data, 2, HAL_MAX_DELAY);
value = (int16_t)data[0];
decimal = data[1];
update_display();
}
static void on_key(WM_MESSAGE *pMsg)
{
int id = WM_GetId(pMsg->hWinSrc);
int code = pMsg->Data.v;
if (id == GUI_ID_BUTTON_DEL) {
if (decimal) {
decimal = 0;
} else {
value /= 10;
}
update_display();
} else if (id == GUI_ID_BUTTON_POINT) {
decimal = 1;
update_display();
} else if (id >= GUI_ID_BUTTON_0 && id <= GUI_ID_BUTTON_9) {
int digit = id - GUI_ID_BUTTON_0;
if (decimal) {
if (digit < 10) {
decimal *= 10;
value += digit * decimal / 10;
decimal /= 10;
update_display();
}
} else {
value = value * 10 + digit;
update_display();
}
} else if (id == GUI_ID_BUTTON_OK) {
store_value();
} else if (id == GUI_ID_BUTTON_CANCEL) {
load_value();
}
}
static void create_keyboard(void)
{
BUTTON_Handle hButton;
int x, y, i;
const int button_width = 40;
const int button_height = 30;
const int margin_h = 10;
const int margin_v = 10;
hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), on_key, WM_HBKWIN, 0, 0);
WM_SetFocus(hWin);
x = margin_h;
y = margin_v;
for (i = 0; i < 10; i++) {
hButton = BUTTON_CreateEx(x, y, button_width, button_height, hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON_0 + i);
BUTTON_SetText(hButton, "0" + i);
x += button_width + margin_h;
if (i == 2 || i == 5) {
x = margin_h;
y += button_height + margin_v;
}
}
hButton = BUTTON_CreateEx(x, y, button_width, button_height, hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON_POINT);
BUTTON_SetText(hButton, ".");
x += button_width + margin_h;
hButton = BUTTON_CreateEx(x, y, button_width, button_height, hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON_DEL);
BUTTON_SetText(hButton, "Del");
x += button_width + margin_h;
hButton = BUTTON_CreateEx(x, y, button_width, button_height, hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON_OK);
BUTTON_SetText(hButton, "OK");
x += button_width + margin_h;
hButton = BUTTON_CreateEx(x, y, button_width, button_height, hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON_CANCEL);
BUTTON_SetText(hButton, "Cancel");
x += button_width + margin_h;
load_value();
update_display();
}
void main(void)
{
GUI_Init();
create_keyboard();
while (1) {
GUI_Delay(100);
}
}
```
在上面的代码中,我们使用了一个对话框来创建0-9数字键盘。每个数字键都有一个ID,用于在按下键时进行处理。我们还添加了一个小数点按钮和一个删除按钮。在按下数字键时,我们会根据当前是否有小数点来更新当前值。在按下删除键时,我们会删除最后输入的数字或小数点。在按下确定键时,我们将当前值保存到FM25CL64存储器的0x100地址处。在按下取消键时,我们将从存储器中加载上一个保存的值。
请注意,上面的代码仅供参考,并可能需要根据您的具体需求进行修改。
阅读全文