1.3”TFT LCD (240 x 3RGB x 240 )驅動配置

haidscs發表於2020-10-21

用SPI匯流排通訊

1、模擬spi:

static void LCD_GPIO_Init(void)
{
    nrf_gpio_cfg_output(LCD_CS_PIN);
    nrf_gpio_pin_set(LCD_CS_PIN);

    nrf_gpio_cfg_output(LCD_RESET_PIN);
    nrf_gpio_cfg_output(LCD_DC_PIN);
    nrf_gpio_cfg_output(LCD_SCLK_PIN);
    nrf_gpio_cfg_output(LCD_SDI_PIN);

    nrf_gpio_cfg_output(LCD_VCC_PIN);
    nrf_gpio_pin_clear(LCD_VCC_PIN);
}

static void enable_cs_pin(void)
{
    nrf_gpio_pin_clear(LCD_CS_PIN);
}

static void disable_cs_pin(void)
{
    nrf_gpio_pin_set(LCD_CS_PIN);
}


void lcd_240x240_reset(void)
{
    /* 
    //---------------------Reset sequence----------// 
    LCD_RESET=1; //
    nrf_delay_ms(1); //Delay 1ms 
    LCD_RESET=0; 
    nrf_delay_ms(10); //Delay 10ms 
    LCD_RESET=1; 
    nrf_delay_ms(120); //Delay 120ms 
    //--------------------------------------------------------------------//
    */
    nrf_gpio_pin_set(LCD_VCC_PIN);
    nrf_delay_ms(10);
    nrf_gpio_pin_set(LCD_RESET_PIN);
    nrf_delay_ms(1);
    nrf_gpio_pin_clear(LCD_RESET_PIN);
    nrf_delay_ms(10);
    nrf_gpio_pin_set(LCD_RESET_PIN);
    nrf_delay_ms(120);

}

void LCD240x240_WriteCMD( unsigned char data)
{
    /*四線SPI介面屏的操作*/
    unsigned char i,MB=0;
    nrf_gpio_pin_clear(LCD_DC_PIN);/*低,0:代表CMD,晶片規格書規定*/
    enable_cs_pin();  //CS low
    MB=data;
    for(i=0; i<8; i++)   /*模擬SPI協義*/
    {
        //nrf_gpio_pin_set(LCD_SCLK_PIN);
        if(MB&0x80)
            nrf_gpio_pin_set(LCD_SDI_PIN);
        else
            nrf_gpio_pin_clear(LCD_SDI_PIN);
        nrf_gpio_pin_set(LCD_SCLK_PIN);
        nrf_gpio_pin_clear(LCD_SCLK_PIN);
        MB<<=1;
    }
    disable_cs_pin();  //CS high

}

void LCD240x240_WriteDAT(unsigned char data)
{
       /*四線SPI介面屏的操作*/
    unsigned char i,MB=0;
    nrf_gpio_pin_set(LCD_DC_PIN);/*高,1,代表資料,晶片規格書規定*/
    enable_cs_pin();  //CS low
    MB=data;
    for(i=0; i<8; i++)
    {
        //nrf_gpio_pin_set(LCD_SCLK_PIN);
        if(MB&0x80)
            nrf_gpio_pin_set(LCD_SDI_PIN);
        else
            nrf_gpio_pin_clear(LCD_SDI_PIN);
        nrf_gpio_pin_set(LCD_SCLK_PIN);
        nrf_gpio_pin_clear(LCD_SCLK_PIN);
        MB<<=1;
    }
    disable_cs_pin();  //CS high
}


