用的U8glib写的,用了两种方式,位图和字符输出
,代码直接打包放下面,默认i2c接线A4 A5。我用的arduino nano ,OLED显示屏显示的位置根据自己需要改代码中相应位置
源程序如下:
- #include "U8glib.h"
- U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
- //U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI SDA A4 SCL A5
- #define u8g_logo_width 38
- #define u8g_logo_height 24
- //static unsigned char u8g_logo_bits[] = {
- int flash=0;
- const uint8_t rook_bitmap5[] U8G_PROGMEM = {
- 0x00,0x00,0xFE,0xFF,0xFF,0xFE,0x00,0x00};//电池满
- const uint8_t rook_bitmap4[] U8G_PROGMEM = {
- 0x00,0x00,0xFE,0xFD,0xFD,0xFE,0x00,0x00};
- const uint8_t rook_bitmap3[] U8G_PROGMEM = {
- 0x00,0x00,0xFE,0xF9,0xF9,0xFE,0x00,0x00};
- const uint8_t rook_bitmap2[] U8G_PROGMEM = {
- 0x00,0x00,0xFE,0xE1,0xE1,0xFE,0x00,0x00};
- const uint8_t rook_bitmap1[] U8G_PROGMEM = {
- 0x00,0x00,0xFE,0x81,0x81,0xFE,0x00,0x00};
- const uint8_t rook_bitmap0[] U8G_PROGMEM = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //电池空
- //读取vcc电压
- long readVcc() {
- long result;
- // Read 1.1V reference against AVcc
- ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
- delay(2); // Wait for Vref to settle
- ADCSRA |= _BV(ADSC); // Convert
- while (bit_is_set(ADCSRA,ADSC));
- result = ADCL;
- result |= ADCH<<8;
- result = 1126400L / result; // Back-calculate AVcc in mV
- return result;
- }
- void loop(void) {
- float vcc_vol=readVcc()/1000; //将读取的vcc电压mv转换为v
- //旋转90°
- //u8g.setRot90(); //or setRot180(); setRot270();
- u8g.setFont(u8g_font_gdr14r);//坐标函数 字体osb18
- // u8g.setPrintPos(0, 20); //定位电池电量图标的起始位置
- if (vcc_vol >=4.5 ){
- u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap5);// u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap0);
- }
- if (vcc_vol <4.5 && vcc_vol>=4 ){
- u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap4);
- }
- if (vcc_vol <4 && vcc_vol>=3.5 ){
- u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap3);
- }
- if (vcc_vol <3.5 && vcc_vol>=3 ){
- u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap2);
- }
- if (vcc_vol <3 ){ //当VCC电压小于3v时图标闪烁,电池电量不足警告
- switch (flash){
- case 0:
- u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap1);
- flash=1;
- break;
- case 1:
- u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, rook_bitmap0);
- flash=0;
- break;
- }
- }
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
XBM.zip
(1.24 KB, 下载次数: 65)
|