linux SPI操作LCD12864液晶
生活随笔
收集整理的這篇文章主要介紹了
linux SPI操作LCD12864液晶
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
液晶型號為JLX12864 COG液晶,資料網上有,linux下面通過SPI與IO控制,IO控制使用的是文件方式,SPI是開發板提供的驅動,這個SPI驅動應該每家提供的都不一樣,需要自己去按照驅動文檔操作,主要不通電就是底層的配置不一樣。
//SPI.c這個是打開SPI驅動
/** SPI.c** Created on: 2018年8月2日* Author: cfan*/ #include <stdio.h> #include <unistd.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <stdint.h> #include <termios.h> #include "SPI.h" #include <errno.h> // 包含errno所需要的頭文件 #include <string.h> // 包含strerror所需要的頭文件 #include "typedef.h"//SPI初始化 int SPI_Init(SPI_HANDLE *pHandle, const char *pSpiDeviceName) {pHandle->fd = -1;if(pSpiDeviceName == NULL || pHandle==NULL || strlen(pSpiDeviceName)>SPI_DEVICE_NAME_MAX_LEN){printf("%s(%d)Check the input parameters!\r\n",__FILE__ , __LINE__);return -1;}strcpy(pHandle->SpiDeviceName, pSpiDeviceName); //記錄串口設備名稱//打開SPIpHandle->fd = open(pSpiDeviceName, O_RDWR|O_NOCTTY|O_NDELAY); //讀寫獨占方式打開SPIif (pHandle->fd < 0){//打印錯誤信息printf("Can't Open SPI(%s) : %s(%d)\n",pSpiDeviceName, strerror(errno), errno);return errno;}else{printf("Open SPI OK!\r\n");}return 0; }//spi.h
/** SPI.h** Created on: 2018年8月2日* Author: cfan*/#ifndef HARDWARE_SPI_H_ #define HARDWARE_SPI_H_ #include "termios.h" #include "typedef.h"#define SPI_DEVICE_NAME_MAX_LEN 35 //SPI名稱最大長度//SPI接口句柄 typedef struct {int fd;char SpiDeviceName[SPI_DEVICE_NAME_MAX_LEN+1]; //SPI名稱 }SPI_HANDLE;//SPI初始化 int SPI_Init(SPI_HANDLE *pHandle, const char *pSpiDeviceName);#endif /* HARDWARE_SPI_H_ *///SPI驅動調用,這個是開發板提供的,各個廠家應該都不一樣,需要自己去查詢
#include "common.h" #include "spi_enum.h" #include "spidev.h" #include <sys/ioctl.h> #include "nano_pi_spi.h" //開發板官方提供的SPI驅動接口#define SPI_MAX_SPEED 25000000 //最大時鐘速度/************************************************************************************************************************* *函數 : int setSPIWriteBitsPerWord(int spi_fd, int bits) *功能 : 設置每次讀SPI設備的字長,單位是比特. *參數 : spi_fd: SPI設備的文件描述符;bits: 字長,單位是比特 *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : 雖然大部分SPI接口的字長是8或者16,仍然會有一些特殊的例子。需要說明的是,如果這個成員為零的話,默認使用8作為字長(ioctl SPI_IOC_WR_BITS_PER_WORD) *************************************************************************************************************************/ int setSPIWriteBitsPerWord(int spi_fd, int bits) {clearLastError();int ret = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_WR_BITS_PER_WORD");}return ret; }/************************************************************************************************************************* *函數 : int setSPIReadBitsPerWord(int spi_fd, int bits) *功能 : 設置每次寫SPI設備的字長,單位是比特 *參數 : spi_fd: SPI設備的文件描述符;bits: 字長,單位是比特 *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : 雖然大部分SPI接口的字長是8或者16,仍然會有一些特殊的例子。需要說明的是,如果這個成員為零的話,默認使用8作為字長(ioctl SPI_IOC_WR_BITS_PER_WORD) *************************************************************************************************************************/ int setSPIReadBitsPerWord(int spi_fd, int bits) {int ret = ioctl(spi_fd, SPI_IOC_RD_BITS_PER_WORD, &bits);clearLastError();if (ret < 0) {setLastError("Can't ioctl SPI_IOC_RD_BITS_PER_WORD");}return ret; }/************************************************************************************************************************* *函數 : int setSPIBitOrder(int spi_fd, int order) *功能 : 設備SPI傳輸時是先傳輸低比特位還是高比特位 *參數 : spi_fd: SPI設備的文件描述符;order: 傳SPIEnum.MSBFIRST或SPIEnum.LSBFIRST *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : 可選的參數有SPIEnum.MSBFIRST和SPIEnum.LSBFIRST *************************************************************************************************************************/ int setSPIBitOrder(int spi_fd, int order) {int ret;int spi_mode = 0;clearLastError();if(order == LSBFIRST) {spi_mode |= SPI_LSB_FIRST;} else {spi_mode &= ~SPI_LSB_FIRST;}ret = ioctl(spi_fd, SPI_IOC_WR_MODE, &spi_mode);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_WR_MODE");return ret;}return ret; }/************************************************************************************************************************* *函數 : int setSPIMaxSpeed(int spi_fd, unsigned int spi_speed) *功能 : 設備SPI傳輸速度 *參數 : spi_fd: SPI設備的文件描述符;spi_speed: 速度(分頻,越小速度越高) *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : *************************************************************************************************************************/ int setSPIMaxSpeed(int spi_fd, unsigned int spi_speed) {int ret;unsigned int realSpeed;clearLastError();if (spi_speed<0 || spi_speed>SPI_MAX_SPEED) {setLastError("invalid spi speed %d", spi_speed);}ret = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_WR_MAX_SPEED_HZ");return ret;}ret = ioctl(spi_fd, SPI_IOC_RD_MAX_SPEED_HZ, &realSpeed);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_RD_MAX_SPEED_HZ");return ret;}return ret; }/************************************************************************************************************************* *函數 : int setSPIDataMode(int spi_fd, int mode) *功能 : 設置SPI設備的模式 *參數 : spi_fd: SPI設備的文件描述符;mode: SPI設備的模式,可傳入SPI_MODE0 ~ SPI_MODE3 *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : *************************************************************************************************************************/ int setSPIDataMode(int spi_fd, int mode) {int ret;int spi_mode = 0;clearLastError();switch(mode) {case SPI_MODE0:spi_mode &= ~(SPI_CPHA|SPI_CPOL);break;case SPI_MODE1:spi_mode &= ~(SPI_CPOL);spi_mode |= (SPI_CPHA);break;case SPI_MODE2:spi_mode |= (SPI_CPOL);spi_mode &= ~(SPI_CPHA);break;case SPI_MODE3:spi_mode |= (SPI_CPHA|SPI_CPOL);break;default:setLastError("error SPIDataMode");return -1;}ret = ioctl(spi_fd, SPI_IOC_WR_MODE, &mode);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_WR_MODE");return ret;}ret = ioctl(spi_fd, SPI_IOC_RD_MODE, &mode);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_RD_MODE");return ret;}return ret; }/************************************************************************************************************************* *函數 : int SPItransferOneByte(int spi_fd, unsigned char byteData, int spi_delay, int spi_speed, int spi_bits) *功能 : 同時發送與接收一個字節的數據 *參數 : spi_fd: SPI設備的文件描述符;byteData:要寫入SPI設備的數據;spi_delay:延時;spi_speed:傳輸速度;spi_bits:字長,單位是比特 *返回 : 成功返回讀到的數據,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : *************************************************************************************************************************/ int SPItransferOneByte(int spi_fd , unsigned char byteData, int spi_delay, int spi_speed, int spi_bits) {int ret;unsigned char tx[1] = {0};unsigned char rx[1] = {0};tx[0] = byteData;struct spi_ioc_transfer tr;tr.tx_buf = (unsigned long)tx;tr.rx_buf = (unsigned long)rx;tr.len = 1;tr.delay_usecs = spi_delay;tr.speed_hz = spi_speed;tr.bits_per_word = spi_bits;clearLastError();ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);if (ret < 0) {setLastError("Can't ioctl SPI_IOC_MESSAGE");return ret;}return rx[0]; }/************************************************************************************************************************* *函數 : int SPItransferBytes(int spi_fd, unsigned char * writeData, int writeLen, unsigned char * readBuffer, *函數 int readLen, int spi_delay, int spi_speed, int spi_bits) *功能 : 同時發送與接收多個字節的數據 *參數 : spi_fd: SPI設備的文件描述符;writeData:要寫入的數據;readBuff: 存放讀取數據的緩沖區;spi_delay:延時;spi_speed:傳輸速度;spi_bits:字長,單位是比特 *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : *************************************************************************************************************************/ int SPItransferBytes(int spi_fd, unsigned char * writeData, int writeLen, unsigned char * readBuffer, int readLen, int spi_delay,int spi_speed, int spi_bits) {unsigned int len = writeLen;if (len > readLen) {len = readLen;}unsigned char * pWriteData = writeData;unsigned char * pReadBuffer = readBuffer;struct spi_ioc_transfer tr;tr.tx_buf = (unsigned long)pWriteData;tr.rx_buf = (unsigned long)pReadBuffer;tr.len = len;tr.delay_usecs = spi_delay;tr.speed_hz = spi_speed;tr.bits_per_word = spi_bits;int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);clearLastError();if (ret < 0) {setLastError("Can't ioctl SPI_IOC_MESSAGE");}return ret; }/************************************************************************************************************************* *函數 : int writeBytesToSPI(int spi_fd, unsigned char * writeData, int writeLen, int spi_delay, int spi_speed, *函數 int spi_bits) *功能 : 寫多個字節的數據到SPI設 備 *參數 : spi_fd: SPI設備的文件描述符;writeData:要寫入的數據;spi_delay:延時;spi_speed:傳輸速度;spi_bits:字長,單位是比特 *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : *************************************************************************************************************************/ int writeBytesToSPI(int spi_fd, unsigned char * writeData, int writeLen, int spi_delay, int spi_speed, int spi_bits) {unsigned int len = writeLen;unsigned char * pWriteData = writeData;struct spi_ioc_transfer tr;tr.tx_buf = (unsigned long)pWriteData;tr.rx_buf = (unsigned long)0;tr.len = len;tr.delay_usecs = spi_delay;tr.speed_hz = spi_speed;tr.bits_per_word = spi_bits;int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);clearLastError();if (ret < 0) {setLastError("Can't ioctl SPI_IOC_MESSAGE");}return ret; }/************************************************************************************************************************* *函數 : int readBytesFromSPI(int spi_fd, unsigned char * readBuffer, int readLen, int spi_delay, int spi_speed, *函數 int spi_bits) *功能 : 從SPI設備讀取多個字節 *參數 : spi_fd: SPI設備的文件描述符;readBuff: 存放讀取數據的緩沖區;readLen:讀取長度(不能超過緩沖區大小)spi_delay:延時;spi_speed:傳輸速度;spi_bits:字長,單位是比特 *返回 : 成功返回0,失敗返回負數 *依賴 : 無 *作者 : Friendly NanoPI-NEO(cp1300@139.com整理) *時間 : 2018-08-12 *最后修改時間 : 2018-08-12 *說明 : *************************************************************************************************************************/ int readBytesFromSPI(int spi_fd, unsigned char * readBuffer, int readLen, int spi_delay, int spi_speed, int spi_bits) {unsigned int len = readLen;unsigned char * pReadBuffer = readBuffer;struct spi_ioc_transfer tr;tr.tx_buf = (unsigned long)0;tr.rx_buf = (unsigned long)pReadBuffer;tr.len = len;tr.delay_usecs = spi_delay;tr.speed_hz = spi_speed;tr.bits_per_word = spi_bits;int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);clearLastError();if (ret < 0) {setLastError("Can't ioctl SPI_IOC_MESSAGE");}return ret; }//JLX12864G.c?液晶硬件底層操作
/************************************************************************************************************** 文件名: JLX12864G.c* 功能: JLX12864G-0088 JLX12864G液晶驅動* 作者: cp1300@139.com* 郵箱: cp1300@139.com* 創建時間: 2012年5月30日20:40* 最后修改時間:2012年5月30日* 詳細: 2016-02-01:增加獲取顯存函數 *************************************************************************************************************/ #include "ascii_8x16.h" #include "JLX12864G.H" #include <stdio.h> #include "typedef.h"//字模取模方式:陰碼,列行式,逆向(低位在前)//漢字支持 #define CHINESE_ENABLE 0#if LCD_BUFF_ENABLE //使能了顯存 //獲取顯存地址 //2016-02-01:增加獲取顯存函數 u8 *JLX12864G_GetGramBuff(JLX12864G_HANDLE *pHandle) {return (u8 *)&pHandle->LCD_BUFF[0][0]; }#endif/************************************************************************************************************************* * 函數 : void JLX12864G_WriteCommand(JLX12864G_HANDLE *pHandle, u8 cmd) * 功能 : 向JLX12864G寫入一字節命令 * 參數 : pHandle:句柄;cmd:命令 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120530 * 最后修改時間 : 2018-08-12 * 說明 : RS=0,時鐘上升沿數據有效,先發送高位 *************************************************************************************************************************/ void JLX12864G_WriteCommand(JLX12864G_HANDLE *pHandle, u8 cmd) {pHandle->SetRS(0); //RS=0pHandle->WriteData(&cmd, 1); //發送數據pHandle->SetRS(1); //RS=1 }/************************************************************************************************************************* * 函數 : static void JLX12864G_SetPageAdd(JLX12864G_HANDLE *pHandle, u8 PageAdd) * 功能 : 設置光標頁地址 * 參數 : pHandle:句柄;PageAdd:頁地址,0-7 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 20120531 * 說明 : 共64行,沒8行為一頁,共8頁 *************************************************************************************************************************/ static void JLX12864G_SetPageAdd(JLX12864G_HANDLE *pHandle, u8 PageAdd) {JLX12864G_WriteCommand(pHandle, 0xb0 + PageAdd); }/************************************************************************************************************************* * 函數 : static void JLX12864G_SetLineAdd(JLX12864G_HANDLE *pHandle, u8 LineAdd) * 功能 : 設置光標列地址 * 參數 : pHandle:句柄;LineAdd:列地址,0-127 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 20120531 * 說明 : 共128列 *************************************************************************************************************************/ static void JLX12864G_SetLineAdd(JLX12864G_HANDLE *pHandle, u8 LineAdd) {LineAdd += JLX12864G_X_OFFSET;JLX12864G_WriteCommand(pHandle, 0x10 + (LineAdd >> 4)); //列地址高4位JLX12864G_WriteCommand(pHandle, 0x00 + (LineAdd & 0x0f)); //列地址低4位 }/************************************************************************************************************************* * 函數 : void JLX12864G_ClearAll(JLX12864G_HANDLE *pHandle) * 功能 : JLX12864G液晶清屏 * 參數 : pHandle:句柄; * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120530 * 最后修改時間 : 20120530 * 說明 : 無 *************************************************************************************************************************/ void JLX12864G_ClearAll(JLX12864G_HANDLE *pHandle) {u8 i,j;u32 data = 0;for(i = 0;i < 9;i ++){JLX12864G_SetPageAdd(pHandle, i);JLX12864G_SetLineAdd(pHandle, 0);for(j = 0;j < 132/4;j ++){pHandle->WriteData((u8 *)&data, 4);}} }/************************************************************************************************************************* * 函數 : void JLX12864G_FillAll(JLX12864G_HANDLE *pHandle) * 功能 : JLX12864G液晶填充 * 參數 : pHandle:句柄; * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120530 * 最后修改時間 : 20120530 * 說明 : 無 *************************************************************************************************************************/ void JLX12864G_FillAll(JLX12864G_HANDLE *pHandle) {u8 i,j;u32 data = 0xffffffff;for(i = 0;i < 9;i ++){JLX12864G_SetPageAdd(pHandle, i);JLX12864G_SetLineAdd(pHandle, 0);for(j = 0;j < 132/4;j ++){pHandle->WriteData((u8 *)&data, 4);}} }/************************************************************************************************************************* * 函數 : void JLX12864G_ShowOneChar(JLX12864G_HANDLE *pHandle,u8 PageAdd,u8 LineAdd,u8 CHAR,u8 FontSize) * 功能 : 在指定位置顯示一個字符 * 參數 : pHandle:句柄;PageAdd:頁,0~7,共8頁;L:0~127共128列,CHAR:需要顯示的字符,FontSize:字體大小 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120530 * 最后修改時間 : 20120530 * 說明 : 顯示一個ASCII字符 *************************************************************************************************************************/ void JLX12864G_ShowOneChar(JLX12864G_HANDLE *pHandle,u8 PageAdd,u8 LineAdd,u8 CHAR,u8 FontSize) {u8 i,j,k;const unsigned char *p;CHAR -= 32;if(CHAR > ASCII_MAX - 1)return;if(FontSize == 12)p = ASCII_8X12[CHAR]; //12號elsep = ASCII_8X16[CHAR]; //16號for(i = 0;i < 2;i ++){JLX12864G_SetPageAdd(pHandle, PageAdd + i);JLX12864G_SetLineAdd(pHandle, LineAdd);k = i * 8;pHandle->WriteData((u8 *)&p[k+j], 8);/*for(j = 0;j < 8;j ++){pHandle->WriteByteData(p[k+j]);}*/} }/************************************************************************************************************************* * 函數 : void LCD_PrintfChar(JLX12864G_HANDLE *pHandle,u8 PageAdd,u8 LineAdd,const char *p,u8 FontSize) * 功能 : 在指定位置顯示字符串 * 參數 : pHandle:句柄;PageAdd:頁,0~7,共8頁;L:0~127共128列;p:字符指針,FontSize:子大小;16或者12 * 返回 : 無 * 依賴 : JLX12864G_ShowOneChar * 作者 : cp1300@139.com * 時間 : 20120601 * 最后修改時間 : 20120601 * 說明 : FontSize = 16或者 12 *************************************************************************************************************************/ void JLX12864G_PrintfChar(JLX12864G_HANDLE *pHandle,u8 PageAdd,u8 LineAdd,const char *p,u8 FontSize) {while(*p != 0){JLX12864G_ShowOneChar(pHandle, PageAdd,LineAdd,*p,FontSize);p ++;LineAdd += 8;} }/************************************************************************************************************************* * 函數 : void JLX12864G_SetConAdj(JLX12864G_HANDLE *pHandle,u8 cont) * 功能 : 設置液晶的對比度 * 參數 : pHandle:句柄;cont:對比度值 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 2014-08-24 * 最后修改時間 : 2014-08-24 * 說明 : 需要先初始化LCD *************************************************************************************************************************/ void JLX12864G_SetConAdj(JLX12864G_HANDLE *pHandle,u8 cont) {if(cont < 25)cont = 25;if(cont > 60)cont = 60;JLX12864G_WriteCommand(pHandle, 0x81); /*微調對比度*/JLX12864G_WriteCommand(pHandle, cont); /*微調對比度的值,可設置范圍0~63*/pHandle->LCD_Cont = cont; //更新對比度 }/************************************************************************************************************************* * 函數 : void JLX12864G_Init(JLX12864G_HANDLE *pHandle,void (*WriteData)(u8 data,u8 len), //寫數據接口void (*SetRS)(u8 level), //設置RS電平void (*SetRST)(u8 level), //設置RST電平void (*DelayMS)(u8 ms), //ms延時u8 LCDCont) * 功能 : 初始化JLX12864G液晶 * 參數 : pHandle:句柄;WriteByteData:寫一字節函數;SetRS:設置RS電平;SetRST:設置RST電平;DelayMS:系統ms延時;LCDCont:對比度 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120530 * 最后修改時間 : 2018-08-12 * 說明 : 初始化JLX12864G液晶 *************************************************************************************************************************/ void JLX12864G_Init(JLX12864G_HANDLE *pHandle,void (*WriteData)(u8 *data,u8 len), //寫數據接口void (*SetRS)(u8 level), //設置RS電平void (*SetRST)(u8 level), //設置RST電平void (*DelayMS)(u8 ms), //ms延時u8 LCDCont) {if(pHandle == NULL){printf("JLX12864G:ERROR *pHandle is NULL!\r\n");return;}if(WriteData == NULL){printf("JLX12864G:ERROR *WriteData is NULL!\r\n");return;}if(SetRS == NULL){printf("JLX12864G:ERROR *SetRS is NULL!\r\n");return;}if(DelayMS == NULL){printf("JLX12864G:ERROR *DelayMS is NULL!\r\n");return;}pHandle->WriteData = WriteData;pHandle->SetRS = SetRS;pHandle->SetRST = SetRST;pHandle->DelayMS = DelayMS;if(pHandle->SetRST != NULL){pHandle->SetRST(0);//液晶復位開始pHandle->DelayMS(1);pHandle->SetRST(1);//液晶復位結束}pHandle->DelayMS(1);JLX12864G_WriteCommand(pHandle, 0xe2); /*軟復位*/JLX12864G_WriteCommand(pHandle, 0x2c); /*升壓步聚1*/JLX12864G_WriteCommand(pHandle, 0x2e); /*升壓步聚2*/JLX12864G_WriteCommand(pHandle, 0x2f); /*升壓步聚3*/JLX12864G_WriteCommand(pHandle, 0x23); /*粗調對比度,可設置范圍20~27*/ // JLX12864G_WriteCommand(0x81); /*微調對比度*/ // JLX12864G_WriteCommand(0x30); /*微調對比度的值,可設置范圍0~63*/JLX12864G_SetConAdj(pHandle, LCDCont);JLX12864G_WriteCommand(pHandle, 0xa2); /*1/9 偏壓比(bias)*/ #if(LCD_ROTATE_180) //旋轉180度顯示JLX12864G_WriteCommand(pHandle, 0xc0); /*行掃描順序:從下到上*/JLX12864G_WriteCommand(pHandle, 0xa1); /*列掃描順序:從右到左*/ #elseJLX12864G_WriteCommand(pHandle, 0xc8); /*行掃描順序:從上到下*/JLX12864G_WriteCommand(pHandle, 0xa0); /*列掃描順序:從左到右*/ #endifJLX12864G_WriteCommand(pHandle, 0x40); //初始化顯示行為0JLX12864G_WriteCommand(pHandle, 0xa4); //常規顯示JLX12864G_WriteCommand(pHandle, 0xaf); /*開顯示*/JLX12864G_ClearAll(pHandle);pHandle->LCD_Cont = LCDCont;//isPowerStatus = TRUE; //上電完成 }/************************************************************************************************************************* * 函數 : void JLX12864G_GRAM_Up(JLX12864G_HANDLE *pHandle, u8 LCD_BUFF[8][128], u8 x1,u8 y1,u8 x2,u8 y2) * 功能 : 更新顯存至液晶 * 參數 : pHandle:句柄;LCD_BUFF:顯存地址;x1,y1:起始坐標;x2,y2:終點坐標 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 2018-08-12 * 說明 : y坐標會頁對齊 *************************************************************************************************************************/ void JLX12864G_GRAM_Up(JLX12864G_HANDLE *pHandle, u8 LCD_BUFF[8][128], u8 x1,u8 y1,u8 x2,u8 y2) {u8 i,j;if(x2 > 127) x2 = 127;y1 /= 8; //計算頁地址y2 /= 8;for(i = 0;i < (y2 - y1 + 1);i ++){JLX12864G_SetPageAdd(pHandle, y1 + i); //寫入頁地址JLX12864G_SetLineAdd(pHandle, x1); //寫入行地址pHandle->WriteData(&LCD_BUFF[y1 + i][x1 + 0], (x2 - x1 + 1));/*for(j = 0;j < (x2 - x1 + 1);j ++){LCD12864_WriteData(pHandle, LCD_BUFF[y1 + i][x1 + j]);}*/} }//LCD12864.c?這個是個內存中的虛擬LCD,每次操作都在內存中操作,然后同步到實際LCD
/** LCD12864_Virtual.c* 虛擬LCD12864* Created on: 2018年8月12日* Author: cfan*/ #include "LCD12864.h" #include "typedef.h" #include "ASCII_8x16.h" #include <stdio.h> #include <string.h>/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_Init(LCD12864_HANDLE *pHandle) * 功能 : LCD12864顯存模式初始化 * 參數 : pHandle:句柄; * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_Init(LCD12864_HANDLE *pHandle) {if(pHandle == NULL){printf("LCD12864:ERROR *pHandle is NULL!\r\n");return;}memset(pHandle->LCD_BUFF, 0, 8*128); }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_DrawPoint(LCD12864_HANDLE *pHandle, u8 x,u8 y) * 功能 : 在顯存里面指定位置畫點 * 參數 : pHandle:句柄;x:X坐標,0-127;y:y坐標,0-63 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_DrawPoint(LCD12864_HANDLE *pHandle, u8 x,u8 y) {if(x > 127 || y > 63)return;pHandle->LCD_BUFF[y / 8][x] |= (1 << (y % 8)); }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_ClearPoint(LCD12864_HANDLE *pHandle, u8 x,u8 y) * 功能 : 擦除顯存里面指定位置的點 * 參數 : pHandle:句柄;x:X坐標,0-127;y:y坐標,0-63 * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_ClearPoint(LCD12864_HANDLE *pHandle, u8 x, u8 y) {if(x > 127 || y > 63)return;pHandle->LCD_BUFF[y / 8][x] &= ~(1 << (y % 8)); }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_ClearAll(LCD12864_HANDLE *pHandle) * 功能 : 清除全部顯存 * 參數 : pHandle:句柄; * 返回 : 無 * 依賴 : 底層宏定義 * 作者 : cp1300@139.com * 時間 : 20120531 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_ClearAll(LCD12864_HANDLE *pHandle) {u8 i,j;for(i = 0;i < 8;i ++){for(j = 0;j < 128;j ++){pHandle->LCD_BUFF[i][j] = 0x00;}} }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_ShowChar(LCD12864_HANDLE *pHandle, u8 x,u8 y,u8 CHAR,LCD12864_FONT_MODE FontMode) * 功能 : 在指定位置顯示一個指定大小的字符 * 參數 : pHandle:句柄;x,y:顯示開始坐標,p:漢子點陣緩沖區;FontMode:漢子顯示模式, * 返回 : 無 * 依賴 : 畫點函數 * 作者 : cp1300@139.com * 時間 : 20120603 * 最后修改時間 : 2018-08-12 * 說明 : *************************************************************************************************************************/ void LCD12864_GRAM_ShowChar(LCD12864_HANDLE *pHandle, u8 x,u8 y,u8 CHAR,LCD12864_FONT_MODE FontMode) {u8 i,j;u8 FontSize = (u8)FontMode&0x0f; //獲取字體大小u8 *p;void (*DrawPoint)(LCD12864_HANDLE *pHandle, u8 i,u8 j);void (*ClearPoint)(LCD12864_HANDLE *pHandle, u8 i,u8 j);CHAR -= 32;if(CHAR > 95 - 1) //限制ASCII范圍return;if(FontSize){FontSize = 12;p = (u8 *)ASCII_8X12[CHAR]; //12號}else{FontSize = 16;p = (u8 *)ASCII_8X16[CHAR]; //16號}if(FontMode & 0x40) //反顯{DrawPoint = LCD12864_GRAM_ClearPoint;ClearPoint = LCD12864_GRAM_DrawPoint;}else //正常模式{ClearPoint = LCD12864_GRAM_ClearPoint;DrawPoint = LCD12864_GRAM_DrawPoint;}if(FontMode & 0x80) //疊加顯示{for(j = 0;j < 8;j ++){for(i = 0;i < 8;i ++){if(*p & (1 << i))(*DrawPoint)(pHandle, x + j,y + i);}p ++;}for(j = 0;j < 8;j ++){for(i = 0;i < FontSize - 8;i ++){if(*p & (1 << i))(*DrawPoint)(pHandle, x + j,y + 8 + i);}p ++;}}else //非疊加顯示{for(j = 0;j < 8;j ++){for(i = 0;i < 8;i ++){if(*p & (1 << i))(*DrawPoint)(pHandle, x + j,y + i);else(*ClearPoint)(pHandle, x + j,y + i);}p ++;}for(j = 0;j < 8;j ++){for(i = 0;i < FontSize - 8;i ++){if(*p & (1 << i))(*DrawPoint)(pHandle, x + j,y + 8 + i);else(*ClearPoint)(pHandle, x + j,y + 8 + i);}p ++;}} }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_Fill(LCD12864_HANDLE *pHandle, u16 xStart, u16 yStart, u16 xEnd, u16 yEnd) * 功能 : 指定位置填充 * 參數 : pHandle:句柄;范圍 * 返回 : 無 * 依賴 : 底層函數 * 作者 : cp1300@139.com * 時間 : 20110920 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_Fill(LCD12864_HANDLE *pHandle, u16 xStart, u16 yStart, u16 xEnd, u16 yEnd) {u16 i,j;for(i = xStart;i < xEnd; i ++){for(j = yStart;j < yEnd;j ++){LCD12864_GRAM_DrawPoint(pHandle,i,j);}} }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_Clear(LCD12864_HANDLE *pHandle, u16 xStart, u16 yStart, u16 xEnd, u16 yEnd) * 功能 : 清除指定位置 * 參數 : pHandle:句柄;范圍 * 返回 : 無 * 依賴 : 底層函數 * 作者 : cp1300@139.com * 時間 : 20110920 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_Clear(LCD12864_HANDLE *pHandle, u16 xStart, u16 yStart, u16 xEnd, u16 yEnd) {u16 i,j;for(i = xStart;i < xEnd; i ++){for(j = yStart;j < yEnd;j ++){LCD12864_GRAM_ClearPoint(pHandle,i,j);}} }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_DrawLine(LCD12864_HANDLE *pHandle,u16 x1, u16 y1, u16 x2, u16 y2) * 功能 : 畫線函數 * 參數 : pHandle:句柄;起點終點坐標 * 返回 : 無 * 依賴 : 畫點函數 * 作者 : cp1300@139.com * 時間 : 20110920 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_DrawLine(LCD12864_HANDLE *pHandle,u16 x1, u16 y1, u16 x2, u16 y2) {u16 t;int xerr=0,yerr=0,delta_x,delta_y,distance;int incx,incy,uRow,uCol;//TFT_LCD_SetRamAddr(0,239,0,319);//設置顯示窗口delta_x=x2-x1; //計算坐標增量delta_y=y2-y1;uRow=x1;uCol=y1;if(delta_x>0)incx=1; //設置單步方向else if(delta_x==0)incx=0;//垂直線else {incx=-1;delta_x=-delta_x;}if(delta_y>0)incy=1;else if(delta_y==0)incy=0;//水平線else{incy=-1;delta_y=-delta_y;}if( delta_x>delta_y)distance=delta_x; //選取基本增量坐標軸else distance=delta_y;for(t=0;t<=distance+1;t++ )//畫線輸出{LCD12864_GRAM_DrawPoint(pHandle, uRow,uCol);//畫點xerr+=delta_x ;yerr+=delta_y ;if(xerr>distance){xerr-=distance;uRow+=incx;}if(yerr>distance){yerr-=distance;uCol+=incy;}} }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_DrawRectangle(LCD12864_HANDLE *pHandle,u16 x1, u16 y1, u16 x2, u16 y2) * 功能 : 在指定位置畫一個矩形 * 參數 : pHandle:句柄;多邊形的兩個坐標 * 返回 : 無 * 依賴 : 畫線函數 * 作者 : cp1300@139.com * 時間 : 20110920 * 最后修改時間 : 2018-08-12 * 說明 : 無 *************************************************************************************************************************/ void LCD12864_GRAM_DrawRectangle(LCD12864_HANDLE *pHandle,u16 x1, u16 y1, u16 x2, u16 y2) {LCD12864_GRAM_DrawLine(pHandle, x1,y1,x2,y1);LCD12864_GRAM_DrawLine(pHandle, x1,y1,x1,y2);LCD12864_GRAM_DrawLine(pHandle, x1,y2,x2,y2);LCD12864_GRAM_DrawLine(pHandle, x2,y1,x2,y2); }/************************************************************************************************************************* * 函數 : void LCD12864_GRAM_ShowString(LCD12864_HANDLE *pHandle, u16 x,u16 y,const char *pStr,LCD12864_FONT_MODE Font_MODE) * 功能 : 在顯存指定位置顯示字符串 * 參數 : pHandle:句柄;x,y:顯示開始坐標,pStr:字符串緩沖區;FontMode:顯示模式, * 返回 : 無 * 依賴 : 畫點函數 * 作者 : cp1300@139.com * 時間 : 2014-08-20 * 最后修改時間 : 2018-08-12 * 說明 : *************************************************************************************************************************/ void LCD12864_GRAM_ShowString(LCD12864_HANDLE *pHandle, u16 x,u16 y,const char *pStr,LCD12864_FONT_MODE Font_MODE) {u8 Font_Size = Font_MODE & 0x0f; #if CHINESE_ENABLEu8 buff[32]; #endifwhile(*pStr != 0){ #if CHINESE_ENABLEif(*pStr > 0x80)//漢字{FONT_GetFontLattice(buff, (u8*)pStr, ST16X16); //獲取漢字點陣LCD12864_GRAM_ShowChina(x,y,buff,Font_MODE); //顯示漢字pStr += 2;if(x > 127 - 16) //自動換行{x = 0;y += 16;}else{x += 16;}}else //ASCII #endif{LCD12864_GRAM_ShowChar(pHandle, x,y,*pStr,Font_MODE);pStr++;if(x > 127 - 8) //自動換行{x = 0;y += Font_Size;}else{x += 8;}}} }//LCD12864.h
/** LCD12864_Virtual.h** Created on: 2018年8月12日* Author: cfan*/#ifndef PROGRAM_LCD_LCD12864_H_ #define PROGRAM_LCD_LCD12864_H_ #include "typedef.h"typedef enum {FONT16_DEFAULT = (0x80+16), //16號,疊加顯示FONT12_DEFAULT = (0x80+12), //12號,疊加顯示FONT16_COVER = (16), //16號,覆蓋顯示FONT12_COVER = (12), //12號,覆蓋顯示FONT16_REVERSE = (0x40+16), //16號,反顯顯示FONT12_REVERSE = (0x40+12), //12號,反顯顯示 }LCD12864_FONT_MODE;//LCD12864 句柄 typedef struct {u8 LCD_BUFF[8][128];//顯存//void (*UpdateGRAM)(u8 LCD_BUFF[8][128], u8 x1,u8 y1,u8 x2,u8 y2); }LCD12864_HANDLE;void LCD12864_GRAM_Init(LCD12864_HANDLE *pHandle);//LCD12864顯存模式初始化 void LCD12864_GRAM_ShowString(LCD12864_HANDLE *pHandle, u16 x,u16 y,const char *pStr,LCD12864_FONT_MODE Font_MODE); //在顯存指定位置顯示字符串#endif /* PROGRAM_LCD_LCD12864_H_ *///測試線程
//測試線程 void *func(void *arg) {float ftemp = 0;char buff[64];SPI_Init(&SPI_Handle, "/dev/spidev0.0"); //打開SPI驅動setSPIReadBitsPerWord(SPI_Handle.fd, 8); //8bit模式setSPIBitOrder(SPI_Handle.fd, MSBFIRST); //高位在前//setSPIMaxSpeed(SPI_Handle.fd, 500); //設備SPI傳輸速度 10KsetSPIDataMode(SPI_Handle.fd, SPI_MODE0); //模式0if(initPinGPIO(BOARD_NANOPI_M1) < 0) //初始化開發板型號{printf("error:gpio init error!\r\n");}exportGPIOPin(LCD_RST_PIN_INDEX); //導出IO文件 RST接口setGPIODirection(LCD_RST_PIN_INDEX, GPIO_OUT); //輸出exportGPIOPin(LCD_RS_PIN_INDEX); //導出IO文件 RS接口setGPIODirection(LCD_RS_PIN_INDEX, GPIO_OUT); //輸出//初始化JLX12864G硬件JLX12864G_Init(&mJLX12864G_Handle, JLX12864G_WriteData, JLX12864G_SetRS, JLX12864G_SetRST, JLX12864G_DelayMS, 40);LCD12864_GRAM_Init(&g_LCD12864_Handle); //初始化虛擬LCD12864屏幕while(1){ftemp = GetCPU_Temp(); //獲取CPU溫度sprintf(buff,"CPU TEMP:%.02f",ftemp); //格式化字符串LCD12864_GRAM_ShowString(&g_LCD12864_Handle, 0 ,0, buff, FONT16_COVER); //覆蓋顯示-將字符串在虛擬LCD12864中顯示JLX12864G_GRAM_Up(&mJLX12864G_Handle, g_LCD12864_Handle.LCD_BUFF, 0,0,128,64); //更新顯存到JLX12864Gsleep(3);} }示例代碼:https://download.csdn.net/download/cp1300/10611732
總結
以上是生活随笔為你收集整理的linux SPI操作LCD12864液晶的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解密中国网络游戏业的黑暗骑士:响尾马(下
- 下一篇: GPS模块数据分析 -- linux