You are on page 1of 15

Experiment 4

Connecting Input Devices

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


Purpose

- To be introduced with Microcontroller Input design and programming


- To familiarize you with programming algorithms and techniques

Required equipment and software

o PC
o Proteus software
o MicorC pro software
o Fritizng

1. Introduction

In this experiment we will introduce you how to write a MikroC pro Program to read data from input
devices.
The Data Input devices can be classified into
A. Digital Data Input
These devises are On/OFF devices such as: switches, encoders

B. Analog Devices
Sensors , transducers

C. Data converters
- Analog to digital converter
- Frequency to Voltage converter
- Current to Voltage *

The basic algorithm for data input programming is follow:

1. Most of pin can be configured as data input


2. Refer to datasheet to know the analog ports
- In 16F84 , you have to connect an ADC
- In 16F877a:

Pin 2 Pin 3 Pin 4 Pin 5 Pin 7 Pin 8 Pin 9 Pin 10


RA0 RA1 RA2 RA3 RA5 RE0 RE1 RE2
AN0 AN1 AN2 AN3 AN4 AN5 AN6 AN7

3. select which you will use as analog /digital input


4. connect the input device to the selected pin
5. configure the ports as analog/digital Input

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


Digital Input to the PIC
Figure 4.1 illustrates how to properly interface different types of components and digital families
of devices as inputs to the PIC.

Figure 4.1 Interface circuits for input devices

All I/O pins of the PIC that are configured as inputs interface through a TTL input buffe or
Schmitt trigger input buffer . The Schmitt trigger enhances noise immunity for a slowly changing
input signal. Because an input pin is TTL buffered in the PIC, interfacing a TTL gate or device to
the PIC can be done directly unless it is has an open-collector output. In this case, an external
pull-up resistor is required. Because the output of a 5 V powered CMOS device swings nearly
from 0 to 5 V, the device will drive a PIC input directly. The weak pull-up option on pins RB0
through RB7 is useful when using mechanical switches or keypads for input. The pull-up FET
maintains a 5 V input until the switch is closed, bringing the input low. Although a TTL input
usually floats high if it is open, the FET pull-up option is useful, because it simplifies the
interface to external devices (e.g., keypad input). Finally, one must be aware of the current
specifications of the PIC input and output pins. For the PIC16F84, there is a 25 mA sink
maximum per pin with a 80 mA maximum for the entire PORTA and a 150 mA maximum for
PORTB.

Simple ON/OFF input mechanisms (mechanical switches )


a. tilt switch

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


b. Click buttons

Example:
+5V

R3
10k

U1
13 33
14
OSC1/CLKIN RB0/INT
34
R2
OSC2/CLKOUT RB1
35
RB2 300
X1 +5V
2
RA0/AN0 RB3/PGM
36
3
RA1/AN1 RB4
37 D1
4 38 LED-YELLOW
RA2/AN2/VREF-/CVREF RB5
5 39
RA3/AN3/VREF+ RB6/PGC
6 40
CRYSTAL RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
R1 RC0/T1OSO/T1CKI
15
10k 8 16
RE0/AN5/RD RC1/T1OSI/CCP2
9 17
RE1/AN6/WR RC2/CCP1
C1 C2 10
RE2/AN7/CS RC3/SCK/SCL
18
22pF 22pF 23
RC4/SDI/SDA
1 24
MCLR/Vpp/THV RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
28
RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
PIC16F877A

#define switch RB0_bit

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


#define LED RB1_bit

#define OFF 0

#define ON 1

bit check_switch;

void main() {

TRISB0_bit=1;

TRISB1_bit=0;

while(1)

if(switch==ON) LED=ON;

else LED=OFF;

Exercise: write a program for the following circuit

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


}

Working with Keypad

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


LCD1
+5V LM016L

OSC1

OSC2
RV1 X1 R5 R4 R3 R2
10k 10k 10k 10k

RD0

RD1

RD2

RD3
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
50%

POT1 CRYSTAL

E
1 C1 C2
2
3

4
5
6

7
8
9
10
11
12
13
14
22pF 22pF
1k

4
+
RB0
RB1
RB2
RB3
C
RD7 D
ON 0 =
POT1

RB4

RB5

