2022-06-01 12:45 AM
2022-06-01 12:46 AM
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
void TFT_Send_A_2Byte_To_TFT_DataPins(uint32_t character)
{
HAL_GPIO_WritePin(TFT_D0_GPIO_Port, TFT_D0_Pin, (character & 0b0000000000000001)>>0);
HAL_GPIO_WritePin(TFT_D1_GPIO_Port, TFT_D1_Pin, (character & 0b0000000000000010)>>1);
HAL_GPIO_WritePin(TFT_D2_GPIO_Port, TFT_D2_Pin, (character & 0b0000000000000100)>>2);
HAL_GPIO_WritePin(TFT_D3_GPIO_Port, TFT_D3_Pin, (character & 0b0000000000001000)>>3);
HAL_GPIO_WritePin(TFT_D4_GPIO_Port, TFT_D4_Pin, (character & 0b0000000000010000)>>4);
HAL_GPIO_WritePin(TFT_D5_GPIO_Port, TFT_D5_Pin, (character & 0b0000000000100000)>>5);
HAL_GPIO_WritePin(TFT_D6_GPIO_Port, TFT_D6_Pin, (character & 0b0000000001000000)>>6);
HAL_GPIO_WritePin(TFT_D7_GPIO_Port, TFT_D7_Pin, (character & 0b0000000010000000)>>7);
}
void Write_Command(uint32_t Command)
{
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 1);
HAL_GPIO_WritePin(TFT_RST_GPIO_Port, TFT_RST_Pin, 1);
HAL_GPIO_WritePin(TFT_CS_GPIO_Port, TFT_CS_Pin, 0);
HAL_GPIO_WritePin(TFT_RS_GPIO_Port, TFT_RS_Pin, 0);
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 0);
TFT_Send_A_2Byte_To_TFT_DataPins(Command);
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 1);
}
void Write_Data(uint32_t Data)
{
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 1);
HAL_GPIO_WritePin(TFT_RST_GPIO_Port, TFT_RST_Pin, 1);
HAL_GPIO_WritePin(TFT_CS_GPIO_Port, TFT_CS_Pin, 0);
HAL_GPIO_WritePin(TFT_RS_GPIO_Port, TFT_RS_Pin, 1);
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 0);
TFT_Send_A_2Byte_To_TFT_DataPins(Data);
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 1);
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 0);
TFT_Send_A_2Byte_To_TFT_DataPins(Data<<8);
HAL_GPIO_WritePin(TFT_WR_GPIO_Port, TFT_WR_Pin, 1);
}
void Initialization()
{ //******* Reset tft driver ***************
HAL_GPIO_WritePin(TFT_RST_GPIO_Port, TFT_RST_Pin, 1);
HAL_Delay(1);
HAL_GPIO_WritePin(TFT_RST_GPIO_Port, TFT_RST_Pin, 0);
HAL_Delay(10); // Delay 10ms // This delay time is necessary
HAL_GPIO_WritePin(TFT_RST_GPIO_Port, TFT_RST_Pin, 1);
HAL_Delay(120);
//************* Start Initial Sequence **********//
Write_Command(0xF2);
Write_Data(0x18);
Write_Data(0xA3);
Write_Data(0x12);
Write_Data(0x02);
Write_Data(0xB2);
Write_Data(0x12);
Write_Data(0xFF);
Write_Data(0x10);
Write_Data(0x00);
Write_Command(0xF1);
Write_Data(0x36);
Write_Data(0xA4);
Write_Command(0xF8);
Write_Data(0x21);
Write_Data(0x04);
Write_Command(0xF9);
Write_Data(0x00);
Write_Data(0x08);
Write_Command(0x36); //Memory Access Control
Write_Data(0x08);
Write_Command(0xB4); //Display Inversion Control
Write_Data(0x00);
Write_Command(0xC1); //Power Control 2
Write_Data(0x41);
//Write_Data(0x00);
Write_Command(0xC5); //VCOM Control
Write_Data(0x08); //is used to set factor to generate VCOM voltage from the reference voltage VREG2OUT.
Write_Command(0xC0); //Power Control 1
Write_Data(0x0D);
Write_Data(0x0D);
Write_Command(0xC2); //Power Control 3
Write_Data(0x00);
Write_Command(0xE0); // PGAMCTRL (Positive Gamma Control)
Write_Data(0x0F);
Write_Data(0x24);
Write_Data(0x1C);
Write_Data(0x0A);
Write_Data(0x0F);
Write_Data(0x08);
Write_Data(0x43);
Write_Data(0x88);
Write_Data(0x32);
Write_Data(0x0F);
Write_Data(0x10);
Write_Data(0x06);
Write_Data(0x0F);
Write_Data(0x07);
Write_Data(0x00);
Write_Command(0xE1); // NGAMCTRL (Negative Gamma Control)
Write_Data(0x0F);
Write_Data(0x38);
Write_Data(0x30);
Write_Data(0x09);
Write_Data(0x0F);
Write_Data(0x0F);
Write_Data(0x4E);
Write_Data(0x77);
Write_Data(0x3C);
Write_Data(0x07);
Write_Data(0x10);
Write_Data(0x05);
Write_Data(0x23);
Write_Data(0x1B);
Write_Data(0x00);
Write_Command(0xE2); // Digital Gamma Control 1
Write_Data(0x0F);
Write_Data(0x32);
Write_Data(0x2E);
Write_Data(0x0B);
Write_Data(0x0D);
Write_Data(0x05);
Write_Data(0x47);
Write_Data(0x75);
Write_Data(0x37);
Write_Data(0x06);
Write_Data(0x10);
Write_Data(0x03);
Write_Data(0x24);
Write_Data(0x20);
Write_Data(0x00);
Write_Command(0xB6); //Display Function Control
Write_Data(0x00);
Write_Data(0x22); //0x42=Rotation 180 degrees //0x22
Write_Data(0x3B);
Write_Command(0x20); // Display Inversion OFF
//Write_Data(0xC8); //C8???
Write_Command(0x3A); // Interface Pixel Format
Write_Data(0x55);
Write_Command(0x2A); // Column Address Set
Write_Data(0x00);
Write_Data(0x00);
Write_Data(0x01);
Write_Data(0x3F);
Write_Command(0x2B); // Page Address Set
Write_Data(0x00);
Write_Data(0x00);
Write_Data(0x01);
Write_Data(0xDF);
Write_Command(0x11); //Sleep out
HAL_Delay(120);
Write_Command(0x38); //Idle Mode Off
Write_Command(0x13); //Normal Mode On
Write_Command(0x29); // Display ON
}
2022-06-01 12:46 AM
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
HAL_Init();
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
Initialization();
Write_Command(0x2C); // Memory Write
HAL_Delay(1);
for(int i=0;i<(480);i++)
{
for(int j=0;j<320;j++)
{
if(i==360)
{
Write_Data(0xFFFF); //White Color
}
if(i==240)
{
Write_Data(0x07E0); //Green Color
}
if(i==120)
{
Write_Data(0xFFE0); //Yellow Color
}
if(j==160)
{
Write_Data(0x0001F); //Blue Color
}
else
{
Write_Data(0xF800); //Red Color
}
}
}
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, TFT_D7_Pin|TFT_D6_Pin|TFT_D5_Pin|TFT_D4_Pin
|TFT_D3_Pin|TFT_WR_Pin|TFT_RS_Pin|TFT_CS_Pin
|TFT_RST_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, TFT_D2_Pin|TFT_D1_Pin|TFT_D0_Pin|TFT_RD_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : TFT_D7_Pin TFT_D6_Pin TFT_D5_Pin TFT_D4_Pin
TFT_D3_Pin TFT_WR_Pin TFT_RS_Pin TFT_CS_Pin
TFT_RST_Pin */
GPIO_InitStruct.Pin = TFT_D7_Pin|TFT_D6_Pin|TFT_D5_Pin|TFT_D4_Pin
|TFT_D3_Pin|TFT_WR_Pin|TFT_RS_Pin|TFT_CS_Pin
|TFT_RST_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : TFT_D2_Pin TFT_D1_Pin TFT_D0_Pin TFT_RD_Pin */
GPIO_InitStruct.Pin = TFT_D2_Pin|TFT_D1_Pin|TFT_D0_Pin|TFT_RD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
/**