适用于STM32的U8G2回调函数例程

2024-06-01 08:36

本文主要是介绍适用于STM32的U8G2回调函数例程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

  1. U8g2 还包括 U8x8 库。U8g2 和 U8x8 的功能包括:

    • U8g2
      包括所有图形程序(线/框/圆画)
      支持很丰富的字体库
      需要微控制器中的一些内存来渲染显示屏(需要消耗较多的ram空间资源)
    • U8x8
      仅文本输出(字符)设备
      仅允许使用每个字符固定大小(8x8 像素)的字体
      直接写到显示屏上,无需微控制器中的缓冲(需要消耗较少的ram空间资源)
  2. 像素点点阵

    OLED其实就是一个M x n 的像素点阵,想显示什么就得把具体位置的像素点亮起来。对于每一个像素点,有可能是1点亮,也有可能是0点亮

  3. 坐标系

    在坐标系中,左上角是原点,向右是X轴正方向,向下是Y轴正方向。

回调函数

这里只有使用IIC驱动OLED的代码示例

软件IIC

//延时和GPIO初始化 回调函数
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){switch(msg){case U8X8_MSG_GPIO_AND_DELAY_INIT:	// called once during init phase of u8g2/u8x8delay_init();//初始化延时函数 IIC_Init();//软件IIC的IO初始化break;							// can be used to setup pinscase U8X8_MSG_DELAY_NANO:			// delay arg_int * 1 nano secondbreak;case U8X8_MSG_DELAY_100NANO:		// delay arg_int * 100 nano seconds__NOP();break;case U8X8_MSG_DELAY_10MICRO:		// delay arg_int * 10 micro secondsdelay_us(10*arg_int);break;case U8X8_MSG_DELAY_MILLI:			// delay arg_int * 1 milli seconddelay_ms(1*arg_int);break;case U8X8_MSG_DELAY_I2C:				// arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHzdelay_us(5*arg_int);break;							// arg_int=1: delay by 5us, arg_int = 4: delay by 1.25uscase U8X8_MSG_GPIO_D0:				// D0 or SPI clock pin: Output level in arg_int//case U8X8_MSG_GPIO_SPI_CLOCK:break;case U8X8_MSG_GPIO_D1:				// D1 or SPI data pin: Output level in arg_int//case U8X8_MSG_GPIO_SPI_DATA:break;case U8X8_MSG_GPIO_D2:				// D2 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D3:				// D3 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D4:				// D4 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D5:				// D5 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D6:				// D6 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D7:				// D7 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_E:				// E/WR pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_CS:				// CS (chip select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_DC:				// DC (data/cmd, A0, register select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_RESET:			// Reset pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_CS1:				// CS1 (chip select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_CS2:				// CS2 (chip select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_I2C_CLOCK:		// arg_int=0: Output low at I2C clock pinPBout(8) = arg_int;break;							// arg_int=1: Input dir with pullup high for I2C clock pincase U8X8_MSG_GPIO_I2C_DATA:			// arg_int=0: Output low at I2C data pinPBout(9) = arg_int;break;							// arg_int=1: 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;
}//测试主程序
int main(void) {//初始化u8g2u8g2_t u8g2;                                                                              // a structure which will contain all the data for one displayu8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay);// init u8g2 structureu8g2_InitDisplay(&u8g2);                                                                      // send init sequence to the display, display is in sleep mode after this,u8g2_SetPowerSave(&u8g2, 0);                                                                  // wake up displaywhile (1){/* Begin of U8G2*/static int x = 30,y = 10;u8g2_ClearBuffer(&u8g2);u8g2_SetFont(&u8g2, u8g2_font_10x20_mf);u8g2_DrawStr(&u8g2, x,y,"Data");if(x >= 70){x = y = 0;}else{x++;y++;}u8g2_SendBuffer(&u8g2);/* End of U8G2 */}
}

硬件IIC

下面代码适用于stm32f1系列的引脚IIC

//为u8g2提供IIC的API接口的函数
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;u8 i;switch(msg){case U8X8_MSG_BYTE_INIT://Init the IIC GPIOIIC_Init();break;case U8X8_MSG_BYTE_START_TRANSFER://clear the indexbuf_idx = 0;break;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_END_TRANSFER:IIC_Start();IIC_Send_Address_Write(0x78);//write your oled address in herefor(i = 0; i < buf_idx; i++){IIC_Send_Byte(buffer[i]);}IIC_Stop();break;case U8X8_MSG_BYTE_SET_DC:/* ignored for i2c */break;default:return 0;}return 1;
}//为u8g2提供延时函数接口的函数
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{switch(msg){case U8X8_MSG_GPIO_AND_DELAY_INIT:delay_init();	    //延时函数初始化break;case U8X8_MSG_DELAY_MILLI:delay_ms(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.
}//测试主程序
void TestIO_Init(void){GPIO_InitTypeDef  GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 		//使能端口时钟GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;	    		//端口配置GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 	//下拉输入GPIO_Init(GPIOA, &GPIO_InitStructure);	  		//根据设定参数初始化
}int main(void){NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	//设置系统中断优先级分为2u8g2_t u8g2;                                                                              // a structure which will contain all the data for one displayu8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay);// init u8g2 structureu8g2_InitDisplay(&u8g2);                                                                      // send init sequence to the display, display is in sleep mode after this,u8g2_SetPowerSave(&u8g2, 0);                                                                  // wake up displaywhile (1){/* Begin of U8G2*/static int x = 30,y = 10;u8g2_ClearBuffer(&u8g2);u8g2_SetFont(&u8g2, u8g2_font_10x20_mf);u8g2_DrawStr(&u8g2, x,y,"Data");if(x >= 70){x = y = 0;}else{x++;y++;}u8g2_SendBuffer(&u8g2);/* End of U8G2 */}
}

这篇关于适用于STM32的U8G2回调函数例程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1020506

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最