void lcd_240x240_init_test(void)
{
    LCD_GPIO_Init();

    lcd_240x240_reset();

    LCD240x240_WriteCMD(0x11); //退出睡眠
    nrf_delay_ms(120);     
    
    LCD240x240_WriteCMD(0x36); // Memory Data Access Control
    LCD240x240_WriteDAT(0x00); 
    
    LCD240x240_WriteCMD(0x3A); // Interface Pixel Format
    LCD240x240_WriteDAT(0x55);
    
    LCD240x240_WriteCMD(0xB2); //Porch Setting
    LCD240x240_WriteDAT(0x0C); 
    LCD240x240_WriteDAT(0x0C); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x33); 
    LCD240x240_WriteDAT(0x33); 

   // LCD240x240_WriteCMD(0x13);//Normal Display Mode On
    
    LCD240x240_WriteCMD(0xB7); // Gate Control
    LCD240x240_WriteDAT(0x75); 
    
    LCD240x240_WriteCMD(0xC2); //VDV and VRH Command Enable
    LCD240x240_WriteDAT(0x01); 
    
    LCD240x240_WriteCMD(0xC3); //VRH Set
    LCD240x240_WriteDAT(0x19); 
    
    LCD240x240_WriteCMD(0xC4);//VDV Set 
    LCD240x240_WriteDAT(0x20);

    LCD240x240_WriteCMD(0xC6); //Frame Rate Control in Normal Mode
    LCD240x240_WriteDAT(0x0F); 
    
    LCD240x240_WriteCMD(0xD0); // Power Control 1
    LCD240x240_WriteDAT(0xA4); 
    LCD240x240_WriteDAT(0xA1); 
    
    LCD240x240_WriteCMD(0xD6); //
    LCD240x240_WriteDAT(0xA1); 
    
    LCD240x240_WriteCMD(0x21); //Display Inversion On
    
    LCD240x240_WriteCMD(0xBB); // VCOM Setting
    LCD240x240_WriteDAT(0x3B);
    
    LCD240x240_WriteCMD(0xE0); //Positive Voltage Gamma Control
    LCD240x240_WriteDAT(0xF0); 
    LCD240x240_WriteDAT(0x0B); 
    LCD240x240_WriteDAT(0x11);
    LCD240x240_WriteDAT(0x0E);
    LCD240x240_WriteDAT(0x0D);
    LCD240x240_WriteDAT(0x19);
    LCD240x240_WriteDAT(0x36);
    LCD240x240_WriteDAT(0x33);
    LCD240x240_WriteDAT(0x4B);
    LCD240x240_WriteDAT(0x07);
    LCD240x240_WriteDAT(0x14);
    LCD240x240_WriteDAT(0x14);
    LCD240x240_WriteDAT(0x2C);
    LCD240x240_WriteDAT(0x2E);
    
    LCD240x240_WriteCMD(0xE1);//Negative Voltage Gamma Control
    LCD240x240_WriteDAT(0xF0);
    LCD240x240_WriteDAT(0x0D);
    LCD240x240_WriteDAT(0x12);
    LCD240x240_WriteDAT(0x0B);
    LCD240x240_WriteDAT(0x09);
    LCD240x240_WriteDAT(0x03);
    LCD240x240_WriteDAT(0x32);
    LCD240x240_WriteDAT(0x44); 
    LCD240x240_WriteDAT(0x48); 
    LCD240x240_WriteDAT(0x39); 
    LCD240x240_WriteDAT(0x16); 
    LCD240x240_WriteDAT(0x16); 
    LCD240x240_WriteDAT(0x2D); 
    LCD240x240_WriteDAT(0x30); 
    
    LCD240x240_WriteCMD(0x2A); // Column Address Set
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0xEF); 
    
    LCD240x240_WriteCMD(0x2B);//  Row Address Set
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0xEF); 
    
    LCD240x240_WriteCMD(0x2C); //  Memory Write

    LCD240x240_WriteCMD(0x29); //Display On
    nrf_delay_ms(120);


#ifdef LCD_240X240

    osThreadDef(sys1000MsTask, Sys1000MsTask, osPriorityLow, 0, 64);
    sys1000MsTaskHandle= osThreadCreate(osThread(sys1000MsTask), NULL);
    
    if( !sys1000MsTaskHandle )
    {
        error_handler(1, _Err_Create_Task_Bad);
    }
