STM32L151C8用硬件SPI驱动LCD,为啥总是不行

2019-07-20 03:25发布

STM32L151C8用硬件SPI驱动LCD,驱动芯片是ST7735S,0.96寸,屏一直是花的,
大神们帮我看看到底哪里出了问题

[mw_shl_code=c,true]SPI_HandleTypeDef SPI1_Handler;

int main(void)
{

  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
LCD_Gpio_Init();
MX_SPI1_Init();
LCD_Init();
HAL_Delay(100);
LCD_Clear(WHITE);
while(1)
  {
}

}

void MX_SPI1_Init(void)
{
                __HAL_RCC_SPI1_CLK_ENABLE();        //使能SPI1时钟
  SPI1_Handler.Instance = SPI1;
  SPI1_Handler.Init.Mode = SPI_MODE_MASTER;
  SPI1_Handler.Init.Direction = SPI_DIRECTION_2LINES;
  SPI1_Handler.Init.DataSize = SPI_DATASIZE_8BIT;
  SPI1_Handler.Init.CLKPolarity = SPI_POLARITY_LOW;
  SPI1_Handler.Init.CLKPhase = SPI_PHASE_1EDGE;
  SPI1_Handler.Init.NSS = SPI_NSS_SOFT;
  SPI1_Handler.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  SPI1_Handler.Init.FirstBit = SPI_FIRSTBIT_MSB;
  SPI1_Handler.Init.TIMode = SPI_TIMODE_DISABLE;
  SPI1_Handler.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  SPI1_Handler.Init.CRCPolynomial = 7;
  HAL_SPI_Init(&SPI1_Handler);                                                //初始化SPI1
   
  __HAL_SPI_ENABLE(&SPI1_Handler);                    //使能SPI1
}

void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_SPI1_CLK_ENABLE();        
       
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7|GPIO_PIN_5, GPIO_PIN_SET);

        GPIO_InitStruct.Pin=GPIO_PIN_5|GPIO_PIN_7;
  GPIO_InitStruct.Mode=GPIO_MODE_AF_PP;              //复用推挽输出
  GPIO_InitStruct.Pull=GPIO_PULLUP;                  //上拉
  GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_VERY_HIGH;   //50Mhz           
  GPIO_InitStruct.Alternate=GPIO_AF5_SPI1;           //PA5  7复用为SPI1
  HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
       
}

/**
* TxData        要写入的字节
* size        写入字节的长度
*/
void LCD_SPI_Send(u8 *TxData,u16 size)
{
        HAL_SPI_Transmit(&SPI1_Handler,TxData,size,1000);
}

void LCD_Gpio_Init(void)
{
        GPIO_InitTypeDef GPIO_Initure;

  __HAL_RCC_GPIOA_CLK_ENABLE();
       
//LCD_PWR:                PA6
//LCD_RST:                PA2
//LCD_DC:                        PA4
//LCD_CS:                        PA3
       
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_4, GPIO_PIN_SET);
         /*Configure GPIO pins : PA2 PA3 PA4 PA6 */
  GPIO_Initure.Pin = GPIO_PIN_2|GPIO_PIN_6;
  GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_Initure.Pull = GPIO_PULLUP;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_Initure);
       
         /*Configure GPIO pin : PA3 */
  GPIO_Initure.Pin = GPIO_PIN_3;
  GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_Initure.Pull = GPIO_PULLUP;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_Initure);
       
        /*Configure GPIO pin : PA4 */
  GPIO_Initure.Pin = GPIO_PIN_4;
  GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_Initure.Pull = GPIO_PULLDOWN;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_Initure);
}


//写命令
void LCD_Write_Cmd(u8 cmd)
{
    LCD_DC(0);
                LCD_CS(0);
    LCD_SPI_Send(&cmd, 1);
                LCD_CS(1);
}

//写一个字节
void LCD_Write_Data1(u8 data)
{
    LCD_DC(1);
                LCD_CS(0);
    LCD_SPI_Send(&data, 1);
                LCD_CS(1);
}

//写2个字节
void LCD_Write_Data(u8 dat)
{
        LCD_DC(1);
        LCD_CS(0);
        LCD_SPI_Send(&dat, 2);
        LCD_CS(1);
}
//设置起点终点坐标
void LCD_Address_Set(u16 x_start, u16 y_start, u16 x_end, u16 y_end)
{
    LCD_Write_Cmd(0x2a);//列
    LCD_Write_Data(x_start);
                LCD_Write_Data(x_end);

    LCD_Write_Cmd(0x2b);//行
    LCD_Write_Data(y_start);
                LCD_Write_Data(y_end);

    LCD_Write_Cmd(0x2C);//写入行列
}

//LCD清屏
void LCD_Clear(u16 Color)
{
        u16 i,j;         
        LCD_Address_Set(0,0,LCD_Width-1,LCD_Height-1);
    for(i=0;i<LCD_Width;i++)
          {
                        for (j=0;j<LCD_Height;j++)
                                {
                                        LCD_Write_Data(Color);
                                }
          }
}

