|
请求帮助,本人用的MINI STM32 ,写的TFTlcd驱动,在执行lcd初始化时一直是灰屏/**
* @file : lcd.c
* @brief : TFT_LCD 显示驱动程序
* @par : none
* @note : none
*
* @History:
* <data> <version> <author> <describe>
* 2018/07/11 v0.1.0 tlx 实现TFT_LCD显示
*/
/** Includes ---------------------------------------------------------------------------------------------*/
#include "lcd.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
/** Define -----------------------------------------------------------------------------------------------*/
#define cs_high() GPIO_SetBits(GPIOC, GPIO_Pin_9)
#define cs_low() GPIO_ResetBits(GPIOC, GPIO_Pin_9)
#define rs_high() GPIO_SetBits(GPIOC, GPIO_Pin_8)
#define rs_low() GPIO_ResetBits(GPIOC, GPIO_Pin_8)
#define wr_high() GPIO_SetBits(GPIOC, GPIO_Pin_7)
#define wr_low() GPIO_ResetBits(GPIOC, GPIO_Pin_7)
#define rd_high() GPIO_SetBits(GPIOC, GPIO_Pin_6)
#define rd_low() GPIO_ResetBits(GPIOC, GPIO_Pin_6)
#define bl_on() GPIO_SetBits(GPIOC, GPIO_Pin_10)
#define bl_off() GPIO_ResetBits(GPIOC, GPIO_Pin_10)
#define READ_ID 0XD3 /**< 读ID指令 */
#define INC_DIR 0X36 /**< 连续读写时,控制 ILI9341 存储器的读写方向 */
#define SET_X 0X2A /**< 设置横坐标 */
#define SET_Y 0X2B /**< 设置纵坐标 */
#define WR_GRAM 0X2C /**< 向GRAM里写入颜色数据 */
#define RD_GRAM 0X2E /**< 从GRAM里读出颜色数据 */
static uint16_t lcd_read_data(void);
static void lcd_write_cmd(uint8_t cmd);
static void lcd_write_data(uint16_t data);
static void lcd_set_cursor(uint16_t x, uint16_t y);
static void lcd_set_color(uint16_t color);
static void lcd_set_y(uint16_t ystart, uint16_t yend);
static void lcd_set_x(uint16_t xstart, uint16_t xend);
static void lcd_set_dir(uint8_t value);
/** Application Function -------------------------------------------------------------------------------------*/
/**
* @func : lcd_init()
* @brief : lcd初始化函数
* @par : none
* @note : none
* @return: none
*/
void lcd_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//使能 PORTB,C 时钟以及 AFIO 时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //开启 SWD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_9 | GPIO_Pin_8 | GPIO_Pin_7 | GPIO_Pin_6; // //PORTC6~10 复用推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //GPIOC
GPIO_SetBits(GPIOC,GPIO_Pin_10 | GPIO_Pin_9 | GPIO_Pin_8 | GPIO_Pin_7 | GPIO_Pin_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; // PORTB 推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOB
GPIO_SetBits(GPIOB, GPIO_Pin_All);
delay_ms(50);
lcd_write_cmd(0xCF);
lcd_write_data(0x00);
lcd_write_data(0xC1);
lcd_write_data(0X30);
lcd_write_cmd(0xED);
lcd_write_data(0x64);
lcd_write_data(0x03);
lcd_write_data(0X12);
lcd_write_data(0X81);
lcd_write_cmd(0xE8);
lcd_write_data(0x85);
lcd_write_data(0x10);
lcd_write_data(0x7A);
lcd_write_cmd(0xCB);
lcd_write_data(0x39);
lcd_write_data(0x2C);
lcd_write_data(0x00);
lcd_write_data(0x34);
lcd_write_data(0x02);
lcd_write_cmd(0xF7);
lcd_write_data(0x20);
lcd_write_cmd(0xEA);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_cmd(0xC0); //Power control
lcd_write_data(0x1B); //VRH[5:0]
lcd_write_cmd(0xC1); //Power control
lcd_write_data(0x01); //SAP[2:0];BT[3:0]
lcd_write_cmd(0xC5); //VCM control
lcd_write_data(0x30); //3F
lcd_write_data(0x30); //3C
lcd_write_cmd(0xC7); //VCM control2
lcd_write_data(0XB7);
lcd_write_cmd(0x36); // Memory Access Control
lcd_write_data(0x48);
lcd_write_cmd(0x3A);
lcd_write_data(0x55);
lcd_write_cmd(0xB1);
lcd_write_data(0x00);
lcd_write_data(0x1A);
lcd_write_cmd(0xB6); // Display Function Control
lcd_write_data(0x0A);
lcd_write_data(0xA2);
lcd_write_cmd(0xF2); // 3Gamma Function Disable
lcd_write_data(0x00);
lcd_write_cmd(0x26); //Gamma curve selected
lcd_write_data(0x01);
lcd_write_cmd(0xE0); //Set Gamma
lcd_write_data(0x0F);
lcd_write_data(0x2A);
lcd_write_data(0x28);
lcd_write_data(0x08);
lcd_write_data(0x0E);
lcd_write_data(0x08);
lcd_write_data(0x54);
lcd_write_data(0XA9);
lcd_write_data(0x43);
lcd_write_data(0x0A);
lcd_write_data(0x0F);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_cmd(0XE1); //Set Gamma
lcd_write_data(0x00);
lcd_write_data(0x15);
lcd_write_data(0x17);
lcd_write_data(0x07);
lcd_write_data(0x06);
lcd_write_data(0x2B);
lcd_write_data(0x56);
lcd_write_data(0x3C);
lcd_write_data(0x05);
lcd_write_data(0x10);
lcd_write_data(0x0F);
lcd_write_data(0x3F);
lcd_write_data(0x3F);
lcd_write_data(0x0F);
lcd_write_cmd(0x2B);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_data(0x01);
lcd_write_data(0x3f);
lcd_write_cmd(0x2A);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_data(0x00);
lcd_write_data(0xef);
lcd_write_cmd(0x11); //Exit Sleep
delay_ms(120);
lcd_write_cmd(0x29); //display on
delay_ms(50);
lcd_write_cmd(0x2c);
bl_on();
lcd_clear(WHITE);
// lcd_draw_window(0, 100, 0, 100, YELLOW);
}
/**
* @func : lcd_write_reg()
* @brief : lcd写寄存器函数
* @par : none
* @note : none
* @return: none
*/
void lcd_write_reg(uint8_t cmd, uint16_t *para, uint16_t size)
{
lcd_write_cmd(cmd);
for(int i = 0; i < size; i ++)
{
lcd_write_data(*para);
para = para +1;
}
}
/**
* @func : lcd_read_reg()
* @brief : lcd读寄存器函数
* @par : none
* @note : none
* @return: none
*/
void lcd_read_reg(uint8_t cmd, uint16_t *data, uint16_t size)
{
lcd_write_cmd(cmd);
GPIOB->CRL = 0X88888888; //PB0-7 上拉输入
GPIOB->CRH = 0X88888888; //PB8-15 上拉输入
GPIOB->ODR = 0XFFFF; //全部输出高
for(int i = 0; i < size; i ++)
{
*data = lcd_read_data();
data = data +1;
}
GPIOB->CRL = 0X33333333; //PB0-7 上拉输出
GPIOB->CRH = 0X33333333; //PB8-15 上拉输出
GPIOB->ODR = 0XFFFF; //全部输出高
}
/**
* @func : lcd_draw_point()
* @brief : lcd画点函数
* @par : none
* @note : none
* @return: none
*/
void lcd_draw_point(uint16_t x, uint16_t y, uint16_t color)
{
lcd_set_cursor(x, y);
lcd_set_color(color);
}
/**
* @func : lcd_draw_point()
* @brief : lcd画点函数
* @par : none
* @note : none
* @return: none
*/
void lcd_draw_window(uint16_t xstart, uint16_t xend, uint16_t ystart, uint16_t yend, uint16_t bkcolor)
{
uint16_t n, i;
n = (xend - xstart + 1) * (yend - ystart + 1);
lcd_set_x(xstart, xend);
lcd_set_y(ystart, yend);
for(i = 0; i < n; i ++)
{
lcd_set_color(bkcolor);
}
}
/**
* @func : lcd_draw_point()
* @brief : lcd画点函数
* @par : none
* @note : none
* @return: none
*/
void lcd_clear(uint16_t color)
{
uint32_t i;
lcd_set_cursor(0x00, 0x00);
lcd_write_cmd(WR_GRAM);
for(i = 0; i < (240 * 320); i ++)
{
lcd_write_data(color);
}
}
/** Private Function -------------------------------------------------------------------------------------*/
/**
* @func : lcd_write_data()
* @brief : lcd写参数函数
* @par : none
* @note : none
* @return: none
*/
static void lcd_write_data(uint16_t data)
{
rd_high();
rs_high();
cs_low();
GPIOB->ODR = data;
wr_low();
wr_high();
cs_high();
}
/**
* @func : lcd_write_cmd()
* @brief : lcd写命令函数
* @par : none
* @note : none
* @return: none
*/
static void lcd_write_cmd(uint8_t cmd)
{
rd_high();
rs_low();
cs_low();
GPIOB->ODR = cmd;
wr_low();
wr_high();
cs_high();
}
/**
* @func : lcd_read_data()
* @brief : lcd读数据函数
* @par : none
* @note : none
* @return: none
*/
static uint16_t lcd_read_data(void)
{
uint16_t rxbuf;
wr_high();
rs_high();
cs_low();
rd_low();
rxbuf = GPIOB->IDR;
rd_high();
cs_high();
return rxbuf;
}
/**
* @func : lcd_set_dir()
* @brief : lcd扫描方向设置函数
* @par : value中高3位决定自增方向,其他位暂为0
* @note : none
* @return: none
*/
static void lcd_set_dir(uint8_t value)
{
lcd_write_cmd(INC_DIR);
lcd_write_data(value);
}
/**
* @func : lcd_set_x()
* @brief : lcd写横坐标函数
* @par : 4个字节数据组成两个横坐标(起始坐标和结束坐标)
* @note : 当画点时xtart等于xend
* @return: none
*/
static void lcd_set_x(uint16_t xstart, uint16_t xend)
{
uint16_t xbuffer[4];
xbuffer[0] = (uint16_t)((xstart & 0xff00) >> 8);
xbuffer[1] = (uint16_t)(xstart & 0x00ff);
xbuffer[2] = (uint16_t)((xend & 0xff00) >> 8);
xbuffer[3] = (uint16_t)(xend & 0x00ff);
lcd_write_reg(SET_X, xbuffer, 4);
}
/**
* @func : lcd_set_y()
* @brief : lcd写纵坐标函数
* @par : 4个字节数据组成两个纵坐标(起始坐标和结束坐标)
* @note : 当画点时ytart等于yend
* @return: none
*/
static void lcd_set_y(uint16_t ystart, uint16_t yend)
{
uint16_t ybuffer[4];
ybuffer[0] = (uint16_t)((ystart & 0xff00) >> 8);
ybuffer[1] = (uint16_t)(ystart & 0x00ff);
ybuffer[2] = (uint16_t)((yend & 0xff00) >> 8);
ybuffer[3] = (uint16_t)(yend & 0x00ff);
lcd_write_reg(SET_Y, ybuffer, 4);
}
/**
* @func : lcd_set_color()
* @brief : lcd设置颜色函数
* @par : 4个字节数据组成两个纵坐标(起始坐标和结束坐标)
* @note : none
* @return: none
*/
static void lcd_set_color(uint16_t color)
{
lcd_write_cmd(WR_GRAM);
lcd_write_data(color);
}
/**
* @func : lcd_set_cursor()
* @brief : lcd设置颜色函数
* @par : 4个字节数据组成两个纵坐标(起始坐标和结束坐标)
* @note : none
* @return: none
*/
static void lcd_set_cursor(uint16_t x, uint16_t y)
{
lcd_set_x(x, x);
lcd_set_y(y, y);
}
|
|