源码:GitHub - olikraus/u8glib: Arduino Monochrom Graphics Library for LCDs and OLEDs
wiki github 下查看帮助 Home · olikraus/u8glib Wiki · GitHub
软件spi
/my init
void ssd1306_spi_init()
{
pinMode(CS,output);
pinMode(DC,output);
//pinMode(ST7735_RST_PIN,output);
pinMode(SCK,output);
pinMode(MOSI,output);
}
uint8_t u8x8_avr_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
// Re-use library for delays
switch(msg)
{
//ms
case U8X8_MSG_DELAY_MILLI:
delay(1);
break;
//10 us
case U8X8_MSG_DELAY_10MICRO:
delay10us();
break;
//1 ns
case U8X8_MSG_DELAY_NANO:
delay1us();
break;
case U8X8_MSG_DELAY_100NANO:
delay1us();
break;
case U8X8_MSG_GPIO_AND_DELAY_INIT: // called once during init phase of u8g2/u8x8
ssd1306_spi_init();
break; // can be used to setup pins
case U8X8_MSG_GPIO_SPI_CLOCK: // Clock pin: Output level in arg_int
if(arg_int)
digitalWrite(SCK,1);
else
digitalWrite(SCK,0);
break;
case U8X8_MSG_GPIO_SPI_DATA: // MOSI pin: Output level in arg_int
if(arg_int)
digitalWrite(MOSI,1);
else
digitalWrite(MOSI,0);
break;
case U8X8_MSG_GPIO_CS: // CS (chip select) pin: Output level in arg_int
if(arg_int)
digitalWrite(CS,1);
else
digitalWrite(CS,0);
break;
case U8X8_MSG_GPIO_DC: // DC (data/cmd, A0, register select) pin: Output level in arg_int
if(arg_int)
digitalWrite(DC,1);
else
digitalWrite(DC,0);
break;
case U8X8_MSG_GPIO_RESET: // Reset pin: Output level in arg_int
default:
break;
}
return 1;
}
初始化
u8g2_t u8g2;
void u8g2_init()
{
u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_avr_gpio_and_delay);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
u8g2_ClearBuffer(&u8g2);
}
软件iic
uint8_t u8x8_avr_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
// Re-use library for delays
switch(msg){
//初始化GPIO
case U8X8_MSG_GPIO_AND_DELAY_INIT: //此状态下初始化IO以及延时函数
// pinMode(SDA,output);
// pinMode(SCL,output);
// digitalWrite(SDA,1);
// digitalWrite(SCL,1);
//Serial.println("u8x8 init");
break;
case U8X8_MSG_DELAY_100NANO: //此状态下对接100ns延时函数
//for(int i=0;i<100;i++);
delayMicroseconds(1);
break;
case U8X8_MSG_DELAY_10MICRO: //此状态下对接10usus延时函数
delayMicroseconds(10);
break;
case U8X8_MSG_DELAY_MILLI: //此状态下对接ms延时函数,延时arg_int ms
delay(1);
break;
case U8X8_MSG_DELAY_I2C: //此状态下对接IIC延时函数
delayMicroseconds(5);
break;
case U8X8_MSG_GPIO_I2C_CLOCK: //此状态下修改IIC SCL信号线电平
if (arg_int)
{
//pinMode(2,output);
digitalWrite(2,1);
}
else
{
// pinMode(2,output);
digitalWrite(2,0);
}
break;
case U8X8_MSG_GPIO_I2C_DATA: //此状态下修改IIC SDA信号线电平
if (arg_int)
{
digitalWrite(3,1);
}
else {
digitalWrite(3,0);
}
break;
default:
u8x8_SetGPIOResult(u8x8, 1); // default return value
break;
}
return 1;
}
初始化
u8g2_t u8g2;
void u8g2_init()
{
ssd1306_iic_init();
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0,u8x8_byte_sw_i2c, u8x8_avr_gpio_and_delay);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
u8g2_ClearBuffer(&u8g2);
//u8x8_SetPin_SW_I2C(getU8x8(), SCL, SDA, 0);
}
硬件spi(asrpro 可能没有硬件spi ; 内部有个spi用于flash读写
硬件iic (包含 asrpro_iic.h头文件
asrpro 硬件IIC地址为PA2 PA3 SDA SCL 引脚复用功能为3
dpmu_set_io_reuse(PA2,THIRD_FUNCTION);
dpmu_set_io_reuse(PA3,THIRD_FUNCTION);
iic_polling_init(IIC0,100,0,LONG_TIME_OUT); //**SSD1306 BEGIN() 使用 ASRPRO_IIC.H
void ssd1306_HW_iic_init()
{
pinMode(SDA,output);
pinMode(SCL,output);
dpmu_set_io_reuse(PA2,THIRD_FUNCTION); //SDA
dpmu_set_io_reuse(PA3,THIRD_FUNCTION);//SCL
iic_polling_init(IIC0,100,0,LONG_TIME_OUT);
//digitalWrite(SDA,1);
//digitalWrite(SCL,1);
}
uint8_t u8x8_avr_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
// Re-use library for delays
switch(msg){
//初始化GPIO
case U8X8_MSG_GPIO_AND_DELAY_INIT: //此状态下初始化IO以及延时函数
break;
case U8X8_MSG_DELAY_MILLI: //此状态下对接ms延时函数,延时arg_int ms
delay(1);
break;
default:
return 0;
break;
}
return 1;
}
uint8_t u8x8_byte_hw_i2c2(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_SET_DC:
/* ignored for i2c */
break;
case U8X8_MSG_BYTE_START_TRANSFER:
buf_idx = 0;
break;
case U8X8_MSG_BYTE_END_TRANSFER:
//i2c_transfer(u8x8_GetI2CAddress(u8x8) >> 1, buf_idx, buffer);
i2c_master_only_send(u8x8_GetI2CAddress(u8x8) >> 1,(uint8_t *)buffer,buf_idx);
break;
default:
return 0;
}
return 1;
}
初始化
u8g2_t u8g2;
void u8g2_init()
{
// u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0,u8x8_byte_sw_i2c, u8x8_avr_gpio_and_delay);
//u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2,U8G2_R0,u8x8_byte_hw_i2c ,u8g2_gpio_and_delay_stm32);
ssd1306_HW_iic_init();
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0,u8x8_byte_hw_i2c2, u8x8_avr_gpio_and_delay);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
u8g2_ClearBuffer(&u8g2);
//u8x8_SetPin_SW_I2C(getU8x8(), SCL, SDA, 0);
}
参考:Porting to new MCU platform · olikraus/u8g2 Wiki · GitHub
https://www.cnblogs.com/frozencandles/p/16358483.html
【开源】硬件/软件i2c两种方式移植u8g2单色图形库驱动0.96吋OLED-腾讯云开发者社区-腾讯云
使用
u8g2c.u8g2_init();
u8g2c.clearBuffer();
u8g2c.drawBox(0,0,10,10);
u8g2c.sendBuffer();
初始化函数差异
软件spi
u8g2_Setup_ssd1306_128x64_noname_f (&u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_avr_gpio_and_delay);
软件i2c
u8g2_Setup_ssd1306_i2c_128x64_noname_f (&u8g2,U8G2_R0,u8x8_byte_sw_i2c, u8x8_avr_gpio_and_delay);
硬件i2c
u8g2_Setup_ssd1306_i2c_128x64_noname_f (&u8g2, U8G2_R0,u8x8_byte_hw_i2c2, u8x8_avr_gpio_and_delay);
dac操作
pcb绘板
u8g2font arduino