Skip to content

Instantly share code, notes, and snippets.

@hayview
Created July 10, 2015 14:41
Show Gist options
  • Save hayview/ea53e546a79bc79c3a38 to your computer and use it in GitHub Desktop.
Save hayview/ea53e546a79bc79c3a38 to your computer and use it in GitHub Desktop.
#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
#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);
}
#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);
#endif
#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
void main()
{
mInitAllLEDs();
delay1Second();
mLED_1 = 1;
mLED_2 = 1;
HD44780_Init();
mLED_1 = 0;
mLED_2 = 0;
HD44780_GoToPoint(1,1);
HD44780_WriteString("A");
HD44780_GoToPoint(1,9);
HD44780_WriteString("B");
HD44780_GoToPoint(2,1);
HD44780_WriteString("C");
HD44780_GoToPoint(2,9);
HD44780_WriteString("D");
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