📄 ili9320l_16bit.c
字号:
/*****************************************************************************
* Module for Microchip Graphics Library
* ILITEK ILI9320 LCD controller driver
* Landscape orientation in 16-bit mode
*****************************************************************************
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Juh醩z R骲ert 24 Dec 2007 Version 1.0 release
* John Leung 7 Dec 2007 Version 1.0 release
*****************************************************************************/
#include "Graphics.h"
// Color
WORD_VAL _color;
// Clipping region control
SHORT _clipRgn;
// Clipping region borders
SHORT _clipLeft;
SHORT _clipTop;
SHORT _clipRight;
SHORT _clipBottom;
/////////////////////// LOCAL FUNCTIONS PROTOTYPES ////////////////////////////
void SetReg(BYTE index, WORD value);
void PutImage1BPP(SHORT left, SHORT top, FLASH_BYTE* bitmap, BYTE stretch);
void PutImage4BPP(SHORT left, SHORT top, FLASH_BYTE* bitmap, BYTE stretch);
void PutImage8BPP(SHORT left, SHORT top, FLASH_BYTE* bitmap, BYTE stretch);
void PutImage16BPP(SHORT left, SHORT top, FLASH_BYTE* bitmap, BYTE stretch);
void PutImage1BPPExt(SHORT left, SHORT top, void* bitmap, BYTE stretch);
void PutImage4BPPExt(SHORT left, SHORT top, void* bitmap, BYTE stretch);
void PutImage8BPPExt(SHORT left, SHORT top, void* bitmap, BYTE stretch);
void PutImage16BPPExt(SHORT left, SHORT top, void* bitmap, BYTE stretch);
/*********************************************************************
* Function: void DelayMs(WORD time)
*
* PreCondition: none
*
* Input: time - delay in ms
*
* Output: none
*
* Side Effects: none
*
* Overview: delays execution on time specified in ms
*
* Note: delay is defined for 16MIPS
*
********************************************************************/
#define DELAY_1MS 16000/9
void DelayMs(WORD time){
unsigned delay;
while(time--)
for(delay=0; delay<DELAY_1MS; delay++);
}
/*********************************************************************
* Function: void SetReg(BYTE index, WORD value)
*
* PreCondition: none
*
* Input: index - register number
* value - value to be set
*
* Output: none
*
* Side Effects: none
*
* Overview: sets graphics controller register
*
* Note: none
*
********************************************************************/
void SetReg(BYTE index, WORD value){
SetIndex(index);
WriteData(((WORD_VAL)value).v[1],((WORD_VAL)value).v[0]);
}
/*********************************************************************
* Function: void ResetDevice()
*
* PreCondition: none
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: resets LCD, initializes PMP
*
* Note: none
*
********************************************************************/
void ResetDevice(void){
// Set LCD CS pin as output
CS_TRIS_BIT = 0;
// Disable LCD
CS_LAT_BIT = 1;
// Set reset pin as output
RST_TRIS_BIT = 0;
// Hold in reset
RST_LAT_BIT = 0;
// PMP setup
PMMODEbits.MODE = 0b10; // Master 2
PMMODEbits.WAITB = 0b00;
PMMODEbits.WAITM = 0b0001;
PMMODEbits.WAITE = 0b00;
PMAEN = 0x01FF; // Address line 8 is enabled for RS & PMA[7:0] for DATA LINES DB[7:0]
PMCONbits.PTRDEN = 1;
PMCONbits.PTWREN = 1;
PMCONbits.PMPEN = 1; //enable PMP module
// Reset controller
RST_LAT_BIT = 0;
DelayMs(2);
RST_LAT_BIT = 1;
DelayMs(2);
Nop();
Nop();
// Enable LCD
CS_LAT_BIT = 0;
/*** Setup display for ILI9320 driver IC ***/
SetReg(0xe5,0x8000); //start osc
SetReg(0x00,0x0001);
DelayMs(10);
SetReg(0xa4,0x0001); //calb
SetReg(0x07,0x0000); //display control(1)
DelayMs(10);
/*** Display Setting ***/
SetReg(0x01, 0x0100); //Driver output control (1)
SetReg(0x02, 0x0700); //LCD driving control
SetReg(0x03, 0x1028); //Entry mode //1030
SetReg(0x04, 0x0000); //Resize control
SetReg(0x08, 0x0202); //Display control (2)
SetReg(0x09, 0x0000); //Display control (3)
/*** Power Control ***/
SetReg(0x07, 0x0101); //power control 1 BT, AP
SetReg(0x17, 0x0001);
SetReg(0x10, 0x0000);
SetReg(0x11, 0x0007); //power control 2 DC, VC
SetReg(0x12, 0x0000); //power control 3 VRH
SetReg(0x13, 0x0000); //power control 4 VCOM amplitude
DelayMs(20);
SetReg(0x10, 0x16B0); //power control 1 BT,AP
SetReg(0x11, 0x0037); //power control 2 DC,VC
DelayMs(50);
SetReg(0x12, 0x013E); //power control 3 VRH
DelayMs(50);
SetReg(0x13, 0x1A00); //power control 4 vcom amplitude
SetReg(0x29, 0x000F); //power control 7 VCOMH
DelayMs(50);
SetReg(0x20, 0x0000); //Horizontal GRAM Address Set
SetReg(0x21, 0x0000); //Vertical GRAM Address Set
SetReg(0x50, 0x0000); //Horizontal Address Start Position
SetReg(0x51, 0x00EF); //Horizontal Address end Position (239)
SetReg(0x52, 0x0000); //Vertical Address Start Position
SetReg(0x53, 0x013F); //Vertical Address end Position (319)
SetReg(0x60, 0x2700); //Driver Output Control 2
SetReg(0x61, 0x0001); //Base Image Display Control
SetReg(0x6a, 0x0000); //Base Image Display Control
SetReg(0x90, 0x0010); //Panel Interface Control 1
SetReg(0x92, 0x0000); //Panel Interface Control 2
SetReg(0x93, 0x0000); //Panel Interface Control 3
/*** GAMMA Control ***/
SetReg(0x30, 0x0007);
SetReg(0x31, 0x0403);
SetReg(0x32, 0x0404);
SetReg(0x35, 0x0002);
SetReg(0x36, 0x0707);
SetReg(0x37, 0x0606);
SetReg(0x38, 0x0106);
SetReg(0x39, 0x0007);
SetReg(0x3c, 0x0700);
SetReg(0x3d, 0x0707);
SetReg(0x07, 0x0173);
// Disable LCD
CS_LAT_BIT = 1;
DelayMs(30);
}
/*********************************************************************
* Function: void PutPixel(SHORT x, SHORT y)
*
* PreCondition: none
*
* Input: x,y - pixel coordinates
*
* Output: none
*
* Side Effects: none
*
* Overview: puts pixel
*
* Note: none
*
********************************************************************/
void PutPixel(SHORT x, SHORT y){
DWORD_VAL address;
if(_clipRgn){
if(x<_clipLeft)
return;
if(x>_clipRight)
return;
if(y<_clipTop)
return;
if(y>_clipBottom)
return;
}
y = GetMaxY() - y;
CS_LAT_BIT = 0;
address.Val = (long)LINE_MEM_PITCH*x + y;
SetAddress(address.v[2],address.v[1],address.v[0]);
WriteData(_color.v[1],_color.v[0]);
CS_LAT_BIT = 1;
}
/*********************************************************************
* Function: WORD GetPixel(SHORT x, SHORT y)
*
* PreCondition: none
*
* Input: x,y - pixel coordinates
*
* Output: pixel color
*
* Side Effects: none
*
* Overview: return pixel color at x,y position
*
* Note: not finished yet!!!
*
********************************************************************/
WORD GetPixel(SHORT x, SHORT y){
DWORD_VAL address;
WORD_VAL result;
y = GetMaxY() - y;
address.Val = (long)LINE_MEM_PITCH*x + y;
CS_LAT_BIT = 0;
SetAddress(address.v[2],address.v[1],address.v[0]);
// Temporary change wait cycles for reading (250ns = 4 cycles)
PMMODE = 0x0210;
PMADDR = 0x0001;
// First RD cycle to move data from GRAM to Read Data Latch
result.v[1] = PMDIN1;
// For reading there's 4 wait cycles > 250ns /RD pulse.
// Only 4 instruction cycles are needed for PMP.
Nop(); Nop(); Nop(); Nop();
// Second RD cycle to move data from GRAM to Read Data Latch
result.v[1] = PMDIN1;
// For reading there's 4 wait cycles > 250ns /RD pulse.
// Only 4 instruction cycles are needed for PMP.
Nop(); Nop(); Nop(); Nop();
// First RD cycle to get data from Read Data Latch
// Read previous dummy value
result.v[1] = PMDIN1;
// For reading there's 4 wait cycles > 250ns /RD pulse.
// Only 4 instruction cycles are needed for PMP.
Nop(); Nop(); Nop(); Nop();
// Second RD cycle to get data from Read Data Latch
// Read MSB
result.v[1] = PMDIN1;
// For reading there's 4 wait cycles > 250ns /RD pulse.
// Only 4 instruction cycles are needed for PMP.
Nop(); Nop(); Nop();
// Disable LCD (it will not accept extra /RD pulse)
CS_LAT_BIT = 1;
// Read LSB
result.v[0] = PMDIN1;
// For reading there's 4 wait cycles > 250ns /RD pulse.
// Only 4 instruction cycles are needed for PMP.
Nop(); Nop(); Nop(); Nop();
// Restore wait cycles for writing (60ns = 1 cycle)
PMMODE = 0x0204;
return result.Val;
}
/*********************************************************************
* Function: void Bar(SHORT left, SHORT top, SHORT right, SHORT bottom)
*
* PreCondition: none
*
* Input: left,top - top left corner coordinates,
* right,bottom - bottom right corner coordinates
*
* Output: none
*
* Side Effects: none
*
* Overview: draws rectangle filled with current color
*
* Note: none
*
********************************************************************/
void Bar(SHORT left, SHORT top, SHORT right, SHORT bottom){
DWORD_VAL address;
register SHORT x,y;
if(_clipRgn){
if(left<_clipLeft)
left = _clipLeft;
if(right>_clipRight)
right= _clipRight;
if(top<_clipTop)
top = _clipTop;
if(bottom>_clipBottom)
bottom = _clipBottom;
}
top = GetMaxY() - top;
bottom = GetMaxY() - bottom;
address.Val = (DWORD)LINE_MEM_PITCH*left + top;
CS_LAT_BIT = 0;
for(y=bottom; y<top+1; y++){
SetAddress(address.v[2],address.v[1],address.v[0]);
for(x=left; x<right+1; x++){
WriteData(_color.v[1],_color.v[0]);
}
address.Val -= 1;
}
CS_LAT_BIT = 1;
}
/*********************************************************************
* Function: void ClearDevice(void)
*
* PreCondition: none
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -