AnsweredAssumed Answered

ili9163 spi LCD Help Please!

Question asked by ghanmy.montassar on Feb 17, 2015
Latest reply on Feb 18, 2015 by jj.sprague
Hi guys , I wrote this simple code based on the ili9163 documentation and what what i found online of the arduino library writen for the same display , yet all what i was able to do is get the Background LED to light up the panel , nothing diplaying apart from that , here is my code:

#include <stm32f4xx.h>
#include <stm32f4xx_adc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_spi.h>




#define CSPin GPIO_Pin_1
#define ResPin GPIO_Pin_2
#define AOPin GPIO_Pin_4
#define DatPin GPIO_Pin_5
#define ClkPin GPIO_Pin_3
#define VCCPin GPIO_Pin_6
#define BLPin GPIO_Pin_7


#define DatPS GPIO_PinSource5
#define ClkPS GPIO_PinSource3


#define Red 0x8000


GPIO_InitTypeDef GP_InitDef;
SPI_InitTypeDef SPI_InitDef;


void initGPIO(){
     RCC->AHB1ENR |= RCC_AHB1Periph_GPIOB; // Clock enable for GPIOB.


     GP_InitDef.GPIO_Pin = CSPin | ResPin | AOPin | BLPin | VCCPin;
     GP_InitDef.GPIO_Mode = GPIO_Mode_OUT;
     GP_InitDef.GPIO_OType = GPIO_OType_PP;
     GP_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GP_InitDef.GPIO_Speed = GPIO_Speed_25MHz;
     GPIO_Init(GPIOB, &GP_InitDef);


     GP_InitDef.GPIO_Pin = DatPin | ClkPin;
     GP_InitDef.GPIO_Mode = GPIO_Mode_AF;
     GPIO_Init(GPIOB, &GP_InitDef);


     GPIO_PinAFConfig(GPIOB, ClkPS, GPIO_AF_SPI3);
     GPIO_PinAFConfig(GPIOB, DatPS, GPIO_AF_SPI3);


}
void initSPI3() {
     // Initialize SPI3.
     SPI_InitDef.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
     SPI_InitDef.SPI_CPHA = SPI_CPHA_1Edge;
     SPI_InitDef.SPI_CPOL = SPI_CPOL_Low;
     SPI_InitDef.SPI_DataSize = SPI_DataSize_8b;
     SPI_InitDef.SPI_FirstBit = SPI_FirstBit_MSB;
     SPI_InitDef.SPI_Mode = SPI_Mode_Master;
     SPI_InitDef.SPI_NSS = SPI_NSS_Soft;
     SPI_Init(SPI3, &SPI_InitDef);
     SPI_Cmd(SPI3, ENABLE);
}
void writeCommand(uint16_t command){
     GPIO_ResetBits(GPIOB, AOPin);     // Set A0 D/C pin LOW for command select.
     SPI_I2S_SendData(SPI3, command);
     while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);
}
void writeData(uint16_t data){
     GPIO_SetBits(GPIOB, AOPin);     // Set A0 D/C pin LOW for data select.
     SPI_I2S_SendData(SPI3, data);
     while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);
}
void writeData16(uint16_t data){
     GPIO_SetBits(GPIOB, AOPin);     // Set A0 D/C pin LOW for data select.
     SPI_I2S_SendData(SPI3, data>>8);
     while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);
     SPI_I2S_SendData(SPI3, data);
     while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);
}
void setCoordinates(){
     writeCommand(0x2A);
     writeData(0x00);
     writeData(05);
     writeData(0x00);
     writeData(50);
     writeCommand(0x2B);
     writeData(0x00);
     writeData(32);
     writeData(0x00);
     writeData(32+50);


     writeCommand(0x2C);
}
int main(void)
{
     int index;
     initGPIO();
     initSPI3();




     GPIO_SetBits(GPIOB, BLPin); // Set The background LED Pin high.


     //
     GPIO_ResetBits(GPIOB, ResPin);
     for (index = 0; index < 50000; index++);
     GPIO_SetBits(GPIOB, ResPin);
     for (index = 0; index < 50000; index++);
     GPIO_ResetBits(GPIOB, CSPin);
     for (index = 0; index < 50000; index++);


     //Start The LCD initializing.
     writeCommand(0x01); //Software reset
     for (index = 0; index < 50000; index++);


     writeCommand(0x11); // Exit Sleep
     for (index = 0; index < 10000; index++);


     writeCommand(0x26); //Set default gamma
     writeData(0x04);
     for (index = 0; index < 5000; index++);




     writeCommand(0xC0); //Set Power Control 1
     writeData(0x1F);
     for (index = 0; index < 5000; index++);


     writeCommand(0xC1); //Set Power Control 2
     writeData(0x00);
     for (index = 0; index < 5000; index++);


     writeCommand(0xC2); //Set Power Control 3
     writeData(0x00);
     writeData(0x07);
     for (index = 0; index < 5000; index++);


     writeCommand(0xC3); //Set Power Control 4 (Idle mode)
     writeData(0x00);
     writeData(0x07);
     for (index = 0; index < 5000; index++);


     writeCommand(0xC5); //Set VCom Control 1
     writeData(0x24); // VComH = 3v
     writeData(0xC8); // VComL = 0v
     for (index = 0; index < 5000; index++);


     writeCommand(0x38); //Idle mode off
     for (index = 0; index < 5000; index++);


     writeCommand(0x3A); //Set pixel mode
     writeData(0x05);
     for (index = 0; index < 5000; index++);


     writeCommand(0x36); //Set Memory access mode
     writeData(0x08);
     for (index = 0; index < 5000; index++);


     writeCommand(0x29); //Display on
     for (index = 0; index < 5000; index++);


     writeCommand(0x2C);//Memory Write
     for (index = 0; index < 5000; index++);


     //Display Something
     int px;
     setCoordinates();
     for (px=0;px < 132; px++){
          writeData16(Red);
     }


     return 0;
}

Any idea what Am I doing wrong ?? I appreciate it !!!

Outcomes