适用于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

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五