号外(相关文章):
【GD32L233C-START评测】02 上电及程序下载调试
【GD32L233C-START评测】03 LED操作和普通定时功能
【GD32L233C-START评测】04 外部中断与定时器PWM
【GD32L233C-START评测】05 串口体验和不定长数据接收
【GD32L233C-START评测】06 制作GD32L233C扩展板
前言
制作的扩展板终于回来了,赶紧将他全副武装上,对照这原理图测试了一下,没有短路等情况,不过发现了一个问题,单片机的资源有点不够,TFT的复位引脚没有分配上,这可不行,初始化还需要对复位引脚进行操作,细数了一下目前的资源决定把NB供电的mos控制引脚改过来,NB的供电通过直连方式,谁让你宣称是低功耗的。
实物效果如下:
目标
体验SPI功能,并初始驱动1.54吋TFT液晶屏。
分析
液晶屏为1.54吋,IPS高清显示,分辨率240*240,驱动芯片为ST7789,支持MCU和SPI两种通信方式,这一次是需要体验SPI,故在设计是M0~M2做出的相对应的选择,同时根据厂家的介绍,SPI通信时只能作为从机接收数据,所以只用到了CS、MOSI和SCK。
接下来就是对应的SPI初始化:
第一:对应IO口设置成复用模式;
第二:SPI初始化并使能;
//******************************************************************************
//* 函数名称 : gd_SPI_init
//* 函数描述 : SPI配置
//* 输入参数 :
//* 参数描述 : SPI初始化配置
//* 输出参数 : 无
//* 返回值 : 无
//******************************************************************************
void gd_SPI_init(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_SPI1);
/* SPI1 GPIO configuration: SCK/PB13, MISO/PB14, MOSI/PB15 */
gpio_af_set(GPIOB, GPIO_AF_6, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
spi_parameter_struct spi_init_struct;
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI1);
spi_struct_para_init(&spi_init_struct);
/* SPI1 parameter configuration */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_8;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI1, &spi_init_struct);
spi_enable(SPI1);
}
对于SPI发送数据,直接使用spi_i2s_data_transmit(SPI1, data)就可以。在使用硬件SPI进行读取时需要注意,对于SPI从来说是无法产生时钟的,也就是说我们直接用spi_i2s_data_receive(SPI1)实际上并没有真正工作起来,是需要主机提供CLK时序,在SPI读取时需要先spi_i2s_data_transmit(SPI1, data),通过这个函数给时钟,然后再接收。
下面就是液晶屏的初始化,每家的初始化代码都不太相同,这些可以和供应商或者淘宝店家要,然后移植过来就可以,初始化代码执行后,一般出现雪花就证明初始化成功,对于IPS的TFT初始化失败不太好观察,初始化失败是不点亮的,不容易观察,对于普通的TFT初始化失败一般是白屏,当然这些现象可以被观察的前提是你的背光要点亮。对于测试阶段建议背光上电就点亮,成品建议在初始化完成并且写全屏单色黑之后点亮。
TFT初始化函数:
//******************************************************************************
//* 函数名称 : void App_tft_init (void)
//* 函数描述 : 1.54吋液晶屏初始化
//* 输入参数 :
//* 参数描述 :
//* 输出参数 : 无
//* 返回值 : 无
//******************************************************************************
void ST7789V_tft_init(void)
{
//------------------ST7789 ResetSequence-------------------------------------
TFT_RESET_SET;
DelaySysTick_ms(1); //Delay 1ms
TFT_RESET_RESET;
DelaySysTick_ms(1); //Delay 1ms
TFT_RESET_SET;
DelaySysTick_ms(120); //Delay 120ms
//--------------------End ST7789 ResetSequence ------------------------------
send_ctrl_cmd(0x11); //Sleep out
DelaySysTick_ms(120); //Delay 120ms
send_ctrl_cmd (0xc1);
send_data_cmd(0x02);
send_data_cmd(0x02);
send_data_cmd(0x02);
// send_ctrl_cmd (0x04);
// TFT_ID[0] = TFT_DATA_ADD;
// TFT_ID[1] = TFT_DATA_ADD;
// TFT_ID[2] = TFT_DATA_ADD;
// TFT_ID[3] = TFT_DATA_ADD;
send_ctrl_cmd(0x36);
send_data_cmd(0x00);
send_ctrl_cmd(0x3A);
send_data_cmd(0x05);
send_ctrl_cmd(0x51);
send_data_cmd(0x00);
//---ST7789 Frame rate setting------//
send_ctrl_cmd(0xb2);
send_data_cmd(0x0c);
send_data_cmd(0x0c);
send_data_cmd(0x00);
send_data_cmd(0x33);
send_data_cmd(0x33);
send_ctrl_cmd(0xb7);
send_data_cmd(0x72);
send_ctrl_cmd(0xbb);
send_data_cmd(0x1c);
send_ctrl_cmd(0xC0);
send_data_cmd(0x2C);
send_ctrl_cmd(0xC2);
send_data_cmd(0x01);
send_ctrl_cmd(0xC3);
send_data_cmd(0x0b);
send_ctrl_cmd(0xC4);
send_data_cmd(0x20);
send_ctrl_cmd(0xC6);
send_data_cmd(0x0F);
send_ctrl_cmd(0xD0);
send_data_cmd(0xA4);
send_data_cmd(0xA1);
send_ctrl_cmd(0x3A);
send_data_cmd(0x05);
//--------------------------------ST7789 gamma setting-----------------------
send_ctrl_cmd(0xe0);
send_data_cmd(0xd0);
send_data_cmd(0x06);
send_data_cmd(0x0b);
send_data_cmd(0x0a);
send_data_cmd(0x09);
send_data_cmd(0x06);
send_data_cmd(0x2f);
send_data_cmd(0x44);
send_data_cmd(0x45);
send_data_cmd(0x18);
send_data_cmd(0x14);
send_data_cmd(0x14);
send_data_cmd(0x27);
send_data_cmd(0x2d);
send_ctrl_cmd(0xe1);
send_data_cmd(0xd0);
send_data_cmd(0x06);
send_data_cmd(0x0b);
send_data_cmd(0x0a);
send_data_cmd(0x09);
send_data_cmd(0x05);
send_data_cmd(0x2e);
send_data_cmd(0x43);
send_data_cmd(0x45);
send_data_cmd(0x18);
send_data_cmd(0x14);
send_data_cmd(0x14);
send_data_cmd(0x27);
send_data_cmd(0x2d);
send_ctrl_cmd(0x21);
send_ctrl_cmd(0x35);
send_data_cmd(0x00);
send_ctrl_cmd(0x44);
send_data_cmd(0x00);
send_data_cmd(0x00);
send_ctrl_cmd(0x11);
DelaySysTick_ms(120);
send_ctrl_cmd(0x36);//设置起点与方向(地址控制)
send_data_cmd(0xC0);
X_offest = 0;
Y_offest = 80;
send_ctrl_cmd(0x29);
send_ctrl_cmd(0x2a);
send_data_cmd(0x00);
send_data_cmd(0x00);
send_data_cmd(0x00);
send_data_cmd(0xEf); //EF
send_ctrl_cmd(0x2b);
send_data_cmd(0x00);
send_data_cmd(0x00);
send_data_cmd(0x01);
send_data_cmd(0x3f);
send_ctrl_cmd(0x2C);
TFT_SetArea(0,0,240,240,Colour_BLACK);
DelaySysTick_ms(20);
TFT_BL_SET;
}
|