#endif
//    LCD240x240_clear(0xF800);
//    LCD240x240_clear(0x07E0);
//    LCD240x240_clear(0x001F);

//    LCD240x240_display_line(100,140,0,100,300);
//    LCD240x240_display_line(150,180,0,240,500);
//    LCD240x240_display_line(0,240,100,120,500);
    //test_display(0x00ee);
}

/*
void Sys1000MsTask(void const * argument)
{
    while(1)
    {
        LCD240x240_clear(0xF800);//R
        //osDelay(500);
        LCD240x240_clear(0x07E0);//G
       // osDelay(500);
        LCD240x240_clear(0x001F);//B
       // osDelay(500);
    }
}
*/
void test_display(uint16_t color)
{
    //LCD240x240_WriteCMD(0x2C); //  Memory Write

    uint8_t colunm;
    uint8_t row;

    LCD240x240_clear(0x0004);
    
    LCD240x240_WriteCMD(0x2A); // Column Address Set 列
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x66); 
    
    LCD240x240_WriteCMD(0x2B);//  Row Address Set 行
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x00); 
    LCD240x240_WriteDAT(0x66); 

    LCD240x240_WriteCMD(0x2C); //  Memory Write
    //LCD240x240_WriteCMD(0x29); //Display On
    //nrf_delay_ms(120);
    
    for(row=0; row<101; row++)
    {
        for(colunm=0; colunm<101; colunm++)
        {
            LCD240x240_WriteDAT(color>>8);
            LCD240x240_WriteDAT(color);
        }
    }
}

void Panel_SleepIn_Mode(void)

    LCD240x240_WriteCMD(0x28); 
    nrf_delay_ms(120); 
    LCD240x240_WriteDAT(0x10); 
    nrf_delay_ms(120); 
}
void Panel_SleepOut_Mode(void) 

    LCD240x240_WriteCMD(0x11); 
    nrf_delay_ms(120); 
    LCD240x240_WriteDAT(0x29); 
    nrf_delay_ms(120); 
}

void LCD240x240_clear(uint16_t clear_color)
{
    uint8_t i,j,k;
    uint16_t MB = 0;
    LCD240x240_setxy( 0, 239, 0, 239);

//    nrf_gpio_pin_set(LCD_DC_PIN);/*高,1,代表資料,晶片規格書規定*/
//    enable_cs_pin();  //CS low
    for(i=0; i<LCD_HEIGHT; i++)
    {
        for(j=0; j<LCD_WIDTH; j++)
        {
            LCD240x240_WriteDAT(clear_color>>8);
            LCD240x240_WriteDAT(clear_color);
//            for(k=0; k<16; k++)
//            {
//                //nrf_gpio_pin_set(LCD_SCLK_PIN);
//                if(MB&0x8000)
//                    nrf_gpio_pin_set(LCD_SDI_PIN);
//                else
//                    nrf_gpio_pin_clear(LCD_SDI_PIN);
//                nrf_gpio_pin_set(LCD_SCLK_PIN);
//                nrf_gpio_pin_clear(LCD_SCLK_PIN);
//                MB<<=1;
//            }
        }
    }
//    disable_cs_pin();  //CS high
    
}

void LCD240x240_setxy(uint8_t Xstart, uint8_t Xend, uint8_t Ystart, uint8_t Yend)
{
    LCD240x240_WriteCMD(0x2A); // Column Address Set
    LCD240x240_WriteDAT(Xstart>>8); 
    LCD240x240_WriteDAT(Xstart); 
    LCD240x240_WriteDAT(Xend>>8); 
    LCD240x240_WriteDAT(Xend); 
    
    LCD240x240_WriteCMD(0x2B);//  Row Address Set
    LCD240x240_WriteDAT(Ystart>>8); 
    LCD240x240_WriteDAT(Ystart); 
    LCD240x240_WriteDAT(Yend>>8); 
    LCD240x240_WriteDAT(Yend); 

    LCD240x240_WriteCMD(0x2C); //  Memory Write
}

