【国民技术N32G457评测】SPI_ST7735 基于RT—Thread Studio
[复制链接]
今天买的LCD屏到货了,芯片是ST7735。9.9元买的。。
前面SPI驱动花了好多时间,今天在前面的基础上驱动SPI显示屏,同时也是巩固rt-thread studio的spi使用熟练度。
1、由于没有st7735的库,买的屏的例程是Luaos驱动,所以花了一点时间去网上找驱动。这里要感谢Hei51的这位网友提供的驱动。STM32- ST7735 1.8寸TFT-LCD程序 - STM32/8
2、这个驱动是基于stm32f103C8的,我下载下来接上线,屏点亮成功,说明屏是好的。
3、例程所写的这模拟spi驱动的,我看了一下,只需要配置把接口全部配置为输出就OK了。
// 功能描述 :N32G45 st7735
// 说明:
// ----------------------------------------------------------------
// GND 电源地
// VCC 3.3v电源
// SCL PA5(SCLK)
// SDA PA7(MOSI)
// RES PC4 33
// DC PC5 34
// CS PA4
// BLK PB0 35
// ----------------------------------------------------------------
#define RES_PIN 33 //PC4
#define DC_PIN 34 //PC5
#define BLK_PIN 35 //PB0
#define SCK_PIN 30 //PA5
#define MOSI_PIN 32 //PA7
#define CS_PIN 29 //PA4
rt_pin_mode(RES_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(DC_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(BLK_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(SCK_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(MOSI_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(CS_PIN, PIN_MODE_OUTPUT);
然后更新宏定义:
#define LCD_SCLK_Clr() rt_pin_write(SCK_PIN, PIN_LOW)//SCL=SCLK
#define LCD_SCLK_Set() rt_pin_write(SCK_PIN, PIN_HIGH)
#define LCD_MOSI_Clr() rt_pin_write(MOSI_PIN, PIN_LOW)//SDA=MOSI
#define LCD_MOSI_Set() rt_pin_write(MOSI_PIN, PIN_HIGH)
#define LCD_CS_Clr() rt_pin_write(CS_PIN, PIN_LOW)//CS
#define LCD_CS_Set() rt_pin_write(CS_PIN, PIN_HIGH)
#define LCD_RES_Clr() rt_pin_write(RES_PIN, PIN_LOW)//RES
#define LCD_RES_Set() rt_pin_write(RES_PIN, PIN_HIGH)
#define LCD_DC_Clr() rt_pin_write(DC_PIN, PIN_LOW)//DC
#define LCD_DC_Set() rt_pin_write(DC_PIN, PIN_HIGH)
#define LCD_BLK_Clr() rt_pin_write(BLK_PIN, PIN_LOW)//BLK
#define LCD_BLK_Set() rt_pin_write(BLK_PIN, PIN_HIGH)
同时把u6、u16、u32等替换成uint8_t,uint16_t,uint32_t。更新man.c,下载就成功点亮。一切还算顺利。
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "lcd_init.h"
#include "lcd.h"
#include "pic.h"
/* defined the LED1 pin: PB5 */
#define LED1_PIN 91
int main(void)
{
uint32_t Speed = 200;
float t=0;
/* set LED1 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
LCD_Init();//LCD初始化
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
LCD_ShowString(0,0,"RTTDEMO",BLUE,WHITE,32,0);
LCD_ShowString(24,36,"N32G45XVL",RED,WHITE,16,0);
LCD_ShowString(24,60,"LCD-W:", BROWN ,WHITE,16,0);
LCD_ShowIntNum(72,60,LCD_W,3, BROWN ,WHITE,16);
LCD_ShowString(24,80,"LCD_H:",RED,WHITE,16,0);
LCD_ShowIntNum(72,80,LCD_H,3,RED,WHITE,16);
LCD_ShowPicture(65,110,40,40,gImage_1);
while (1)
{
LCD_ShowFloatNum1(20,110,t,4,BLACK,WHITE,16);
t+=0.11;
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(Speed);
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_thread_mdelay(Speed);
}
}
模拟spi是速度好象不是很快,刷新的速度好慢。下面准备更新为硬件spi。
我看了一下spi输出函数,主要就是把void LCD_Writ_Bus(uint8_t dat) 更改为spi的rt_spi_send函数就行了。
于是在lcdinit.c下面增加spi初始化函数:int rt_hw_spi_config(void)
struct n32_hw_spi_cs
{
GPIO_Module* GPIOx;
uint32_t GPIO_Pin;
};
struct rt_spi_device *st7753_spi_dev;
int rt_hw_spi_config(void)
{
static struct n32_hw_spi_cs spi_cs1;
static struct rt_spi_device spi_dev_1 ;
rt_err_t res;
spi_cs1.GPIO_Pin = GPIO_PIN_4;
spi_cs1.GPIOx = GPIOA;
rt_pin_mode(CS_PIN, PIN_MODE_OUTPUT);
res = rt_spi_bus_attach_device(&spi_dev_1, SPI1__DEVICE_NAME, SPI1_BUS_NAME, (void*)&spi_cs1);
if (res != RT_EOK)
{
rt_kprintf("rt_spi_bus_attach_device!\r\n");
return res;
}
spi_dev_1.bus->owner = &spi_dev_1;
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_3 | RT_SPI_MSB ;
cfg.max_hz = 40*1000 *1000; /* 40M,SPI max 42MHz, 3-wire spi */
if(spi_dev_1.bus->owner==RT_NULL)
{
rt_kprintf("RT_OK\r\n");
}
rt_kprintf("owner= %d\r\n",*spi_dev_1.bus->owner);
res = rt_spi_configure(&spi_dev_1, &(cfg));
if(res != RT_EOK)
{
rt_kprintf("configure spi1 ERR!\r\n");
}
}
st7753_spi_dev = (struct rt_spi_device *)rt_device_find(SPI1__DEVICE_NAME);
if(!&st7753_spi_dev)
{
rt_kprintf("spi1 test run failed! can't find %s device!\n", SPI1__DEVICE_NAME);
}
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_spi_config);
这里需要注意的是st7735的模式要配置成RT_SPI_MODE_3 /* CPOL = 1, CPHA = 1 */ 。我前面配置为1-2时,数据是有输出但是屏死活不亮,后面找了数据手册才知道要配置成这个模式才能点亮。
|