Professional Documents
Culture Documents
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 *
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.
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 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;
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
char txt[6];
void main() {
Lcd_Out(1, 1, "1");
Lcd_Out(2, 1, "Times:");
do {
while (!kp);
switch (kp) {
//case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3
//default: kp += 48;
cnt = 1;
oldstate = kp;
cnt++;
cnt = 0;
} while (1);
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"
void main(){
char txt[10];
Lcd_Init();
ADC_Init();
while(1){
ADC_Value = ADC_Get_Sample(0);
Delay_ms(5);
POTValue=( 5.0*ADC_VAlue/1023) ;
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)*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
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"
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);
Appendix
Measuring Temperature With PIC
Program
void main() {
float logRt,Rt,T;
Vo = ADC_Read(ThermistorPin);
logRt = log(Rt);