[ESP32 IDF+Vscode]OLED多级菜单显示(摇杆控制)

ADC介绍

ESP32集成了两个 12SAR(逐次逼近寄存器)adc ,共支持18个测量通道。

1、两个 ADC 通道简介:

ADC1:

  • 支持 8 个通道,包括:GPIO32 - GPIO39(并非按顺序)

ADC2:

  • 支持 10 个通道,包括:GPIO0, GPIO2, GPIO4, GPIO12 - GPIO15, GOIO25 - GPIO27(并非按顺序)

ESP32 内置霍尔传感器,采用 ADC1 的通道0和3(GPIO36 和 GPIO39)。如果想要使用ESP32内置的霍尔传感器,请不要对这两个GPIO做任何配置! 

接线方式

Ps2---        ----ESP32

VRY                ADC_CHANNEL_6(GPIO 34)

VRX                ADC_CHANNEL_7(GPIO 35)

SW                  GPIO_NUM_12

效果

 

 [ESP32 IDF+Vscode]OLED多级菜单显示(摇杆控制)_第1张图片

 代码

void Get_keyNum(void)
{
    uint8_t KeyNum = 0;
    BaseType_t err;
    //int val_x = adc1_get_raw(PS2_X_CHANNEL);

    if (adc1_get_raw(PS2_X_CHANNEL) ==4095) {
        Delay_ms(20);
        while (adc1_get_raw(PS2_X_CHANNEL)==4095);
        Delay_ms(20);
        KeyNum = 1;

    } else if (adc1_get_raw(PS2_X_CHANNEL)==0) {
        Delay_ms(20);
        while (adc1_get_raw(PS2_X_CHANNEL)==0);
        Delay_ms(20);
        KeyNum = 2;
    }
    // 检测SW按键是否按下
    if (btn_intr_flag == 1) {
        KeyNum = 3;
        btn_intr_flag = 0;
    }
    if((key_queue!=NULL)&&(KeyNum))
    {
        err=xQueueSend(key_queue,&KeyNum,10);
        if(err!= pdPASS)
        {
            printf("Send Failed\r\n");
        }
    }
    //printf("KeyNum:%d\n", KeyNum);
}

OLED

#include 
#include "menu.h"
#include "dht11.h"
#include "OLED.h"
#include "bmp.h"

#define Number 25
 
//定义时间,日期,星期变量
uint16_t hour = 17;
uint16_t minute = 51;
uint16_t second = 0;
uint16_t day = 27;
uint16_t month = 8;
uint16_t year = 23;
uint8_t week = 7;
 
 
//索引数组相关
void (*current_operation_index)(); //执行当前操作函数
uint8_t func_index = 0;
 

 
/*定义了索引数组,索引数组要配合主程序按键值进行选择*/
 Menu_table  table[26]=
{
	   //序号,下一项,确定,返回
    {0, 1, 4, 0,(*show1)},//一级界面    图片
    {1, 2, 11, 1,(*show2)},//一级界面   时间
    {2, 3, 12, 2,(*show3)},//一级界面   led
    {3, 0, 13, 3,(*show4)},//一级界面   游戏
		
		//图片的下层界面
	{4, 5, 4, 0, (*show5)},  //二级界面 猫
	{5, 6, 5, 0, (*show10)}, //二级界面
	{6, 7, 6, 0, (*show6)}, //二级界面
    {7, 8, 7, 0, (*show7)}, //二级界面
    {8, 9, 8, 0, (*show8)}, //二级界面
    {9, 10, 9, 0,( *show9)},    //二级界面
    {10, 4 ,10, 0, (*show12)},  //二级界面 
		
		//时间的下层界面
    {11, 11, 11, 1, (*show13)},  //二级界面  时间
    //LED的下层界面
    {12, 12, 2, 2,(*show14)}, //二级界面 		//控制led
		//游戏的下层界面
	{13, 13, 13, 3, (*show11)}, //二级界面 
		
};

 
//按键
void  Menu_key_set(uint8_t KeyNum)
{
    OLED_Clear();
    if(KeyNum == 1)
    {   
        func_index=table[func_index].next;//按键next按下后的索引号
        OLED_Clear();
    }
    if(KeyNum == 3)
    {
        func_index=table[func_index].enter;
        OLED_Clear();
    }
    if(KeyNum == 2)
    {   
        func_index=table[func_index].back;//按键next按下后的索引号
        OLED_Clear();
    }
	current_operation_index=table[func_index].current_operation;//执行当前索引号所对应的功能函数。
    (*current_operation_index)();//执行当前操作函数


}
 
 
 