//LCD初始化
void LCD_Init(void)
{
        LCD_RST(0);
        HAL_Delay(20);
        LCD_RST(1);
        HAL_Delay(20);
        LCD_PWR(1);
       
        LCD_Write_Cmd(0x11); //sleep out
        HAL_Delay(100);

        LCD_Write_Cmd(0x21); //display inverson on

        LCD_Write_Cmd(0xB1); //frame rate control
        LCD_Write_Data1(0x05);
        LCD_Write_Data1(0x3A);
        LCD_Write_Data1(0x3A);

        LCD_Write_Cmd(0xB2);//frame rate control
        LCD_Write_Data1(0x05);
        LCD_Write_Data1(0x3A);
        LCD_Write_Data1(0x3A);

        LCD_Write_Cmd(0xB3); //frame rate control
        LCD_Write_Data1(0x05);  
        LCD_Write_Data1(0x3A);
        LCD_Write_Data1(0x3A);
        LCD_Write_Data1(0x05);
        LCD_Write_Data1(0x3A);
        LCD_Write_Data1(0x3A);

        LCD_Write_Cmd(0xB4);//display inversion on
        LCD_Write_Data1(0x03);

        LCD_Write_Cmd(0xC0);//power control1
        LCD_Write_Data1(0x62);
        LCD_Write_Data1(0x02);
        LCD_Write_Data1(0x04);

        LCD_Write_Cmd(0xC1);//power control2
        LCD_Write_Data1(0xC0);

        LCD_Write_Cmd(0xC2);//power control3
        LCD_Write_Data1(0x0D);
        LCD_Write_Data1(0x00);

        LCD_Write_Cmd(0xC3);//power control4
        LCD_Write_Data1(0x8D);
        LCD_Write_Data1(0x6A);   

        LCD_Write_Cmd(0xC4);//power control5
        LCD_Write_Data1(0x8D);
        LCD_Write_Data1(0xEE);

        LCD_Write_Cmd(0xC5);  /*VCOM*/
        LCD_Write_Data1(0x0E);   

        LCD_Write_Cmd(0xE0);//gamma +polarity
        LCD_Write_Data1(0x10);
        LCD_Write_Data1(0x0E);
        LCD_Write_Data1(0x02);
        LCD_Write_Data1(0x03);
        LCD_Write_Data1(0x0E);
        LCD_Write_Data1(0x07);
        LCD_Write_Data1(0x02);
        LCD_Write_Data1(0x07);
        LCD_Write_Data1(0x0A);
        LCD_Write_Data1(0x12);
        LCD_Write_Data1(0x27);
        LCD_Write_Data1(0x37);
        LCD_Write_Data1(0x00);
        LCD_Write_Data1(0x0D);
        LCD_Write_Data1(0x0E);
        LCD_Write_Data1(0x10);

        LCD_Write_Cmd(0xE1);//gamma -polarity
        LCD_Write_Data1(0x10);
        LCD_Write_Data1(0x0E);
        LCD_Write_Data1(0x03);
        LCD_Write_Data1(0x03);
        LCD_Write_Data1(0x0F);
        LCD_Write_Data1(0x06);
        LCD_Write_Data1(0x02);
        LCD_Write_Data1(0x08);
        LCD_Write_Data1(0x0A);
        LCD_Write_Data1(0x13);
        LCD_Write_Data1(0x26);
        LCD_Write_Data1(0x36);
        LCD_Write_Data1(0x00);
        LCD_Write_Data1(0x0D);
        LCD_Write_Data1(0x0E);
        LCD_Write_Data1(0x10);

        LCD_Write_Cmd(0x3A); //interface pixel format
        LCD_Write_Data1(0x05);

        LCD_Write_Cmd(0x36);//mamory data access control
        LCD_Write_Data1(0x78);

        LCD_Write_Cmd(0x29); //display on
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
6条回答
sammy902583
1楼-- · 2019-07-20 08:17
 精彩回答 2  元偷偷看……
1208
2楼-- · 2019-07-20 11:48
本帖最后由 1208 于 2019-3-2 18:40 编辑

硬件BS1和BS2有没接地

你可以参考论坛大佬做过的
http://www.openedv.com/forum.php ... ght=%D3%B2%BC%FESPI
流芷静兰
3楼-- · 2019-07-20 15:44
1208 发表于 2019-3-2 18:39
硬件BS1和BS2有没接地

你可以参考论坛大佬做过的

好的,多谢多谢~~~
流芷静兰
4楼-- · 2019-07-20 17:40
 精彩回答 2  元偷偷看……
1208
5楼-- · 2019-07-20 21:37
流芷静兰 发表于 2019-3-6 16:45
SPI也有BS1和BS2吗?

是OLED屏上有
流芷静兰
6楼-- · 2019-07-21 00:55
sammy902583 发表于 2019-3-2 14:33
看起来初始化程式是对了, 只是资料没有正确写入, 看看下面这个代码是不是改了就正确了.

//写2个字节

改好了,多谢大佬

一周热门 更多>