void LCD240x240_display_line(uint8_t Xstart, uint8_t Xend, uint8_t Ystart, uint8_t Yend, uint16_t color)
{
    uint8_t line_clumn;//列
    uint8_t line_row;//行
    
    if(Xstart > LCD_WIDTH|| Xend > LCD_WIDTH || Ystart > LCD_HEIGHT || Yend > LCD_HEIGHT|| Xstart > Xend || Ystart > Yend)
    {
        return;
    }

    LCD240x240_setxy(Xstart, Xend, Ystart, Yend);

    for(line_clumn=0; line_clumn<(Xend - Xstart + 1); line_clumn++)
    {
        for(line_row=0; line_row<(Yend - Ystart + 1); line_row++) 
        {
            LCD240x240_WriteDAT(color>>8);
            LCD240x240_WriteDAT(color);
        }
    } 
}

2、硬體spi

void lcd240x240_set_addr_window(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
{
    uint8_t column_temp;
    uint8_t row_temp;
    ASSERT(x0 <= x1);
    ASSERT(y0 <= y1);   
       
    lcd240x240_write_command_m(0x2A); //column x
    lcd240x240_write_data_m((x0*2)>>8);                       
    lcd240x240_write_data_m(x0*2);//x0*2
    lcd240x240_write_data_m((x1*2+1)>>8);                       
    lcd240x240_write_data_m((x1*2+1));//x1*2+1

    lcd240x240_write_command_m(0x2B);//row y
#ifdef LCD_ROTATE_180
    lcd240x240_write_data_m((y0*16+80)>>8);                      
    lcd240x240_write_data_m(y0*16+80);//y0*16+80
    lcd240x240_write_data_m(((y1+1)*16-1+80)>>8);                       
    lcd240x240_write_data_m((y1+1)*16-1+80);//(y1+1)*16-1+80
#else
    lcd240x240_write_data_m((y0*16)>>8);                      
    lcd240x240_write_data_m(y0*16);//y0*16
    lcd240x240_write_data_m(((y1+1)*16-1)>>8);                       
    lcd240x240_write_data_m((y1+1)*16-1);//(y1+1)*16-1
#endif
    lcd240x240_write_command_m(0x2C);

}

void LCD_spi240x240_setxy(void)
{   
    lcd240x240_write_command_m(0x2A);     //Column Address Set
    lcd240x240_write_data_m(0x00);   
    lcd240x240_write_data_m(0x00);  //0
    lcd240x240_write_data_m(0x00);   
    lcd240x240_write_data_m(0xEF);   //239

    lcd240x240_write_command_m(0x2B);     //Row Address Set
#ifdef LCD_ROTATE_180
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x50); //0x00
    lcd240x240_write_data_m(0x01); //0x00
    lcd240x240_write_data_m(0x3F); //0xEF
#else
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x00); //0x00
    lcd240x240_write_data_m(0x00); //0x00
    lcd240x240_write_data_m(0xEF); //0xEF

#endif  

    lcd240x240_write_command_m(0x2C);     
}

void lcd240x240_hw_spi_pixel_draw(uint16_t x, uint16_t y, uint32_t color)
{
    lcd240x240_set_addr_window(x, y, x, y);
    //LCD_spi240x240_setxy();

    //color = RGB2BGR(color);
    const uint8_t data[2] = {color >> 8, color};

    nrf_gpio_pin_set(LCD_DC_PIN);

    lcd240x240_spim_write(data, sizeof(data));

    nrf_gpio_pin_clear(LCD_DC_PIN);
}

void lcd240x240_hw_spi_color_draw(uint16_t color)
{
    const uint16_t data[4] = {color,color,color,color};
    
    nrf_gpio_pin_set(LCD_DC_PIN);

    lcd240x240_spim_write(data, sizeof(data));
       
    nrf_gpio_pin_clear(LCD_DC_PIN);
}

