Created
July 24, 2015 14:19
-
-
Save hayview/ca9b068625c28ca00685 to your computer and use it in GitHub Desktop.
hd44780 lcd routine with custom character function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DELAY_H_ | |
#define DELAY_H_ | |
#include <delays.h> | |
#define delay1MicroSecond() {Delay10TCYx(1); Delay1TCY(); Delay1TCY();} | |
#define delay5MicroSecond() Delay10TCYx(6) | |
#define delay10MicroSecond() Delay10TCYx(12) | |
#define delay50MicroSecond() Delay10TCYx(60) | |
#define delay100MicroSecond() Delay100TCYx(12) | |
#define delay200MicroSecond() Delay100TCYx(24) | |
#define delay1MiliSecond() Delay1KTCYx(12) | |
#define delay2MiliSecond() Delay1KTCYx(24) | |
#define delay5MiliSecond() Delay1KTCYx(60) | |
#define delay10MiliSecond() Delay10KTCYx(12) | |
#define delay50MiliSecond() Delay10KTCYx(60) | |
#define delay100MiliSecond() Delay10KTCYx(120) | |
#define delay200MiliSecond() Delay10KTCYx(240) | |
#define delay1Second() {Delay10KTCYx(240); Delay10KTCYx(240); Delay10KTCYx(240);\ | |
Delay10KTCYx(240); Delay10KTCYx(240);} | |
#define delay2Second() {delay1Second(); delay1Second();} | |
#define delay5Second() {delay2Second(); delay2Second(); delay1Second();} | |
#define delay10Second() {delay5Second(); delay5Second();} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hd44780.h" | |
#include "delay.h" | |
void HD44780_Clock(void); | |
void HD44780_WriteByte(char reg, char dat); | |
void HD44780_WriteData(char dat); | |
void HD44780_WriteCommand(char dat); | |
void HD44780_Clock(void) | |
{ | |
HD44780_EN_PIN=1; // starts data read | |
delay50MicroSecond(); | |
HD44780_EN_PIN=0; // starts data write | |
} | |
void HD44780_WriteByte(char reg, char dat) | |
{ | |
if(reg == HD44780_REGISTER_DATA) | |
{ | |
HD44780_RS_PIN=1; | |
} | |
if(reg == HD44780_REGISTER_COMMAND) | |
{ | |
HD44780_RS_PIN=0; | |
} | |
HD44780_EN_PIN=0; | |
HD44780_DATA_PORT = dat; | |
HD44780_Clock(); | |
} | |
void HD44780_WriteData(char dat) | |
{ | |
HD44780_WriteByte(HD44780_REGISTER_DATA, dat); | |
delay50MicroSecond(); | |
} | |
void HD44780_WriteCommand(char dat) | |
{ | |
HD44780_WriteByte(HD44780_REGISTER_COMMAND, dat); | |
delay2MiliSecond(); | |
} | |
void HD44780_Init(void) | |
{ | |
//Using initialize by instruction | |
//After power on Wait for LCD to Initialize | |
delay100MiliSecond(); | |
HD44780_initPinIO(); | |
HD44780_EN_PIN = 0; | |
HD44780_RS_PIN = 0; | |
HD44780_DATA_PORT = 0x30; | |
HD44780_Clock(); | |
delay5MiliSecond(); | |
HD44780_DATA_PORT = 0x30; | |
HD44780_Clock(); | |
delay200MicroSecond(); | |
HD44780_DATA_PORT = 0x30; | |
HD44780_Clock(); | |
delay200MicroSecond(); | |
// FUNCTION SET | 0 0 1 1 N F * * | | |
// BIN_OR_BIT_MASK: { 0 0 1 1 1 0 0 0 } | |
HD44780_WriteCommand(0x38); // N = 1; 2 lines, F = 0; 5x8 dots | |
delay5MiliSecond(); | |
// DISPLAY OFF | |
HD44780_WriteCommand(0x0C); | |
delay5MiliSecond(); | |
// DISPLAY CLEAR | |
HD44780_WriteCommand(0x01); | |
delay5MiliSecond(); | |
// ENTRY MODE SET | 0 0 0 0 0 1 I/D S | | |
// BIN_OR_BIT_MASK: { 0 0 0 0 0 1 1 0 } | |
HD44780_WriteCommand(0x06); // I/D = 1; Increment by 1, S = 0; No shift | |
delay5MiliSecond(); | |
// HOME POSITION | |
HD44780_WriteCommand(0x80); | |
} | |
void HD44780_WriteString(const rom char *s) | |
{ | |
while(*s) | |
{ | |
HD44780_WriteData(*s); | |
s++; | |
} | |
} | |
void HD44780_GoToPoint(char row, char col) | |
{ | |
char DDRAM_address; | |
if(row<1) row=1; // first position user-defined as (1,1) | |
if(col<=1)col=0; // but HD44780 defines as (0,0) | |
else col=col-1; | |
if(row==1) DDRAM_address = 0x80+col; // 0x80 for set DDRAM address command | |
if(row==2) DDRAM_address = 0x80+0x40+col; // 0x40 for second line DDRAM address | |
HD44780_WriteCommand(DDRAM_address); | |
} | |
void HD44780_AddCharacter(char addr, const rom char *pattern) | |
{ | |
char i; | |
// 0x40 for set CGRAM address command | |
HD44780_WriteCommand(0x40 + (addr<<3)); // bit3 to bit5 for CGRAM address | |
// bit0 to bit2 for character pattern line position | |
for(i=0;i<8;i++) | |
{ | |
HD44780_WriteData(pattern[i]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HD44780_H | |
#define HD44780_H | |
#include <p18cxxx.h> | |
#include "delay.h" | |
#define HD44780_REGISTER_COMMAND 0x10 | |
#define HD44780_REGISTER_DATA 0x01 | |
#define HD44780_initPinIO() {TRISBbits.RB4=0; TRISBbits.RB5=0; TRISD=0x00;} | |
#define HD44780_EN_PIN LATBbits.LATB5 | |
#define HD44780_RS_PIN LATBbits.LATB4 | |
#define HD44780_DATA_PORT LATD | |
void HD44780_Init(void); | |
void HD44780_WriteString(const rom char *s); | |
void HD44780_GoToPoint(char row, char col); | |
void HD44780_AddCharacter(char addr, const rom char *pattern); | |
void HD44780_WriteData(char dat); | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma config PLLDIV = 5 // (20 MHz crystal on PICDEM FS USB board) | |
#pragma config CPUDIV = OSC1_PLL2 | |
#pragma config USBDIV = 2 // Clock source from 96MHz PLL/2 | |
#pragma config FOSC = HSPLL_HS | |
#pragma config FCMEN = OFF | |
#pragma config IESO = OFF | |
#pragma config PWRT = OFF | |
#pragma config BOR = ON | |
#pragma config BORV = 3 | |
#pragma config VREGEN = ON //USB Voltage Regulator | |
#pragma config WDT = OFF | |
#pragma config WDTPS = 32768 | |
#pragma config MCLRE = ON | |
#pragma config LPT1OSC = OFF | |
#pragma config PBADEN = OFF | |
// #pragma config CCP2MX = ON | |
#pragma config STVREN = ON | |
#pragma config LVP = OFF | |
// #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming | |
#pragma config XINST = OFF // Extended Instruction Set | |
#pragma config CP0 = OFF | |
#pragma config CP1 = OFF | |
// #pragma config CP2 = OFF | |
// #pragma config CP3 = OFF | |
#pragma config CPB = OFF | |
// #pragma config CPD = OFF | |
#pragma config WRT0 = OFF | |
#pragma config WRT1 = OFF | |
// #pragma config WRT2 = OFF | |
// #pragma config WRT3 = OFF | |
#pragma config WRTB = OFF // Boot Block Write Protection | |
#pragma config WRTC = OFF | |
// #pragma config WRTD = OFF | |
#pragma config EBTR0 = OFF | |
#pragma config EBTR1 = OFF | |
// #pragma config EBTR2 = OFF | |
// #pragma config EBTR3 = OFF | |
#pragma config EBTRB = OFF | |
#include <p18f4553.h> | |
#include "hd44780.h" | |
#include "delay.h" | |
#define mInitAllLEDs() {TRISBbits.TRISB6=0; TRISBbits.TRISB7=0;} | |
#define mLED_1 LATBbits.LATB6 | |
#define mLED_2 LATBbits.LATB7 | |
rom char battery1[8] = { | |
0b01110, | |
0b01010, | |
0b10001, | |
0b10001, | |
0b10001, | |
0b10001, | |
0b10001, | |
0b11111 | |
}; | |
rom char battery2[8] = { | |
0b01110, | |
0b01010, | |
0b10001, | |
0b10001, | |
0b10001, | |
0b10001, | |
0b11111, | |
0b11111 | |
}; | |
rom char battery3[8] = { | |
0b01110, | |
0b01010, | |
0b10001, | |
0b10001, | |
0b10001, | |
0b11111, | |
0b11111, | |
0b11111 | |
}; | |
rom char battery4[8] = { | |
0b01110, | |
0b01010, | |
0b10001, | |
0b10001, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111 | |
}; | |
rom char battery5[8] = { | |
0b01110, | |
0b01010, | |
0b10001, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111 | |
}; | |
rom char battery6[8] = { | |
0b01110, | |
0b01010, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111 | |
}; | |
rom char battery7[8] = { | |
0b01110, | |
0b01110, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111, | |
0b11111 | |
}; | |
void main() | |
{ | |
mInitAllLEDs(); | |
delay1Second(); | |
mLED_1 = 1; | |
mLED_2 = 1; | |
HD44780_Init(); | |
mLED_1 = 0; | |
mLED_2 = 0; | |
HD44780_AddCharacter(0, battery1); | |
HD44780_AddCharacter(1, battery2); | |
HD44780_AddCharacter(2, battery3); | |
HD44780_AddCharacter(3, battery4); | |
HD44780_AddCharacter(4, battery5); | |
HD44780_AddCharacter(5, battery6); | |
HD44780_AddCharacter(6, battery7); | |
HD44780_GoToPoint(1,1); | |
HD44780_WriteString("Battery State"); | |
HD44780_GoToPoint(2,1); | |
HD44780_WriteData(0x00); | |
HD44780_WriteData(0x01); | |
HD44780_WriteData(0x02); | |
HD44780_WriteData(0x03); | |
HD44780_WriteData(0x04); | |
HD44780_WriteData(0x05); | |
HD44780_WriteData(0x06); | |
while(1) | |
{ | |
delay1Second(); | |
mLED_1 = 1; | |
mLED_2 = 0; | |
delay1Second(); | |
mLED_1 = 0; | |
mLED_2 = 1; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment