cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F051 SPI problem, help

monta
Associate II
Posted on November 06, 2015 at 16:10

Hi there,

I'm trying to communicate to my SPI TFT LCD module. Previously I used to do it with an F4 discovery board, now I want to use an STM32F051 , here is my code:

#include <stm32f0xx.h>

#include <stm32f0xx_rcc.h>

#include <stm32f0xx_gpio.h>

#include <stm32f0xx_spi.h>

#define CSPin GPIO_Pin_1

#define ResPin GPIO_Pin_0

#define AOPin GPIO_Pin_4

#define DatPin GPIO_Pin_15

#define ClkPin GPIO_Pin_13

#define BLPin GPIO_Pin_5

enum Colours{

Black = 0x0000,

Blue = 0x0010,

Red = 0x8000,

Magenta = 0x8010,

Green = 0x0400,

Cyan = 0x0410,

Yellow = 0x8400,

White = 0xFFFF,

FlatBlue = 0x1DBF

};

typedef uint16_t Colours8;

#define XPix 128

#define YPix 128

GPIO_InitTypeDef GP_InitDef;

SPI_InitTypeDef SPI_InitDef;

void initGPIO(){

RCC->AHBENR |= RCC_AHBPeriph_GPIOB;

GP_InitDef.GPIO_Pin = CSPin | ResPin | AOPin;

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_10MHz;

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, ClkPin, GPIO_AF_0);

GPIO_PinAFConfig(GPIOB, DatPin, GPIO_AF_0);

}

void initSPI(){

RCC->APB2ENR |= RCC_APB2Periph_SPI1;

SPI_InitDef.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

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(SPI1, &SPI_InitDef);

SPI_Cmd(SPI1, ENABLE);

}

void writeCommand(uint16_t command){

GPIO_ResetBits(GPIOB, AOPin); // Set A0 D/C pin LOW for command select.

SPI_I2S_SendData16(SPI1, command);

while(SPI_I2S_GetFlagStatus(SPI1, 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_SendData16(SPI1, data);

while(SPI_I2S_GetFlagStatus(SPI1, 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_SendData16(SPI1, data>>8);

while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

SPI_I2S_SendData16(SPI1, data);

while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

}

void SetAddr(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2){

writeCommand(0x2A);

writeData(0x00);

writeData(X1);

writeData(0x00);

writeData(X2);

writeCommand(0x2B);

writeData(0x00);

writeData(32+Y1);

writeData(0x00);

writeData(32+Y2);

writeCommand(0x2C);

}

void SetScrn(Colours8 Colour){

uint16_t XCnt, YCnt;

SetAddr(0, 0, XPix-1, YPix-1);

for(XCnt = 0; XCnt<XPix; XCnt++){

for(YCnt = 0; YCnt<YPix; YCnt++){

switch(Colour){

case Black:

writeData16(0x0000);

break;

case Blue:

writeData16(0x0010);

break;

case Red:

writeData16(0x8000);

break;

case Magenta:

writeData16(0x8010);

break;

case Green:

writeData16(0x0400);

break;

case Cyan:

writeData16(0x0410);

break;

case Yellow:

writeData16(0x8400);

case FlatBlue:

writeData16(0x1DBF);

break;

case White:

writeData16(0xFFFF);

break;

}

}

}

}

void initLCD() {

int index;

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

SetScrn(Black);

for (index = 0; index < 5000; index++);

return ;

}

int main(void)

{

initGPIO();

initSPI();

initLCD();

return 0;

}

I connected everything as it should be , but not getting any response from the LCD. Just bright white screen ( backlight )

0 REPLIES 0