/* p3_3.c: Initialize and display "hello" on the LCD using 4-bit data mode. * It uses delay to wait out the time LCD controller is busy. * * Data and control pins share Port B07-00. * This program does not poll the status of the LCD. * Timing is more relax than the HD44780 datasheet to accommodate the * variations among the LCD modules. * You may want to adjust the amount of delay to optimize performance. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a */ #include "samd21.h" #define RS 0x01 /* PB00 mask for Register Select*/ #define EN 0x04 /* PB02 mask for Enable */ void delayMs(int n); void LCD_nibble_write(unsigned char data, unsigned char control); void LCD_command(unsigned char command); void LCD_data(unsigned char data); void LCD_init(void); int main(void) { LCD_init(); for(;;) { LCD_command(1); /* clear display */ delayMs(500); LCD_command(0x80); /* set cursor at beginning of first line */ LCD_data('h'); /* write the word "hello" */ LCD_data('e'); LCD_data('l'); LCD_data('l'); LCD_data('o'); delayMs(500); } } void LCD_init(void) { REG_PORT_DIRSET1 = 0xFF; /* make PB07-00 pins output for data and controls */ delayMs(30); /* initialization sequence */ LCD_nibble_write(0x30, 0); delayMs(10); 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 after each char */ LCD_command(0x01); /* clear screen, move cursor to home */ LCD_command(0x0F); /* turn on display, cursor blinking */ } /* With 4-bit mode, each command or data is sent twice with the upper * nibble first then the lower nibble. */ void LCD_nibble_write(unsigned char data, unsigned char control) { data &= 0xF0; /* clear lower nibble for control */ control &= 0x0F; /* clear upper nibble for data */ REG_PORT_OUTCLR1 = 0xFF; /* clear data bus */ REG_PORT_OUTSET1 = data | control; /* RS = 0, R/W = 0 */ REG_PORT_OUTSET1 = EN; /* pulse E */ delayMs(0); REG_PORT_OUTCLR1 = EN; /* clear E */ } 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(4); /* commands 1, 2 need up to 1.64ms */ else delayMs(1); /* all others 40 us */ } void LCD_data(unsigned char data) { LCD_nibble_write(data & 0xF0, RS); /* upper nibble first */ LCD_nibble_write(data << 4, RS); /* then lower nibble */ delayMs(1); } /* millisecond delay based on 1 MHz system clock */ void delayMs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 199; i++) __asm("nop"); }