[小黑科技] 破解一個電控升降桌,並改形成語音控制(上)—— 模擬中間人與控制手柄交互


一、電路圖破譯

其電路板以下,是一個程序員專用升降桌的控制手柄:html

簡單看電路板,其原理圖猜想是8路並轉串,用來收集按鍵按下信息;兩個串轉8路並來驅動3個7段數碼管:git


二、破解其數碼管顯示原理

所以簡單寫一個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]微信


三、讓數碼管根據本身的意願顯示

所以,數字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位數,是經過快速顯示個十百位來實現的,代碼實現以下:app

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中)ui


四、破解其按鍵操做邏輯

簡單寫一個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);
    }
}

按動不一樣按鍵,看讀取的數據:.net

按鍵 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




: 小黑科技:將非智能升降桌改成智能升降桌
: 你們以爲不錯,能夠點推薦給更多人~


[1]. 亮紅色共陽0.36英寸3位數碼管共陰紅光3361AS/BS
[2]. 單片機芯片之——圖解74HC595(第一部分)
[3]. ESP8266開發之旅 基礎篇③ ESP8266與Arduino的開發說明


@beautifulzzzz
以藍牙技術爲基礎的的末梢無線網絡系統架構及創新型應用探索!
領域:智能硬件、物聯網、自動化、前沿軟硬件
博客:https://www.cnblogs.com/zjutlitao/
微信交流羣|微信:園友交流羣|btfzzzz
相關文章
相關標籤/搜索