A microcontroller communicates with the outside world using its input–output ports. These ports can be analog or digital. Analog ports are used to read data from analog devices, e.g. the voltage across a resistor. Digital ports are used to read data from digital devices, e.g. the state of a switch. Microcontroller outputs are usually connected to LEDs, LCDs, buzzers, seven- segment displays and similar devices. The input can be a push-button switch, a keyboard or a similar device.
PIC microcontroller output ports can source and sink 25 mA of current. When an output port is sourcing current, the current flows out of the port pin. Similarly, when an output port is sinking current, the current flows towards the output pin. Devices such as LEDs, LCDs and other small devices which operate with up to 25 mA can be connected to the microcontroller output ports. Devices which require larger currents can be connected to the microcontroller ports by using a transistor switch to increase the current capability of the port pin.
4.23.1 Connecting an LED
Most LEDs operate with about 2V and they draw about 10 mA from the power supply. An LED can be connected to the output port of the PIC microcontroller by using a series current limiting resistor, as shown in Figure 4.3.
Assuming the output voltage Voof the port pin is+5 V when it is at logic 1, the value of the resistor can be calculated as
R= Vo−2
10 m A =5V −2V
10 m A ≈330. Example 4.6
An LED is connected to bit 0 of port B (i.e. pin RB0) of a PIC16F84 microcontroller. Write a program to flash the LED at 100 ms intervals. The hardware set-up is shown in Figure 4.4, where the microcontroller is operated with a 4 MHz crystal.
2V Vo Output port
PIC Microcontroller
LED 10mA R
Figure 4.3 Connecting an LED
+5V 14
6 330
LED RB0
VDD 4 MCLR 4.7K
PIC 16F84
OSC1 OSC2
Vss 5
16
C1 C2
22pF 4MHZ 22pF
15
Figure 4.4 Hardware setup for the Example 4.6
Solution
The required program is given in Figure 4.5. At the beginning of the program port B is configured as an output port. The LED is then turned on by sending a logic 1 to the port pin (RB0=1). After 100 ms delay the LED is turned off (RB =0) and this cycle is repeated forever.
PIC MICROCONTROLLER INPUT–OUTPUT INTERFACE 109
//*******************************************************************
//
// LED FLASHING PROGRAM // =====================
//
// Author: D. Ibrahim // Date: May, 2005 // File: FLASH.C //
// This program flashes an LED conencted to port RB0 of a PIC16F84 // microcontroller.
// The microcontroller is operated with a 4MHz crystal.
//
//******************************************************************
#include <pic.h>
#include <delay.c>
//
// Main Program // ************
//
main(void) {
TRISB = 0; /* PORT B is output */
for(;;) /* Do FOREVER */
{
RB0 =1; /* Turn ON LED */
DelayMs(100); /* 100ms delay */
RB0 = 0; /* Turn OFF LED */
DelayMs(100); /* 100ms delay */
} }
Figure 4.5 Program to flash the LED 4.23.2 Connecting a push-button switch
A push-button switch is an input to the microcontroller. The simplest way to connect a switch is to connect the switch to one of the port pins of the microcontroller and pull up the port pin to the supply voltage+V using a resistor. The port pin is thus normally at logic 1 level. When the switch is pressed the port pin can be shorted to ground to make the input go to logic 0. An example is given below.
Example 4.7
An LED is connected to bit 0 of port B (i.e. pin RB0) of a PIC16F84 microcontroller. Also, a push-button switch is connected to bit 7 of port B (i.e. pin RB7) using a resistor. Write a program which will turn ON the LED when the switch is pressed. The hardware set-up is shown in Figure 4.6.
Solution
The required program is given in Figure 4.7. At the beginning of the program port pin RB0 is configured as an output and port pin RB7 is configured as an input (bit pattern
+5V
+5V
14
6 330
RB0
LED VDD
MCLR
Push-button Switch
R
PIC 16F84
OSC2 OSC1
C1 C2
22pF 22pF 4MHZ
Vss 5
16 15
4.7K 4
Figure 4.6 Hardware setup for Example 4.7
//*******************************************************************
//
// PUSH-BUTTON AND LED PROGRAM // ============================
//
// Author: D. Ibrahim // Date: May, 2005 // File: BUTTON.C //
// This program turns ON an LED when a push-button switch is pressed.
// The LED is conencted to port pin RB0 and the switch is connected // RB7. The microcontroller is operated with a 4MHz crystal.
//
//*******************************************************************
#include <pic.h>
//
// Main Program // ************
//
main(void) {
TRISB = 0x80; /* RB0 is output, RB7 is input */
RB0 = 0; /* Make sure the LED is OFF when started */
while(RB7 == 1); /* Wait until switch is pressed */
RB0 = 1; /* Turn ON LED */
}
Figure 4.7 Program for Example 4.7
PIC MICROCONTROLLER INPUT–OUTPUT INTERFACE 111 10000000=0×80 is sent to TRISB register). The state of the switch is then checked contin- uously and as soon as the switch is pressed the LED is turned on.
4.23.3 Connecting an LCD
LCD displays are commonly used in microcontroller based systems to display the value of a variable, to prompt the user for data, or to give information to the user. LCDs can either be text based or graphical. Text based LCDs are used in most microcontroller applications. These LCDs are easier to program and their costs are much lower than graphical displays.
One of the most popular LCD displays is based on a controller known as the HD44780.
There are several models of LCDs using this controller:
LM016L 2 rows×16 characters per row LM017L 2 rows×20 characters per row LM018L 2 rows×40 characters per row LM044L 4 rows×20 characters per row
The programming of an LCD is generally a complex task and the programmer needs to know the internal operations of the LCD controller. Fortunately, the PICC language supports the HD44780 type LCDs and any data can easily be displayed on an LCD using simple function calls. The following functions are available:
lcd init initialize the LCD
lcd clear clear the LCD and home the cursor lcd goto go to the specified cursor position lcd write send a character to the LCD lcd puts send a text string to the LCD
HD44780 type LCDs normally have 14 pins. Table 4.2 shows the pin numbers and the function of each pin. Pin 3 is used to control the contrast of the display. Typically this pin is connected to the supply voltage using a potentiometer, and the contrast is changed by moving the arm of the potentiometer. The RS pin is used to send a control message or a text message to the LCD. When the R/W pin is at logic 0, a command or a text message can be sent to the LCD, and this is the normal operating mode. When R/W is at logic 1, the LCD status can be read.
The LCD is enabled when the E pin is at logic 0. Pins D0 to D7 are the data inputs. The LCD can either be used in full 8-bit mode, or in 4-bit half mode where only the upper four data pins are used. In most applications the 4-bit mode is selected since it uses fewer pins and frees the microcontroller input–output pins. The PICC language configures the LCD in 4-bit mode.
In order to use the above LCD functions, an LCD must be connected in a certain way to the microcontroller port pins. The default connection is:
Port pin LCD pin
RB0 D4
RB1 D5
RB2 D6
RB3 D7
RA2 RS
RA3 E
Table 4.2 CD pin configuration
Pin no. Name Function
1 Vss Ground
2 Vdd +V supply
3 Vee Contrast control
4 RS Select
5 R/W Read/write
6 E Enable
7 D0 Data 0
8 D1 Data 1
9 D2 Data 2
10 D3 Data 3
11 D4 Data 4
12 D5 Data 5
13 D6 Data 6
14 D7 Data 7
This connection can be changed by modifying the LCD configuration file <lcd.c> supplied by the PICC compiler.
Example 4.8
An LCD is connected to a PIC16F84 microcontroller as shown in Figure 4.8. Write a program to display the string ‘CONTROL’ on the LCD.
+5V 14 VDD MCLR
RB0 RB1 RB2 RB3 RA2 RA3 PIC 16F84
Vss OSC2 OSC1
15 C2 C1
16
4MHZ 22pF
22pF
2
5
1 6 5 1
Vss LCD
RS R/W
2 11
6 4
4.7K
7 8 9
12 13
D4 D5 D6 14 D7
3
VDD VEE
E 4
Figure 4.8 Connecting an LCD
PIC MICROCONTROLLER INPUT–OUTPUT INTERFACE 113
Solution
The program listing is shown in Figure 4.9. At the beginning of the program ports A and B are configured as outputs. Then the LCD is initialized and the string ‘CONTROL’ is sent to the LCD using the command lcd puts.
A more complex microcontroller example using an analog temperature sensor, an A/D converter and an LCD is given below.
Example 4.9
An LM35DZ type analog temperature integrated circuit is connected to analog input AN0 (or RA0) of a PIC16F877 microcontroller. Also, an LCD is connected to the microcontroller as shown in Figure 4.10. Write a program to display the ambient temperature every second on the LCD. The display should show the temperature as ‘TEMP=nn’, where nn is the ambient temperature. This is an example of a digital thermometer.
//*******************************************************************
//
// LCD DISPLAY PROGRAM // ====================
//
// Author: D. Ibrahim // Date: May, 2005 // File: LCD.C //
// This program sends the message CONTROL to an LCD.
// The LCD is connected to a PIC microcontroller as specified by the // PIC C language compiler. The microcontroller is operated with a // 4MHz crystal.
//
//*******************************************************************
#include <pic.h>
#include <delay.c>
#include <lcd.c>
//
// Main Program // ************
//
main(void) {
TRISA = 0; /* PORT A is output */
TRISB = 0; /* PORT B is output */
lcd init(); /* Initialize the LCD */
lcd clear(); /* Clear the display */
lcd puts("CONTROL"); /* Send text CONTROL to the LCD */
}
Figure 4.9 Program listing for Example 4.8
C1 C2 +5V 14 VDD 4 MCLR 4.7K
RA0
RB0 RB1 RB2 RB3
RA2 PIC RA3 16F84
OSC1 OSC2
16 15
22pF 22pF 4MHZ
Vss 5 1 9 8
11D4 D5 D6 D7
LCD RS E R/W
VDD VEE
Vss 1 5 6 4 12 13 14 7
6
2
2 3
LM 35
Analog Temperature Sensor
10mV/C 2
Figure 4.10 Hardware set-up of the digital thermometer
Solution
The LM35DZ is a 3-pin integrated circuit which gives an output voltage which is directly proportional to the temperature. The device can be used to measure temperatures in the range 0–125◦C. Two pins of the integrated circuit are connected to the supply and the ground. The third pin is the output Vo, where Vo=10 mV/◦C. Thus, for example, at 20◦C the output voltage is 200 mV, at 30◦C the output is 300 mV and so on. The sensor is directly connected to analog channel AN0 of the microcontroller. The LCD is connected as described in Example 4.8.
The program listing of the digital thermometer is given in Figure 4.11. At the beginning of the program bit 0 of port A is configured as input and port B is configured as output. Then, ADCON1 register is configured (see Chapter 3) by sending the bit pattern ‘10001110 = 0× 8E’, so that the RA0 port pin is analog, and the RA2 and RA3 port pins are digital. Also, bit 7 (ADFM) of ADCON1 is set to 1 so that the 8 bits of the converted data will be in the register ADRESL and the upper 2 bits will be in bits 0 and 1 of the register ADRESH. The A/D converter clock is chosen as fosc/8 and channel 0 of the A/D converter is selected by configuring the ADCON0 register.
The A/D converter is started by setting bit 2 of the ADCON0 register (sending 0×45 to ADCON0). The program then waits (using a while loop) for the completion of the conversion.
At the end of the conversion bit 2 of ADCON0 is cleared by the microcontroller and this is sensed by the program. The program then exits the while loop and reads the lower and upper bytes of the converted 10-bit data, combines the data into a single variable called temp, and then converts the data into real value, i.e. millivolts. The actual temperature in degrees Celsius is then obtained by dividing this voltage by 10. The temperature is then converted into a string called tempc and is sent to the LCD display using the lcd puts command. The program clears
//*******************************************************************
//
// DIGITAL THERMOMETER PROGRAM // ============================
//
// Author: D. Ibrahim // Date: May, 2005 // File: THERMOMETER.C //
// This program reads the temperature form a LM35DZ type analog // sensor every second and then displays the temperature on an LCD.
// The microcontroller is operated with a 4MHz crystal.
//
//*******************************************************************
#include <pic.h>
#include <delay.c>
#include <lcd.c>
#include <stdio.h>
//
// Function to wait a second //
void wait a second() {
unsigned int j;
for(j = 0; j < 4; j++)DelayMs(250);
}
//
// Main Program // ************
//
main(void) {
const float lsb = 5000.0/1024.0;
float mV, temp, templ, temph;
unsigned int tempc;
unsigned char disp[] = "TEMP = ";
TRISA = 1; /* RA0 is input, others output */
TRISB = 0; /* PORT B is output */
ADCON1 = 0x8E; /* RA0=analog, RA2,RA3=digital */
ADCON0 = 0x41; /* Configure A/D clock and select channel 0 */
for(;;) {
ADCON0 = 0x45; /* Start A/D conversion */
while(ADCON0 & 4) != 0); /* Wait for conversion */
temph = ADRESH; /* Read upper 2 bits */
templ = ADRESL; /* Read lower 8 bits */
temp = 256.0*temph + templ; /* Temperature in digital */
mV = temp *lsb; /* Temperature in mV */
tempc = mV / 10.0; /* Temperature in Centig. (10mV/C) */
sprintf(disp+7, "%d",tempc); /* Convert temperature to a string */
lcd puts(tempc); /* Display temperature on LCD */
wait a second(); /* Wait a second */
lcd clear(); /* Clear display */
} }
Figure 4.11 Program listing for Example 4.9
115
the LCD display and repeats continuously after 1 s delay. The delay is created by using a function called wait a second. The program displays the temperature as T E M P =nn where nn is the ambient temperature.
Notice that the program uses the header files <pic.h>, <delay.c>, <lcd.c>, and <stdio.h>. The file <pic.h> contains the PIC microcontroller definitions. <delay.c> is used to create delays in the program. <lcd.c> is used for the LCD initialization and control functions. Finally, <stdio.h>
is used to convert an integer into a string so that it can be displayed on the LCD.