1、電路圖破譯
其電路板如下,是一個程序員專用升降桌的控制手柄:
簡單看電路板,其原理圖猜測是8路並轉串,用來收集按鍵按下信息;兩個串轉8路並來驅動3個7段數碼管:
2、破解其數碼管顯示原理
因此簡單寫一個74HC595串行寫數據邏輯,分別寫一些數據觀察顯示效果(代碼基於ESP8266 RTOS的GPIO DEMO改造而來):
#define GPIO_D4 2
#define GPIO_D5 14
#define GPIO_D6 12
#define GPIO_D7 13
#define GPIO_D8 15
#define GPIO_SCK GPIO_D5
#define GPIO_MISO GPIO_D6
#define GPIO_MOSI GPIO_D7
#define GPIO_NSS GPIO_D4
#define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_SCK) | (1ULL<<GPIO_MOSI) | (1ULL<<GPIO_NSS))
#define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_MISO))
static void gpio_task_example(void *arg)
{
int d = 0xFFDF;//待傳輸給74HC595的16bits數據
int x = 0;
while (1) {
vTaskDelay(100 / portTICK_RATE_MS);
gpio_set_level(GPIO_MOSI, (d>>x) & 0x01);//數據的一bit
gpio_set_level(GPIO_SCK, 0);//SCK上升沿數據移入
vTaskDelay(1 / portTICK_RATE_MS);
gpio_set_level(GPIO_SCK, 1);
x++;
if(x == 16){//當所有16bits數據都送入了,NSS給一個上升沿,數據送到並口
x=0;
gpio_set_level(GPIO_NSS, 0);
vTaskDelay(1 / portTICK_RATE_MS);
gpio_set_level(GPIO_NSS, 1);
}
}
}
void app_main(void)
{
gpio_config_t io_conf;
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
//bit mask of the pins that you want to set,e.g.GPIO15/16
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//disable pull-down mode
io_conf.pull_down_en = 0;
//disable pull-up mode
io_conf.pull_up_en = 0;
//configure GPIO with the given settings
gpio_config(&io_conf);
//bit mask of the pins, use GPIO4/5 here
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
//set as input mode
io_conf.mode = GPIO_MODE_INPUT;
//enable pull-up mode
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
while (1) {
vTaskDelay(1000000 / portTICK_RATE_MS);
}
}
其中74HC595的原理參考:https://blog.csdn.net/weixin_41445387/article/details/80500046
其中3位數7段碼類似這樣:[#2]#2
3、讓數碼管根據自己的意願顯示
因此,數字0~9對應的編碼如下(是16bits data的高8bits):
數字 | 編碼 | 十六進制 |
---|---|---|
0 | 1111 1100 | 0xFC |
1 | 0110 0000 | 0x60 |
2 | 1101 1010 | 0xDA |
3 | 1111 0010 | 0xF2 |
4 | 0110 0110 | 0x66 |
5 | 1011 0110 | 0xB6 |
6 | 1011 1110 | 0xBE |
7 | 1110 0000 | 0xE0 |
8 | 1111 1110 | 0xFE |
9 | 1111 0110 | 0xF6 |
讓上述0~9數字顯示在不同位是通過16bits中的低字節的高3bits實現的:
位 | 編碼 |
---|---|
百位 | xxxx xxxx 0010 0000 |
十位 | xxxx xxxx 0100 0000 |
個位 | xxxx xxxx 1000 0000 |
如果想要顯示一個3位數,是通過快速顯示個十百位來實現的,代碼實現如下:
char digital[10] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6};//0~9
char position[3] = {0x80,0x40,0x20};//百十個
void app_show_digital(int d){//顯示1位數字
for(int i=0;i<16;i++){
gpio_set_level(GPIO_MOSI, (d>>i) & 0x01);
gpio_set_level(GPIO_SCK, 0);
gpio_set_level(GPIO_SCK, 1);
}
gpio_set_level(GPIO_NSS, 0);
gpio_set_level(GPIO_NSS, 1);
}
void app_show_num(int num){//分時復用顯示3位數字
if(num < 0 || num > 999)return;
static int show_pos = 0;
int gsb[3];//個十百
gsb[0] = digital[num%10];//個
gsb[1] = digital[num/10%10];//十
gsb[2] = digital[num/100];//百
app_show_digital(gsb[show_pos] << 8 | position[show_pos]);
show_pos++;
if(show_pos == 3)show_pos = 0;
}
int show_num = 0;
static void gpio_task_example(void *arg)
{
while (1) {
app_show_num(show_num);
vTaskDelay(9 / portTICK_RATE_MS);
}
}
注:這里有一點要注意,在make config中,將RTOS的頻率從100Hz調到200Hz,否則vTaskDelay(10 / portTICK_RATE_MS) 中10以下的值全都作為0處理,會導致看門狗被餓死而系統重啟(看門狗的26.1S超時也在config中)
4、破解其按鍵操作邏輯
簡單寫一個74HC165串行讀數據邏輯,用串口將讀取的數據打印下來(代碼基於ESP8266 RTOS的GPIO DEMO改造而來):
char app_read_key(void){
char data = 0;
for(int i=0;i<8;i++){
gpio_set_level(GPIO_SCK, 0);
gpio_set_level(GPIO_SCK, 1);
char bit = gpio_get_level(GPIO_MISO) == 0?0:1;
data <<= 1;
data |= bit;
}
return data;
}
int show_num = 0;
static void gpio_task_example(void *arg)
{
while (1) {
app_show_num(show_num);
ESP_LOGI(TAG, "key: %X\n", app_read_key());
vTaskDelay(9 / portTICK_RATE_MS);
}
}
按動不同按鍵,看讀取的數據:
按鍵 | CODE | 二進制 |
---|---|---|
不按 | 7F | 0111 1111 |
L1 上 | 7D | 0111 1101 |
L2 下 | 7B | 0111 1011 |
L3 一檔 | 77 | 0111 0111 |
L4 二檔 | 6F | 0110 1111 |
L5 三檔 | 5F | 0101 1111 |
L6 四檔 | 3F | 0011 1111 |
- : 小黑科技:將非智能升降桌改為智能升降桌
- 大家覺得不錯,可以點推薦給更多人~
LINKS
[1]. 亮紅色共陽0.36英寸3位數碼管共陰紅光3361AS/BS
[2]. 單片機芯片之——圖解74HC595(第一部分)
[3]. ESP8266開發之旅 基礎篇③ ESP8266與Arduino的開發說明
@beautifulzzzz
以藍牙技術為基礎的的末梢無線網絡系統架構及創新型應用探索!
領域:智能硬件、物聯網、自動化、前沿軟硬件
博客:https://www.cnblogs.com/zjutlitao/
微信交流群|微信:園友交流群|btfzzzz