任务2 按键切屏(arduino程序)

本教程使用的是SSD1306芯片的屏幕,同理也可使用其他屏幕

一、安装库U8glib.h

众所周知U8g是个万能库,我们就用此库来点亮一块12864oled屏

 

二、安装完库后选择hello world示例,将示例代码复制到新的工程中修改

选择这个芯片:

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);

三、学会使用取模软件

       有很多款取模软件,操作大同小异具体百度,再通过u8g.drawXBMP这条函数显示出来

四、按键判断:

       这里只使用了一个按键

void button_State()
{ 
//A
  if(digitalRead(button1)){ //检测按键是否按下
      delay(20); //延时去抖动
      if(digitalRead(button1)){//判断长短键
        openState++;
      }
      while(digitalRead(button1));
  }
}

最后,附上完整代码:

#include "U8glib.h"
#define button1 13               //触摸按键接口定义
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);

uint8_t openState = 0;
uint8_t c = 0;

static unsigned char deng[] U8G_PROGMEM = {0x00,0x00,0x7E,0x3E,0x40,0x22,0x40,0x12,0x42,0x12,0x24,0x0A,0x28,0x12,0x10,0x12,
0x10,0x22,0x28,0x22,0x28,0x22,0x44,0x16,0x42,0x0A,0x01,0x02,0x00,0x02,0x00,0x02,/*"邓",0*/};
static unsigned char zhao[] U8G_PROGMEM = {0x00,0x00,0xBE,0x3F,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x29,0xBE,0x10,0x22,0x3F,
0x22,0x21,0x22,0x21,0x22,0x21,0x3E,0x3F,0x00,0x00,0x12,0x11,0x22,0x22,0x21,0x22,/*"照",1*/};
static unsigned char quan[] U8G_PROGMEM = {0x08,0x00,0xC8,0x3F,0x88,0x20,0x88,0x20,0xBF,0x20,0x08,0x11,0x0C,0x11,0x1C,0x11,
0x2A,0x0A,0x2A,0x0A,0x09,0x04,0x08,0x04,0x08,0x0A,0x08,0x11,0x88,0x20,0x68,0x40,/*"权",2*/};

static unsigned char bmp[] U8G_PROGMEM = { 
0xFF,0xFF,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
0xFF,0xFF,0xFF,0xFF,0x1F,0xA8,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x3F,0x10,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xBF,0x01,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x23,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x00,0x00,0x00,0x00,0x00,0xFC,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0xFC,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0xFC,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0xFA,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0x00,0x00,0x00,0x00,0x00,0xF8,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x00,0x00,0x00,0x00,0x00,0xD8,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x5F,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0xFF,0xFF,0xFF,};


void button_State()
{ 
//A
  if(digitalRead(button1)){ //检测按键是否按下
      delay(20); //延时去抖动
      if(digitalRead(button1)){//判断长短键
        openState++;
      }
      while(digitalRead(button1));
  }
}

void draw0(void) {
  u8g.drawXBMP( 0, 16, 16, 16, deng);
  u8g.drawXBMP( 16, 16, 16, 16, zhao);
  u8g.drawXBMP( 32, 16, 16, 16, quan);
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 45, "DengZhaoquan:");
  u8g.drawStr( 0, 12, "190306238");

//  u8g.setPrintPos(42,45);
//  u8g.print(c++);
}

void draw1(void) {
//  u8g.drawXBMP( 0, 16, 16, 16, deng);
//  u8g.drawXBMP( 16, 16, 16, 16, zhao);
//  u8g.drawXBMP( 32, 16, 16, 16, quan);
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 45, "Numb:");

  u8g.setPrintPos(42,45);
  u8g.print(c++);
}

void draw2(void) {
  u8g.drawXBMP( 0, 0, 128, 64, bmp);
//  u8g.drawXBMP( 16, 16, 16, 16, zhao);
//  u8g.drawXBMP( 32, 16, 16, 16, quan);
  u8g.setFont(u8g_font_unifont);
//  u8g.drawStr( 0, 45, "190306238:");

  u8g.setPrintPos(42,45);
//  u8g.print(c++);
}

void mode_0()
{
  u8g.firstPage();  
  do {
    draw0();
  } while( u8g.nextPage() );  
}

void mode_1()
{
  u8g.firstPage();  
  do {
    draw1();
  } while( u8g.nextPage() );  
}

void mode_2()
{
  u8g.firstPage();  
  do {
    draw2();
  } while( u8g.nextPage() );  
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(button1, INPUT);
  pinMode(12, OUTPUT);
  digitalWrite(12, HIGH);
}

void loop() {
  button_State();
  // put your main code here, to run repeatedly:


  switch (openState)
  {
    case 0:
//      openState = 0;
      mode_0();     //
      break;
    case 1:
      mode_1();     //
      break;
    case 2:
      mode_2();     //
      break;
    case 3:
//      mode_3();     //
      break;
    default:

      break;
  }
  if(openState == 3) openState = 0;
  Serial.println(openState);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值