Hello,
mUbase wrote:
Now I just have to work out how it works in C and how I can assign the pins on the MCU port of the DEMOJM board and how to write to it etc.... hmmmm....
The key to these questions is the header file.
// File: LCD.h// Provides 4-bit interface with Hitachi HD44780 device./************************************************************************/#ifndef LCD_MACROS#define LCD_MACROS#include "derivative.h" // For 'byte' typedef// Macros for LCD usage.#define _NEW_COP /* Comment out if old COP timer with single write to SRS register. */#ifndef BUSFREQ /* BUSFREQ may already be defined (main.c). If so, #include this file after the definition. */#define BUSFREQ 4000000 // Default bus frequency in Hz#endif#define LCD_LINE_LEN 24 // Adjust to suit LCD display/************************************************************************//* Hardware connections for LCD (4-bit mode): (Alter to suit actual MCU port usage)PTB0-PTB3 = DB4-DB7 Data busPTB4 = RS Register selectPTB5 = R/W Read / WritePTB6 = E EnablePORTB 0b00000000 |||++++- data bus 0x0F ||+----- RS 0x10 |+------ R/W 0x20 +------- Enable 0x40*/#define LCD_PORT PTBD#define LCD_DDIR PTBDD#define LCD_DDMASK 0x7F // Data direction mask#define LCD_DMASK 0x0F // LCD data uses lower nybble#define LCD_RS PTBD_PTBD4#define LCD_RW PTBD_PTBD5#define LCD_E PTBD_PTBD6/************************************************************************/// LCD control codes:#define DCLR 0x01 // Clear display and home cursor#define HOME1 0x80 // Home cursor to start of line 1#define HOME2 0xC0 // Home cursor to start of line 2#define SHFTOFF 0x06 // Shift off#define SHFTON 0x07 // Shift on#define DOFF 0x08 // Display off#define CURSOFF 0x0C // Display on, cursor off#define CURSON 0x0E // Display on, steady cursor#define CURSBL 0x0F // Display on, blinking cursor#define CURSL 0x10 // Move cursor left#define CURSR 0x14 // Move cursor right#define SHFTL 0x18 // Shift display left#define SHFTR 0x1C // Shift display right#define SINGLE 0x20 // Single line mode & 4-bit I/F#define DUAL 0x28 // Dual line mode & 4-bit I/F// Cursor position macros:#define MOVL1(p) LCD_ctrl((p) + HOME1) // Set addr counter to line 1 position#define MOVL2(p) LCD_ctrl((p) + HOME2) // Set addr counter to line 2 position/************************************************************************/// Function prototypes:void LCD_init( void); // Initialise LCD module (4-bit mode)void LCD_ctrl( byte ch); // Send control byte to LCDvoid LCD_char( byte ch); // Send display character to LCDvoid LCD_str( const byte *sptr); // Send string data to LCDvoid LCD_partstr_L1( const byte *sptr); // Send partial string to L1 of LCDvoid LCD_partstr_L2( const byte *sptr); // Send partial string to L2 of LCD#endif
The hardware connections for the LCD are currently assumed to be via port B. However, another port may be assigned by altering the macros for LCD_PORT, etc. I have assumed that the control signals will occupy the same port as the data nybble. These could easily be assigned to different port(s), provided the data direction setting for these pins is handled elsewhere.
The operation of the Hitachi device is slow, and delays are required throughout the code for correct operation. These are minimum delay requirements - actual delays in excess of the minimum delay do not affect the display operation. Two factors will determine the delay calibration - the MCU bus frequency used, and the COP timer handling method employed within the MCU - so these need to be defined.
An additional thought - perhaps the default bus frequency should be altered to the maximum allowable bus frequency.
#define BUSFREQ 20000000 // 20MHz
Then if the actual bus frequency is not specified within main.c, the delays generated will always exceed the minimum requirement.
From the function prototype list, the first four functions are the usual basic ones. The last two functions may be useful for special applications that require display scrolling. The LCD_LINE_LEN macro is used by the latter functions only. There are also macros available to position the cursor. These are called similarly to a function.
For a definition of what each control code does, you will need to refer to the display datasheet, or the Hitachi datasheet.
Regards,
Mac