江西景德镇建设厅网站广州响应式网站制作
web/
2025/10/7 23:32:06/
文章来源:
江西景德镇建设厅网站,广州响应式网站制作,深圳注册公司个人数字证书,邢台市文章目录 一、简介#xff08;1#xff09;U8g2#xff08;2#xff09;U8x8 二、配置要求三、移植步骤#xff08;1#xff09;文件准备和添加#xff08;2#xff09;实现回调接口(I2C的读写函数)①软件I2C②硬件I2C #xff08;3#xff09;功能裁剪① u8g2_d_set… 文章目录 一、简介1U8g22U8x8 二、配置要求三、移植步骤1文件准备和添加2实现回调接口(I2C的读写函数)①软件I2C②硬件I2C 3功能裁剪① u8g2_d_setup.c② u8g2_d_memory.c③ 关于字库 在低端芯片上使用U8G2
一、简介
U8g2:单色显示库版本2
U8g2是一个用于嵌入式设备的单色图形库。U8g2支持单色oled和lcd其中包括以下控制器:Ssd1305、ssd1306、ssd1309、ssd1312、ssd1316、ssd1318、ssd1320、ssd1322、ssd1325、ssd1327、ssd1327、ssd1327、ssd1327、ssd1327、ssd1327、ssd1106、sh1107、sh1108、ssd1607、hx1230、uc1601、uc1604、uc1608、pcd8544、pcf8812、hx1230、uc1601、uc1611、uc1617、uc1638、uc1701、st7511、st7528、st7565、st7567、st7586、st7588、st75160、st75256、st75320、nt7534、st7920、ist3020、ist3088、ist7920、ld7032、ks0108、ks0713、hd44102、t7932、sed1520、sbn1661、il3820、max7219、gp1287、gp1247、GU800(查看完整列表)。
Arduino库U8g2可以从Arduino IDE的库管理器中安装。U8g2还包括U8x8库:
1U8g2 包括所有图形程序(线/框/圆绘制)。 支持多种字体。(几乎)对字体高度没有限制。 在微控制器中需要一些内存来呈现显示。
2U8x8 仅文本输出(字符)设备。 只允许适合8x8像素网格的字体。 直接写入显示。在微控制器中不需要缓冲区。
二、配置要求
最少要求 ROM122354125630472331272134280122850241228810119210723986byte RAM0x800x010x81129byte RAM范围[128,1024]越大刷新越快。 Demo工程整体大小NOSIICGPIOU8G2
Total RO Size (Code RO Data) 9472 ( 9.25kB)Total RW Size (RW Data ZI Data) 880 ( 0.86kB)Total ROM Size (Code RO Data RW Data) 9492 ( 9.27kB)
三、移植步骤
1文件准备和添加
下载文件(Gitee)解压csrc到工程根目录keil添加目录文件
注意带_d_是用户自定义文件,可以移动到工程中修改。
keil添加头文件 [csrc的目录] 添加驱动文件 u8g2里面支持多种驱动芯片以u8x8_d_xxx.c命名的就是驱动文件本文使用的是0.96吋oled芯片是ssd1306因此只需将u8x8_d_ssd1312_128x64_noname.c这个驱动文件添加到工程中
2实现回调接口(I2C的读写函数)
void u8g2_Setup_ssd1306_i2c_128x64_noname_1(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
byte_cb是通信相关的函数比如i2c写数据 gpio_and_delay_cb是延时相关的函数。
①软件I2C u8g2_Setup_ssd1306_i2c_128x64_noname_1(u8g2, U8G2_R0, u8x8_byte_sw_i2c, gpio_and_delay); // init u8g2 structure u8x8_byte_sw_i2c官方已经实现 –u8x8_byte.c我们需要实现gpio_and_delay函数
uint8_t gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {//printf(%s:msg %d,arg_int %d\r\n,__FUNCTION__,msg,arg_int);switch(msg) {case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds__NOP();break;case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro secondsfor (uint16_t n 0; n 320; n) {__NOP();} break;case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli secondHAL_Delay(1);break;case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 400 KHz//delay 5usdelay_us(5); // arg_int1: delay by 5us, arg_int 4: delay by 1.25uscase U8X8_MSG_GPIO_I2C_CLOCK: // arg_int0: Output low at I2C clock pinif(arg_int 1) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);} else if(arg_int 0) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET); } break; // arg_int1: Input dir with pullup high for I2C clock pincase U8X8_MSG_GPIO_I2C_DATA: // arg_int0: Output low at I2C data pin// printf(U8X8_MSG_GPIO_I2C_DATA:%d\r\n,arg_int);if(arg_int 1) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);} else if(arg_int 0) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); } break; // arg_int1: Input dir with pullup high for I2C data pincase U8X8_MSG_GPIO_MENU_SELECT:u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);break;case U8X8_MSG_GPIO_MENU_NEXT:u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);break;case U8X8_MSG_GPIO_MENU_PREV:u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);break;case U8X8_MSG_GPIO_MENU_HOME:u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);break;default:u8x8_SetGPIOResult(u8x8, 1); // default return valuebreak;}return 1;
}②硬件I2C
跟软件实现方式类似 u8g2_Setup_ssd1306_i2c_128x64_noname_1(u8g2, U8G2_R0, u8x8_byte_hw_i2c, gpio_and_delay); // init u8g2 structure #define I2C_SPEED_RATE 100 //uint khz
I2C_HandleTypeDef i2c_test {0};
FlagStatus i2c_int RESET;main:
{__HAL_RCC_I2C_CLK_ENABLE();gpioi2c.Pin GPIO_PIN_5;gpioi2c.Mode GPIO_MODE_AF; // GPIO端口复用功能 gpioi2c.Alternate GPIO_AF4_I2C_SDA;gpioi2c.OpenDrain GPIO_OPENDRAIN; // 开漏输出gpioi2c.Debounce.Enable GPIO_DEBOUNCE_DISABLE; // 禁止输入去抖动gpioi2c.SlewRate GPIO_SLEW_RATE_HIGH; // 电压转换速率gpioi2c.DrvStrength GPIO_DRV_STRENGTH_HIGH; // 驱动强度gpioi2c.Pull GPIO_PULLUP; // 上拉HAL_GPIO_Init(GPIOB, gpioi2c);gpioi2c.Pin GPIO_PIN_4;gpioi2c.Mode GPIO_MODE_AF;gpioi2c.Alternate GPIO_AF4_I2C_SCL;gpioi2c.OpenDrain GPIO_OPENDRAIN; gpioi2c.Debounce.Enable GPIO_DEBOUNCE_DISABLE;gpioi2c.SlewRate GPIO_SLEW_RATE_HIGH;gpioi2c.DrvStrength GPIO_DRV_STRENGTH_HIGH;gpioi2c.Pull GPIO_PULLUP; HAL_GPIO_Init(GPIOB, gpioi2c);i2c_test.Instance I2C;i2c_test.Init.master I2C_MASTER_MODE_ENABLE; // 主机模式使能i2c_test.Init.slave I2C_SLAVE_MODE_DISABLE; // 从机模式禁止i2c_test.Mode HAL_I2C_MODE_MASTER; // 主机模式i2c_test.Init.broadack I2C_BROAD_ACK_DISABLE; // 广播地址应答禁止i2c_test.Init.speedclock I2C_SPEED_RATE; // I2C传输速率 i2c_test.State HAL_I2C_STATE_RESET; //HAL_I2C_Init(i2c_test);
}uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */static uint8_t buf_idx;uint8_t *data;switch(msg){case U8X8_MSG_BYTE_SEND:data (uint8_t *)arg_ptr; while( arg_int 0 ){buffer[buf_idx] *data;data;arg_int--;} break;case U8X8_MSG_BYTE_INIT:/* add your custom code to init i2c subsystem */break;case U8X8_MSG_BYTE_START_TRANSFER:buf_idx 0;break;case U8X8_MSG_BYTE_END_TRANSFER:HAL_I2C_Master_Transmit(i2c_test, buffer[0], buffer[1], buf_idx - 1);break;default:return 0;}return 1;
}uint8_t gpio_and_delay(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
{switch(msg){case U8X8_MSG_GPIO_AND_DELAY_INIT:break;case U8X8_MSG_DELAY_MILLI:HAL_Delay(arg_int);break;case U8X8_MSG_GPIO_I2C_CLOCK: break; case U8X8_MSG_GPIO_I2C_DATA: break;default: return 0;}return 1; // command processed successfully.
}到此移植完毕但是编译失败空间不足
3功能裁剪
① u8g2_d_setup.c
注释全部函数找到u8g2_Setup_ssd1306_i2c_128x64_noname_1函数解除注释。 注用缓存128举例。有条件的可以使用2561024. u8g2_Setup_ssd1306_i2c_128x64_noname_1() --缓存128byte u8g2_Setup_ssd1306_i2c_128x64_noname_2() --缓存256byte u8g2_Setup_ssd1306_i2c_128x64_noname_f() --缓存1024byte
② u8g2_d_memory.c
注释全部函数找到u8g2_m_16_8_1函数解除注释。
注用缓存128举例。有条件的可以使用2561024. u8g2_Setup_ssd1306_i2c_128x64_noname_1() --缓存128byte u8g2_Setup_ssd1306_i2c_128x64_noname_2() --缓存256byte u8g2_Setup_ssd1306_i2c_128x64_noname_f() --缓存1024byte
u8g2_Setup_ssd1306_128x64_noname_1、
u8g2_Setup_ssd1306_128x64_noname_2、
u8g2_Setup_ssd1306_128x64_noname_f
这些都是spi接口的
u8g2_Setup_ssd1306_i2c_128x64_noname_1、
u8g2_Setup_ssd1306_i2c_128x64_noname_2、
u8g2_Setup_ssd1306_i2c_128x64_noname_f
这些都是i2c接口的
后缀1、2、f代表缓冲区大小的不同
1代表128字节
2代表256字节
f代表1024字节
根据单片机空间的大小选择合适的接口缓冲区小的刷新lcd/oled的时候就比较耗时反之。③ 关于字库
“u8g2_fonts.c”文件中定义了各种字库这些字库比较占用空间根据使用情况屏蔽掉没有使用的。
demo工程gitee
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88747.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!