U8g2代码下载:
https://github.com/olikraus/u8g2
1,准备一个正常运行的KEIL5 MDK模板
2,下载u8g2的源码和 u8g2的STM32实例模板
源码: https://github.com/olikraus/u8g2
STM32实例模板: https://github.com/nikola-v/u8g2_template_stm32f103c8t63,去掉无用的驱动文件,只保留u8x8_d_ssd1306_128x64_noname.c
4,精简u8g2_d_setup.c,只保留u8g2_Setup_ssd1306_i2c_128x64_noname_f()5,精简u8g2_d_memory.c,只保留u8g2_m_16_8_f()
6,对工程进行设置并剔除编译错误
7,更改u8x8_gpio_and_delay()
1.在csrc文件中,保留驱动文件u8x8_d_ssd1306_128*64_noname_f
2.将这些文件,放到keil中
在main.c中包含头文件:
#include "u8g2.h"
并包含路径
3.移植好OLED的驱动
4.精简u8g2_d_setup.c,只保留u8g2_Setup_ssd1306_i2c_128x64_noname_f()
5.精简u8g2_d_memory.c,只保留u8g2_m_16_8_f()
6,对工程进行设置并剔除编译错误
7,更改u8x8_gpio_and_delay()
uint8_t gpio_and_delay_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
switch(msg)
{
case U8X8_MSG_DELAY_MILLI://Function which implements a delay, arg_int contains the amount of ms
Delayxms(arg_int);
break;
case U8X8_MSG_DELAY_10MICRO://Function which delays 10us
Delayxus(10);
break;
case U8X8_MSG_DELAY_100NANO://Function which delays 100ns
__NOP();
break;
case U8X8_MSG_GPIO_I2C_CLOCK:
if (arg_int) GPIO_WriteBit(GPIOB, GPIO_Pin_8,Bit_SET );
else GPIO_WriteBit(GPIOB, GPIO_Pin_8,Bit_RESET );
break;
case U8X8_MSG_GPIO_I2C_DATA:
if (arg_int) GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET);
else GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_RESET);
break;
default:
return 0; //A message was received which is not implemented, return 0 to indicate an error
}
return 1; // command processed successfully.
}
8.main.c的代码
#include "stm32f10x.h" // Device header
#include "led.h"
#include "u8g2.h"
#include "OLED.h"
#include "Delay.h"
u8g2_t u8g2;
static const unsigned char u8g_logo_bits[] U8X8_PROGMEM =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0xE0,0x01,0x00,0x00,0xF0,0x01,0x00,0x00,0xF8,0x01,0x00,0x00,0xFC,0x01,0x00,0x00,0xFE,0x01,0x00,
0x30,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,0x3C,0xFE,0xFF,0x01,
0x3C,0xFE,0xFF,0x00,0x3C,0xFE,0xFF,0x00,0x3C,0xFE,0xFF,0x00,0x3C,0xFE,0x7F,0x00,0x3C,0xFE,0x7F,0x00,0x3C,0xFE,0x7F,0x00,0x3C,0xFE,0x3F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,/*G:\正在开工\U8g2相关资料20230324\相关资料\点赞.bmp*/0
};
uint8_t gpio_and_delay_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
void u8g2_init(void);
void draw(u8g2_t *u8g2);
int main(void)
{
OLED_Init();
u8g2_init();
Delayxms(1000);
u8g2_DrawLine(&u8g2, 0,0, 127, 63);//测试:起点:横坐标,纵坐标;终点:横坐标,纵坐标
u8g2_DrawLine(&u8g2, 127,0, 0, 63);
u8g2_SendBuffer(&u8g2);
Delayxms(1000);
u8g2_ClearBuffer(&u8g2);
draw(&u8g2);//官方测试函数,显示U8g2的logo
Delayxms(1000);
u8g2_ClearBuffer(&u8g2);
u8g2_DrawXBMP(&u8g2, 30, 20, 25, 25, u8g_logo_bits);//显示点赞图案
u8g2_SendBuffer(&u8g2);
while(1)
{
}
}
uint8_t gpio_and_delay_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
switch(msg)
{
case U8X8_MSG_DELAY_MILLI://Function which implements a delay, arg_int contains the amount of ms
Delayxms(arg_int);
break;
case U8X8_MSG_DELAY_10MICRO://Function which delays 10us
Delayxus(10);
break;
case U8X8_MSG_DELAY_100NANO://Function which delays 100ns
__NOP();
break;
case U8X8_MSG_GPIO_I2C_CLOCK:
if (arg_int) GPIO_WriteBit(GPIOB, GPIO_Pin_8,Bit_SET );
else GPIO_WriteBit(GPIOB, GPIO_Pin_8,Bit_RESET );
break;
case U8X8_MSG_GPIO_I2C_DATA:
if (arg_int) GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET);
else GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_RESET);
break;
default:
return 0; //A message was received which is not implemented, return 0 to indicate an error
}
return 1; // command processed successfully.
}
void u8g2_init(void)
{
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, gpio_and_delay_stm32);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
u8g2_ClearBuffer(&u8g2);
}
void draw(u8g2_t *u8g2)
{
u8g2_ClearBuffer(u8g2);
u8g2_SetFontMode(u8g2, 1);
u8g2_SetFontDirection(u8g2, 0);
u8g2_SetFont(u8g2, u8g2_font_inb24_mf);
u8g2_DrawStr(u8g2, 0, 20, "U");
u8g2_SetFontDirection(u8g2, 1);
u8g2_SetFont(u8g2, u8g2_font_inb30_mn);
u8g2_DrawStr(u8g2, 21,8,"8");
u8g2_SetFontDirection(u8g2, 0);
u8g2_SetFont(u8g2, u8g2_font_inb24_mf);
u8g2_DrawStr(u8g2, 51,30,"g");
u8g2_DrawStr(u8g2, 67,30,"\xb2");
u8g2_DrawHLine(u8g2, 2, 35, 47);
u8g2_DrawHLine(u8g2, 3, 36, 47);
u8g2_DrawVLine(u8g2, 45, 32, 12);
u8g2_DrawVLine(u8g2, 46, 33, 12);
u8g2_SetFont(u8g2, u8g2_font_4x6_tr);
u8g2_DrawStr(u8g2, 1,54,"github.com/olikraus/u8g2");
// u8g2_ClearDisplay(u8g2);
u8g2_SendBuffer(u8g2);
Delayxms(1000);
}
移植的代码链接:
链接:https://pan.baidu.com/s/1TfzhoXhK8OaRLax6CmnC6Q?pwd=tikd
提取码:tikd
--来自百度网盘超级会员V6的分享