idf的spi还有问题
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
#include "lcd.h"
|
||||
|
||||
|
||||
static const char *TAG = "LCD";
|
||||
|
||||
/******************************************************************************
|
||||
函数说明:设置起始和结束地址
|
||||
入口数据:x1,x2 设置列的起始和结束地址
|
||||
y1,y2 设置行的起始和结束地址
|
||||
返回值: 无
|
||||
******************************************************************************/
|
||||
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
|
||||
{
|
||||
|
||||
#if USE_HORIZONTAL==0
|
||||
lcd_spi_reg(0x2a);//列地址设置
|
||||
lcd_spi_write8(x1);
|
||||
lcd_spi_write8(x2);
|
||||
lcd_spi_reg(0x2b);//行地址设置
|
||||
lcd_spi_write8(y1);
|
||||
lcd_spi_write8(y2);
|
||||
lcd_spi_reg(0x2c);//储存器写
|
||||
#elif USE_HORIZONTAL==1
|
||||
lcd_spi_reg(0x2a);//列地址设置
|
||||
lcd_spi_write8(x1);
|
||||
lcd_spi_write8(x2);
|
||||
lcd_spi_reg(0x2b);//行地址设置
|
||||
lcd_spi_write8(y1+80);
|
||||
lcd_spi_write8(y2+80);
|
||||
lcd_spi_reg(0x2c);//储存器写
|
||||
#elif USE_HORIZONTAL==2
|
||||
lcd_spi_reg(0x2a);//列地址设置
|
||||
lcd_spi_write8(x1);
|
||||
lcd_spi_write8(x2);
|
||||
lcd_spi_reg(0x2b);//行地址设置
|
||||
lcd_spi_write8(y1);
|
||||
lcd_spi_write8(y2);
|
||||
lcd_spi_reg(0x2c);//储存器写
|
||||
#elif USE_HORIZONTAL==3
|
||||
lcd_spi_reg(0x2a);//列地址设置
|
||||
lcd_spi_write8(x1+80);
|
||||
lcd_spi_write8(x2+80);
|
||||
lcd_spi_reg(0x2b);//行地址设置
|
||||
lcd_spi_write8(y1);
|
||||
lcd_spi_write8(y2);
|
||||
lcd_spi_reg(0x2c);//储存器写
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void lcd_init(){
|
||||
ESP_LOGI(TAG,"等待初始化LCD");
|
||||
vTaskDelay(25); //
|
||||
ESP_LOGI(TAG,"初始化LCD");
|
||||
lcd_spi_reg(0x36);
|
||||
if(USE_HORIZONTAL==0)lcd_spi_write8(0x00);
|
||||
else if(USE_HORIZONTAL==1)lcd_spi_write8(0xC0);
|
||||
else if(USE_HORIZONTAL==2)lcd_spi_write8(0x70);
|
||||
else lcd_spi_write8(0xA0);
|
||||
|
||||
lcd_spi_reg(0x3A);
|
||||
lcd_spi_write8(0x05);
|
||||
|
||||
lcd_spi_reg(0xB2);
|
||||
lcd_spi_write8(0x0C);
|
||||
lcd_spi_write8(0x0C);
|
||||
lcd_spi_write8(0x00);
|
||||
lcd_spi_write8(0x33);
|
||||
lcd_spi_write8(0x33);
|
||||
|
||||
lcd_spi_reg(0xB7);
|
||||
lcd_spi_write8(0x35);
|
||||
|
||||
lcd_spi_reg(0xBB);
|
||||
lcd_spi_write8(0x19);
|
||||
|
||||
lcd_spi_reg(0xC0);
|
||||
lcd_spi_write8(0x2C);
|
||||
|
||||
lcd_spi_reg(0xC2);
|
||||
lcd_spi_write8(0x01);
|
||||
|
||||
lcd_spi_reg(0xC3);
|
||||
lcd_spi_write8(0x12);
|
||||
|
||||
lcd_spi_reg(0xC4);
|
||||
lcd_spi_write8(0x20);
|
||||
|
||||
lcd_spi_reg(0xC6);
|
||||
lcd_spi_write8(0x0F);
|
||||
|
||||
lcd_spi_reg(0xD0);
|
||||
lcd_spi_write8(0xA4);
|
||||
lcd_spi_write8(0xA1);
|
||||
|
||||
lcd_spi_reg(0xE0);
|
||||
lcd_spi_write8(0xD0);
|
||||
lcd_spi_write8(0x04);
|
||||
lcd_spi_write8(0x0D);
|
||||
lcd_spi_write8(0x11);
|
||||
lcd_spi_write8(0x13);
|
||||
lcd_spi_write8(0x2B);
|
||||
lcd_spi_write8(0x3F);
|
||||
lcd_spi_write8(0x54);
|
||||
lcd_spi_write8(0x4C);
|
||||
lcd_spi_write8(0x18);
|
||||
lcd_spi_write8(0x0D);
|
||||
lcd_spi_write8(0x0B);
|
||||
lcd_spi_write8(0x1F);
|
||||
lcd_spi_write8(0x23);
|
||||
|
||||
lcd_spi_reg(0xE1);
|
||||
lcd_spi_write8(0xD0);
|
||||
lcd_spi_write8(0x04);
|
||||
lcd_spi_write8(0x0C);
|
||||
lcd_spi_write8(0x11);
|
||||
lcd_spi_write8(0x13);
|
||||
lcd_spi_write8(0x2C);
|
||||
lcd_spi_write8(0x3F);
|
||||
lcd_spi_write8(0x44);
|
||||
lcd_spi_write8(0x51);
|
||||
lcd_spi_write8(0x2F);
|
||||
lcd_spi_write8(0x1F);
|
||||
lcd_spi_write8(0x1F);
|
||||
lcd_spi_write8(0x20);
|
||||
lcd_spi_write8(0x23);
|
||||
|
||||
lcd_spi_reg(0x21);
|
||||
|
||||
lcd_spi_reg(0x11);
|
||||
vTaskDelay(12);
|
||||
|
||||
lcd_spi_reg(0x29);
|
||||
|
||||
ESP_LOGI(TAG,"初始化LCD完成");
|
||||
|
||||
}
|
||||
|
||||
|
||||
void LCD_Clear(uint16_t Color)
|
||||
{
|
||||
uint16_t i,j;
|
||||
LCD_Address_Set(0,0,LCD_WW-1,LCD_HH-1);
|
||||
for(i=0;i<LCD_WW;i++)
|
||||
{
|
||||
for (j=0;j<LCD_HH;j++)
|
||||
{
|
||||
lcd_spi_write16(Color);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef LCD_H
|
||||
#define LCD_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_log.h"
|
||||
#include "spi.h"
|
||||
|
||||
#define USE_HORIZONTAL 0 //设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
|
||||
#define LCD_WW 240
|
||||
#define LCD_HH 320
|
||||
|
||||
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);
|
||||
void lcd_init();
|
||||
|
||||
void LCD_Clear(uint16_t Color);
|
||||
|
||||
#endif
|
||||
+91
-11
@@ -1,15 +1,8 @@
|
||||
#include "spi.h"
|
||||
|
||||
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define VSPI_MISO -1 //接收引脚不使用,仅发送
|
||||
#define VSPI_MOSI 3
|
||||
#define VSPI_SCLK 2
|
||||
|
||||
#define LCD_CS 7
|
||||
#define LCD_DS 6
|
||||
|
||||
static const char *TAG = "SPI_2";
|
||||
|
||||
@@ -17,6 +10,39 @@ spi_device_handle_t lcd_spi;
|
||||
|
||||
void spi_init()
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "配置GPIO");
|
||||
|
||||
// 1. 配置GPIO
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = (1ULL << LCD_DS), // LCD_DS
|
||||
.mode = GPIO_MODE_OUTPUT, // 输出模式
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE, //
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE, //
|
||||
.intr_type = GPIO_INTR_DISABLE // 禁用中断
|
||||
};
|
||||
|
||||
// 2. 应用配置
|
||||
gpio_config(&io_conf);
|
||||
|
||||
// io_conf.pin_bit_mask=LCD_CS;
|
||||
// gpio_config(&io_conf);
|
||||
|
||||
// io_conf.pin_bit_mask=VSPI_SCLK;
|
||||
// gpio_config(&io_conf);
|
||||
|
||||
// io_conf.pin_bit_mask=VSPI_MOSI;
|
||||
// gpio_config(&io_conf);
|
||||
|
||||
// 3. 设置初始电平
|
||||
gpio_set_level(LCD_DS, 0); // 输出低电平
|
||||
// gpio_set_level(LCD_CS,0);
|
||||
// gpio_set_level(VSPI_SCLK,0);
|
||||
// gpio_set_level(VSPI_MOSI,0);
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "配置GPIO完成");
|
||||
|
||||
ESP_LOGI(TAG,"初始化SPI IO总线");
|
||||
esp_err_t ret;
|
||||
|
||||
@@ -31,7 +57,7 @@ void spi_init()
|
||||
};
|
||||
|
||||
// 2. 初始化 SPI 总线
|
||||
ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
|
||||
ret = spi_bus_initialize(SPI2_HOST, &buscfg,SPI_DMA_DISABLED );
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SPI2 IO总线初始化失败");
|
||||
return;
|
||||
@@ -43,11 +69,11 @@ void spi_init()
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.mode = 0, // SPI 模式 0
|
||||
.clock_speed_hz = 80 * 1000 * 1000, //
|
||||
.clock_speed_hz = 40 * 1000 * 1000, //
|
||||
.spics_io_num = LCD_CS,
|
||||
.queue_size = 7, // 队列深度
|
||||
.command_bits = 8, // 命令 8 位
|
||||
.address_bits = 8,
|
||||
.flags = 0, // 可选标志
|
||||
|
||||
};
|
||||
|
||||
ret = spi_bus_add_device(SPI2_HOST, &devcfg, &lcd_spi);
|
||||
@@ -56,7 +82,61 @@ void spi_init()
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_write8(uint8_t data) {
|
||||
spi_transaction_t trans = {
|
||||
.length = 8, // 数据位数
|
||||
.tx_buffer = &data, // 发送缓冲区
|
||||
.rx_buffer = NULL, // 不接收
|
||||
};
|
||||
|
||||
return spi_device_transmit(lcd_spi, &trans);
|
||||
}
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_write16(uint16_t data) {
|
||||
spi_transaction_t trans = {
|
||||
.length = 16, // 数据位数
|
||||
.tx_buffer = &data, // 发送缓冲区
|
||||
.rx_buffer = NULL, // 不接收
|
||||
};
|
||||
|
||||
return spi_device_transmit(lcd_spi, &trans);
|
||||
}
|
||||
|
||||
// SPI 写命令
|
||||
esp_err_t lcd_spi_reg(uint8_t data) {
|
||||
esp_err_t err;
|
||||
gpio_set_level(LCD_DS, 0);
|
||||
err=lcd_spi_write8(data);
|
||||
gpio_set_level(LCD_DS, 1);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "命令发送失败");
|
||||
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void lcd_ds_test()
|
||||
{
|
||||
esp_err_t err;
|
||||
static uint8_t this_flag=0;
|
||||
if(this_flag)
|
||||
{
|
||||
this_flag=0;
|
||||
err=gpio_set_level(LCD_DS, 0);
|
||||
}else{
|
||||
this_flag=1;
|
||||
err=gpio_set_level(LCD_DS, 1);
|
||||
}
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "GPIO失败");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,32 @@
|
||||
#ifndef SPI_H
|
||||
#define SPI_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#define VSPI_MISO -1 //接收引脚不使用,仅发送
|
||||
#define VSPI_MOSI 3
|
||||
#define VSPI_SCLK 2
|
||||
|
||||
#define LCD_CS 7
|
||||
#define LCD_DS 6
|
||||
|
||||
void spi_init();
|
||||
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_write8(uint8_t data);
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_write16(uint16_t data);
|
||||
|
||||
// SPI 写命令
|
||||
esp_err_t lcd_spi_reg(uint8_t data);
|
||||
|
||||
void lcd_ds_test();
|
||||
|
||||
#endif /* SPI_H */
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
#include "spi.h"
|
||||
|
||||
#include "lcd.h"
|
||||
|
||||
static const char *TAG = "SYS";
|
||||
|
||||
void app_main(void)
|
||||
@@ -50,9 +52,13 @@ void app_main(void)
|
||||
ESP_LOGI(TAG,"复位原因: %d\n", esp_reset_reason());
|
||||
|
||||
spi_init();
|
||||
lcd_init();
|
||||
|
||||
LCD_Clear(0x1234);
|
||||
while(1){
|
||||
|
||||
//LCD_Clear(rand());
|
||||
// lcd_ds_test();
|
||||
vTaskDelay(1); // 必须让出CPU
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user