//显示菜单函数
 
void show0(void)//眼睛
{
    OLED_DrawBMPFast(yanjing);
}
 
void show1(void)//图片
{
    OLED_DrawBMPFast(tupian);
 
}
 
void show2(void)//时间
{
    OLED_ShowCHinese(50,3,0);
    OLED_ShowCHinese(66,3,1);
     
}
 
void show3(void)//LED
{
    OLED_DrawBMPFast(led);

}
 
void show4(void)//游戏图标
{
    OLED_DrawBMPFast(youxiji);
}
 
//显示图片
void show5(void)
{
    OLED_DrawBMPFast(cat);
}
void show6(void)
{
    OLED_DrawBMPFast(baozi);
}
void show7(void)
{
    OLED_DrawBMPFast(baoziyi);
}
void show8(void)
{
    OLED_DrawBMPFast(daishu);
}
void show9(void)
{
 
    OLED_DrawBMPFast(mapi);
}
void show10(void)
{
    OLED_DrawBMPFast(dog);
}
//显示太空人
void show11(void)
{
 
    OLED_DrawBMPFast(gImage_1);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_2);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_3);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_4);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_5);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_6);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_7);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_8);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_9);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_10);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_11);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_12);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_13);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_14);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_15);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_16);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_17);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_18);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_19);
    Delay_ms(Number);
    OLED_DrawBMPFast(gImage_20);
    Delay_ms(Number);
}
void show12(void)
{
    OLED_ShowCHinese(50,3,9);
    OLED_ShowCHinese(66,3,10);
}
 
//显示时间
void show13(void)
{
    OLED_ShowCHinese(0,3,2);
    OLED_ShowCHinese(16,3,3);
    OLED_ShowChar(32,3,':',16);
    OLED_ShowNum(40,3,20,2,16);
    OLED_ShowNum(56,3,year/10,1,16);
    OLED_ShowNum(64,3,year%10,1,16);
    OLED_ShowChar(72,3,'-',16);
    OLED_ShowNum(80,3,month/10,1,16);
    OLED_ShowNum(88,3,month%10,1,16);
    OLED_ShowChar(98,3,'-',16);
    OLED_ShowNum(104,3,day/10,1,16);
    OLED_ShowNum(112,3,day%10,1,16);
    //显示时间
    OLED_ShowCHinese(0,1,0);
    OLED_ShowCHinese(16,1,1);
    OLED_ShowChar(32,1,':',16);
    OLED_ShowNum(40,1,hour/10,1,16);
    OLED_ShowNum(48,1,hour%10,1,16);
    OLED_ShowChar(56,1,':',16);
    OLED_ShowNum(64,1,minute/10,1,16);
    OLED_ShowNum(72,1,minute%10,1,16);
    OLED_ShowChar(80,1,':',16);
    OLED_ShowNum(88,1,second/10,1,16);
    OLED_ShowNum(96,1,second%10,1,16);
    //显示星期
    OLED_ShowCHinese(0,5,4);
    OLED_ShowCHinese(16,5,5);
    OLED_ShowChar(32,5,':',16);
    OLED_ShowNum(70,5,week,1,16);
}
 
//控制led
void show14(void)
{
    OLED_DrawBMPFast(dengbao);

}
//void show15(void)
//{
//}
//void show16(void)
//{
//}
 
//控制时间,日期与星期增加
void Date_Time(void)
{
        //显示日期
 
        //判断日期
        if(hour>=24)
        {
            day++;
            week++;
            if(week >= 7)week = 1;
            if(day==30)
            {
                month++;
                day = 0;
                if(month==12)
                {
                    year++;
                    month = 0;
                    if(year > 2050 || year < 2000) year = 0;
                }
            }
        }
}



 完整代码:

ESP32 IDF Vscode: ESP32开发 - Gitee.com

你可能感兴趣的:(ESP32,vscode,esp32)