Yet another CFAG320240CX-TFHTTS to HCS12 intialization problem
I have searched extensively through the forum for similar problems but cannot clearly detect my own problem still!
I've attempted initialization on the LCD screen in a handful of different ways, including following the indirect addressing initialization instructions as outlined on page 103 in Appendix C, following the C++ example as outlined in Appendix B, and following the example in the downloadable WinTest with no success.
Would someone please take a look through my code? I have checked my connections over and over and verified them.
On my 9S12, I am using PORT T to send commands and PORT A/D (as general purpose I/O) to send data to the LCD.
PORT T:
Bit 0: E
Bit 1: /CS
Bit 2: A0
Bit 3: /RES
and I have R/W hardwired low, and DISPOFF as N/C.
Everytime I load it up on my LCD, all I get are tiny bent lines on the top and bottom edges of the screen, randomly each time in different places along the edges. I can't write even one word, yet I am looking to write out a bitmap hopefully soon!
Any help is greatly appreciated. Thanks in advance!
I have searched extensively through the forum for similar problems but cannot clearly detect my own problem still!
I've attempted initialization on the LCD screen in a handful of different ways, including following the indirect addressing initialization instructions as outlined on page 103 in Appendix C, following the C++ example as outlined in Appendix B, and following the example in the downloadable WinTest with no success.
Would someone please take a look through my code? I have checked my connections over and over and verified them.
On my 9S12, I am using PORT T to send commands and PORT A/D (as general purpose I/O) to send data to the LCD.
PORT T:
Bit 0: E
Bit 1: /CS
Bit 2: A0
Bit 3: /RES
and I have R/W hardwired low, and DISPOFF as N/C.
Everytime I load it up on my LCD, all I get are tiny bent lines on the top and bottom edges of the screen, randomly each time in different places along the edges. I can't write even one word, yet I am looking to write out a bitmap hopefully soon!
Code:
//============================================================================
// Macros
#define CLEAR_E PORTT|=0x01
#define SET_E PORTT&=~0x01
#define CLEAR_CS PORTT|=0x02
#define SET_CS PORTT&=~0x02
#define SET_A0 PORTT|=0x04
#define CLEAR_A0 PORTT&=~0x04
#define CLEAR_RES PORTT|=0x08
#define SET_RES PORTT&=~0x08
//============================================================================
// Commonly used commands
#define Uchar unsigned char
#define Uint unsigned int
#define SYSTEM_SET 0x40
Uchar SystemSetArray[] = {
0x38, 0x87, 0x07, 0x3F, 0x49, 0x7F, 0x80, 0x00
};
#define SCROLL 0x44
#define SAD1 0x00
#define SAD2 0x10
#define SAD3 0x04
#define SAD4 0x30
Uchar ScrollArray[] = {
0x00, SAD1, 0x40, 0x00, SAD2, 0x40, 0x00, SAD3, 0x00, SAD4
};
#define HDOT_SCR 0x5A
#define OVLAY 0x5B
#define DISP_OFF 0x58
#define DISP_ON 0x59
#define CSRW 0x46
#define CSR_FORM 0x5D
#define CSR_DIR_R 0x4C
#define CSR_DIR_L 0x4D
#define CSR_DIR_U 0x4E
#define CSR_DIR_D 0x4F
#define MWRITE 0x42
#define MREAD 0x43
#define BUSY 0x40 // 0b0100 0000
#define PARA_P9 0x28 // 320 x 240
#define SLEEP_IN 0x53
//============================================================================
// Function prototypes
void delay(Uint);
void writeCommand(Uchar);
void writeData(Uchar);
void clearLayer1(void);
void clearLayer2(void);
void lcdInit(void);
//============================================================================
// Main
int main(void)
{
Uint i;
DDRAD = 0xFF; // Configure Port AD as output
DDRT = 0xFF; // Configure Port T as output
PTAD = 0x00;
PORTT = 0x00;
delay(6000); // Time for LCD to get ready
// Initialize LCD
lcdInit();
// Write to LCD; display something
writeCommand(MWRITE);
writeData(0x20); // Character: space
writeData(0x4A); // Character: J
writeData(0x65); // Character: e
writeData(0x72); // Character: r
writeData(0x6B); // Character: k
writeData(0x66); // Character: f
writeData(0x61); // Character: a
writeData(0x63); // Character: c
writeData(0x65); // Character: e
writeCommand(CSRW);
writeData(0x00); // Set cursor to start of second screen block
writeData(0x10);
writeCommand(CSR_DIR_D); // Set cursor shift direction to down
writeCommand(MWRITE);
for (i = 0; i < 9; i++)
writeData(0xFF); // Fill in a square to the left of the 'J'
return 0;
}
//============================================================================
// Functional specification
// Time delay in ticks
void delay(Uint ticks)
{
while(ticks != 0)
ticks--;
}
// COM_W
void writeCommand(Uchar CommandByte)
{
PTAD = CommandByte;
SET_A0;
CLEAR_CS;
//R/W is hardwired low
SET_E;
CLEAR_E;
SET_CS;
}
// DATA_W
void writeData(Uchar dataW)
{
PTAD = dataW;
CLEAR_A0;
CLEAR_CS;
//R/W is hardwired low
SET_E;
CLEAR_E;
SET_CS;
}
void clearLayer1(void)
{
Uint i;
Uchar c;
c = 'A';
// CALL POS1
writeCommand(CSRW);
writeData(0x00);
writeData(0x00);
// Clear loop
writeCommand(MWRITE);
CLEAR_A0;
CLEAR_CS;
for (i = 0; i < ((0x28) * 30); i++) // 30 * APL
{
PTAD = ' ';
if ('Z' < c)
c = 'A';
SET_E;
CLEAR_E;
}
SET_CS;
}
void clearLayer2(void)
{
Uint i;
// CALL POS2
writeCommand(CSRW);
writeData(0x60);
writeData(0x09);
// Clear loop
writeCommand(MWRITE);
CLEAR_A0;
CLEAR_CS;
for (i = 0; i < ((0x28) * 240); i++) // 240 * APL
{
PTAD = 0x00;
SET_E;
CLEAR_E;
}
SET_CS;
}
// Initialization
void lcdInit(void)
{
Uchar i;
// Idle the control lines & reset the display
CLEAR_RES;
CLEAR_E;
CLEAR_A0;
SET_CS;
PTAD = 0x00;
SET_RES;
writeCommand(SYSTEM_SET);
for (i = 0; i < 8; i++)
{
writeData(SystemSetArray[i]);
}
writeCommand(SCROLL); // Initialize screen block start addresses
for (i = 0; i < 10; i++)
{
writeData(ScrollArray[i]);
}
writeCommand(HDOT_SCR);
writeData(0x00); // Set horizontal pixel shift to zero
writeCommand(OVLAY);
writeData(0x01); // First & third screen blocks are text mode
writeCommand(DISP_OFF);
writeData(0x56); // Flash cursor at 2 Hz, and turn all screen blocks ON
clearLayer1(); // Fill first screen layer with space characters
clearLayer2(); // Fill second screen layer memory with blank data
writeCommand(CSRW);
writeData(0x00); // Set cursor to start of first screen block
writeData(0x00);
writeCommand(CSR_FORM);
writeData(0x04); // Horizontal cursor size = 5 pixels
writeData(0x86); // Vertical cursor size = 7 pixels
writeCommand(DISP_ON);
writeCommand(CSR_DIR_R); // Set cursor direction to right
}
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
Last edited: