|
发表于 2013-8-27 13:57:53
|
显示全部楼层
- /*******************************************************************************
- * File Name :
- * Author :
- * Version :
- * Date :
- * Description :
- ********************************************************************************
- *
- *
- *
- *
- *
- *
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "LCD.h"
- /* Private typedef -----------------------------------------------------------*/
- typedef void (*pFunctionr)(void);
- typedef uint8_t (*pFunctionw)(void);
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- #define SET_WR GPIO_SetBits(GPIOC,WR)
- #define RESET_WR GPIO_ResetBits(GPIOC,WR)
- #define SET_CE GPIO_SetBits(GPIOC,CE)
- #define RESET_CE GPIO_ResetBits(GPIOC,CE)
- #define SET_RD GPIO_SetBits(GPIOC,RD)
- #define RESET_RD GPIO_ResetBits(GPIOC,RD)
- #define SET_CD GPIO_SetBits(GPIOA,CD)
- #define RESET_CD GPIO_ResetBits(GPIOA,CD)
- #define SET_RST GPIO_SetBits(GPIOA,RST)
- #define RESET_RST GPIO_ResetBits(GPIOA,RST)
- #define SET_D0 GPIO_SetBits(GPIOC,D0)
- #define RESET_D0 GPIO_ResetBits(GPIOC,D0)
- #define SET_D1 GPIO_SetBits(GPIOC,D1)
- #define RESET_D1 GPIO_ResetBits(GPIOC,D1)
- #define SET_D2 GPIO_SetBits(GPIOC,D2)
- #define RESET_D2 GPIO_ResetBits(GPIOC,D2)
- #define SET_D3 GPIO_SetBits(GPIOB,D3)
- #define RESET_D3 GPIO_ResetBits(GPIOB,D3)
- #define SET_D4 GPIO_SetBits(GPIOB,D4)
- #define RESET_D4 GPIO_ResetBits(GPIOB,D4)
- #define SET_D5 GPIO_SetBits(GPIOB,D5)
- #define RESET_D5 GPIO_ResetBits(GPIOB,D5)
- #define SET_D6 GPIO_SetBits(GPIOB,D6)
- #define RESET_D6 GPIO_ResetBits(GPIOB,D6)
- #define SET_D7 GPIO_SetBits(GPIOB,D7)
- #define RESET_D7 GPIO_ResetBits(GPIOB,D7)
- #define VERTICAL_LENGTH 128
- #define VERTICAL_MODULE 16
- #define HORIZONTAL_LENGTH 240
- #define HORIZONTAL_MODULE 30
- #define TextDisRamSize HORIZONTAL_MODULE * VERTICAL_MODULE
- #define GrapDisRamSize HORIZONTAL_MODULE * VERTICAL_LENGTH
- #define TextRamOffset 0
- #define GrapRamOffset HORIZONTAL_MODULE * VERTICAL_LENGTH
- #define __Cursor 0x01
- #define __CGRAM 0x02
- #define __DisBuf 0x04
- #define CMDSetAddrPointer 0x20
- #define CMDSetTextRamOffset 0x40
- #define CMDSetTextRamProperty 0X41
- #define CMDSetGrapRamOffset 0x42
- #define CMDSetGrapRamProperty 0X43
- #define __OR 0x00
- #define __XOR 0x01
- #define __AND 0x03
- #define __Double 0x04
- #define CMDSynthetizeRamModel 0x80
- #define __EnableCursorFlash 0x01
- #define __EnableCursor 0x02
- #define __EnableTextDisplay 0x04
- #define __EnableGrapDisplay 0x08
- #define CMDSetDisplayShape 0x90
- #define __OneLine 0x00
- #define __TwoLine 0x01
- #define __ThreeLine 0x02
- #define __FourLine 0x03
- #define __Fiveine 0x04
- #define __SixLine 0x05
- #define __SevenLine 0x06
- #define __EightLine 0x07
- #define CMDSetCursorModel 0xA0
- #define __Write 0x00
- #define __Read 0x01
- #define __Esc 0x02
- #define CMDSetAutoDataTrans 0xB0
- #define __WritePlus 0x00
- #define __ReadPlus 0x01
- #define __WriteSubtract 0x02
- #define __ReadSubtract 0x03
- #define __WriteNoOffset 0x04
- #define __ ReadNoOffset 0x05
- #define CMDSetOneTimesDataTrans 0xC0
- #define CMDReadOneByteDisRam 0xE0
- #define CMDReadOneLineDisRam 0xE8
- #define CMDBitWrite 0xF0
- #define OneByteTransRdy 0x03
- #define AutoWriteTransRdy 0x08
- #define AutoReadTransRdy 0x04
- /* Private variables ---------------------------------------------------------*/
- const uint8_t SetTextRamOffset[] = {TextRamOffset % 256,TextRamOffset >> 8,CMDSetTextRamOffset};
- const uint8_t SetTextRamProperty[] = {HORIZONTAL_MODULE % 256,HORIZONTAL_MODULE >> 8,CMDSetTextRamProperty};
- const uint8_t SetGrapRamOffset[] = {GrapRamOffset % 256,GrapRamOffset >> 8,CMDSetGrapRamOffset};
- const uint8_t SetGrapRamProperty[] = {HORIZONTAL_MODULE % 256,HORIZONTAL_MODULE >> 8,CMDSetGrapRamProperty};
- const uint8_t SetSynthetizeRamModel = CMDSynthetizeRamModel | __OR;
- const uint8_t SetDisplayShape = CMDSetDisplayShape | //__EnableCursorFlash | __EnableCursor |
- __EnableTextDisplay | __EnableGrapDisplay;
- const uint8_t SetCursorSetting = CMDSetCursorModel | __TwoLine;
- const uint8_t SetAutoTransRead = CMDSetAutoDataTrans | __Read;
- const uint8_t SetAutoTransWrite = CMDSetAutoDataTrans | __Write;
- const uint8_t SetAutoTransEsc = CMDSetAutoDataTrans | __Esc;
- uint8_t SetAddrPointer[] = {0X00,0X00,CMDSetAddrPointer | __DisBuf};
- uint8_t SetCursorPointer[] = {0X00,0X00,CMDSetAddrPointer | __Cursor};
- uint8_t SetOneTimesDataTrans[] = {0x00,CMDSetOneTimesDataTrans |__WritePlus};
- uint8_t TextDisBuf [HORIZONTAL_MODULE * VERTICAL_MODULE] = {0};
- uint8_t GrapDisBuf [HORIZONTAL_MODULE * VERTICAL_LENGTH] = {0}; //
- /* Private function prototypes -----------------------------------------------*/
- void SetD0(void)
- {GPIO_WriteBit(GPIOC,D0,Bit_SET);}
- void ResetD0(void)
- {GPIO_WriteBit(GPIOC,D0,Bit_RESET);}
- void SetD1(void)
- {GPIO_WriteBit(GPIOC,D1,Bit_SET);}
- void ResetD1(void)
- {GPIO_WriteBit(GPIOC,D1,Bit_RESET);}
- void SetD2(void)
- {GPIO_WriteBit(GPIOB,D2,Bit_SET);}
- void ResetD2(void)
- {GPIO_WriteBit(GPIOB,D2,Bit_RESET);}
- void SetD3(void)
- {GPIO_WriteBit(GPIOB,D3,Bit_SET);}
- void ResetD3(void)
- {GPIO_WriteBit(GPIOB,D3,Bit_RESET);}
- void SetD4(void)
- {GPIO_WriteBit(GPIOB,D4,Bit_SET);}
- void ResetD4(void)
- {GPIO_WriteBit(GPIOB,D4,Bit_RESET);}
- void SetD5(void)
- {GPIO_WriteBit(GPIOB,D5,Bit_SET);}
- void ResetD5(void)
- {GPIO_WriteBit(GPIOB,D5,Bit_RESET);}
- void SetD6(void)
- {GPIO_WriteBit(GPIOB,D6,Bit_SET);}
- void ResetD6(void)
- {GPIO_WriteBit(GPIOB,D6,Bit_RESET);}
- void SetD7(void)
- {GPIO_WriteBit(GPIOB,D7,Bit_SET);}
- void ResetD7(void)
- {GPIO_WriteBit(GPIOB,D7,Bit_RESET);}
- uint8_t ReadD0(void) {return (GPIO_ReadInputDataBit(GPIOC,D0) << 0);}
- uint8_t ReadD1(void) {return (GPIO_ReadInputDataBit(GPIOC,D1) << 1);}
- uint8_t ReadD2(void) {return (GPIO_ReadInputDataBit(GPIOB,D2) << 2);}
- uint8_t ReadD3(void) {return (GPIO_ReadInputDataBit(GPIOB,D3) << 3);}
- uint8_t ReadD4(void) {return (GPIO_ReadInputDataBit(GPIOB,D4) << 4);}
- uint8_t ReadD5(void) {return (GPIO_ReadInputDataBit(GPIOB,D5) << 5);}
- uint8_t ReadD6(void) {return (GPIO_ReadInputDataBit(GPIOB,D6) << 6);}
- uint8_t ReadD7(void) {return (GPIO_ReadInputDataBit(GPIOB,D7) << 7);}
- const pFunctionr wpl[8] = {ResetD0,ResetD1,ResetD2,ResetD3,ResetD4,ResetD5,ResetD6,ResetD7};
- const pFunctionr wph[8] = {SetD0,SetD1,SetD2,SetD3,SetD4,SetD5,SetD6,SetD7};
- pFunctionr wp[8] = {ResetD0,ResetD1,ResetD2,ResetD3,ResetD4,ResetD5,ResetD6,ResetD7};
- const pFunctionw rp[8] = {ReadD0,ReadD1,ReadD2,ReadD3,ReadD4,ReadD5,ReadD6,ReadD7};
- /* Private functions ---------------------------------------------------------*/
- uint8_t ReadDataBus(void)
- {
- uint8_t data = 0;
- uint8_t index = 0;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- GPIO_InitStructure.GPIO_Pin = D0 | D1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = D2 | D3 | D4 | D5| D6 | D7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- for (index = 0; index < 8;index++)
- {
- data |= rp[index]();
- }
- return data;
- }
- void WriteDataBus(uint8_t data)
- {
- uint8_t index = 0;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- GPIO_InitStructure.GPIO_Pin = D0 | D1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = D2 | D3 | D4 | D5| D6 | D7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- for(index = 0;index < 8;index++)
- {
- wp[index] = (0x01 << index) & data ? wph[index] : wpl[index];
- }
- for(index = 0;index < 8;index++)
- {
- wp[index]();
- }
- }
- uint8_t GetStatusBit()
- {
- uint8_t temp = 0;
-
- SET_CD;
- RESET_RD;
- SET_WR;
- RESET_CE;
- temp = ReadDataBus();
- SET_CE;
- SET_CD;
- SET_RD;
- SET_WR;
- return temp;
- }
- void WriteData(uint8_t data)
- {
- while((GetStatusBit() & OneByteTransRdy) != OneByteTransRdy);
- WriteDataBus(data);
- RESET_CD;
- SET_RD;
- RESET_WR;
- RESET_CE;
- RESET_CE;
- SET_CE;
- SET_CD;
- SET_RD;
- SET_WR;
- while((GetStatusBit() & OneByteTransRdy) != OneByteTransRdy);
- }
- void WriteCMD(uint8_t data)
- {
-
- while((GetStatusBit() & OneByteTransRdy) != OneByteTransRdy);
- WriteDataBus(data);
- SET_CD;
- SET_RD;
- RESET_WR;
- RESET_CE;
- RESET_CE;
- SET_CE;
- SET_CD;
- SET_RD;
- SET_WR;
- while((GetStatusBit() & OneByteTransRdy) != OneByteTransRdy);
- }
- void AutoReadData(uint8_t *p,uint16_t len)
- {
- uint8_t temp = 0;
- WriteCMD(SetAutoTransRead);
- while(len-- != 0)
- {
- while((GetStatusBit() & AutoReadTransRdy) != AutoReadTransRdy);
- RESET_CD;
- SET_WR;
- RESET_RD;
- RESET_CE;
- temp = ReadDataBus();
- *p++ = temp;;
- SET_CE;
- SET_CD;
- SET_RD;
- SET_WR;
- }
- while((GetStatusBit() & AutoReadTransRdy) != AutoReadTransRdy);
- WriteCMD(SetAutoTransEsc);
- }
- void AutoWriteData(uint8_t *p,uint16_t len)
- {
- WriteCMD(SetAutoTransWrite);
- while(len-- != 0)
- {
- while((GetStatusBit() & AutoWriteTransRdy) != AutoWriteTransRdy);
- WriteDataBus(*p++);
- RESET_CD;
- SET_RD;
- RESET_WR;
- RESET_CE;
- RESET_CE;
- SET_CE;
- SET_CD;
- SET_RD;
- SET_WR;
- }
- while((GetStatusBit() & AutoWriteTransRdy) != AutoWriteTransRdy);
- WriteCMD(SetAutoTransEsc);
- }
- void WriteOneByte(uint8_t data)
- {
-
- }
- void LCDZeroInit(void)
- {
- uint16_t index = 0;
- /* clr text buffer */
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = TextRamOffset >> 8;
- SetAddrPointer[0] = TextRamOffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- for (index = 0;index < TextDisRamSize;index++)
- TextDisBuf[index] = 0x00;
- AutoWriteData(TextDisBuf,TextDisRamSize);
- #ifdef LCD_USER_DEBUG
- /* set text buffer */
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = TextRamOffset >> 8;
- SetAddrPointer[0] = TextRamOffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- for (index = 0;index < TextDisRamSize;index++)
- TextDisBuf[index] = 0x01;
- AutoWriteData(TextDisBuf,TextDisRamSize);
- /* read text buffer */
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = TextRamOffset >> 8;
- SetAddrPointer[0] = TextRamOffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- for (index = 0;index < TextDisRamSize;index++)
- TextDisBuf[index] = 0x00;
- AutoReadData(TextDisBuf,TextDisRamSize);
- for (index = 0;index < TextDisRamSize;index++)
- if (TextDisBuf[index] != 0x01)
- while(1);
- #endif
- /* clr grap buffer */
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = GrapRamOffset >> 8;
- SetAddrPointer[0] = GrapRamOffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- for (index = 0;index < GrapDisRamSize;index++)
- GrapDisBuf[index] = 0x00;
- AutoWriteData(GrapDisBuf,GrapDisRamSize);
- #ifdef LCD_USER_DEBUG
- /* set grap buffer */
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = GrapRamOffset >> 8;
- SetAddrPointer[0] = GrapRamOffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- for (index = 0;index < GrapDisRamSize;index++)
- GrapDisBuf[index] = 0x81;
- AutoWriteData(GrapDisBuf,GrapDisRamSize);
-
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = GrapRamOffset >> 8;
- SetAddrPointer[0] = GrapRamOffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- for (index = 0;index < GrapDisRamSize;index++)
- GrapDisBuf[index] = 0x00;
- AutoReadData(GrapDisBuf,GrapDisRamSize);
- for (index = 0;index < GrapDisRamSize;index++)
- if (GrapDisBuf[index] != 0x81)
- while(1);
- #endif
- }
- void LCDInit(void)
- {
- uint32_t index = 0;
- for (index = 0;index < 500000;index++)
- RESET_RST;
- for (index = 0;index < 500000;index++)
- SET_RST;
- WriteData(SetTextRamOffset[0]);
- WriteData(SetTextRamOffset[1]);
- WriteCMD(SetTextRamOffset[2]);
-
- WriteData(SetTextRamProperty[0]);
- WriteData(SetTextRamProperty[1]);
- WriteCMD(SetTextRamProperty[2]);
-
- WriteData(SetGrapRamOffset[0]);
- WriteData(SetGrapRamOffset[1]);
- WriteCMD(SetGrapRamOffset[2]);
- WriteData(SetGrapRamProperty[0]);
- WriteData(SetGrapRamProperty[1]);
- WriteCMD(SetGrapRamProperty[2]);
-
-
- WriteCMD(SetSynthetizeRamModel);
- WriteCMD(SetDisplayShape);
- WriteCMD(SetCursorSetting);
-
- // SetCursorPointer[2] = CMDSetAddrPointer | __Cursor;
- // SetCursorPointer[1] = 0x00;//GrapRamOffset >> 8;
- // SetCursorPointer[0] = 0x00;//GrapRamOffset % 256;
- // WriteData(SetCursorPointer[0]);
- // WriteData(SetCursorPointer[1]);
- // WriteCMD(SetCursorPointer[2]);
-
- LCDZeroInit();
- }
- void ReadGrapRam(LCDData *data)
- {
- uint8_t col_index = 0;
- uint8_t row_index = 0;
- uint16_t index = 0;
-
- uint8_t src_bit = 0;
- uint8_t src_byte = 0;
-
- uint8_t des_bit = 0;
-
- for (row_index = 0;row_index < data->heigth;row_index++)
- {
- for (col_index = 0;col_index < data->width;col_index++)
- {
- index = (data->x_pixel + col_index) / 8 + (row_index + data->y_pixel) * HORIZONTAL_MODULE;
-
- src_byte = *(GrapDisBuf + index );
-
- src_bit = (src_byte & (0x80 >> ((data->x_pixel + col_index) % 8)))? 0x80 : 0x00;
-
- des_bit = src_bit >> (col_index % 8);
-
- *(data->data + (col_index + row_index * data->width) / 8) |= (uint8_t)des_bit;
- }
- }
- }
- void WriteGrapRam(LCDData *data)
- {
- uint8_t col_index = 0;
- uint8_t row_index = 0;
- uint16_t index = 0;
-
- uint8_t src_bit = 0;
- uint8_t src_byte = 0;
-
- uint8_t des_bit = 0;
- for (row_index = 0;row_index < data->heigth;row_index++)
- {
- for (col_index = 0;col_index < data->width;col_index++)
- {
- index = (data->x_pixel + col_index) / 8 + (row_index + data->y_pixel) * HORIZONTAL_MODULE;
-
- src_byte = *(data->data + (col_index + row_index * data->width) / 8 );
- src_bit = (src_byte & (0x80 >> ((col_index + row_index * data->width) % 8))) ? 0x80 : 0x00;
-
- des_bit = src_bit >> (data->x_pixel + col_index) % 8;
-
- GrapDisBuf[index] |= (uint8_t)des_bit;
-
- }
- }
-
- }
- void ClrGrapRam(LCDData *data)
- {
- uint8_t col_index = 0;
- uint8_t row_index = 0;
- uint16_t index = 0;
- /* clr grap ram */
- for (row_index = 0;row_index < data->heigth;row_index++)
- {
- index = (row_index + data->y_pixel) * HORIZONTAL_MODULE;
- GrapDisBuf[index + data->x_pixel / 8] &= (~(0xFF >> (data->x_pixel % 8)));
- GrapDisBuf[index + (data->x_pixel + data->width) / 8] &= (0xFF >> (data->x_pixel + data->width) % 8);
- for (col_index = data->x_pixel / 8 + 1;col_index < (data->x_pixel + data->width) / 8;col_index++)
- {
- GrapDisBuf[index + col_index] = 0;
- }
- }
- }
- void CopyClrGrapRam(LCDData *srcdata,LCDData* dstdata)
- {
-
-
- }
- void RefurbishLCD(LCDData *data)
- {
- uint8_t index = 0;
- uint8_t length = 0;
- uint32_t ramoffset = 0;
- uint32_t bufoffset = 0;
- for (index = 0;index < data->heigth;index++)
- {
- if ((data->x_pixel) % 8 == 0 && (data->width) % 8 == 0)
- {
- bufoffset = data->x_pixel / 8 + (data->y_pixel + index) * HORIZONTAL_MODULE;
- ramoffset = GrapRamOffset + bufoffset;
- length = data->width / 8;
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = ramoffset >> 8;
- SetAddrPointer[0] = ramoffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- AutoWriteData(GrapDisBuf + bufoffset,length);
- }
- else
- {
- bufoffset = ((data->x_pixel - (data->x_pixel % 8)) / 8) + (data->y_pixel + index) * HORIZONTAL_MODULE;
- ramoffset = GrapRamOffset + bufoffset;
- length = ((data->x_pixel + data->width) - ((data->x_pixel + data->width) % 8) + 8) / 8 - (data->x_pixel - (data->x_pixel % 8)) / 8;
- SetAddrPointer[2] = CMDSetAddrPointer | __DisBuf;
- SetAddrPointer[1] = ramoffset >> 8;
- SetAddrPointer[0] = ramoffset % 256;
- WriteData(SetAddrPointer[0]);
- WriteData(SetAddrPointer[1]);
- WriteCMD(SetAddrPointer[2]);
- AutoWriteData(GrapDisBuf + bufoffset,length);
- }
- }
- }
- #ifdef USER_DEBUG
- void CheckGrapRam(void)
- {
- LCDData __data;
- __data.x_pixel = 0;
- __data.y_pixel = 0;
- __data.width = 240;
- __data.heigth = 128;
- RefurbishLCD(&__data);
- }
- #endif
复制代码 |
|