/* Initialize LCD controller and flash HELLO on LCD. * * The connections between the LCD controller of EduPad * board and the MSP432 LaunchPad are * * P3.6 - D4 - J2-11 * PA2 - D4 - P3.6 * P5.2 - D5 - J2-12 * PA3 - D5 - P5.2 * P5.0 - D6 - J2-13 * PA4 - D6 - P5.0 * P4.6 - D7 - J1-8 * PA5 - D7 - P6.4 * P3.0 - R/S - J2-18 * PE0 - R/S - P3.0 * P6.7 - EN - J4-35 * PC6 - EN - P6.7 * For simplicity, all delay below 1 ms uses 1 ms. * * 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 0x80 /* BIT7 mask for E */ #define BL 0x20 /* BIT5 mask for backlight */ 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 PORTS_init(void); 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 SSI2 then initialize LCD controller */ void LCD_init(void) { PORTS_init(); 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 PORTS_init(void) { P3->DIR |= 0x40; /* LCD D4-D7 */ P5->DIR |= 4; P5->DIR |= 1; P4->DIR |= 0x40; P3->DIR |= 1; /* P3_0 for LCD R/S */ P6->DIR |= 0x80; /* P6_7 for LCD EN */ } void LCD_nibble_write(char data, unsigned char control) { /* populate data bits */ if (data & 0x10) P3->OUT |= 0x40; else P3->OUT &= ~0x40; if (data & 0x20) P5->OUT |= 4; else P5->OUT &= ~4; if (data & 0x40) P5->OUT |= 1; else P5->OUT &= ~1; if (data & 0x80) P4->OUT |= 0x40; else P4->OUT &= ~0x40; /* set R/S bit */ if (control & RS) P3->OUT |= 1; else P3->OUT &= ~1; /* pulse E */ P6->OUT |= 0x80; delayMs(0); P6->OUT &= ~0x80; } 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, MSP432P401R Rev. C, Start-up v2.2.1 */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* Delay */ }