#include #include /******************************************************** * * Program Title: Sample for 4 Bit Hitachi 44780 LCD Device * * Program File Name: LCD_4_bit.c * * Microprocessors A 17.383 and Microprocessors B 17.384 * * xxxxxxxx - Put in Semester (i.e. Spring 2010) here * * xxxxxxxx - Put in your name here * * xx/xx/xx - Put date here * ********************************************************/ /******************************************************** * Function: main * * Description: Sample for 4 Bit Hitachi 44780 LCD Device - Displays a counting value * * Notes: * * This Program Initializes Hitachi 44780 Based LCD in 4 Bit Mode * * The following Devices have been verified to work with this code (HI-TECH PICC Software) * * JHD 162A * * Pinout using the PIC16F684 * * RC3:RC0 - LCD I/O D7:D4 (Pins 14:11) * RC4 - LCD E Clocking Pin * RC5 - LCD R/S Pin * * Returns: This routine contains an infinite loop * ********************************************************/ __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \ & UNPROTECT & BORDIS & IESODIS & FCMDIS); int i, j, k, n; // Use Global Variables for Debug // 1234567890123456 //Template for 16 spaces " " #define E RC4 // Define the LCD Control Pins #define RS RC5 const int Twentyms = 1250; // Declare a Constant for 20 ms Delay const int Fivems = 300; const int TwoHundredus = 10; void LCDWrite(int LCDData, int RSValue); void Init_LCD(void); void Clear_LCD(void); void LCD_Display(char Display[16]); main() { char Display[16]; int count; PORTC = 0; // Start with Everything Low CMCON0 = 7; // Turn off Comparators ANSEL = 0; // Turn off ADC TRISC = 0; // All of PORTC are Outputs count = 0; Init_LCD(); //initial the LCD Display Clear_LCD(); // Clears the LCD screen while(1 == 1) // Loop Forever { Clear_LCD(); // Clears the LCD screen sprintf(Display,"The count is ..."); LCD_Display(Display); LCDWrite(0b11000000, 0); // Move Cursor to the Second Line sprintf(Display," %4d", count); LCD_Display(Display); NOP(); // Used for 10 ms Timing for (i = 0; i < 660; i++); // 10 ms Delay Loop NOP(); // Used for 10 ms Timing j = j + 1; // Increment the Counter? if (25 == j) // 1/4 Second Passed? { count++; // Increment the Counter j = 0; // Reset for another 1/4 Second } // End if } // End while } // End main // ************************************************************************ void LCDWrite(int LCDData, int RSValue) { PORTC = (LCDData >> 4) & 0x0F; // Get High 4 Bits for Output RS = RSValue; E = 1; E = 0; // Toggle the High 4 Bits Out PORTC = LCDData & 0x0F; // Get Low 4 Bits for Output RS = RSValue; E = 1; E = 0; // Toggle the Low 4 Bits Out if ((0 == (LCDData & 0xFC)) && (0 == RSValue)) n = Fivems; // Set Delay Interval else n = TwoHundredus; for (k = 0; k < n; k++); // Delay for Character } // End LCDWrite // ************************************************************************ void Init_LCD(void) { // Initialize LCD according to the Web Page j = Twentyms; for (i = 0; i < j; i++); // Wait for LCD to Power Up PORTC = 3; // Start Initialization Process E = 1; E = 0; // Send Reset Command j = Fivems; for (i = 0; i < j; i++); E = 1; E = 0; // Repeat Reset Command j = TwoHundredus; for (i = 0; i < j; i++); E = 1; E = 0; // Repeat Reset Command Third Time j = TwoHundredus; for (i = 0; i < j; i++); PORTC = 2; // Initialize LCD 4 Bit Mode E = 1; E = 0; j = TwoHundredus; for (i = 0; i < j; i++); LCDWrite(0b00101000, 0); // LCD is 4 Bit I/F, 2 Line LCDWrite(0b00000001, 0); // Clear LCD LCDWrite(0b00000110, 0); // Move Cursor After Each Character LCDWrite(0b00001110, 0); // Turn On LCD and Enable Cursor } // End Init_LCD // ************************************************************************ void Clear_LCD(void) { LCDWrite(0b00000001, 0); // Clear LCD } //End Clear_LCD // ************************************************************************ void LCD_Display(char Display[16]) { for (i = 0; Display[i] != 0; i++) LCDWrite(Display[i], 1); } // End LCD_Display // ************************************************************************