Author: Dogan Ibrahim
Date: July 2007
File: SEVEN5.C
************************************************/
#define DIGIT1 PORTB.F0
#define DIGIT2 PORTB.F1
unsigned char Cnt = 0;
unsigned char Flag = 0;
//
// This function finds the bit pattern to be sent to the port to display a number
// on the 7-segment LED. The number is passed in the argument list of the function.
//
unsigned char Display(unsigned char no) {
unsigned char Pattern;
unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F};
Pattern = SEGMENT[no]; // Pattern to return
return (Pattern);
}
//
// TMR0 timer interrupt service routine. The program jumps to the
// ISR at every 5ms.
//
void interrupt() {
unsigned char Msd, Lsd;
TMR0L = 100; // Re-load TMR0
INTCON = 0x20; // Set T0IE and clear T0IF
Flag = ~Flag; // Toggle Flag
if (Flag == 0) // Do digit 1
{
DIGIT2 = 0;
Msd = Cnt / 10; // MSD digit
if (Msd != 0) {
PORTC = Display(Msd); // Send to PORTC
DIGIT1 = 1; // Enable digit 1
}
} else { // Do digit 2
DIGIT1 = 0; // Disable digit 1
Lsd = Cnt % 10; // LSD digit
PORTC = Display(Lsd); // Send to PORTC
DIGIT2 = 1; // Enable digit 2
}
}
//
// Start of MAIN Program. configure PORTB and PORTC as outputs.
// In addition, configure TMR0 to interrupt at every 10ms
//
void main() {
TRISC = 0; // PORTC are outputs
TRISB = 0; // RB0, RB1 are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
//
// Configure TMR0 timer interrupt
//
T0CON = 0xC4; // Prescaler = 32
TMR0L = 100; // Load TMR0 with 100
INTCON = 0xA0; // Enable TMR0 interrupt
Delay_ms(1000);
for(;;) // Endless loop
{
Cnt++; // Increment Cnt
if (Cnt == 100) Cnt = 0; // Count between 0 and 99
Delay_ms(1000); // Wait 1 second
}
}
Figure 6.37: Modified program
PROJECT 6.8 — Voltmeter with LCD Display
In this project a voltmeter with LCD display is designed. The voltmeter can be used to measure voltages 0–5V. The voltage to be measured is applied to one of the analog inputs of a PIC18F452-type microcontroller. The microcontroller reads the analog voltage, converts it into digital, and then displays it on an LCD.
In microcontroller systems the output of a measured variable is usually displayed using LEDs, 7-segment displays, or LCD displays. LCDs make it possible to display alphanumeric or graphical data. Some LCDs have forty or more character lengths with the capability to display several lines. Other LCD displays can be used to display graphics 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 interface technique is concerned: parallel and serial. Parallel LCDs (e.g., Hitachi HD44780) are connected to a microcontroller by more than one data line and the data is transferred in parallel form. Both four and eight data lines are commonly used. A four-wire connection saves I/O pins but is slower since the data is transferred in two stages. Serial LCDs are connected to the microcontroller by only one data line, and data is usually sent to the LCD using the standard RS-232 asynchronous data communication protocol. Serial LCDs are much easier to use, but they cost more than the parallel ones.
The programming of a parallel LCD is a complex task and requires a good understanding of the internal operation of the LCD controllers, including the timing diagrams. Fortunately, the mikroC language provides special library commands for displaying data on alphanumeric as well as graphic LCDs. All the user has to do is connect the LCD to the microcontroller, define the LCD connection in the software, and then send special commands to display data on the LCD.
The HD44780 is one of the most popular alphanumeric LCD modules and is used both in industry and by hobbyists. This module is monochrome and comes in different sizes.
Table 6.10: Pin configuration of HD44780 LCD module
Pin no. |
Name |
Function |
1 |
V SS |
Ground |
2 |
V DD |
+ ve supply |
3 |
V EE |
Contrast |
4 |
RS |
Register select |
5 |
R/W |
Read/write |
6 |
E |
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 |
Modules with 8, 16, 20, 24, 32, and 40 columns are available. Depending on the model chosen, the number of rows may be 1, 2, or 4. The display provides a 14-pin (or 16-pin) connector to a microcontroller. Table 6.10 gives the pin configuration and pin functions of a 14-pin LCD module. The following is a summary of the pin functions:
V SSis the 0V supply or ground. The V DDpin should be connected to the positive supply. Although the manufacturers specify a 5V DC supply, the modules will usually work with as low as 3V or as high as 6V.
Pin 3, named V EE, is the contrast control pin. This pin is used to adjust the contrast of the display and should be connected to a variable voltage supply. A potentiometer is normally connected between the power supply lines with its wiper arm connected to this pin so that the contrast can be adjusted.
Pin 4 is the register select (RS), and when this pin is LOW, data transferred to the display 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) line. 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 (E) 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 line. 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.
Pins 7 to 14 are the eight data bus lines (D0 to D7). Data can be transferred between the microcontroller and the LCD module using either a single 8-bit byte or as two 4-bit nibbles. In the latter case, only the upper four data lines (D4 to D7) are used. The 4-bit mode means that four fewer I/O lines are used to communicate with the LCD. In this book we are using only an alphanumeric-based LCD and only the 4-bit interface.
Читать дальше