Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Can not get my ili9163 spi tft to work on stm32f4

Status
Not open for further replies.

Montassar Ghanmy

Junior Member level 2
Junior Member level 2
Joined
Feb 17, 2015
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
159
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:

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 !!!
 

Some observations:
- respin is asserted low but never released => controller stays in reset
- how do you connect/control SPI chip select pin CSX?

In addition, are you sure about suitable SPI clock speed, correct SPI mode?
 
Hi there , thankx for quick reply.
-As for the respin , that's exactly the same though I got once i saw the code of another library that tweaked to write this. Do you suggest adding
Code:
GPIO_ResetBits(GPIOB, ResPin);
after ?
-The chip select pin in my code is referred to as CSPin which is PB1 on the board , I just pull it Low when I need which is done once in this example.
-The Clock is set to 25MHz however on the other library it's set to 10Mhz , do you think changing that affacted my code ? As for the Mode it's set to Master for the stm so I'm thinking it's okay.
Please let me know
 

20150217_130819.jpg

Yes I totally forgot to enable the clock for SPI3 , I added
Code:
RCC->APB1ENR |= RCC_APB1Periph_SPI3;
And I got the above result in the picture , eventhough it's quiet a step , the LCD is still not displaying a Red rectangle as written in my code.
"CS pin not driven" what do you mean please ??

Thank you :thumbsup:
 

You have defined CSPin, but never used it. Don't you know how the SPI works? You should force CS low before transmission and high after.

- - - Updated - - -

I greatly recomend you to learn about real time debugging. Your code without turning on clocking SPI3, should stuck on while(..) cycle forever
Code:
void writeCommand(uint16_t command){
	GPIO_ResetBits(GPIOB, AOPin);	// Set A0 D/C pin LOW for command select.
	SPI_I2S_SendData(SPI3, command);
	[COLOR="#FF0000"]while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET);[/COLOR]
}
 
Thank you for recommending that. As for the CSPin it needs to be low anyway (that's what's written in the datasheet)
I managed to get the LCD working , changed the background color and drew some recetangles and circles :) Thank you for responding !!!!!
 

As for the CSPin it needs to be low anyway (that's what's written in the datasheet)
I saw regular SPI frames with CS being asserted and deasserted each time in the datasheet. Some SPI periperals manage to work with CS pulled permantly low, the disadavantage is that after a single spike on the clock line, the device won't ever synchronize. So it's better to avoid this "poor man's" kind of SPI operation.
 
You mean if the clock line gets cut or something during data being transmitted the TFT will never work again with the way I'm doing it ?? I'm really a beginner and barely managed to get this working.
What do you recommend ?
Thank you for letting me know.
 

It is not your case. You pooling finishing flag after sending a data. So, you only have to be sure that time from cs becomes low and first clock plulse rising/falling should be enought according the manual. But usualy we are never care about this even with 180MHz core and bitbang register using. This happens rarely.
 
20150218_100254.jpg
I see , thankx for explaining . I managed to display text and an icon , turned to be not as hard as I thought. I posted an image to my final results :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top