Выбрать главу

/***********************************************************************

                      READING FROM THE EEPROM

                      =========================

This program reads data from addresses 0 to 0x2F of the EEPROM and then

sends this data to PORTB of the microcontroller.

Programmer: Dogan Ibrahim

File:       EEPROM.C

Date:       May, 2007

************************************************************************/

void main() {

 unsigned int j;

 unsigned char Temp;

 TRISB = 0; // Configure PORTB as output

 for (j=0; j <= 0x2F; j++) {

  Temp = Eeprom_Read(j);

  PORTB = Temp;

  Delay_ms(20);

 }

}

Figure 4.18: Program to read from the EEPROM

4.3.2 LCD Library

One thing all microcontrollers lack is some kind of video display. A video display would make a microcontroller much more user-friendly, enabling text messages, graphics, and numeric values to be output in a more versatile manner than with 7-segment displays, LEDs, or alphanumeric displays. Standard video displays require complex interfaces and their cost is relatively high. LCDs are alphanumeric (or graphic) displays which are frequently used in microcontroller-based applications. These display devices come in different shapes and sizes. Some LCDs have forty or more character lengths with the capability to display several lines. Others can be programmed to display graphic images. Some modules offer color displays, while others incorporate backlighting so they can be viewed in dimly lit conditions.

There are basically two types of LCDs as far as the interfacing technique is concerned: parallel and serial. Parallel LCDs (e.g., the Hitachi HD44780 series) are connected to the microcontroller circuitry such that data is transferred to the LCD using more than one line, usually four or eight data lines. Serial LCDs are connected to a microcontroller using one data line only, and data is transferred using the RS232 asynchronous data communications protocol. Serial LCDs are generally much easier to work with but more costly than parallel ones. In this book only parallel LCDs are discussed, as they are used more often in microcontroller-based projects.

Low-level programming of a parallel LCD is usually a complex task and requires a good understanding of the internal operation of the LCD, including the timing diagrams. Fortunately, mikroC language provides functions for both text-based and graphic LCDs, simplifying the use of LCDs in PIC-microcontroller-based projects. The HD44780 controller is a common choice in LCD-based microcontroller applications. A brief description of this controller and information on some commercially available LCD modules follows.

The HD44780 LCD Controller

The HD44780 is one of the most popular LCD controllers, being used both in industrial and commercial applications and also by hobbyists. The module is monochrome and comes in different shapes and sizes. Modules with 8, 16, 20, 24, 32, and 40 characters are available. Depending on the model, the display provides a 14-pin or 16-pin connector for interfacing. Table 4.3 shows the pin configuration and pin functions of a typical 14-pin LCD.

Table 4.3: Pin configuration of the HD44780 LCD module

Pin no. Name Function
1 VSS Ground
2 VDD +ve supply
3 VEE Contrast
4 RS Register select
5 R/W Read/write
6 EN Enable
7 D0 Data bit 0
8 D1 Data bit 1
9 D2 Data bit 2
10 D3 Data bit 3
11 D4 Data bit 4
12 D5 Data bit 5
13 D6 Data bit 6
14 D7 Data bit 7

VSS is the 0V supply or ground. The VDD pin should be connected to the positive supply. Although the manufacturers specify a 5V DC supply, the modules usually work with as low as 3V or as high as 6V.

Pin 3 is named as VEE and is the contrast control pin. It is used to adjust the contrast of the display and should be connected to a DC supply. A potentiometer is usually connected to the power supply with its wiper arm connected to this pin and the other leg of the potentiometer connected to the ground. This way the voltage at the VEE pin, and hence the contrast of the display, can be adjusted as desired.

Pin 4 is the register select (RS) and when this pin is LOW, data transferred to the LCD is treated as commands. When RS is HIGH, character data can be transferred to and from the module.

Pin 5 is the read/write (R/W) pin. This pin is pulled LOW in order to write commands or character data to the LCD module. When this pin is HIGH, character data or status information can be read from the module.

Pin 6 is the enable (EN) pin, which is used to initiate the transfer of commands or data between the module and the microcontroller. When writing to the display, data is transferred only on the HIGH to LOW transition of this pin. When reading from the display, data becomes available after the LOW to HIGH transition of the enable pin, and this data remains valid as long as the enable pin is at logic HIGH.