Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
ili9341
用rt_thread studio怎么驱动ili9341 ?
发布于 2021-08-02 15:05:09 浏览:1130
订阅该版
```c #include "drv_9341.h" #include "stdlib.h" #include "stdio.h" //LCD的画笔颜色和背景色 uint16_t POINT_COLOR = 0x0000; //画笔颜色 uint16_t BACK_COLOR = 0xFFFF; //背景色 //管理LCD重要参数 //默认为竖屏 _lcd_dev lcddev; void lcd_write_cmd(uint16_t data) { LCD_RS = 0;//写地址 LCD_CS = 0; LCD_DATA_OUT(data); LCD_WR = 0; LCD_WR = 1; LCD_CS = 1; } //写数据函数 //可以替代lcd_write_dataX宏,拿时间换空间. //data:寄存器值 void lcd_write_dataX(uint16_t data) { LCD_RS = 1; LCD_CS = 0; LCD_DATA_OUT(data); LCD_WR = 0; LCD_WR = 1; LCD_CS = 1; } uint16_t lcd_read_data(void) { uint16_t t; GPIOB->CRL = 0X88888888; //PB0-7 上拉输入 GPIOB->CRH = 0X88888888; //PB8-15 上拉输入 GPIOB->ODR = 0X0000; //全部输出0 LCD_RS = 1; LCD_CS = 0; //读取数据(读寄存器时,并不需要读2次) LCD_RD = 0; t = LCD_DATA_IN; LCD_RD = 1; LCD_CS = 1; GPIOB->CRL = 0X33333333; //PB0-7 上拉输出 GPIOB->CRH = 0X33333333; //PB8-15 上拉输出 GPIOB->ODR = 0XFFFF; //全部输出高 return t; } void lcd_write_reg(uint16_t addr, uint16_t LCD_RegValue) { lcd_write_cmd(addr); lcd_write_data(LCD_RegValue); } uint16_t lcd_read_reg(uint16_t addr) { lcd_write_cmd(addr); //写入要读的寄存器号 return lcd_read_data(); } void opt_delay(uint8_t i) { while (i--); } void lcd_displayON(void) { lcd_write_cmd(0X29); //开启显示 } void lcd_displayOFF(void) { lcd_write_cmd(0X28); } void lcd_setCursor(uint16_t Xpos, uint16_t Ypos) { lcd_write_cmd(lcddev.setxcmd); lcd_write_data(Xpos >> 8); lcd_write_data(Xpos & 0XFF); lcd_write_cmd(lcddev.setycmd); lcd_write_data(Ypos >> 8); lcd_write_data(Ypos & 0XFF); } void lcd_setScanDir(uint8_t dir) { uint16_t regval = 0; uint16_t dirreg = 0; uint16_t temp; switch (dir) //方向转换 { case 0: dir = 6; break; case 1: dir = 7; break; case 2: dir = 4; break; case 3: dir = 5; break; case 4: dir = 1; break; case 5: dir = 0; break; case 6: dir = 3; break; case 7: dir = 2; break; } switch (dir) { case L2R_U2D://从左到右,从上到下 regval |= (0 << 7) | (0 << 6) | (0 << 5); break; case L2R_D2U://从左到右,从下到上 regval |= (1 << 7) | (0 << 6) | (0 << 5); break; case R2L_U2D://从右到左,从上到下 regval |= (0 << 7) | (1 << 6) | (0 << 5); break; case R2L_D2U://从右到左,从下到上 regval |= (1 << 7) | (1 << 6) | (0 << 5); break; case U2D_L2R://从上到下,从左到右 regval |= (0 << 7) | (0 << 6) | (1 << 5); break; case U2D_R2L://从上到下,从右到左 regval |= (0 << 7) | (1 << 6) | (1 << 5); break; case D2U_L2R://从下到上,从左到右 regval |= (1 << 7) | (0 << 6) | (1 << 5); break; case D2U_R2L://从下到上,从右到左 regval |= (1 << 7) | (1 << 6) | (1 << 5); break; } dirreg = 0X36; regval |= 0X08; //5310/5510/1963不需要BGR lcd_write_reg(dirreg, regval); if (regval & 0X20) { if (lcddev.width < lcddev.height) //交换X,Y { temp = lcddev.width; lcddev.width = lcddev.height; lcddev.height = temp; } } else { if (lcddev.width > lcddev.height) //交换X,Y { temp = lcddev.width; lcddev.width = lcddev.height; lcddev.height = temp; } } lcd_write_cmd(lcddev.setxcmd); lcd_write_data(0); lcd_write_data(0); lcd_write_data((lcddev.width - 1) >> 8); lcd_write_data((lcddev.width - 1) & 0XFF); lcd_write_cmd(lcddev.setycmd); lcd_write_data(0); lcd_write_data(0); lcd_write_data((lcddev.height - 1) >> 8); lcd_write_data((lcddev.height - 1) & 0XFF); } void lcd_drawPoint(uint16_t x, uint16_t y, uint16_t color) { lcd_write_cmd(lcddev.setxcmd); lcd_write_data(x >> 8); lcd_write_data(x & 0XFF); lcd_write_cmd(lcddev.setycmd); lcd_write_data(y >> 8); lcd_write_data(y & 0XFF); LCD_RS = 0; LCD_CS = 0; LCD_DATA_OUT(lcddev.wramcmd);//写指令 LCD_WR = 0; LCD_WR = 1; LCD_CS = 1; lcd_write_data(color); //写数据 } //SSD1963 背光设置 //pwm:背光等级,0~100.越大越亮. void lcd_setBL(uint8_t pwm) { lcd_write_cmd(0xBE); //配置PWM输出 lcd_write_data(0x05); //1设置PWM频率 lcd_write_data(pwm * 2.55); //2设置PWM占空比 lcd_write_data(0x01); //3设置C lcd_write_data(0xFF); //4设置D lcd_write_data(0x00); //5设置E lcd_write_data(0x00); //6设置F } //设置LCD显示方向 //dir:0,竖屏;1,横屏 void lcd_setDisplayDir(uint8_t dir) { if (dir == 0) //竖屏 { lcddev.dir = 0; //竖屏 lcddev.width = 240; lcddev.height = 320; lcddev.wramcmd = 0X2C; lcddev.setxcmd = 0X2A; lcddev.setycmd = 0X2B; } else //横屏 { lcddev.dir = 1; //横屏 lcddev.width = 320; lcddev.height = 240; lcddev.wramcmd = 0X2C; lcddev.setxcmd = 0X2A; lcddev.setycmd = 0X2B; } lcd_setScanDir(DFT_SCAN_DIR); //默认扫描方向 } void lcd_gpio_init(void) { /* rt_pin_mode(LCD_BL_PIN, GPIO_MODE_OUTPUT_PP); rt_pin_mode(LCD_CS_PIN, GPIO_MODE_OUTPUT_PP); rt_pin_mode(LCD_RD_PIN, GPIO_MODE_OUTPUT_PP); rt_pin_mode(LCD_RS_PIN, GPIO_MODE_OUTPUT_PP); rt_pin_mode(LCD_WR_PIN, GPIO_MODE_OUTPUT_PP); rt_pin_write(LCD_BL_PIN, 1); rt_pin_write(LCD_CS_PIN, 1); rt_pin_write(LCD_RD_PIN, 1); rt_pin_write(LCD_RS_PIN, 1); rt_pin_write(LCD_WR_PIN, 1); */ GPIO_InitTypeDef GPIO_InitStructure; __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_SWJ_NOJTAG(); GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6 | GPIO_PIN_5 | GPIO_PIN_4; HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); //GPIOC GPIO_InitStructure.Pin = GPIO_PIN_2; //背光引脚PD2 HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); //GPIOD GPIO_InitStructure.Pin = GPIO_PIN_All; HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7 | GPIO_PIN_6 | GPIO_PIN_5 | GPIO_PIN_4, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_All, GPIO_PIN_SET); } void lcd_init(uint16_t color) { lcd_gpio_init(); rt_thread_mdelay(50); // delay 50 ms lcd_write_reg(0x0000, 0x0001); rt_thread_mdelay(50); // delay 50 ms lcddev.id = lcd_read_reg(0x0000); if (lcddev.id < 0XFF || lcddev.id == 0XFFFF || lcddev.id == 0X9300) //读到ID不正确,新增lcddev.id==0X9300判断,因为9341在未被复位的情况下会被读成9300 { //尝试9341 ID的读取 lcd_write_cmd(0XD3); lcd_read_data(); //dummy read lcd_read_data(); //读到0X00 lcddev.id = lcd_read_data(); //读取93 lcddev.id <<= 8; lcddev.id |= lcd_read_data(); //读取41 } printf("LCD ID:%x\r\n", lcddev.id); //打印LCD ID lcd_write_cmd(0xCF); lcd_write_dataX(0x00); lcd_write_dataX(0xC1); lcd_write_dataX(0X30); lcd_write_cmd(0xED); lcd_write_dataX(0x64); lcd_write_dataX(0x03); lcd_write_dataX(0X12); lcd_write_dataX(0X81); lcd_write_cmd(0xE8); lcd_write_dataX(0x85); lcd_write_dataX(0x10); lcd_write_dataX(0x7A); lcd_write_cmd(0xCB); lcd_write_dataX(0x39); lcd_write_dataX(0x2C); lcd_write_dataX(0x00); lcd_write_dataX(0x34); lcd_write_dataX(0x02); lcd_write_cmd(0xF7); lcd_write_dataX(0x20); lcd_write_cmd(0xEA); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_cmd(0xC0); //Power control lcd_write_dataX(0x1B); //VRH[5:0] lcd_write_cmd(0xC1); //Power control lcd_write_dataX(0x01); //SAP[2:0];BT[3:0] lcd_write_cmd(0xC5); //VCM control lcd_write_dataX(0x30); //3F lcd_write_dataX(0x30); //3C lcd_write_cmd(0xC7); //VCM control2 lcd_write_dataX(0XB7); lcd_write_cmd(0x36); // Memory Access Control lcd_write_dataX(0x48); lcd_write_cmd(0x3A); lcd_write_dataX(0x55); lcd_write_cmd(0xB1); lcd_write_dataX(0x00); lcd_write_dataX(0x1A); lcd_write_cmd(0xB6); // Display Function Control lcd_write_dataX(0x0A); lcd_write_dataX(0xA2); lcd_write_cmd(0xF2); // 3Gamma Function Disable lcd_write_dataX(0x00); lcd_write_cmd(0x26); //Gamma curve selected lcd_write_dataX(0x01); lcd_write_cmd(0xE0); //Set Gamma lcd_write_dataX(0x0F); lcd_write_dataX(0x2A); lcd_write_dataX(0x28); lcd_write_dataX(0x08); lcd_write_dataX(0x0E); lcd_write_dataX(0x08); lcd_write_dataX(0x54); lcd_write_dataX(0XA9); lcd_write_dataX(0x43); lcd_write_dataX(0x0A); lcd_write_dataX(0x0F); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_cmd(0XE1); //Set Gamma lcd_write_dataX(0x00); lcd_write_dataX(0x15); lcd_write_dataX(0x17); lcd_write_dataX(0x07); lcd_write_dataX(0x11); lcd_write_dataX(0x06); lcd_write_dataX(0x2B); lcd_write_dataX(0x56); lcd_write_dataX(0x3C); lcd_write_dataX(0x05); lcd_write_dataX(0x10); lcd_write_dataX(0x0F); lcd_write_dataX(0x3F); lcd_write_dataX(0x3F); lcd_write_dataX(0x0F); lcd_write_cmd(0x2B); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_dataX(0x01); lcd_write_dataX(0x3f); lcd_write_cmd(0x2A); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_dataX(0x00); lcd_write_dataX(0xef); lcd_write_cmd(0x11); //Exit Sleep rt_thread_mdelay(120); lcd_write_cmd(0x29); //display on lcd_setDisplayDir(1); //1为横屏 LCD_BK = 1; //点亮背光 lcd_clear(color); } void lcd_clear(uint16_t color) { uint32_t index = 0; uint32_t totalpoint = lcddev.width; totalpoint *= lcddev.height; //得到总点数 lcd_setCursor(0x00, 0x0000); //设置光标位置 lcd_write_cmd(lcddev.wramcmd); //开始写入GRAM for (index = 0; index < totalpoint; index++) lcd_write_data(color); } void lcd_fill(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color) { uint16_t i, j; uint16_t xlen = 0; xlen = ex - sx + 1; for (i = sy; i <= ey; i++) { lcd_setCursor(sx, i); //设置光标位置 lcd_write_cmd(lcddev.wramcmd); //开始写入GRAM for (j = 0; j < xlen; j++) lcd_write_data(color); //设置光标位置 } } ```
查看更多
3
个回答
默认排序
按发布时间排序
天空下着沙
2021-08-02
这家伙很懒,什么也没写!
下边是.h 文件,用的是野火mini开发版,STM32F103RCT6芯片,引脚已经对应上了,编译没有问题,可是屏幕一直不亮。 ```c #ifndef __DRV_9341_H__ #define __DRV_9341_H__ #include "sys.h" #include "drv_common.h" #include "rtthread.h" #include "rtdevice.h" typedef enum {FALSE = 0, TRUE = !FALSE} bool; typedef enum { DIR_X = 0, DIR_Y }direction; /** LCD_BK = PD2 LCD_CS = PC4 LCD_RS = PC7 LCD_WR = PC6 LCD_RD = PC5 LCD_DATA_INT = GPIOB->IDR LCD_DATA_OUT(x) GPIOB->ODR=x; **/ #define LCD_BK_PIN GET_PIN(D, 2) #define LCD_CS_PIN GET_PIN(C, 4) #define LCD_RS_PIN GET_PIN(C, 7) #define LCD_WR_PIN GET_PIN(C, 6) #define LCD_RD_PIN GET_PIN(C, 5) #define LCD_BK PDout(2) #define LCD_CS PCout(4) #define LCD_RS PCout(7) #define LCD_WR PCout(6) #define LCD_RD PCout(5) #define LCD_DATA_OUT(data) GPIOB->ODR=data #define LCD_DATA_IN GPIOB->IDR //LCD重要参数集 typedef struct { uint16_t width; //LCD 宽度 uint16_t height; //LCD 高度 uint16_t id; //LCD ID uint8_t dir; //横屏还是竖屏控制:0,竖屏;1,横屏。 uint16_t wramcmd; //开始写gram指令 uint16_t setxcmd; //设置x坐标指令 uint16_t setycmd; //设置y坐标指令 } _lcd_dev; //LCD参数 extern _lcd_dev lcddev; //管理LCD重要参数 //LCD的画笔颜色和背景色 extern uint16_t POINT_COLOR;//默认红色 extern uint16_t BACK_COLOR; //背景颜色.默认为白色 #define L2R_U2D 0 //从左到右,从上到下 #define L2R_D2U 1 //从左到右,从下到上 #define R2L_U2D 2 //从右到左,从上到下 #define R2L_D2U 3 //从右到左,从下到上 #define U2D_L2R 4 //从上到下,从左到右 #define U2D_R2L 5 //从上到下,从右到左 #define D2U_L2R 6 //从下到上,从左到右 #define D2U_R2L 7 //从下到上,从右到左 //#define DFT_SCAN_DIR L2R_U2D //屏幕旋转180度 #define DFT_SCAN_DIR R2L_D2U //画笔颜色 #define WHITE 0xFFFF #define BLACK 0x0000 #define BLUE 0x001F #define BRED 0XF81F #define GRED 0XFFE0 #define GBLUE 0X07FF #define RED 0xF800 #define MAGENTA 0xF81F #define GREEN 0x07E0 #define CYAN 0x7FFF #define YELLOW 0xFFE0 #define BROWN 0XBC40 //棕色 #define BRRED 0XFC07 //棕红色 #define GRAY 0X8430 //灰色 //GUI颜色 #define DARKBLUE 0X01CF //深蓝色 #define LIGHTBLUE 0X7D7C //浅蓝色 #define GRAYBLUE 0X5458 //灰蓝色 //以上三色为PANEL的颜色 #define LIGHTGREEN 0X841F //浅绿色 #define LGRAY 0XC618 //浅灰色(PANNEL),窗体背景色 #define LGRAYBLUE 0XA651 //浅灰蓝色(中间层颜色) #define LBBLUE 0X2B12 //浅棕蓝色(选择条目的反色) #define LCD_SIZE_X 240 #define LCD_SIZE_Y 320 void lcd_init(uint16_t color); //初始化 void lcd_clear(uint16_t color); //清屏 void lcd_setCursor(uint16_t Xpos, uint16_t Ypos); //设置光标 void lcd_fill(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color); //填充单色 void lcd_drawPoint(uint16_t x, uint16_t y, uint16_t color); void lcd_write_reg(uint16_t LCD_Reg, uint16_t LCD_RegValue); uint16_t lcd_read_reg(uint16_t LCD_Reg); void lcd_setScanDir(uint8_t dir); //设置屏扫描方向 void lcd_setDisplayDir(uint8_t dir); //设置屏幕显示方向 void lcd_setBL(uint8_t pwm); //写数据函数 #define lcd_write_data(data){\ LCD_RS = 1;\ LCD_CS = 0;\ LCD_DATA_OUT(data);\ LCD_WR = 0;\ LCD_WR = 1;\ LCD_CS = 1;\ } #endif ```
whj467467222
认证专家
2021-08-02
开源,分享,交流,共同进步
我猜 [ILI9488](https://gitee.com/mirrors/ART-Pi/blob/master/libraries/drivers/drv_spi_ili9488.c) 是差不多的,所以你参考一下这个试试看
StackYuan
认证专家
2021-08-03
这家伙很懒,什么也没写!
屏幕不亮有可能是背光问题,屏幕白屏或者灰屏才可能是驱动问题
撰写答案
登录
注册新账号
关注者
0
被浏览
1.1k
关于作者
天空下着沙
这家伙很懒,什么也没写!
提问
1
回答
1
被采纳
0
关注TA
发私信
相关问题
1
i9341 TFT 读取ID
2
正点原子探拓者stm32f407+ili9341 rt-gui 成功
3
STM32F103VE指南者使用ILI9341求助
4
RT-Thread Studio软件无法添加ili9341软件包,怎么解决?
5
package包添加不成功
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
机器人操作系统 (ROS2) 和 RT-Thread 通信
4
国产MCU移植系列教程汇总,欢迎查看!
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
mcxn947 dht11温湿度采集
2
【24嵌入式设计大赛】基于RT-Thread星火1号的AI广告终端设计
3
基于stm32f407 芯片创建 rt-thread工程——使用Arduino兼容层RTduino
4
星火1号:CAN通信初步
5
【24嵌入式设计大赛】基于RT-Thread火星一号智能终端采集系统
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
文件系统
DMA
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
ota在线升级
I2C_IIC
WIZnet_W5500
UART
flash
packages_软件包
cubemx
PWM
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
中断
flashDB
socket
Debug
GD32
编译报错
msh
keil_MDK
at_device
MicroPython
rt_mq_消息队列_msg_queue
ulog
C++_cpp
本月问答贡献
出出啊
1513
个答案
341
次被采纳
小小李sunny
1438
个答案
289
次被采纳
张世争
775
个答案
167
次被采纳
crystal266
546
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部