olikraus / u8g2

U8glib library for monochrome displays, version 2
Other
5.16k stars 1.05k forks source link

1.3" OLED (SH1106): Scrambled display output via HardWare SPI #2126

Open ggxxgxxgg opened 1 year ago

ggxxgxxgg commented 1 year ago

The display is scrambled, as shown in the image below.Cpu is STM32ZET6.I use the HAL library hardware SPI to drive the screen. When I used the sample code from the screen provider, the screen displayed properly. The initialization sequence for sh1106 was not very different from that for ssd1306, and I wrote the initialization sequence just like the sample code, but it still didn't display properly.

Any idea what i am doing wrong, how to fix this?

IMG_9148 The code.

uint8_t spi_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和GPIO的初始化,在main中已经初始化完成了*/
            break;
        case U8X8_MSG_DELAY_MILLI: /*延时函数*/
            HAL_Delay(arg_int);
            break;
        case U8X8_MSG_GPIO_CS: /*片选信号*/
            // HAL_GPIO_WritePin(LCD_PORT,LCD_CS,(GPIO_PinState)arg_int);
            break;
        case U8X8_MSG_GPIO_DC: /*设置DC引脚,表明发送的是数据还是命令*/
            HAL_GPIO_WritePin(LCD_PORT,LCD_DC,(GPIO_PinState)arg_int);
            break;
        case U8X8_MSG_GPIO_RESET:
            HAL_GPIO_WritePin(LCD_PORT,LCD_RST,(GPIO_PinState)arg_int);
            break;
    }
    return 1;
}
uint8_t u8x8_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,void *arg_ptr)
{
    switch (msg)
    {
        case U8X8_MSG_BYTE_SEND: /*通过SPI发送arg_int个字节数据*/
            HAL_SPI_Transmit(&SPI1_Handler,(uint8_t *)arg_ptr,arg_int,200);
            break;
        case U8X8_MSG_BYTE_INIT: /*初始化函数*/
            break;
        case U8X8_MSG_BYTE_SET_DC: /*设置DC引脚,表明发送的是数据还是命令*/
        HAL_GPIO_WritePin(LCD_PORT,LCD_DC,(GPIO_PinState)arg_int);
            break;
        case U8X8_MSG_BYTE_START_TRANSFER: 
            break;
        case U8X8_MSG_BYTE_END_TRANSFER: 
            break;
        default:
            return 0;
    }
    return 1;
}
void u8g2_Init_SPI(u8g2_t *u8g2)
{
    u8g2_Setup_sh1106_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_4wire_hw_spi, spi_gpio_and_delay);
    u8g2_InitDisplay(u8g2);
    u8g2_SetPowerSave(u8g2, 0);                                                              
}
olikraus commented 1 year ago

Multiple reasons are possible: