Файл: Microcontroller based applied digital control (D. Ibrahim, 2006).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3296
Скачиваний: 1
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
110 PROGRAMMING PIC MICROCONTROLLERS IN C
+5V |
|||
14 |
|||
4.7K |
VDD |
6 |
330 |
4 MCLR |
RB0 |
LED |
|
+5V |
|||
R |
|||
Push-button |
|||
Switch |
PIC |
||
16F84 |
|||
Vss 5 |
|||
OSC1 |
OSC2 |
||
16 |
15 |
||
C 1 |
4MHZ |
C 2 |
|
22pF |
22pF |
||
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; while(RB7 == 1); RB0 = 1;
}
/* Make sure the LED is OFF when started */ /* Wait until switch is pressed */
/* 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 continuously 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 |
112 PROGRAMMING PIC MICROCONTROLLERS IN C
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 |
2 |
3 |
||||||
4.7K |
VDD |
RB0 |
6 |
11 |
D4 |
VDD |
VEE |
|
4 MCLR |
RB1 |
7 |
12 |
D5 |
||||
RB2 |
8 |
13 |
D6 |
LCD |
||||
RB3 |
9 |
14 |
D7 |
|||||
RS E |
R/W |
Vss |
||||||
1 |
4 |
6 |
5 |
1 |
||||
RA2 2 |
||||||||
RA3 |
||||||||
PIC |
||||||||
16F84 |
5 |
|||||||
Vss |
||||||||
OSC1 |
OSC2 |
|||||||
16 |
15 |
|||||||
C 1 |
4MHZ |
C 2 |
||||||
22pF |
22pF |
|||||||
Figure 4.8 Connecting an LCD