/* Initialize LCD controller and flash HELLO on LCD. * * The LCD controller of Wytec EduPad board is connected to * a shift register which is connected to UCB0 in SPI mode. * P6.7 is used for slave select of the shift register. * * The connections between the shift register and LCD are * Bit 0 - RS (register select) * Bit 1 - E (enable) * Bit 2 - * Bit 4 - Data 4 * Bit 5 - Data 5 * Bit 6 - Data 6 * Bit 7 - Data 7 * * Built and tested with MSP432P401R Rev. C, Keil MDK-ARM v5.24a and MSP432P4xx_DFP v3.1.0 */ #include "msp.h" #define RS 1 // BIT0 mask for reg select #define EN 2 // BIT1 mask for E void delayMs(int n); void LCD_nibble_write(char data, unsigned char control); void LCD_command(unsigned char command); void LCD_data(char data); void LCD_init(void); void UCB0_Write(unsigned char data); int main(void) { // initialize LCD controller LCD_init(); while(1) { // Write "HELLO" on LCD LCD_data('H'); LCD_data('E'); LCD_data('L'); LCD_data('L'); LCD_data('O'); delayMs(1000); // clear LCD display LCD_command(1); delayMs(1000); } } // initialize UCB0 then initialize LCD controller void LCD_init(void) { EUSCI_B0->CTLW0 = 0x0001; EUSCI_B0->CTLW0 = 0xE9C1; EUSCI_B0->CTLW0 &= ~0x0001; P1->SEL0 |= 0x60; /* P1.5, P1.6 for UCB0 */ P1->SEL1 &= ~0x60; /* PP6.7 as slave select */ P6->DIR |= 0x80; /* P6.7 set as output */ P6->OUT |= 0x80; /* /SS idle high */ delayMs(20); // LCD controller reset sequence LCD_nibble_write(0x30, 0); delayMs(5); LCD_nibble_write(0x30, 0); delayMs(1); LCD_nibble_write(0x30, 0); delayMs(1); LCD_nibble_write(0x20, 0); // use 4-bit data mode delayMs(1); LCD_command(0x28); // set 4-bit data, 2-line, 5x7 font LCD_command(0x06); // move cursor right LCD_command(0x01); // clear screen, move cursor to home LCD_command(0x0F); // turn on display, cursor blinking } void UCB0_Write(unsigned char data) { P6->OUT &= ~0x80; // assert chip select EUSCI_B0->TXBUF = data; /* write data */ while(EUSCI_B0->STATW & 0x01) ; /* wait for transmit done */ P6->OUT |= 0x80; // deassert chip select } void LCD_nibble_write(char data, unsigned char control) { data &= 0xF0; // clear lower nibble for control control &= 0x0F; // clear upper nibble for data UCB0_Write (data | control); // RS = 0, R/W = 0 UCB0_Write (data | control | EN); // pulse E delayMs(0); UCB0_Write (data); } void LCD_command(unsigned char command) { LCD_nibble_write(command & 0xF0, 0); // upper nibble first LCD_nibble_write(command << 4, 0); // then lower nibble if (command < 4) delayMs(2); // command 1 and 2 needs up to 1.64ms else delayMs(1); // all others 40 us } void LCD_data(char data) { LCD_nibble_write(data & 0xF0, RS); // upper nibble first LCD_nibble_write(data << 4, RS); // then lower nibble delayMs(1); } /* system clock at 3 MHz */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* Delay */ }