This is a piece of code for the BoostC compiler which I wrote recently. It drives the LCD on a EasyPIC4 board:
Code: Select all
#ifndef LCDDRIVER_H
#define LCDDRIVER_H
/** I N C L U D E S **********************************************************/
#include <system.h>
/** D E F I N I T I O N S ****************************************************/
volatile unsigned char LCD@LATD;
volatile bit RS@LATD.2;
volatile bit E@LATD.3;
#define LCDTris trisd
/** P R O T O T Y P E S ******************************************************/
//-- public prototypes -------------------------------------------------------/
void clearLCD ( void );
void sendLCDChar ( char c , char row , char position );
void sendLCDString ( char* str , char row , char position );
void initLCD ( void );
//-- private prototypes ------------------------------------------------------/
void sendLCD ( char data , char rs );
#endif
Code: Select all
/** I N C L U D E S **********************************************************/
#include "lcdDriver.h"
/** D E C L A R A T I O N S **************************************************/
/******************************************************************************
* Function: void clearLCD (void)
*
* Input: None
*
* Output: None
*
* Overview: Calls the function sendLCD to send the right command to
* clear the LCD.
*
*****************************************************************************/
void clearLCD (){
sendLCD ( 0x01 , 0 );
delay_ms(2);
}
/******************************************************************************
* Function: void sendLCD ( char data , char rs )
*
* Input: char data: the data to be send to the display
* char rs : '1' means character, '0' means command
*
* Output: None
*
* Overview: Sends a character or command to the display.
*
*****************************************************************************/
void sendLCD ( char data , char rs ){
LCD = data & 0xF0;
RS = rs;
E = 1;
E = 0;
delay_10us(4);
LCD = data << 4;
RS = rs;
E = 1;
E = 0;
delay_10us(4);
}
/******************************************************************************
* Function: void sendLCDChar ( char c , char row , char position )
*
* Input: char c : The character to be displayed
* char row : The row on which the character should be
* displayed.
* char position : The position where the character should be
* displayed.
*
* Output: None
*
* Overview: Puts a character on the display on a specified position
* using the function sendLCD
*
*****************************************************************************/
void sendLCDChar ( char c , char row , char position ){
// Check if given position is valid
if( row<0 || row>1 || position<0 || position>15 )
return;
if( row == 0 ){
position = 0x80 + position;
} else if( row == 1 ){
position = 0xC0 + position;
}
sendLCD ( position , 0 );
sendLCD ( c , 1 );
}
/******************************************************************************
* Function: void sendLCDString ( char* str , char row , char position )
*
* Input: char* str: Pointer to an array of characters which must be
* displayed. Last character should be 0 or '\n'.
* char row : Row on which the string of characters must be
* displayed.
* char position : Position where the string of characters
* must be displayed.
*
* Output: None
*
* Overview: Puts a string on the display on a specified position
* using the function sendLCD
*
*****************************************************************************/
void sendLCDString ( char* str , char row , char position ){
// Check if given position is valid
if( row<0 || row>1 || position<0 || position>15 )
return;
if( row == 0 ){
position = 0x80 + position;
} else if( row == 1 ){
position = 0xC0 + position;
}
int ptr = 0;
sendLCD ( position , 0 );
while (str[ptr] != 0 && str[ptr] != '\n')
sendLCD ( str[ptr++] , 1 );
}
/******************************************************************************
* Function: void initLCD (void)
*
* Input: None
*
* Output: None
*
* Overview: Initiates the LCD display by sending the necessary commands
* using the function sendLCD
*
*****************************************************************************/
void initLCD (){
LCDTris = 0;
char a;
delay_ms(20);
for ( a = 0 ; a < 3 ; a++ )
{
sendLCD ( 0x20 , 0 );
delay_ms(6);
}
sendLCD ( 0x28 , 0 );
sendLCD ( 0x01 , 0 );
delay_ms(2);
sendLCD ( 0x0C , 0 );
sendLCD ( 0x06 , 0 );
}
This should be very easy to translate to the MikroC compiler, so good luck with it !
Best regards,
RoDeNtJe