ili9341v read register and get point color in 4-line SPI

作者:Joey 分类: 液晶显示 发布于:2019-4-29 15:06 ė6457次浏览 60条评论

/* Includes ------------------------------------------------------------------*/
#include <intrins.h>
#include "STC11.h"
#include "ascii.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
sbit CS  = P3^5;
sbit RST = P2^0;
sbit RS  = P2^7;
sbit SCL = P3^7;
sbit SDA = P3^6;
sbit SDO = P0^0;

/* Private macro -------------------------------------------------------------*/
#define LcdXPixel (240)
#define LcdYPixel (320)

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
void DelayMS(u16 MS)
{
    u8 us,usn,i;
    while(MS!=0)
    {
        usn = 45;
        while(usn!=0)
        {
            us=182;
            while (us!=0){us--;};
            usn--;
        }
        for(i=0;i<3;i++);
        MS--;
    }
}

static void LcdWrCmd( u8 CByte )
{
    u8 i;
    CS = 0;
    RS = 0; //RS=0=index  RS=1=register data
    //直接展开以提高速度
    i = (CByte >> 7)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 6)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 5)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 4)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 3)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 2)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 1)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (CByte >> 0)&0x01; SCL = 0; SDA = i; SCL = 1;
        
    CS  = 1;
}

static void LcdWrDat( u8 DByte )
{
    u8 i;
    CS = 0;
    RS = 1; //RS=0=index  RS=1=register data
    //直接展开以提高速度
    i = (DByte >> 7)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 6)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 5)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 4)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 3)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 2)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 1)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (DByte >> 0)&0x01; SCL = 0; SDA = i; SCL = 1;
    
    CS = 1;
}

static u32 LcdRdReg( u8 ID_REG )
{
    u8 i;
    u32 in=0;
    
    CS = 0;
    //直接展开以提高速度
    RS = 0; //RS=0=index  RS=1=register data
    i = (ID_REG >> 7)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 6)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 5)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 4)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 3)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 2)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 1)&0x01; SCL = 0; SDA = i; SCL = 1;
    i = (ID_REG >> 0)&0x01; SCL = 0; SDA = i; SCL = 1;
        
    SCL = 0;        //dummy clock
    SCL = 1;
    //在SPI模式读取的寄存器只有一个字节返回的时候,是不需要dummy clock,参考SPI接口Read Cycle Sequence.
    
    for(i=0;i<32;i++)
    {
        in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    }
    
    CS = 1;
    return in;
}

static u8 LcdRdDat( void )
{
    u8 in=0;
    CS = 0;
    RS = 1; //RS=0=index  RS=1=register data
    //直接展开以提高速度
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    in <<= 1; SCL = 0; in |= SDO; SCL = 1;
    
//  CS = 1;     //需要连读读取时(读取字节>1),CS不能为高,否则第二个开始读出的都是0xFF
    return in;
}

void LCD_DispICID(u16 x, u16 y)
{
    u32 Val_LcdID=0;
    Val_LcdID = LcdRdReg(0x09); //为了测试SDO信号脚,读RDDST寄存器验证一下
    if(Val_LcdID == 0x84530600)
    {
        LCD_DispStr(x,y,"SDO Test OK");
    }
    else
    {
        LCD_DispStr(x,y,"SDO Error !!!xxx!!!");
    }
}

u16 LCD_GetPoint(u16 x, u16 y)
{
    u16 Val=0,c1=0,c2=0,c3=0;
    LcdWrCmd(0x2A);         //Col Address Set
    LcdWrDat(x>>8);         //Col Address Start Hbyte
    LcdWrDat(x&0xFF);       //Col Address Start Lbyte
    LcdWrDat(x>>8);         //Col Address End Hbyte
    LcdWrDat(x&0xFF);       //Col Address End Lbyte
    LcdWrCmd(0x2B);         //Row Address Set
    LcdWrDat(y>>8);         //Row Address Start Hbyte
    LcdWrDat(y&0xFF);       //Row Address Start Lbyte
    LcdWrDat(y>>8);         //Row Address End Hbyte
    LcdWrDat(y&0xFF);       //Row Address End Lbyte
    LcdWrCmd(0x2E);         //Write Read Start
    c1  = LcdRdDat();       //dummy read
    c1  = LcdRdDat();       //1st 8bit
    c2  = LcdRdDat();       //2nd 8bit
    c3  = LcdRdDat();       //3rd 8bit
    Val = ((c1&0xF8)<<8);   //get Red value
    Val|= ((c2&0xFC)<<3);   //get Green value
    Val|= ((c3&0xF8)>>3);   //get Blue value
    return Val;
}

本文出自 LcdBBS,转载时请注明出处及相应链接。


Ɣ回顶部