Device = 18F4520
Clock = 20
// some LCD options...
#option LCD_DATA = PORTD.4 // Assign the LCD connections
#option LCD_EN = PORTD.3 //
#option LCD_RS = PORTD.2 //
// import LCD library...
Include "LCD.bas"
Include "convert.bas"
Dim Variable As Word
// Start Of Program...
DelayMS(150) // Let the LCD warm up
LCD.Cls // Clear the LCD screen
LCD.WriteAt(1,1,"Hello World") // Send some text to the LCD
Variable = 0 // Clear the "Variable" register
While True
Inc(Variable) // Increment the "Var1" register
// Convert to a string,
// and always display 5 characters
LCD.WriteAt(2,1,Convert.DecToStr(Variable,5))
DelayMS(1000) // Delay for 1 second
Wend
[code/]
#include <stdio.h>
#include <pic.h>
#include <htc.h>
#include "lcd_prog.c"
__CONFIG(INTIO & WDTDIS & PWRTEN & BORDIS);
void init (void);
/*******************************************/
// MAIN - LCD COMMUNICATION
/*******************************************/
void main (void)
{
char i;
init();
init_display();
char txt[]="PUT_TEXT_HERE";
for(i=0;i<15;i++){
print_data(txt[i]);
if(i==8){
print_instr(0xC0);
}
}
}
/*******************************************/
// INIT - RUN AT STARTUP
/*******************************************/
void init (void)
{
// Init values
PORTC=0;
PORTA=0; // RA0 is output
ANSEL=0;
// Port directions, PIN # is on LCD
TRISC0 = 0; //-> PIN 11 (DATA, DB4)
TRISC1 = 0; //-> PIN 12 (DATA, DB5)
TRISC2 = 0; //-> PIN 13 (DATA, DB6)
TRISC3 = 0; //-> PIN 14 (DATA, DB7)
TRISC4 = 0; //-> PIN 6 (E)
TRISC5 = 0; //-> PIN 4 (RS)
TRISA0 = 0; //-> PIN 5 (R/W) or connect to ground
}
// lcd_prog.c
/*******************************************/
// Prototypes
/*******************************************/
void init_display(void);
void print_data(char data);
void print_instr(char instr);
void E(void);
void delay(void);
void long_delay(void);
/*******************************************/
// Definitions
/*******************************************/
#define CLEAR 0x01
/*******************************************/
// Function that initiate the display
/*******************************************/
void init_display(void)
{
// See the datasheet flow chart for the procedure below
// This part initiates the LCD in 4-bit mode...
long_delay();
long_delay();
PORTC=0x03;
E();
PORTC=0x03;
E();
PORTC=0x03;
E();
PORTC=0x02;
E();
// Set LCD properties...
print_instr(0x28); // 2: Dual line (even though it is single line)
print_instr(0x0F); // 3: Display on, cursor, blink
print_instr(CLEAR); // 4: Clear
print_instr(0x06); // 5: Entry mode set
}
// LCD is now initialized...
/*******************************************/
// Print chars to LCD with 4-bit method
/*******************************************/
void print_data(char data)
{
PORTC = (data>>4) & 0x0F;
RC5=1; // RS
E();
PORTC = data & 0x0F;
RC5=1; // RS
E();
}
/*******************************************/
// Print instruction to LCD with 4-bit method
/*******************************************/
void print_instr(char instr)
{
PORTC = (instr >> 4) & 0x0F;
E();
PORTC = instr & 0x0F;
E();
}
/*******************************************/
// Toggle E to execute command/instruction
/*******************************************/
void E(void)
{
delay();
RC4=0;
delay();
RC4=1;
delay();
RC4=0;
delay();
}
/*******************************************/
// short delay
/*******************************************/
void delay(void)
{
unsigned int del;
for(del=0;del<2000;del++){;}
}
/*******************************************/
// long delay, used at init...
/*******************************************/
void long_delay(void)
{
unsigned int j;
for(j=0;j<60000;j++){;}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?