Sei sulla pagina 1di 3

/* A simple program to drive a 2x16 LCD module

E. Skelton EI9GQ 2010 */

#define PIC18F4550
#include<string.h>

/* I/O pins for LCD RS and ENA lines */


#define RS 19
#define ENA 18

/* Function Prototypes */
void lcdinit(void); //Initialise LCD module
void line2(void); //Move to start of line 2
void home(void); //Move display cursor to home position
void clear(void); //Clear LCD display
void command(unsigned char lcd_comm); //Send command to LCD
void display(unsigned char lcd_data); //Send character to display
void display_string(char *lcd_message); // Send text string to display

/* Variables */
int x; // Every program should have one
char *lcd_message; // Pointer to the address of the String to send
to LCD

void setup()
{
/* Basic housekeeping. I/O pin configuration */
PORTE=0x00;
ADCON1=0x0F; // All pins are digital I/O
CMCON=0x07; // Comparators off. PORTA for digital I/O
TRISE=0x00; // All PORTE pins are outputs

PORTB=0x00;
TRISB=0x00; // All PORTB pins are outputs
}

//Pinguino main loop


void loop()
{
lcdinit(); // Initialise LCD
display_string("Hello World!"); // Message
while(1){} // Wait forever
}

// Initialise LCD module in 8 bit mode


void lcdinit(void){
delay(100); // 100ms delay after power-up

command(0x30); // Function set


delay(1); // Delay more than 39us

command(0x30); // Function set


delay(1); // Delay more than 39us

command(0x30); // Function set


delay(1); // Delay more than 39us
command(0x3C); // 1/16 duty cycle. 5x11 font
delay(1); // Delay more than 39us

command(0x0C); // Disp on, Blink off, Cursor off


delay(1); // Delay more than 39us

command(0x01); // Clear/Home
delay(2); // Delay more than 1.53ms

command(0x06); // Increment right


delay(2); // Delay more than 1.53ms
}

/* Move to start of line 2 */


void line2(void)
{
command(0xC0);
}

/* Clear display */
void clear(void)
{
command(0x01);
}

/* Move display cursor to home position */


void home(void)
{
command(0x02);
}

/* Send command to LCD display */


void command(unsigned char lcd_comm)
{
digitalWrite(RS,LOW); //RS command mode
PORTB=lcd_comm; //Command on PORTB
digitalWrite(ENA,HIGH); //Enable line high
delayMicroseconds(1); // Delay
digitalWrite(ENA,LOW); //Enable line low
delay(2); // Delay
digitalWrite(RS,HIGH); //RS display data
}

/* Send single character to LCD display */


void display(unsigned char lcd_data)
{
digitalWrite(RS,HIGH); //RS data (not command)
PORTB=lcd_data; //Character on PORTB
digitalWrite(ENA,HIGH); //Enable line high
delayMicroseconds(1); // Delay
digitalWrite(ENA,LOW); //Enable line low
delay(1); // Delay
}

/* Send text string to LCD display */


void display_string(char *lcd_message)
{
for(x=0;x<strlen(lcd_message);x++)
{
display(lcd_message[x]); // Display string characters one-by-one
if(x==15){line2();} // If the message is more than 16
characters, use line 2.
}
}

Potrebbero piacerti anche