/************************************** * Author: Nathan House * * Device: PIC18F4550 * * E-Mail: roboticsguy@roboticsguy.com * * Website: www.robotenthusiasts.com * * Date Modified: 07/27/2011 * **************************************/ #include // Always include the header file for the device /*************************** * Device configuration * ***************************/ #pragma config FOSC = HSPLL_HS // Using 20 MHz crystal with PLL #pragma config PLLDIV = 5 // Divide by 5 to provide the 96 MHz PLL with 4 MHz input #pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock #pragma config USBDIV = 2 // USB clock comes from 96 MHz PLL output / 2 #pragma config FCMEN = OFF // Disable Fail-Safe Clock Monitor #pragma config IESO = OFF // Disable Oscillator Switchover mode #pragma config PWRT = OFF // Disable Power-up timer #pragma config BOR = OFF // Disable Brown-out reset #pragma config VREGEN = ON // Use internal USB 3.3V voltage regulator #pragma config WDT = OFF // Disable Watchdog timer #pragma config MCLRE = ON // Enable MCLR Enable #pragma config LVP = OFF // Disable low voltage ICSP #pragma config ICPRT = OFF // Disable dedicated programming port (only on 44-pin devices) #pragma config CP0 = OFF // Disable code protection /*************************** * Defines * ***************************/ #define LCD_COMMAND 0 #define LCD_DATA 1 /** Note: When changing the I/O pins used on the PIC, be sure to change the corresponding TRIS bits in config() in addition to the below definitions. **/ // Only six I/O lines are needed in 4-bit mode. I put the lines into two groups of three // so that I could use 3-pin male header connectors to make plugging/unplugging them easy. #define LCD_RS LATBbits.LATB2 // High means data, low means command. (pin 10) #define LCD_EN LATBbits.LATB1 // Enable, works like the clock for transfers. (pin 9) #define LCD_DB7 LATDbits.LATD5 // MSb (pin 3) #define LCD_DB6 LATDbits.LATD6 // (pin 4) #define LCD_DB5 LATDbits.LATD7 // (pin 5) #define LCD_DB4 LATBbits.LATB0 // LSb in 4-bit mode (pin 8) /**************************** * Function declarations * ****************************/ void config(void); void LCD(void); void LCD_concat(unsigned char* str1, unsigned char* str2, unsigned char* result, int resultSize); void LCD_itoa(int num, unsigned char* dest); /************************************ * Variables / Data structures * ************************************/ union { struct { unsigned bit0: 1; unsigned bit1: 1; unsigned bit2: 1; unsigned bit3: 1; unsigned bit4: 1; unsigned bit5: 1; unsigned bit6: 1; unsigned bit7: 1; }; unsigned char LCD_dataByte; }LCD_dataBits; // Timer variables unsigned int timerValue = 0; unsigned char* timerValuePtr = (unsigned char*)&timerValue; // timerValuePtr is used to put TMRL & TMRH into timerValue. // These character arrays are where you put your data to be displayed - ONLY do so when the lcd is NOT updating unsigned char LCD_data[2][16] = { {0x0, 0x1, 0x2, ' ', 'B', 'o', 'w', ' ', 'T', 'i', 'e', 's', ' ', 0x0, 0x1, 0x2}, {0x3, 0x4, 0x5, ' ', 'A ', 'r ', 'e', ' ', 'C ', 'o ', 'o ', 'l ', ' ', 0x3, 0x4, 0x5} }; // Misc. char LCD_cursorPos = 0; // Keeps track of where we're writing data to char LCD_configComplete = 0; char LCD_linePos = 1; // Keeps track of the line we're writing data on char LCD_state = 0; char LCD_dataType = 0; // 1 = data, 0 = command char LCD_configurationState = 0; char LCD_doneUpdating = 1; // 1 = done updating, 0 = updating LCD int LCD_initialDelay = 703; // Initial delay of about 15ms to allow LCD time to start up // Initialization commands for HD44780 LCD (16x2, 4-bit mode) #define LCD_numConfigStates 5 unsigned char LCD_initializationData[LCD_numConfigStates] = { 0x2, // Function set: 4-bit mode 0x28, // Function set: 4-bit mode, 2 lines 0x1, // Clear Display: Clears the display & set cursor position to line 1 column 0 0x6, // Entry mode set: Cursor moves to the right 0xC, // Display control: Display on, cursor off, cursor blink off }; #define LCD_numCustomChars 6 int LCD_CGRAM_adressSet = 0; int LCD_customCharsConfigured = 0; unsigned char LCD_customChars[LCD_numCustomChars][8] = { { 0x00, 0x0C, 0x12, 0x11, 0x10, 0x10, 0x10, 0x10 }, { 0x00, 0x00, 0x00, 0x00, 0x11, 0x0E, 0x0E, 0x11 }, { 0x00, 0x06, 0x09, 0x11, 0x01, 0x01, 0x01, 0x01 }, { 0x11, 0x12, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x11, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 }, }; void main (void) { int i = 0; config(); // Configures I/O and timers while(1) // Program loop { if(LCD_doneUpdating) // we don't want to try to update the LCD when it's in the middle of updating.. { LCD_doneUpdating = 0; // Set this to zero to update LCD. Do not modify LCD strings while this is zero. } LCD(); // Execute every loop; Set LCD_doneUpdating = 0 (then leave it alone until it's set) to actually update the screen. } } void LCD_concat (unsigned char* str1, unsigned char* str2, unsigned char* result, int resultSize) // Concatenates second string to the end of the first string and stores in result { int str1End = 0; int concat_i = 0; while(*(str1 + str1End) != 0) { *(result + str1End) = *(str1 + str1End); // copy str1 over to result str1End++; // find the end of str1 (where we start appending str2) } for(concat_i = 0; *(str2 + concat_i) != 0 && (str1End + concat_i) < (resultSize-2); concat_i++) *(result + str1End + concat_i) = *(str2 + concat_i); } void LCD_itoa (int num, unsigned char* dest) // Converts integer number to a string and stores in passed array { int itoa_i = 0; int digits = 0; // Get the number of digits if(num >= 1000) digits = 4; else if(num >= 100) digits = 3; else if(num >= 10) digits = 2; else digits = 1; // Convert digits to ascii and put in dest array for(itoa_i = (digits-1); itoa_i >= 0; itoa_i--) { *(dest + itoa_i) = num%10 + '0'; // '0' + int = ascii representation of int num /= 10; // discard least significant digit } *(dest + digits) = 0; // Null terminate the string } void LCD (void) { if(LCD_doneUpdating) // No point in executing all the code below if there's nothing to update.. return; // Get the timer's value *timerValuePtr = TMR0L; *(timerValuePtr+1) = TMR0H; if(timerValue >= 47 + LCD_initialDelay) // 47 = 1ms (initialDelay is used to let the LCD start up before trying to send commands to it) { LCD_initialDelay = 0; LCD_EN = 0; // Should be low at initialization if(LCD_state == 0) // If data is not currently being written { if(LCD_configComplete == 0) // Only enter here during configuration { if(LCD_configurationState < LCD_numConfigStates) // Sends all the LCD configuration bytes { LCD_dataBits.LCD_dataByte = LCD_initializationData[LCD_configurationState]; LCD_dataType = LCD_COMMAND; LCD_state = 1; // Start the state machine LCD_configurationState++; } else if(LCD_customCharsConfigured < LCD_numCustomChars) { if(LCD_CGRAM_adressSet == 0) { LCD_CGRAM_adressSet = 1; LCD_dataType = LCD_COMMAND; LCD_dataBits.LCD_dataByte = 0x40; // Set CGRAM Address: 0 LCD_state = 1; // start the data transmission state machine } else if(LCD_cursorPos < 8) { LCD_dataType = LCD_DATA; LCD_dataBits.LCD_dataByte = LCD_customChars[LCD_customCharsConfigured][LCD_cursorPos]; LCD_state = 1; // Start the state machine LCD_cursorPos++; // Next loop, move on to the next byte in the array } else { LCD_cursorPos = 0; // Reset the cursor position to 0 LCD_customCharsConfigured++; } } else if(LCD_configurationState == LCD_numConfigStates) { LCD_configComplete = 1; // The LCD is now configured LCD_dataBits.LCD_dataByte = 0x1; LCD_dataType = LCD_COMMAND; LCD_state = 1; // Start the state machine } } else if(LCD_configComplete == 1 && LCD_state == 0) // Once the LCD has been configured, continuously update screen contents { if(LCD_cursorPos == 16) // If we're at the end of the string { if(LCD_linePos == 1) LCD_linePos = 2; else // else we're on line 2 { LCD_linePos = 1; LCD_doneUpdating = 1; } // Move the cursor back to the first position of the line if(LCD_linePos == 1) LCD_dataBits.LCD_dataByte = 0x80; // Set DDRAM address 0 else if(LCD_linePos == 2) LCD_dataBits.LCD_dataByte = 0xC0; // Set DDRAM address 40 hex, or 64 dec. (i.e. the first position on the second line.) LCD_cursorPos = 0; // The cursor will be back at the first position LCD_dataType = LCD_COMMAND; // high for text data or low for command LCD_state = 1; // Start the state machine } else { LCD_dataBits.LCD_dataByte = LCD_data[LCD_linePos-1][LCD_cursorPos]; LCD_dataType = LCD_DATA; LCD_state = 1; // Start the state machine LCD_cursorPos++; } } } // if LCD_state == 0 else if(LCD_state == 1) // E line should be low already { LCD_RS = LCD_dataType; // Set the Register Select line (data = 1, command = 0) LCD_EN = 1; // Set EN line high // Put most significant nibble on I/O pins LCD_DB4 = LCD_dataBits.bit4; LCD_DB5 = LCD_dataBits.bit5; LCD_DB6 = LCD_dataBits.bit6; LCD_DB7 = LCD_dataBits.bit7; LCD_state++; } else if(LCD_state == 2) { LCD_EN = 0; // Set E low to latch data LCD_state++; } else if(LCD_state == 3) { LCD_EN = 1; // Set E high so it can be set to low again to latch the second nibble. // Put least significant nibble on I/O pins LCD_DB4 = LCD_dataBits.bit0; LCD_DB5 = LCD_dataBits.bit1; LCD_DB6 = LCD_dataBits.bit2; LCD_DB7 = LCD_dataBits.bit3; LCD_state++; } else if(LCD_state == 4) { LCD_EN = 0; // Set E low to latch data. Remains low until another write sequence is started. LCD_state = 0; } TMR0H = 0; TMR0L = 0; } } void config (void) // configures I/O and timers { // Interrupt control register configuration (disable interrupts) INTCONbits.GIEH = 0; INTCONbits.TMR0IE = 0; // Timer0 configuration T0CONbits.TMR0ON = 0; // Stop the timer T0CONbits.T08BIT = 0; // Run in 16-bit mode T0CONbits.T0CS = 0; // Use system clock to increment timer T0CONbits.PSA = 0; // A prescaler is assigned for Timer0 T0CONbits.T0PS2 = 1; // Use a 1:256 prescaler T0CONbits.T0PS1 = 1; T0CONbits.T0PS0 = 1; INTCONbits.TMR0IF = 0; // Clear the timer interrupt flag T0CONbits.TMR0ON = 1; // Start the timer // Pin TRIS configurations TRISDbits.TRISD4 = 0; // LED output TRISDbits.TRISD5 = 0; // output TRISDbits.TRISD6 = 0; // output TRISDbits.TRISD7 = 0; // output TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output TRISBbits.TRISB2 = 0; // output }