RD6 C 1 2 3
U1
OSC1
OSC2
13
14
OSC1/CLKIN
OSC2/CLKOUT
RB0/INT
RB1
33
34
RB0
RB1
RB5 B 4 5 6
35
RB2 RB2
2 36
+5V RA0/AN0 RB3/PGM RB3
3
4
RA1/AN1
RA2/AN2/VREF-/CVREF
RB4
RB5
37
38
RB4
RB5
RD4 A 7 8 9
5 39
RA3/AN3/VREF+ RB6/PGC
6 40
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
R1 RC0/T1OSO/T1CKI
15
10k 8 16
RE0/AN5/RD RC1/T1OSI/CCP2
9 17
RE1/AN6/WR RC2/CCP1
10 18
RE2/AN7/CS RC3/SCK/SCL
23
RC4/SDI/SDA
1 24
MCLR/Vpp/THV RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0 RD0
20
RD1/PSP1 RD1
21
RD2/PSP2 RD2
22
RD3/PSP3 RD3
27
RD4/PSP4 RD4
28
RD5/PSP5 RD5
29
RD6/PSP6 RD6
30
RD7/PSP7 RD7
PIC16F877A

Program

unsigned short kp, cnt, oldstate = 0;

char txt[6];

// Keypad module connections

char keypadPort at PORTD;

// End Keypad module connections

// LCD module connections

sbit LCD_RS at RB4_bit;

sbit LCD_EN at RB5_bit;

sbit LCD_D4 at RB0_bit;

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


sbit LCD_D5 at RB1_bit;

sbit LCD_D6 at RB2_bit;

sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;

sbit LCD_EN_Direction at TRISB5_bit;

sbit LCD_D4_Direction at TRISB0_bit;

sbit LCD_D5_Direction at TRISB1_bit;

sbit LCD_D6_Direction at TRISB2_bit;

sbit LCD_D7_Direction at TRISB3_bit;

// End LCD module connections

void main() {

cnt = 0; // Reset counter

Keypad_Init(); // Initialize Keypad

Lcd_Init(); // Initialize LCD

Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

Lcd_Out(1, 1, "1");

Lcd_Out(1, 1, "Key :"); // Write message text on LCD

Lcd_Out(2, 1, "Times:");

do {

kp = 0; // Reset key code variable

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


// Wait for key to be pressed and released do

// kp = Keypad_Key_Press(); // Store key code in kp variable

kp = Keypad_Key_Click(); // Store key code in kp variable

while (!kp);

// Prepare value for output, transform key to it's ASCII value

switch (kp) {

//case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3

//case 11: kp = 48; break; // '0'

//case 12: kp = 35; break; // '#'

//default: kp += 48;

case 1: kp = 49; break; // 1 // Uncomment this block for keypad4x4

case 2: kp = 50; break; // 2

case 3: kp = 51; break; // 3

case 4: kp = 65; break; // A

case 5: kp = 52; break; // 4

case 6: kp = 53; break; // 5

case 7: kp = 54; break; // 6

case 8: kp = 66; break; // B

case 9: kp = 55; break; // 7

case 10: kp = 56; break; // 8

case 11: kp = 57; break; // 9

case 12: kp = 67; break; // C

case 13: kp = 42; break; // *

case 14: kp = 48; break; // 0

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


case 15: kp = 35; break; // #

case 16: kp = 68; break; // D

if (kp != oldstate) { // Pressed key differs from previous

cnt = 1;

oldstate = kp;

else { // Pressed key is same as previous

cnt++;

Lcd_Chr(1, 10, kp); // Print key ASCII value on LCD

if (cnt == 255) { // If counter varialble overflow

cnt = 0;

Lcd_Out(2, 10, " ");

WordToStr(cnt, txt); // Transform counter value to string

Lcd_Out(2, 10, txt); // Display counter value on LCD

} while (1);

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


LCD1
LM016L

VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
1
2
3

4
5
6

7
8
9
10
11
12
13
14
C1
VDD

22p
X1 RV1
CRYSTAL
C2

22p
VDD
1k
U1
VDD RV2 13 33
OSC1/CLKIN RB0/INT
14 34
OSC2/CLKOUT RB1
35
RB2
2 36
50%

RA0/AN0 RB3/PGM
3 37
RA1/AN1 RB4
4 38
RA2/AN2/VREF-/CVREF RB5
R1 5
RA3/AN3/VREF+ RB6/PGC
39
10K 10K 6 40
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
15
RC0/T1OSO/T1CKI
8 16
RE0/AN5/RD RC1/T1OSI/CCP2
9 17
RE1/AN6/W R RC2/CCP1
10 18
RE2/AN7/CS RC3/SCK/SCL
23
RC4/SDI/SDA
1 24
MCLR/Vpp/THV RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
28
RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
PIC16F877A

#include "d://PICPROJECT1//test11.h"

// LCD module connections

void main(){

float ADC_Value, POTValue;

char txt[10];

Lcd_Init();

ADC_Init();

while(1){

ADC_Value = ADC_Get_Sample(0);

Delay_ms(5);

POTValue=( 5.0*ADC_VAlue/1023) ;

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


FloatToStr(POTValue, txt);

Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

Lcd_Out(1,1,txt); // Write text in first row

Delay_ms(5);

After reading the ADC tutorial given above you will note the the PIC MCU's ADC gives us the value
between 0-1023 for input voltage of 0 to 5v provided it is configured exactly as in the above tutorial. So if
the reading is 0 then input is 0v, if reading is 1023 then input is 5v. So in general form if the adc read out
is val then voltage is.
unsigned int val;
val=ADCRead(0); //Read Channel 0
voltage= ((val)/1023.0)*5;
The above formula give voltage in Volts, to get Voltage in mili Volts (mV) we must multiply it with 1000, so

voltage=((val)/1023.0)*5*1000); //Voltage is in mV
since 10mV = 1 degree, to get temperature we must divide it by 10, so

t=((val)/1023.0)*5*100); //t is in degree centigrade


simplifying further we get

t=((val/1023.0)*500);
t=(val*0.48876);
we round off this value, so

t=round(val*0.48876);
remember round() is a standard c library function
LM35

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


LCD1
LM016L

VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
1
2
3

4
5
6

7
8
9
10
11
12
13
14
C1
VDD

22p
X1 RV1
CRYSTAL
C2

VDD 22p
1k
U1
VDD RV2 13 33
OSC1/CLKIN RB0/INT
14 34
OSC2/CLKOUT RB1
35
RB2
2 36
50%

RA0/AN0 RB3/PGM
3 37
RA1/AN1 RB4
4 38
RA2/AN2/VREF-/CVREF RB5
R1 5
RA3/AN3/VREF+ RB6/PGC
39
10K 10K 6 40
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
15
RC0/T1OSO/T1CKI
8 16
VDD RE0/AN5/RD RC1/T1OSI/CCP2
9 17
RE1/AN6/W R RC2/CCP1
10 18
RE2/AN7/CS RC3/SCK/SCL
23
RC4/SDI/SDA
1 24
MCLR/Vpp/THV RC5/SDO
25
RC6/TX/CK
26
1 U2 RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
27.0 RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
2 28
VOUT RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
3 LM35 PIC16F877A

#include "d://PICPROJECT1//test11.h"

// LCD module connections

void main(){
float ADC_Value1,ADC_Value2, POTValue,LM35_Voltage;
int t;
char txt1[10], txt2[10];
Lcd_Init();
ADC_Init();
while(1){
ADC_Value1 = ADC_Get_Sample(0);
ADC_Value2 = ADC_Get_Sample(1);
LM35_Voltage=(( 5.0*ADC_VAlue1/1023)) ;
t=((ADC_Value2/1023.0)*500);
POTValue=( 5.0*ADC_VAlue1/1023) ;
FloatToStr(POTValue, txt1);
IntToStr(t, txt2);

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,1,txt2);
Delay_ms(1000);
}

Appendix
Measuring Temperature With PIC

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes


.

Program

void main() {

float R = 9870.0; // Fixed resistance in the voltage divider

float logRt,Rt,T;

float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

int ThermistorPin = 1; // Analog input pin for thermistor voltage

int Vo; // Integer value of voltage reading

ADC_Init(); // Initialize ADC module with default settings

Vo = ADC_Read(ThermistorPin);

Rt = R*( 1023.0 / (float)Vo - 1.0 );

logRt = log(Rt);

T = ( 1.0 / (c1 + c2*logRt + c3*logRt*logRt*logRt ) ) - 273.15;

Microprocessor and Microcontroller LabPrepared by: Dr. Tariq Younes

You might also like