void commands_control_init(void)
{
    lcd240x240_write_command_m(0x11); //退出睡眠
    nrf_delay_ms(120);   

    lcd240x240_write_command_m(0x36); // Memory Data Access Control
#ifdef LCD_ROTATE_180
    lcd240x240_write_data_m(0xC0);
#else
    lcd240x240_write_data_m(0x00);
#endif
    nrf_delay_ms(1);

    lcd240x240_write_command_m(0x3A); // Interface Pixel Format
    lcd240x240_write_data_m(0x55);

    lcd240x240_write_command_m(0xB2); //Porch Setting
    lcd240x240_write_data_m(0x0C); //5
    lcd240x240_write_data_m(0x0C); 
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x33); 
    lcd240x240_write_data_m(0x33); 

   // lcd240x240_write_command_m(0x13);//Normal Display Mode On
        
    lcd240x240_write_command_m(0xB7); // Gate Control
    lcd240x240_write_data_m(0x75);

    lcd240x240_write_command_m(0xC2); //VDV and VRH Command Enable
    lcd240x240_write_data_m(0x01);

    lcd240x240_write_command_m(0xC3); //VRH Set
    lcd240x240_write_data_m(0x19);
    
    lcd240x240_write_command_m(0xC4);//VDV Set 
    lcd240x240_write_data_m(0x20);

    lcd240x240_write_command_m(0xC6); //Frame Rate Control in Normal Mode
    lcd240x240_write_data_m(0x0F);
    
    lcd240x240_write_command_m(0xD0); // Power Control 1
    lcd240x240_write_data_m(0xA4);
    lcd240x240_write_data_m(0xA1); 
    
    lcd240x240_write_command_m(0xD6); //
    lcd240x240_write_data_m(0xA1);
    
    lcd240x240_write_command_m(0x21); //Display Inversion On
    
    lcd240x240_write_command_m(0x21); //Idle Mode On

    lcd240x240_write_command_m(0xBB); // VCOM Setting
    lcd240x240_write_data_m(0x3B);
    
    lcd240x240_write_command_m(0xE0); //Positive Voltage Gamma Control
    lcd240x240_write_data_m(0xF0); 
    lcd240x240_write_data_m(0x0B); 
    lcd240x240_write_data_m(0x11);
    lcd240x240_write_data_m(0x0E);
    lcd240x240_write_data_m(0x0D);
    lcd240x240_write_data_m(0x19);
    lcd240x240_write_data_m(0x36);
    lcd240x240_write_data_m(0x33);
    lcd240x240_write_data_m(0x4B);
    lcd240x240_write_data_m(0x07);
    lcd240x240_write_data_m(0x14);
    lcd240x240_write_data_m(0x14);
    lcd240x240_write_data_m(0x2C);
    lcd240x240_write_data_m(0x2E);
    
    lcd240x240_write_command_m(0xE1);//Negative Voltage Gamma Control
    lcd240x240_write_data_m(0xF0);
    lcd240x240_write_data_m(0x0D);
    lcd240x240_write_data_m(0x12);
    lcd240x240_write_data_m(0x0B);
    lcd240x240_write_data_m(0x09);
    lcd240x240_write_data_m(0x03);
    lcd240x240_write_data_m(0x32);
    lcd240x240_write_data_m(0x44); 
    lcd240x240_write_data_m(0x48); 
    lcd240x240_write_data_m(0x39); 
    lcd240x240_write_data_m(0x16); 
    lcd240x240_write_data_m(0x16); 
    lcd240x240_write_data_m(0x2D); 
    lcd240x240_write_data_m(0x30); 

    lcd240x240_write_command_m(0x2A); // Column Address Set
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0xEF); 
    
    lcd240x240_write_command_m(0x2B);//  Row Address Set
