/*P12_1.c: Programming PCD8544 GLCD via SPI with FRDM-KL25Z * PTD1 pin as SPI SCK * PTD2 pin as SPI MOSI * PTC8 reset pin * PTD0 register select pin * PTC9 chip select */ #include "MKL25Z4.h" #define RESET 0x0100 /* PTC8 reset pin */ #define DC 0x0001 /* PTD0 register select pin */ #define CE 0x0200 /* PTC9 chip select */ /* define the pixel size of display */ #define GLCD_WIDTH 84 #define GLCD_HEIGHT 48 void GLCD_setCursor(unsigned char x, unsigned char y); void GLCD_clear(void); void GLCD_init(void); void GLCD_data_write(unsigned char data); void GLCD_command_write(unsigned char data); void SPI0_init(void); void SPI0_write(unsigned char data); void GLCD_putchar(int c); /* sample font table */ const char font_table[][6] = { {0x7e, 0x11, 0x11, 0x11, 0x7e, 0}, /* A */ {0x7f, 0x49, 0x49, 0x49, 0x36, 0}, /* B */ {0x3e, 0x41, 0x41, 0x41, 0x22, 0}}; /* C */ int main(void) { GLCD_init(); /* initialize the GLCD controller */ GLCD_clear(); /* clear display and home the cursor */ GLCD_putchar(0); /* display letter A */ GLCD_putchar(1); /* display letter B */ GLCD_putchar(2); /* display letter C */ while(1) { } } void GLCD_putchar(int c) { int i; for (i = 0; i < 6; i++) GLCD_data_write(font_table[c][i]); } void GLCD_setCursor(unsigned char x, unsigned char y) { GLCD_command_write(0x80 | x); /* column */ GLCD_command_write(0x40 | y); /* bank (8 rows per bank) */ } /* clears the GLCD by writing zeros to the entire screen */ void GLCD_clear(void) { int32_t index; for (index = 0 ; index < (GLCD_WIDTH * GLCD_HEIGHT / 8) ; index++) GLCD_data_write(0x00); GLCD_setCursor(0, 0); /*After we clear the display, return to the home position */ } void SPI0_init(void) { SIM->SCGC5 |= 0x1000; /* enable clock to Port D */ SIM->SCGC5 |= 0x0800; /* enable clock to Port C */ PORTD->PCR[1] = 0x200; /* make PTD1 pin as SPI SCK */ PORTD->PCR[2] = 0x200; /* make PTD2 pin as SPI MOSI */ PORTD->PCR[0] = 0x100; /* make PTD0 pin as DC */ PORTC->PCR[8] = 0x100; /* make PTC8 pin as RST */ PORTC->PCR[9] = 0x100; /* make PTC9 pin as CE */ PTD->PDDR |= 0x01; /* make PTD0 as output pin for DC */ PTC->PDDR |= 0x0200; /* make PTC9 as output pin for /CE */ PTC->PSOR = CE; /* deassert /CE */ PTC->PDDR |= 0x0100; /* make PTC8 as output pin for RESET */ PTC->PCOR = RESET; /* assert reset */ SIM->SCGC4 |= 0x400000; /* enable clock to SPI0 */ SPI0->C1 = 0x10; /* disable SPI and make SPI0 master */ SPI0->BR = 0x60; /* set Baud rate to 1 MHz */ SPI0->C1 |= 0x40; /* Enable SPI module */ } /* send the initialization commands to PCD8544 GLCD controller */ void GLCD_init(void) { SPI0_init(); /* hardware reset of GLCD controller */ PTC->PSOR = RESET; /* deassert reset */ GLCD_command_write(0x21); /* set extended command mode */ GLCD_command_write(0xB0); /* set LCD Vop for contrast */ GLCD_command_write(0x04); /* set temp coefficient */ GLCD_command_write(0x14); /* set LCD bias mode 1:48 */ GLCD_command_write(0x20); /* set normal command mode */ GLCD_command_write(0x0C); /* set display normal mode */ } /* write to GLCD controller data register */ void GLCD_data_write(unsigned char data) { /* select data register */ PTD->PSOR = DC; /* set DC */ /* send data via SSI */ SPI0_write(data); } /* write to GLCD controller command register */ void GLCD_command_write(unsigned char data) { /* select command register */ PTD->PCOR = DC; /* clear DC */ /* send data via SSI */ SPI0_write(data); } void SPI0_write(unsigned char data) { volatile char dummy; PTC->PCOR = CE; /* assert /CE */ while(!(SPI0->S & 0x20)) { } /* wait until tx ready */ SPI0->D = data; /* send register address */ while(!(SPI0->S & 0x80)) { } /* wait until tx complete */ dummy = SPI0->D; /* clear SPRF */ PTC->PSOR = CE; /* deasssert /CE */ }