本帖最后由 eltonchang2001 于 2022-11-9 13:57 编辑
本次使用的是1.8寸的TFT,以前比较喜欢用0.96寸的OLED,使用OLED的话,显示数字、汉字这些基本功能可以满足了,但是OLED只有单色,在某些场合用着还是不够。正好手上有一个之前9.9买的TFT,在这里使用模拟iic的方式将屏幕点亮。
硬件设计:
使用的是模拟IIC进行通信,使用的引脚可以随意选择,如下所示:
程序设计:
添加LCD文件库
添加头文件路径
初始化I/O口
CKCU_PeripClockConfig_TypeDef GPIOlock = {{ 0 }};
GPIOlock.Bit.AFIO = 1;
GPIOlock.Bit.PC = 1; //使能PC
GPIOlock.Bit.PA = 1; //使能PC
CKCU_PeripClockConfig(GPIOlock, ENABLE); //使能时钟
AFIO_GPxConfig(GPIO_PA, AFIO_PIN_5|AFIO_PIN_6, AFIO_FUN_GPIO);
AFIO_GPxConfig(GPIO_PA, AFIO_PIN_0|AFIO_PIN_1|AFIO_PIN_2|AFIO_PIN_3, AFIO_FUN_GPIO);
GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_5|GPIO_PIN_6, GPIO_PR_DOWN);
GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PR_DOWN);
GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_5|GPIO_PIN_6, GPIO_DIR_OUT);
GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_DIR_OUT);
使用宏定义的方式进行语句的使用,也方便移植到其他MCU 上。
模拟IIC部分
//向SPI总线传输一个8位数据
void SPI_WriteData(u8 Data)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
if(Data&0x80)
{
LCD_SDA_SET; //输出数据
}
else
{
LCD_SDA_CLR;
}
LCD_SCL_CLR;
LCD_SCL_SET;
Data<<=1;
}
}
//向液晶屏写一个8位指令
void Lcd_WriteIndex(u8 Index)
{
//SPI 写命令时序开始
LCD_CS_CLR;
LCD_RS_CLR;
SPI_WriteData(Index);
LCD_CS_SET;
}
//向液晶屏写一个8位数据
void Lcd_WriteData(u8 Data)
{
LCD_CS_CLR;
LCD_RS_SET;
SPI_WriteData(Data);
LCD_CS_SET;
}
//向液晶屏写一个16位数据
void LCD_WriteData_16Bit(u16 Data)
{
LCD_CS_CLR;
LCD_RS_SET;
SPI_WriteData(Data>>8); //写入高8位数据
SPI_WriteData(Data); //写入低8位数据
LCD_CS_SET;
}
void Lcd_WriteReg(u8 Index,u8 Data)
{
Lcd_WriteIndex(Index);
Lcd_WriteData(Data);
}
void Lcd_Reset(void)
{
LCD_RST_CLR;
delay_ms(100);
LCD_RST_SET;
delay_ms(50);
}
//LCD Init For 1.44Inch LCD Panel with ST7735R.
void Lcd_Init(void)
{
LCD_GPIO_Init();
Lcd_Reset(); //Reset before LCD Init.
//LCD Init For 1.44Inch LCD Panel with ST7735R.
Lcd_WriteIndex(0x11);//Sleep exit
delay_ms (120);
//
//ST7735R Frame Rate
Lcd_WriteIndex(0xB1);
Lcd_WriteData(0x01);
Lcd_WriteData(0x2C);
Lcd_WriteData(0x2D);
Lcd_WriteIndex(0xB2);
Lcd_WriteData(0x01);
Lcd_WriteData(0x2C);
Lcd_WriteData(0x2D);
Lcd_WriteIndex(0xB3);
Lcd_WriteData(0x01);
Lcd_WriteData(0x2C);
Lcd_WriteData(0x2D);
Lcd_WriteData(0x01);
Lcd_WriteData(0x2C);
Lcd_WriteData(0x2D);
Lcd_WriteIndex(0xB4); //Column inversion
Lcd_WriteData(0x07);
//ST7735R Power Sequence
Lcd_WriteIndex(0xC0);
Lcd_WriteData(0xA2);
Lcd_WriteData(0x02);
Lcd_WriteData(0x84);
Lcd_WriteIndex(0xC1);
Lcd_WriteData(0xC5);
Lcd_WriteIndex(0xC2);
Lcd_WriteData(0x0A);
Lcd_WriteData(0x00);
Lcd_WriteIndex(0xC3);
Lcd_WriteData(0x8A);
Lcd_WriteData(0x2A);
Lcd_WriteIndex(0xC4);
Lcd_WriteData(0x8A);
Lcd_WriteData(0xEE);
Lcd_WriteIndex(0xC5); //VCOM
Lcd_WriteData(0x0E);
Lcd_WriteIndex(0x36); //MX, MY, RGB mode
Lcd_WriteData(0xC0);
//ST7735R Gamma Sequence
Lcd_WriteIndex(0xe0);
Lcd_WriteData(0x0f);
Lcd_WriteData(0x1a);
Lcd_WriteData(0x0f);
Lcd_WriteData(0x18);
Lcd_WriteData(0x2f);
Lcd_WriteData(0x28);
Lcd_WriteData(0x20);
Lcd_WriteData(0x22);
Lcd_WriteData(0x1f);
Lcd_WriteData(0x1b);
Lcd_WriteData(0x23);
Lcd_WriteData(0x37);
Lcd_WriteData(0x00);
Lcd_WriteData(0x07);
Lcd_WriteData(0x02);
Lcd_WriteData(0x10);
Lcd_WriteIndex(0xe1);
Lcd_WriteData(0x0f);
Lcd_WriteData(0x1b);
Lcd_WriteData(0x0f);
Lcd_WriteData(0x17);
Lcd_WriteData(0x33);
Lcd_WriteData(0x2c);
Lcd_WriteData(0x29);
Lcd_WriteData(0x2e);
Lcd_WriteData(0x30);
Lcd_WriteData(0x30);
Lcd_WriteData(0x39);
Lcd_WriteData(0x3f);
Lcd_WriteData(0x00);
Lcd_WriteData(0x07);
Lcd_WriteData(0x03);
Lcd_WriteData(0x10);
Lcd_WriteIndex(0x2a);
Lcd_WriteData(0x00);
Lcd_WriteData(0x00);
Lcd_WriteData(0x00);
Lcd_WriteData(0x7f);
Lcd_WriteIndex(0x2b);
Lcd_WriteData(0x00);
Lcd_WriteData(0x00);
Lcd_WriteData(0x00);
Lcd_WriteData(0x9f);
Lcd_WriteIndex(0xF0); //Enable test command
Lcd_WriteData(0x01);
Lcd_WriteIndex(0xF6); //Disable ram power save mode
Lcd_WriteData(0x00);
Lcd_WriteIndex(0x3A); //65k mode
Lcd_WriteData(0x05);
Lcd_WriteIndex(0x29);//Display on
}
主函数,这里使用的是之前使用FreeRTOS的程序,灯依然会闪,只是加入了显示的部分。
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] IP/Example/main.c
* [url=home.php?mod=space&uid=895143]@version[/url] $Rev:: 4869 $
* [url=home.php?mod=space&uid=212281]@date[/url] $Date:: 2020-08-05 #$
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program.
*************************************************************************************************************
* @attention
*
* Firmware Disclaimer Information
*
* 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
* proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
* other intellectual property laws.
*
* 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
* other than HOLTEK and the customer.
*
* 3. The program technical documentation, including the code, is provided "as is" and for customer reference
* only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
* the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
* the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
*
* <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
************************************************************************************************************/
// <<< Use Configuration Wizard in Context Menu >>>
/* Includes ------------------------------------------------------------------------------------------------*/
#include "ht32.h"
#include "ht32_board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "delay.h"
#include "QDTFT_demo.h"
#include "Lcd_Driver.h"
#include "ht32f5xxxx_gpio.h"
/* Settings ------------------------------------------------------------------------------------------------*/
/* Private types -------------------------------------------------------------------------------------------*/
/* Private constants ---------------------------------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------------------------------------*/
void NVIC_Configuration(void);
void CKCU_Configuration(void);
void GPIO_Configuration(void);
#if (ENABLE_CKOUT == 1)
void CKOUTConfig(void);
#endif
//任务堆栈大小
#define LED1_TASK_STACK 256
#define LED2_TASK_STACK 215
//任务优先级
#define LED1_TASK_PRIORITY 5
#define LED2_TASK_PRIORITY 3
xTaskHandle startTask;
xTaskHandle LED1Task;
xTaskHandle LED2Task;
portTASK_FUNCTION(vLED1Task, pvParameters)
{
while(1)
{
HT32F_DVB_LEDOn(HT_LED1);
vTaskDelay(200);
HT32F_DVB_LEDOff(HT_LED1);
vTaskDelay(200);
}
}
portTASK_FUNCTION(vLED2Task, pvParameters)
{
while(1)
{
HT32F_DVB_LEDOn(HT_LED2);
vTaskDelay(500);
HT32F_DVB_LEDOff(HT_LED2);
vTaskDelay(500);
}
}
/**********************************************************************************************************
*函 数 名: vStartTask
*功能说明: 系统启动任务,调用各类初始化函数,并创建消息队列和要运行的用户任务
*形 参: 无
*返 回 值: 无
**********************************************************************************************************/
portTASK_FUNCTION(vStartTask, pvParameters)
{
xTaskCreate(vLED1Task, (char const*)"SensorReadTask",LED1_TASK_STACK, NULL, LED1_TASK_PRIORITY, &LED1Task);
xTaskCreate(vLED2Task, (char const*)"SensorUpdateTask",LED2_TASK_STACK, NULL,LED2_TASK_PRIORITY, &LED2Task);
//删除本任务
vTaskDelete(NULL);
}
/* Private macro -------------------------------------------------------------------------------------------*/
/* Global variables ----------------------------------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------------------------------------*/
/* Global functions ----------------------------------------------------------------------------------------*/
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program.
* @retval None
***********************************************************************************************************/
int main(void)
{
NVIC_Configuration(); /* NVIC configuration */
CKCU_Configuration(); /* System Related configuration */
GPIO_Configuration(); /* GPIO Related configuration */
RETARGET_Configuration(); /* Retarget Related configuration */
HT32F_DVB_LEDInit(HT_LED1);
HT32F_DVB_LEDInit(HT_LED2);
HT32F_DVB_LEDInit(HT_LED3);
HT32F_DVB_LEDOn(HT_LED1);
HT32F_DVB_LEDOff(HT_LED2);
HT32F_DVB_LEDOn(HT_LED3);
Lcd_Init();
LCD_LED_SET;
Lcd_Clear(GRAY0);
Gui_DrawFont_GBK16(20,20,BLACK,GRAY0,"HT32 & 21ic");
Gui_DrawFont_GBK16(20,40,BLACK,GRAY0,"1.8 TFT test");
//创建启动任务
xTaskCreate(vStartTask, "startTask", 128, NULL, 0, &startTask);
// //OS调度器启动
vTaskStartScheduler();
}
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configure the NVIC vector table.
* @retval None
***********************************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_SetVectorTable(NVIC_VECTTABLE_FLASH, 0x0); /* Set the Vector Table base location at 0x00000000 */
}
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configure the system clocks.
* @retval None
***********************************************************************************************************/
void CKCU_Configuration(void)
{
/*
//<e0> Enable Peripheral Clock
// <h> Communication
// <q5> EBI
// <q11> I2C0 <q12> I2C1
// <q23> I2S
// <q21> SCI0 <q22> SCI1
// <q13> SPI0 <q14> SPI1
// <q17> UART0 <q18> UART1
// <q15> USART0 <q16> USART1
// <q3> USB
// </h>
// <h> IO
// <q7> GPIO Port A <q8> GPIO Port B <q9> GPIO Port C <q10> GPIO Port D
// <q19> AFIO
// <q20> EXTI
// </h>
// <h> System
// <q32> ADC
// <q4> CKREF
// <q6> CRC
// <q31> CMP
// <q2> PDMA
// <q26> PWRCU
// </h>
// <h> Timer
// <q29> BFTM0 <q30> BFTM1
// <q33> SCTM0 <q34> SCTM1 <q35> SCTM2 <q36> SCTM3
// <q27> GPTM0 <q28> GPTM1
// <q24> MCTM0
// <q26> RTC <q25> WDT
// </h>
//</e>
*/
#if 1
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.PDMA = 0;
CKCUClock.Bit.USBD = 0;
CKCUClock.Bit.CKREF = 0;
CKCUClock.Bit.EBI = 0;
CKCUClock.Bit.CRC = 0;
CKCUClock.Bit.PA = 1;
CKCUClock.Bit.PB = 1;
CKCUClock.Bit.PC = 1;
CKCUClock.Bit.PD = 1;
CKCUClock.Bit.I2C0 = 0;
CKCUClock.Bit.I2C1 = 0;
CKCUClock.Bit.SPI0 = 0;
CKCUClock.Bit.SPI1 = 0;
CKCUClock.Bit.USART0 = 1;
CKCUClock.Bit.USART1 = 0;
CKCUClock.Bit.UART0 = 0;
CKCUClock.Bit.UART1 = 0;
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.EXTI = 0;
CKCUClock.Bit.SCI0 = 0;
CKCUClock.Bit.SCI1 = 0;
CKCUClock.Bit.I2S = 0;
CKCUClock.Bit.MCTM0 = 0;
CKCUClock.Bit.WDT = 0;
CKCUClock.Bit.BKP = 0;
CKCUClock.Bit.GPTM0 = 0;
CKCUClock.Bit.GPTM1 = 0;
CKCUClock.Bit.BFTM0 = 0;
CKCUClock.Bit.BFTM1 = 0;
CKCUClock.Bit.CMP = 0;
CKCUClock.Bit.ADC = 0;
CKCUClock.Bit.SCTM0 = 0;
CKCUClock.Bit.SCTM1 = 0;
CKCUClock.Bit.SCTM2 = 0;
CKCUClock.Bit.SCTM3 = 0;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
#endif
#if (ENABLE_CKOUT == 1)
CKOUTConfig();
#endif
}
#if (ENABLE_CKOUT == 1)
/*********************************************************************************************************//**
* @brief Configure the debug output clock.
* @retval None
***********************************************************************************************************/
void CKOUTConfig(void)
{
{ /* Enable peripheral clock */
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
}
AFIO_GPxConfig(GPIO_PA, AFIO_PIN_9, AFIO_MODE_15);
{ /* Configure CKOUT */
CKCU_CKOUTInitTypeDef CKOUTInit;
CKOUTInit.CKOUTSRC = CKCU_CKOUTSRC_HCLK_DIV16;
CKCU_CKOUTConfig(&CKOUTInit);
}
}
#endif
/*********************************************************************************************************//**
* @brief Configure the GPIO ports.
* @retval None
***********************************************************************************************************/
void GPIO_Configuration(void)
{
/* !!! NOTICE !!!
Shall be modified according to the part number.
*/
#if (RETARGET_PORT == RETARGET_USART0)
//AFIO_GPxConfig(GPIO_PA, AFIO_PIN_2 | AFIO_PIN_3, AFIO_FUN_USART_UART);
#endif
#if (RETARGET_PORT == RETARGET_USART1)
//AFIO_GPxConfig(GPIO_PA, AFIO_PIN_4 | AFIO_PIN_5, AFIO_FUN_USART_UART);
#endif
#if (RETARGET_PORT == RETARGET_UART0)
//AFIO_GPxConfig(GPIO_PC, AFIO_PIN_4 | AFIO_PIN_5, AFIO_FUN_USART_UART);
#endif
#if (RETARGET_PORT == RETARGET_UART1)
//AFIO_GPxConfig(GPIO_PC, AFIO_PIN_1 | AFIO_PIN_3, AFIO_FUN_USART_UART);
#endif
}
#if (HT32_LIB_DEBUG == 1)
/*********************************************************************************************************//**
* @brief Report both the error name of the source file and the source line number.
* @param filename: pointer to the source file name.
* @param uline: error line source number.
* @retval None
***********************************************************************************************************/
void assert_error(u8* filename, u32 uline)
{
/*
This function is called by IP library that the invalid parameters has been passed to the library API.
Debug message can be added here.
Example: printf("Parameter Error: file %s on line %d\r\n", filename, uline);
*/
while (1)
{
}
}
#endif
显示结果(在上面的程序基础上加入了其他显示功能):
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
共1人点赞
|