#ifdef LCD_ROTATE_180
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x50); //0x00
    lcd240x240_write_data_m(0x01); //0x00
    lcd240x240_write_data_m(0x3F); //0xEF
#else
    lcd240x240_write_data_m(0x00); 
    lcd240x240_write_data_m(0x00); //0x00
    lcd240x240_write_data_m(0x00); //0x00
    lcd240x240_write_data_m(0xEF); //0xEF

#endif
    lcd240x240_write_command_m(0x2C); //  Memory Write

    lcd240x240_write_command_m(0x29); //Display On
    nrf_delay_ms(120);    
    
}

void spim_hardware_init(void)
{

    driver_spim_config.sck_pin        = LCD_SCLK_PIN;
    driver_spim_config.mosi_pin       = LCD_SDI_PIN;
    driver_spim_config.miso_pin       = 0xFF;
    driver_spim_config.ss_pin         = LCD_CS_PIN;

    driver_spim_config.ss_active_high = false;
    driver_spim_config.irq_priority   = NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY;
    driver_spim_config.orc            = 0xFF;

    driver_spim_config.frequency      = SPIM_FREQUENCY_FREQUENCY_M8;  
    driver_spim_config.bit_order      = NRF_SPIM_BIT_ORDER_MSB_FIRST;
    driver_spim_config.mode           = NRF_SPIM_MODE_0;

#ifndef SPIM2
    driver_spim_config.use_hw_ss      = true;
    driver_spim_config.ss_duration    = 0x00;
    driver_spim_config.rx_delay       = 0x00;
    driver_spim_config.dcx_pin        = 0xFF;
 #endif  
   APP_ERROR_CHECK(nrfx_spim_init(&driver_spim, &driver_spim_config, spi_event_handler_m, NULL));
}

static void lcd240x240_gpio_init(void)
{
    nrf_gpio_cfg_output(LCD_VCC_PIN);
    nrf_gpio_pin_clear(LCD_VCC_PIN);
    nrf_gpio_cfg_output(LCD_DC_PIN);
    nrf_gpio_cfg_output(LCD_RESET_PIN);
}

void lcd240x240_reset(void)
{
    nrf_gpio_pin_set(LCD_VCC_PIN);
    nrf_delay_ms(10);
    nrf_gpio_pin_set(LCD_RESET_PIN);
    nrf_delay_ms(1);
    nrf_gpio_pin_clear(LCD_RESET_PIN);
    nrf_delay_ms(10);
    nrf_gpio_pin_set(LCD_RESET_PIN);
    nrf_delay_ms(120);
}

void lcd_240x240_init(void)
{
    ret_code_t err_code;
   
//    spi_xfer_done = false;
    spi_xfer_donem = false;

    lcd240x240_gpio_init();
    lcd240x240_reset();
    spim_hardware_init();
  
    commands_control_init();
        
//    lcd240x240_hw_spi_clear(0);
    LCD_buffer_init();

    nrf_gpio_pin_clear(LCD_VCC_PIN);

    lcd240x240_write_command_m(0x28);//add by linch display off
    lcd240x240_write_command_m(0x10);
    nrf_delay_ms(200);

    return err_code;
}

void lcd240x240_hw_spi_clear(uint16_t  clear_color)
{
    uint8_t i,j;
    uint16_t MB;
    //clear_color = RGB2BGR(clear_color);
    uint16_t data[1];
    //memset(data,0,sizeof(data));
//    for(MB=0; MB<4; MB++)
    {
        data[0] = clear_color;
    }
    //memcpy(data,clear_color,sizeof(data));

    LCD_spi240x240_setxy();

    nrf_gpio_pin_set(LCD_DC_PIN);
    for(i=0; i<240; i++)//LCD_HEIGHT
    {
        for(j=0; j<240; j++)//LCD_WIDTH
        {
            lcd240x240_spim_write(data, sizeof(data));         
        }
    }
    nrf_gpio_pin_clear(LCD_DC_PIN);
}
 

相關文章