1.背景说明
最近在搞一个DTU 的小项目,使用的是中移动的ML302,关于ML302的相关资料,在中移动的官网(http://iot.10086.cn 、http://onemo10086.com)上就有很多教程,而且配套的openCPU SDK里面也有相关的demo示例,一般应付大部分的开发都没有问题了
在我开发的时候,被安排配置一款LCD的屏,我查了一下openCPU里有自带的lcd接口,按照一般的剧情发展,硬件参考硬件设计,软件参考lcd接口开发,十拿九稳的事情,有什么好写的。但是这次硬件剑走偏锋,没有接在lcd的硬件引脚上,而且普通的GPIO上,GPIO需要模拟SPI进行与屏的通信,进而点亮屏幕。
第一次这样开发操作,收获颇多,必须记录一下。当时没有拍照,实物就是如图我点亮时候的拍的
2.SPI简介
SPI(Serial Peripheral Interface)是串行外围接口,是Motorola 首先在MC68HCXX 系列处理器上定义的,是一种高速、全双工、同步的通信总线,主要应用在EEPROM、FLASH、实时时钟、AD 转换器,还有数字信号处理器和数字信号解码器之间
SPI总线是一种4线总线,通常由一个主模块和一个或者多个从模块组成,主模块选择一个从模块进行同步通信,从而完成数据的交互。其是一个环形结构,以主从方式工作,通信时需要至少4根线
-
MISO:主设备输入/从设备输出 主机从这条信号线读入数据,从机的数据由这条信号线输出到主机,即在这条线上数据的方向为:从机到主机 -
MOSI:主设备输出/从设备输入 主机的数据从这条信号线输出,从机由这条信号线读入主机发送的数据,即这条线上数据的方向为:主机到从机 -
SCLK:时钟信号线 它由主设备控制,用于通信数据同步,其决定了通信的速率,不同的设备支持的最高时钟频率不一样,两个设备之间通讯时,通讯速率受限于低速设备 -
CS:从设备选择信号线 常称为片选信号线,由主设备控制。当主机要选择从设备时,把该从设备的CS 信号线设置为低电平,该从设备即被选中,即片选有效,接着主机开始与被选中的从设备进行SPI 通信
当有多个SPI 从设备与SPI 主机相连时,采用“三加一”的连接方式,如下: (1) 设备的其它信号线SCLK、MOSI及MISO 同时并联到相同的SPI 总线上,即无论有多少个从设备,都共同只使用这3 条总线; (2) 每个从设备都有独立的这一条CS 信号线,CS信号线独占主机的一个引脚,即有多少个从设备,就有多少条CS信号线。 综上可知,SPI 通信以CS 线置低电平为开始信号,以CS 线被拉高作为结束信号。
主机与从机连接方式如下图所示:
以Cat.1模组 ML302为例,SPI主设备与从机连接示意图如下: (上述简介引用中移动官方SPI接口介绍)
3.配置流程
3.1 查阅屏资料
配置屏幕,当然是先把屏的资料看一遍先,首先重点关注一下屏的相关引脚与引脚的定义。如下图所示
3.2 查看项目硬件原理图
查看硬件原理图一般先从屏接口座子开始看,一直看到ML302主控上面,看看接的是什么脚
3.3 ML302引脚资料查看
然后查看官方给的ML302资料,看看相关硬件接的引脚在openCPU里面对应的分别为多少,以便于后面研发编写代码
4.代码的编写
4.1 查看与熟悉GPIO接口
4.2 GPIO模拟SPI编写
定义相关宏
#define LcmXPixel 128
#define LcmYPixel 64
#define SPI_SCK_PIN CM_GPIO_3
#define SPI_MOSI_PIN CM_GPIO_2
#define SPI_A0_PIN CM_GPIO_1
#define SPI_CS_PIN CM_GPIO_0
#define LCD_RST CM_GPIO_11
#define SCK_H cm_gpio_write(SPI_SCK_PIN,CM_GPIO_HIGH);
#define SCK_L cm_gpio_write(SPI_SCK_PIN,CM_GPIO_LOW);
#define MOSI_H cm_gpio_write(SPI_MOSI_PIN,CM_GPIO_HIGH);
#define MOSI_L cm_gpio_write(SPI_MOSI_PIN,CM_GPIO_LOW);
#define A0_H cm_gpio_write(SPI_A0_PIN,CM_GPIO_HIGH);
#define A0_L cm_gpio_write(SPI_A0_PIN,CM_GPIO_LOW);
#define CS_H cm_gpio_write(SPI_CS_PIN,CM_GPIO_HIGH);
#define CS_L cm_gpio_write(SPI_CS_PIN,CM_GPIO_LOW);
#define RST_H cm_gpio_write(LCD_RST,CM_GPIO_HIGH);
#define RST_L cm_gpio_write(LCD_RST,CM_GPIO_LOW);
初始化相关GPIO口
cm_gpio_cfg_t cfg_sck,cfg_si,cfg_ao,cfg_cs,cfg_rst;
static void lcd_hal_init(void)
{
cfg_sck.id=SPI_SCK_PIN,cfg_sck.dir=CM_GPIO_OUT,cfg_sck.irq_enable = 0;
cfg_si.id=SPI_MOSI_PIN,cfg_si.dir=CM_GPIO_OUT,cfg_si.irq_enable = 0;
cfg_ao.id=SPI_A0_PIN,cfg_ao.dir=CM_GPIO_OUT,cfg_ao.irq_enable = 0;
cfg_cs.id=SPI_CS_PIN,cfg_cs.dir=CM_GPIO_OUT,cfg_cs.irq_enable = 0;
cfg_rst.id=LCD_RST,cfg_rst.dir=CM_GPIO_OUT,cfg_rst.irq_enable = 0;
cm_gpio_init(&cfg_sck);
cm_gpio_init(&cfg_si);
cm_gpio_init(&cfg_ao);
cm_gpio_init(&cfg_cs);
cm_gpio_init(&cfg_rst);
CS_H
}
LCD复位
static void lcd_reset(void)
{
RST_H
osDelay(1000);
RST_L
osDelay(1000);
}
LCD写指令
void lcd_write_com(uint8_t com)
{
uint8_t i;
SCK_H
A0_L
CS_L
for( i = 0; i < 8; i++ )
{
SCK_H
SCK_L
if( com & 0x80 )
{
MOSI_H;
}
else
{
MOSI_L;
}
SCK_L
SCK_H
com <<= 1;
}
CS_H
SCK_H
}
LCD写数据
void lcd_write_data(uint8_t data)
{
uint8_t i;
SCK_H
A0_H
CS_L
for( i = 0; i < 8; i++ )
{
SCK_H
SCK_L
if( data & 0x80 )
{
MOSI_H;
}
else
{
MOSI_L;
}
SCK_L
SCK_H
data <<= 1;
}
CS_H
SCK_H
}
5. demo code
最后附上全部的点屏代码,供学习参考,以上如有不对的地方,麻烦请指正了
LCD.c
#include "lcd.h"
#include "cm/cm_gpio.h"
#define LcmXPixel 128
#define LcmYPixel 64
#define SPI_SCK_PIN CM_GPIO_3
#define SPI_MOSI_PIN CM_GPIO_2
#define SPI_A0_PIN CM_GPIO_1
#define SPI_CS_PIN CM_GPIO_0
#define LCD_RST CM_GPIO_11
#define SCK_H cm_gpio_write(SPI_SCK_PIN,CM_GPIO_HIGH);
#define SCK_L cm_gpio_write(SPI_SCK_PIN,CM_GPIO_LOW);
#define MOSI_H cm_gpio_write(SPI_MOSI_PIN,CM_GPIO_HIGH);
#define MOSI_L cm_gpio_write(SPI_MOSI_PIN,CM_GPIO_LOW);
#define A0_H cm_gpio_write(SPI_A0_PIN,CM_GPIO_HIGH);
#define A0_L cm_gpio_write(SPI_A0_PIN,CM_GPIO_LOW);
#define CS_H cm_gpio_write(SPI_CS_PIN,CM_GPIO_HIGH);
#define CS_L cm_gpio_write(SPI_CS_PIN,CM_GPIO_LOW);
#define RST_H cm_gpio_write(LCD_RST,CM_GPIO_HIGH);
#define RST_L cm_gpio_write(LCD_RST,CM_GPIO_LOW);
cm_gpio_cfg_t cfg_sck,cfg_si,cfg_ao,cfg_cs,cfg_rst;
unsigned char ASCIIchardot[16*96] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0E,0x1F,0x1F,0x0E,0x00,0x00,0x00,0x00,0x00,0xB0,0xB0,0x00,0x00,0x00,
0x00,0x1C,0x1C,0x00,0x00,0x1C,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x04,0x1F,0x1F,0x04,0x1F,0x1F,0x04,0x00,0x40,0xF0,0xF0,0x40,0xF0,0xF0,0x40,
0x00,0x0C,0x1E,0x73,0x71,0x18,0x08,0x00,0x00,0x20,0x30,0x1C,0x9C,0xF0,0x60,0x00,
0x18,0x3C,0x24,0x3D,0x1B,0x06,0x0C,0x00,0x00,0x60,0xC0,0xB0,0x78,0x48,0x78,0x30,
0x00,0x0D,0x1F,0x12,0x1E,0x0C,0x00,0x00,0x00,0xE0,0xF0,0x10,0x90,0xE0,0xF0,0x90,
0x00,0x00,0x00,0x1C,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x03,0x0F,0x1C,0x10,0x00,0x00,0x00,0x00,0xE0,0xF8,0x1C,0x04,0x00,0x00,
0x00,0x00,0x10,0x1C,0x0F,0x03,0x00,0x00,0x00,0x00,0x04,0x1C,0xF8,0xE0,0x00,0x00,
0x00,0x01,0x05,0x07,0x03,0x07,0x05,0x01,0x00,0x00,0x40,0xC0,0x80,0xC0,0x40,0x00,
0x00,0x01,0x01,0x07,0x07,0x01,0x01,0x00,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x3C,0x38,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,0x00,0x00,
0x00,0x00,0x00,0x01,0x07,0x1E,0x18,0x00,0x00,0x18,0x78,0xE0,0x80,0x00,0x00,0x00,
0x00,0x00,0x0F,0x1F,0x10,0x16,0x1F,0x0F,0x00,0x00,0xE0,0xF0,0xD0,0x10,0xF0,0xE0,
0x00,0x04,0x04,0x0C,0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,
0x00,0x0C,0x1C,0x10,0x11,0x1F,0x0E,0x00,0x00,0x30,0x70,0xD0,0x90,0x10,0x10,0x00,
0x00,0x0C,0x1C,0x11,0x11,0x1F,0x0E,0x00,0x00,0x60,0x70,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x00,0x1F,0x1F,0x00,0x07,0x07,0x00,0x00,0xC0,0xC0,0x40,0x40,0xF0,0xF0,0x40,
0x00,0x1F,0x1F,0x11,0x11,0x11,0x10,0x00,0x00,0x10,0x10,0x10,0x30,0xE0,0xC0,0x00,
0x00,0x03,0x07,0x1E,0x1A,0x13,0x01,0x00,0x00,0xE0,0xF0,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x10,0x10,0x11,0x17,0x1E,0x18,0x00,0x00,0x00,0x70,0xF0,0x80,0x00,0x00,0x00,
0x00,0x0E,0x1F,0x13,0x11,0x1F,0x0E,0x00,0x00,0xE0,0xF0,0x10,0x90,0xF0,0xE0,0x00,
0x00,0x0F,0x1F,0x10,0x10,0x1F,0x0F,0x00,0x00,0x00,0x90,0xB0,0xF0,0xC0,0x80,0x00,
0x00,0x00,0x00,0x06,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,0x00,0x00,
0x00,0x00,0x00,0x06,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x34,0x3C,0x38,0x00,0x00,
0x00,0x01,0x03,0x06,0x0C,0x18,0x10,0x00,0x00,0x00,0x80,0xC0,0x60,0x30,0x10,0x00,
0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x10,0x18,0x0C,0x06,0x03,0x01,0x00,0x00,0x10,0x30,0x60,0xC0,0x80,0x00,0x00,
0x00,0x0C,0x1C,0x11,0x13,0x1E,0x0C,0x00,0x00,0x00,0x00,0xB0,0xB0,0x00,0x00,0x00,
0x0F,0x1F,0x10,0x11,0x13,0x12,0x1F,0x0F,0xE0,0xF0,0x10,0x90,0xD0,0x50,0xD0,0xD0,
0x00,0x07,0x0F,0x18,0x18,0x0F,0x07,0x00,0x00,0xF0,0xF0,0x80,0x80,0xF0,0xF0,0x00,
0x00,0x1F,0x1F,0x11,0x11,0x1F,0x0E,0x00,0x00,0xF0,0xF0,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x0F,0x1F,0x10,0x10,0x1C,0x0C,0x00,0x00,0xE0,0xF0,0x10,0x10,0x70,0x60,0x00,
0x00,0x1F,0x1F,0x10,0x18,0x0F,0x07,0x00,0x00,0xF0,0xF0,0x10,0x30,0xE0,0xC0,0x00,
0x00,0x1F,0x1F,0x11,0x11,0x11,0x10,0x00,0x00,0xF0,0xF0,0x10,0x10,0x10,0x10,0x00,
0x00,0x1F,0x1F,0x11,0x11,0x11,0x10,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x1F,0x10,0x10,0x1C,0x0C,0x00,0x00,0xE0,0xF0,0x10,0x90,0xF0,0xF0,0x00,
0x00,0x1F,0x1F,0x01,0x01,0x1F,0x1F,0x00,0x00,0xF0,0xF0,0x00,0x00,0xF0,0xF0,0x00,
0x00,0x00,0x10,0x1F,0x1F,0x10,0x00,0x00,0x00,0x00,0x10,0xF0,0xF0,0x10,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0x1F,0x00,0x00,0x60,0x70,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x1F,0x1F,0x01,0x07,0x1E,0x18,0x00,0x00,0xF0,0xF0,0x00,0xC0,0xF0,0x30,0x00,
0x00,0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0x10,0x10,0x10,0x10,0x00,
0x00,0x1F,0x1F,0x04,0x03,0x04,0x1F,0x1F,0x00,0xF0,0xF0,0x00,0x80,0x00,0xF0,0xF0,
0x00,0x1F,0x1F,0x06,0x03,0x01,0x1F,0x1F,0x00,0xF0,0xF0,0x00,0x00,0x80,0xF0,0xF0,
0x00,0x0F,0x1F,0x10,0x10,0x1F,0x0F,0x00,0x00,0xE0,0xF0,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x1F,0x1F,0x11,0x11,0x1F,0x0E,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x1F,0x10,0x10,0x1F,0x0F,0x00,0x00,0xE0,0xF0,0x10,0x18,0xFC,0xE4,0x00,
0x00,0x1F,0x1F,0x11,0x11,0x1F,0x0E,0x00,0x00,0xF0,0xF0,0x00,0x80,0xF0,0x70,0x00,
0x00,0x0C,0x1E,0x13,0x11,0x18,0x08,0x00,0x00,0x20,0x30,0x10,0x90,0xF0,0x60,0x00,
0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,
0x00,0x1F,0x1F,0x00,0x00,0x1F,0x1F,0x00,0x00,0xE0,0xF0,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x1F,0x1F,0x00,0x00,0x1F,0x1F,0x00,0x00,0xC0,0xE0,0x30,0x30,0xE0,0xC0,0x00,
0x00,0x1F,0x1F,0x00,0x03,0x00,0x1F,0x1F,0x00,0x80,0xF0,0x70,0x80,0x70,0xF0,0x80,
0x00,0x18,0x1C,0x07,0x03,0x1C,0x18,0x00,0x00,0x70,0xF0,0x00,0x80,0xF0,0x70,0x00,
0x00,0x1E,0x1F,0x01,0x01,0x1F,0x1E,0x00,0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,
0x00,0x10,0x10,0x11,0x13,0x1E,0x1C,0x00,0x00,0x70,0xF0,0x90,0x10,0x10,0x10,0x00,
0x00,0x00,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,0xFE,0xFE,0x02,0x02,0x00,0x00,
0x00,0x18,0x1E,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0x78,0x18,0x00,
0x00,0x00,0x10,0x10,0x1F,0x1F,0x00,0x00,0x00,0x00,0x02,0x02,0xFE,0xFE,0x00,0x00,
0x00,0x10,0x30,0x60,0x60,0x30,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x00,0x00,0x40,0x60,0x70,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x04,0x04,0x04,0x07,0x03,0x00,0x00,0x60,0xF0,0x90,0x90,0xF0,0xF0,0x00,
0x00,0x1F,0x1F,0x04,0x04,0x07,0x03,0x00,0x00,0xF0,0xF0,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x03,0x07,0x04,0x04,0x06,0x02,0x00,0x00,0xE0,0xF0,0x10,0x10,0x30,0x20,0x00,
0x00,0x03,0x07,0x04,0x04,0x1F,0x1F,0x00,0x00,0xE0,0xF0,0x10,0x10,0xF0,0xF0,0x00,
0x00,0x03,0x07,0x04,0x04,0x07,0x03,0x00,0x00,0xE0,0xF0,0x90,0x90,0x90,0x80,0x00,
0x00,0x01,0x0F,0x1F,0x11,0x11,0x11,0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,
0x00,0x03,0x07,0x04,0x04,0x07,0x07,0x00,0x00,0xE2,0xF2,0x12,0x12,0xFE,0xFC,0x00,
0x00,0x1F,0x1F,0x04,0x04,0x07,0x03,0x00,0x00,0xF0,0xF0,0x00,0x00,0xF0,0xF0,0x00,
0x00,0x04,0x04,0x37,0x37,0x00,0x00,0x00,0x00,0x10,0x10,0xF0,0xF0,0x10,0x10,0x00,
0x00,0x00,0x04,0x04,0x37,0x37,0x00,0x00,0x00,0x02,0x02,0x02,0xFE,0xFC,0x00,0x00,
0x00,0x1F,0x1F,0x00,0x01,0x07,0x06,0x00,0x00,0xF0,0xF0,0x80,0xC0,0x70,0x30,0x00,
0x00,0x10,0x10,0x1F,0x1F,0x00,0x00,0x00,0x00,0x10,0x10,0xF0,0xF0,0x10,0x10,0x00,
0x00,0x07,0x07,0x04,0x07,0x04,0x07,0x03,0x00,0xF0,0xF0,0x00,0xE0,0x00,0xF0,0xF0,
0x00,0x07,0x07,0x04,0x04,0x07,0x03,0x00,0x00,0xF0,0xF0,0x00,0x00,0xF0,0xF0,0x00,
0x00,0x03,0x07,0x04,0x04,0x07,0x03,0x00,0x00,0xE0,0xF0,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x07,0x07,0x04,0x04,0x07,0x03,0x00,0x00,0xFE,0xFE,0x10,0x10,0xF0,0xE0,0x00,
0x00,0x03,0x07,0x04,0x04,0x07,0x07,0x00,0x00,0xE0,0xF0,0x10,0x10,0xFE,0xFE,0x00,
0x00,0x07,0x07,0x01,0x02,0x06,0x06,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0x07,0x04,0x04,0x04,0x04,0x00,0x00,0x10,0x90,0x90,0x90,0xF0,0x60,0x00,
0x00,0x04,0x1F,0x1F,0x04,0x04,0x04,0x00,0x00,0x00,0xE0,0xF0,0x10,0x10,0x10,0x00,
0x00,0x07,0x07,0x00,0x00,0x07,0x07,0x00,0x00,0xE0,0xF0,0x10,0x10,0xF0,0xF0,0x00,
0x00,0x07,0x07,0x00,0x00,0x07,0x07,0x00,0x00,0xC0,0xE0,0x30,0x30,0xE0,0xC0,0x00,
0x00,0x07,0x07,0x00,0x03,0x00,0x07,0x07,0x00,0xC0,0xF0,0x30,0xC0,0x30,0xF0,0xC0,
0x00,0x06,0x07,0x01,0x01,0x07,0x06,0x00,0x00,0x30,0x70,0xC0,0xC0,0x70,0x30,0x00,
0x00,0x07,0x07,0x00,0x00,0x07,0x07,0x00,0x02,0xE2,0xF2,0x16,0x1C,0xF8,0xE0,0x00,
0x00,0x04,0x04,0x04,0x05,0x07,0x06,0x00,0x00,0x30,0x70,0xD0,0x90,0x10,0x10,0x00,
0x00,0x00,0x01,0x0F,0x1E,0x10,0x00,0x00,0x00,0x80,0xC0,0x78,0x3C,0x04,0x00,0x00,
0x00,0x00,0x00,0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0x00,0x00,0x00,
0x00,0x00,0x10,0x1E,0x0F,0x01,0x00,0x00,0x00,0x00,0x04,0x3C,0x78,0xC0,0x80,0x00,
0x0C,0x18,0x10,0x18,0x0C,0x04,0x0C,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x00,0x00,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0x00,
};
static void lcd_hal_init(void)
{
cfg_sck.id=SPI_SCK_PIN,cfg_sck.dir=CM_GPIO_OUT,cfg_sck.irq_enable = 0;
cfg_si.id=SPI_MOSI_PIN,cfg_si.dir=CM_GPIO_OUT,cfg_si.irq_enable = 0;
cfg_ao.id=SPI_A0_PIN,cfg_ao.dir=CM_GPIO_OUT,cfg_ao.irq_enable = 0;
cfg_cs.id=SPI_CS_PIN,cfg_cs.dir=CM_GPIO_OUT,cfg_cs.irq_enable = 0;
cfg_rst.id=LCD_RST,cfg_rst.dir=CM_GPIO_OUT,cfg_rst.irq_enable = 0;
cm_gpio_init(&cfg_sck);
cm_gpio_init(&cfg_si);
cm_gpio_init(&cfg_ao);
cm_gpio_init(&cfg_cs);
cm_gpio_init(&cfg_rst);
CS_H
}
static void lcd_reset(void)
{
RST_H
osDelay(1000);
RST_L
osDelay(1000);
}
void lcd_write_com(uint8_t com)
{
uint8_t i;
SCK_H
A0_L
CS_L
for( i = 0; i < 8; i++ )
{
SCK_H
SCK_L
if( com & 0x80 )
{
MOSI_H;
}
else
{
MOSI_L;
}
SCK_L
SCK_H
com <<= 1;
}
CS_H
SCK_H
}
void lcd_write_data(uint8_t data)
{
uint8_t i;
SCK_H
A0_H
CS_L
for( i = 0; i < 8; i++ )
{
SCK_H
SCK_L
if( data & 0x80 )
{
MOSI_H;
}
else
{
MOSI_L;
}
SCK_L
SCK_H
data <<= 1;
}
CS_H
SCK_H
}
void lcd_clear_screen()
{
uint8_t i,j;
for(i = 0; i < 8; i++)
{
CS_L;
lcd_write_com(0x40);
lcd_write_com(0xb0 + i);
lcd_write_com(0x10);
lcd_write_com(0x00);
for(j = 0; j < 128; j++)
{
lcd_write_data(0xff);
}
}
}
void lcd_init(void)
{
lcd_hal_init();
lcd_reset();
lcd_write_com(0xa2);
lcd_write_com(0xD1);
lcd_write_com(0xa0);
lcd_write_com(0xc8);
lcd_write_com(0xa6);
lcd_write_com(0x2f);
lcd_write_com(0x25);
lcd_write_com(0x81);
lcd_write_com(18);
lcd_write_com(0x40);
lcd_write_com(0xaf);
lcd_clear_screen();
}
void lcd_deinit(void)
{
cm_gpio_deinit(LCD_RST);
cm_gpio_deinit(SPI_CS_PIN);
cm_gpio_deinit(SPI_A0_PIN);
cm_gpio_deinit(SPI_SCK_PIN);
cm_gpio_deinit(SPI_MOSI_PIN);
}
uint8_t lcd_buf[1024];
uint8_t ComTable[8]={3,2,1,0,7,6,5,4};
void LcmPutChar(uint8_t col,uint8_t page,uint8_t Order)
{
uint8_t i;
uint32_t x;
x = (Order-0x20)*0x10;
lcd_write_com(ComTable[page&0x07]|0xB0);
lcd_write_com( ((col+1)>>4) | 0x10);
lcd_write_com( (col+1)&0x0F );
for(i=0;i<8;i++)
{
lcd_write_data( ASCIIchardot[x] );
x++;
}
page++;
lcd_write_com(ComTable[page&0x07]|0xB0);
lcd_write_com( ((col+1)>>4) | 0x10);
lcd_write_com( (col+1)&0x0F );
for(i=0;i<8;i++)
{
lcd_write_data( ASCIIchardot[x] );
x++;
}
page--;
}
void LcmPutStr(uint8_t col,uint8_t page,uint8_t *puts)
{
while(*puts != '\0')
{
if(col>(LcmXPixel-8))
{
page=page+2;
col=0;
}
if(page>(LcmYPixel/8-2))
{
page=0;
col=0;
}
LcmPutChar(col,page,*puts);
puts++;
col=col+8;
}
}
void lcd_proc(void)
{
LcmPutStr(0,0,"ABCDEFG");
LcmPutStr(0,2,"1234567");
}
LCD.h
#ifndef __LCD_H__
#define __LCD_H__
typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned long int uint64_t;
typedef signed long int int64_t;
typedef unsigned int uintptr_t;
void lcd_init(void);
void lcd_deinit(void);
void lcd_proc(void);
#endif
|