PROGRAMMING PIC MICROCONTROLLERS IN C:PIC MICROCONTROLLER INPUT–OUTPUT INTERFACE

PIC MICROCONTROLLER INPUT–OUTPUT INTERFACE

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.

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 Vo of the port pin is +5 V when it is at logic 1, the value of the

Programming-PIC-Microcontrollers-in-[5]

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.

Programming-PIC-Microcontrollers-in-[17]

Programming-PIC-Microcontrollers-in-[16]

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

Programming-PIC-Microcontrollers-in-[6]

10000000 = 0 × 80 is sent to TRISB register). The state of the switch is then checked continuously and as soon as the switch is pressed the LED is turned on.

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:

Programming-PIC-Microcontrollers-in-[27]

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:

Programming-PIC-Microcontrollers-in-[25]

Programming-PIC-Microcontrollers-in-[13]

Solution

clip_image0083_thumbThe 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.

Programming-PIC-Microcontrollers-in-[29]

Programming-PIC-Microcontrollers-in-[18]

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

clip_image017_thumbADCON0). 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

Programming-PIC-Microcontrollers-in-[10]

clip_image0072_thumbclip_image022_thumbthe 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 TE 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.

Leave a comment

Your email address will not be published. Required fields are marked *