/********************************************************************* * * COG display (HD44780 compatible) * ********************************************************************* * FileName: LCD.h * Processor: PIC32 * Compiler: MPLAB C32 * * Developer: Bert De Deckere * * Software License Agreement: * * The software is owned by the Developer, and is protected under a * applicable copyright laws. All rights are reserved. * Any use in violation of the foregoing restrictions may subject the * user to criminal sanctions under applicable laws, as well as to * civil liability for the breach of the terms and conditions of this * license. * * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. * * Author Date Comment *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Bert De Deckere 25/07/2010 ********************************************************************/ #define RS _LATB2 #define DB7 _LATB7 #define DB6 _LATB6 #define DB5 _LATB5 #define DB4 _LATB4 #define E _LATB3 #define FOSC 80 // oscillator frequency (MHz) unsigned char Data, rs; void Delayms( unsigned int a) { a = (a * (FOSC*13)); // multiply with integer value to get 1ms delay while (a--) { } } void Delayus( unsigned int b) { b = (b * (FOSC/80)); while (b--) // 1us delay { } } void Transmit() { E = 1; Delayus(50); E = 0; } void LCDwrite(rs, Data) { RS = rs; Delayus(40); // small delay as specified in datasheet DB4 = (1 && (Data & 0b00010000)); // put the data on the output pins DB5 = (1 && (Data & 0b00100000)); DB6 = (1 && (Data & 0b01000000)); DB7 = (1 && (Data & 0b10000000)); Transmit(); // transmit data on falling edge DB4 = (1 && (Data & 0b00000001)); // put the data on the output pins DB5 = (1 && (Data & 0b00000010)); DB6 = (1 && (Data & 0b00000100)); DB7 = (1 && (Data & 0b00001000)); Transmit(); // transmit data on falling edge Delayus(10); } void LCDclear() { LCDwrite(0, 0x01); Delayms(2); } void LCDout(const char *s) { while (*s) { switch (*s) { case '\b': LCDwrite(0, 0x10); break; // backspace case '\f': LCDclear; break; // clear display case '\n': LCDwrite(0, 0xC0); break; // newline default : LCDwrite(1, *s); break; // write characters } *s++; } } void LCDshift(unsigned char LR, unsigned char i, unsigned int Delay) { for (i = i; i > 0; i--) { Delayms(Delay); if (LR == 1) {LCDwrite(0, 0b11100);} else {LCDwrite(0, 0b11000);} } } void LCDgoto(unsigned char x, unsigned char y) { y --; if(x == 1) {LCDwrite(0, 0x80 + y);} else if(x == 2) {LCDwrite(0, 0xC0 + y);} } void LCDinit() { RS = 0; // Put the outputs in a defined state E = 0; DB4 = 0; DB5 = 0; DB6 = 0; DB7 = 0; Delayms(30); // Wait for Vcc to stabilize LCDwrite(0, 0x02); LCDwrite(0, 0x28); // Set interface length LCDwrite(0, 0xC); // display on, cursor off, blink Off LCDclear(); // clear display LCDwrite(0, 0x6); // entry mode set }