Top Banner
YWT-1 PIC 16F84(A) Practical Part(1) Basic Introduction To view the following file names with underline, press mouse’s left click. But your windows must have corresponding software. 1. .jpg files need photo viewer software to view this images. 2. .avi files and .mp4 files - need video viewer software to view this video. 3. .rpp files need Real PIC Simulator software to open this project files. When you place mouse pointer over these names for 2 seconds, detail file location will be appeared. Parts list to be used in circuits using project board from Chapter 1 to 3 Main PIC16F84A IC x 1 4 MHz (clock) crystal x 1 22pF ceramic disc capacitor x 2 4.7kΩ (or) 10KΩ resistor (½ Watt) x 1 Project Board x 1 network cable(CAT5) 4 inch x 3 (to use as connector wires) Accessories LED red x 8 330Ω resistor(½ Watt) x 8 Micro switch x 3 10kΩ resistor (½ Watt) x 3 Speaker(8Ω,0.2Watt) x 1 10μF capacitor x 1 Optional (not important) Transparent Red LED x 1 Transparent Orange LED x 1 Transparent Yellow LED x 1 Transparent Green LED x 1 Transparent Blue LED x 1 Transparent Violet LED x 1 Transparent Pink LED x 1 Transparent RGB color LED x 1 Transparent White LED x 1 Transparent White Short LED x 1 Yellow LED x 1 Green LED x 1 100Ω resistor(½ Watt) x 3 150Ω resistor(½ Watt) x 5 220Ω resistor(½ Watt) x 2 330Ω resistor(½ Watt) x 2 5V Power Adaptor (USB phone charger) x 1 USB cable( to use with phone charger) x 1
68

PIC 16F84 Practical Ch1 to 3

Dec 11, 2015

Download

Documents

Author :Ko Yee Wint Thu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PIC 16F84 Practical Ch1 to 3

YWT-1

PIC 16F84(A) Practical

Part(1) – Basic

Introduction

To view the following file names with underline, press mouse’s left click. But your

windows must have corresponding software.

1. .jpg files – need photo viewer software to view this images.

2. .avi files and .mp4 files - need video viewer software to view this video.

3. .rpp files – need Real PIC Simulator software to open this project files.

When you place mouse pointer over these names for 2 seconds, detail file

location will be appeared.

Parts list to be used in circuits using project board from Chapter 1 to 3

Main

PIC16F84A IC x 1

4 MHz (clock) crystal x 1

22pF ceramic disc capacitor x 2

4.7kΩ (or) 10KΩ resistor (½ Watt) x 1

Project Board x 1

network cable(CAT5) 4 inch x 3

(to use as connector wires)

Accessories

LED red x 8

330Ω resistor(½ Watt) x 8

Micro switch x 3

10kΩ resistor (½ Watt) x 3

Speaker(8Ω,0.2Watt) x 1

10µF capacitor x 1

Optional (not important)

Transparent Red LED x 1

Transparent Orange LED x 1

Transparent Yellow LED x 1

Transparent Green LED x 1

Transparent Blue LED x 1

Transparent Violet LED x 1

Transparent Pink LED x 1

Transparent RGB color LED x 1

Transparent White LED x 1

Transparent White Short LED x 1

Yellow LED x 1

Green LED x 1

100Ω resistor(½ Watt) x 3

150Ω resistor(½ Watt) x 5

220Ω resistor(½ Watt) x 2

330Ω resistor(½ Watt) x 2

5V Power Adaptor (USB phone charger) x 1

USB cable( to use with phone charger) x 1

Page 2: PIC 16F84 Practical Ch1 to 3

YWT-2

Photo of require components(Parts list)

=>img_283.jpg

=>img_285.jpg

=>img_286.jpg

Internal Connection of Project Board

Image => ProjectBoard_Internal_Connection.jpg

If you want to use other color LED instead of red LED, the following resistor

values can be used. Resistor value may be change if size of LED change.

Use 150Ω resistor(½ Watt) for (Red, Orange, Yellow, Pink, Green) transparent

LED.

Use 330Ω resistor(½ Watt) for (Red, Yellow) normal LED.

Use 220Ω resistor(½ Watt) for (Green) normal LED.

Use 100Ω resistor(½ Watt) for (White, Violet, Blue) transparent LED.

Use 330Ω resistor(½ Watt) for RGB color transparent LED. While power on, this

LED always changes its color with (Red, Green and Blue) combination color.

Video file => MVI_0203.avi

Hidden lines on money can be seen with violet transparent LED.

Image => img_0158.jpg

img_0159.jpg

Supply 5V to PIC circuit on the project board

(1) You can supply PIC circuit directly from 5V power supply.

Image => img_0195.jpg

(OR)

(2) You can supply PIC circuit by 5V USB phone charger .Use modified USB

power cable.

Image =>img_0378.jpg

(OR)

Page 3: PIC 16F84 Practical Ch1 to 3

YWT-3

(3) You can supply PIC circuit by 5V USB phone power bank(battery). Use

modified USB power cable.

Image =>img_0375.jpg

(OR)

(4) You can get 5V from 9V battery by using 7805 IC.

Image => img_0200.jpg

(OR)

(5) You can get 5V from DC 12V adaptor by using 7805 IC.

Image => img_0384.jpg

Prepare 12V adaptor to be used easily with project board

Step 1 – Cut 12V adaptor dc cable and strip wires.

Image => img_0380.jpg

Step 2 – Find two wire by using meter which are +12V and ground. Extend +12V

wire with red wire and extend ground wire with black wire.

Image => img_0381.jpg

Step 3 – use wire tape not to touch two joints each other.

Image => img_0382.jpg

Page 4: PIC 16F84 Practical Ch1 to 3

YWT-4

7805

Vin Vout

Gnd9V(or)12V5V

+ +

__

Fig 1-1.Convert from 9V (or) 12V to 5V by using 7805 IC

Making modified USB power cable from bad usb mouse/keyboard

Step 1 – cut usb cable from bad usb mouse/keyboard and strip wires.

Image => img_0365.jpg

Step 2 – find two wire by using meter which are 5V and ground. Extend +5V wire

with red wire and extend ground wire with black wire.

Image => img_0368.jpg

img_0366.jpg

Step 3 – use wire tape not to touch two joints each other.

Image => img_0370.jpg

Supply 5V to PIC Board

(1) You can supply PIC Board directly from 5V USB phone charger.

Image => img_0289.jpg

(OR)

Page 5: PIC 16F84 Practical Ch1 to 3

YWT-5

(2) You can supply PIC board by 5V USB phone charger .Use modified USB power

cable.

Image => img_0373.jpg

img_0374.jpg

(OR)

(3) You can supply PIC Board directly from 5V USB phone power bank.

Image => img_0364.jpg

(OR)

(4) You can supply PIC board by 5V USB phone power bank. Use modified USB

power cable.

Image => img_0371.jpg

img_0372.jpg

(OR)

(5) You can supply PIC board directly from 5V power supply.

Image => img_0196.jpg

(OR)

(6) You can get 5V from 9V battery by using 7805 IC.

Image => img_0287.jpg

(OR)

(7) You can get 5V from DC 12V adaptor by using 7805 IC.

Image => img_0385.jpg

img_0387.jpg

Warning – Do not use Computer’s USB port to get 5V for this board. It can

damage the computer due to error of short circuit by board error or user error.

Prepare PIC to be ready to work with input/output pins

To run a PIC, need some steps.

Step 1 –Connect Vdd pin to +5V and Vss pin to ground. In pic16f84 Vdd pin is 14

and Vss pin is 5.

Page 6: PIC 16F84 Practical Ch1 to 3

YWT-6

Step 2 -Connect MCLR pin to +5V using 4.7kΩ(or 10kΩ) resistor. In pic16f84, MCLR

pin is 4.

Step 3 -Some modern PIC can run without external clock because they have built-

in (internal) clock(oscillator).But pic16F84 need external clock circuit (in this

example, use one 4MHz crystal and two of 22pf capacitor to operate as 4Mhz

external clock circuit.).

Connect OSC1 and OSP2 pins with the clock circuit. In pic16f84, OSC1 pin

is 16 and OSC2 pin is 15.

Now,pic16F84 is in running mode and any input/output pins can be used.

Image => img_0023.jpg

img_0195.jpg

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ

(or)

10KΩ

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

Fig 1-2.Basic PIC configuration with Crystal oscillator mode.

Page 7: PIC 16F84 Practical Ch1 to 3

YWT-7

Fig 1-3 - PIC 16F84 with 8 LED(colour) PCB Board

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ

+5

VG

nd

3x4 Project Board

3x3 Project Board

+5V

USB

connector

For

5V power

Screw

connector

330Ω RGB

LED

100Ω Blue

LED

100Ω Violet

LED

150Ω Pink

LED

Green

LED

Ground

Jumper

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84A (or) PIC 16F84

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

RA1RA0 RA2 RA3 RA4

connector

connector connector

150Ω

150Ω

150Ω

Yellow

LED

Orange

LED

Red

LED

+5V in/out

Gnd

150Ω

+5V

R

The value of R may be vary depend upon the color, size and type of LED.

By Ohm Law(V=IR) => R = V/I =>So, Resistance for LED=(Supply Voltage – LED

Voltage)/LED current

If ( red, orange, yellow, pink, green ) transparent LED use 2V & 20mA,

R=(5V-2V)/20mA = 150Ω,so 150Ω resistor can be used.

Page 8: PIC 16F84 Practical Ch1 to 3

YWT-8

If (white, violet, blue) transparent LED use 3V & 20mA, R=(5V-3V)/20mA =

100Ω, so 100Ω resistor can be used.

If (red, yellow) LED use 1.5V & 10mA, R=(5V-1.5V)/10mA = 350Ω, so 330Ω

resistor can be used.

If RGB color LED use 1.5V & 10mA, R=(5V-1.5V)/10mA = 350Ω, so 330Ω

resistor can be used.

If green LED use 2V & 15mA, R=(5V-2V)/15mA = 200Ω, so 220Ω resistor

can be used.

If LED size is larger , more current may be needed. So the required resistor

value may be smaller.

This PCB contain

(1) Basic PIC circuit as shown in Fig 1.1

(2) Connect all PORTB pins with color LED.( to use as binary display for

up/down counter, Temperature sensor, Light sensor, Timer circuits, etc… )

(3) Connect all PORTA pins with connectors.( to use with other components

and circuits )

(4) 3x3 project board , 3x4 project board , +5V(1) connectors and Ground(0)

connectors. (to use as external project board for adding a few

components.)

(5) USB connector and screw connector to get 5V power.

This PCB board can be used with PIC16F84, PIC16F84A, PIC16F627, PIC16F627A,

PIC16F628, PIC16F628A.

Datasheet for PIC16F84A => PIC16F84A_DataSheet.pdf

Datasheet for PIC16F627A, PIC16F628A => PIC16F628A_Datasheet.pdf

Image => IMG_1942.jpg

IMG_1943.jpg

IMG_0005.jpg

IMG_0004.jpg

Image with ZIF => IMG_0003.jpg

IMG_0022.jpg

Image with red LED => IMG_0021.jpg

Page 9: PIC 16F84 Practical Ch1 to 3

YWT-9

Chapter (1)

OUTPUT with LED

In PIC 16F84 or PIC 16F627A, any I/O pin from PortA(RA0,RA1,RA2,RA3,RA4) and

any I/O pin from PortB (RB0 ,RB1 ,RB2 ,RB3, RB4, RB5, RB6,RB7) can be use

separately as input or output as you like. But you must declare them as input or

output before use them. To assign input or output pins for portA, we use TRISA

(Tri-state A) register, and TRISB (Tri-state B) register for portB. If we assign pins

for neither input nor output, the program assume these pins as output.

Fig 1-4

For example ,in Fig 1-4, assign RA0,RA1,RA4 as input(mark by "1") and

RA2,RA3 as output(mark by "0"),so enter binary (0b00010011) or hexa (0x13) or

decimal(19) to TRISA.

After assigning as output pins, if you enter that these pins to "logic 1",they

will become 5V.And if you enter these pins to "logic 0",they will become 0V.Input

pins can be neglect when enter values to output pins. In Fig 1-4, enter PORTA to

8(or 0b00001000 or 0x08) to become RA2 to 0V and RA3 to 5V.

Fig 1-5 is for PORTB with TRISB=0xF0; and PORTB=0x03;

TRISA

0x85

PORTA

0x05

RA0RA1RA2RA3RA4

0 0 111xxx

oi iio

01xxx

5VG

0 11

(LSB)

0V5V5V

RA0RA1RA2RA3RA4 (LSB)

TRISB

0x86

PORTB

0x06

RB0(LSB)RB1RB2RB3RB4

RB0(LSB)RB1RB2RB3RB4

0 01

oi o

0 11

0 0111

o oiii

0 1

5V

RB5RB6RB7

RB5RB6RB7

00 1

5V0V0V5V5VGG

Page 10: PIC 16F84 Practical Ch1 to 3

YWT-10

Example 1-0 => After burning Exp1_0.hex to PIC, PortB’s RB0 pin will be assign

as output pin and produce 5V( logic “1” ).So, the red LED will be ON.

Fig 1-6

Exp1_0.c

According to “TRISB=0;” , all of 8 bits in TRISB register will be fill with “0”

and it means that it declare that all of 8 pins in PORTB(RB0 to RB7) will be used

as output pins.

In, “PORTB=1” , convert “1” to binary is “0000 0001”.So last bit(bit 0=RB0)

of PORTB will be “1” and it mean that RB0 pin will produce 5V.

When logic “1”, RB0 pin produce 5V which exceed Red LED’s normal

working voltage(1.5V with 10mA) and the LED may be burn.

Therefore, a resistor is needed to reduce 5V to 1.5V.The require resistance

is (Total Volt-LED Volt)/(LED current) = (5-1.5)/10m=350 ,So 330 Ohm resistor can

be used.

Fig 1-7 . Complete circuit diagram for example 1-0 and 1-1

330 Ohm Red LED

RB0

main()

TRISB=0;

PORTB=1;

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ

(or)

10KΩ

330

OhmRed

LED

Page 11: PIC 16F84 Practical Ch1 to 3

YWT-11

Creating my first very simple program of Exp1-0 and burn(write) it to a PIC

Step 1 – install MikroC to write C program.

Video => InstallMikroC.avi

Step 2 - write a program with mikroC and compile it to be a hex file.

Video => CreatingHex.avi

In our circuit,we use pic16f84A and 4MHz crystal. So, in this video file, I

select pic16f84A in device section and 004.0000 in clock. And click default button

for default setting and click OK .

After creating hex file, you can also use that hex file virtually with Simulator

softwares (such as Real PIC Simulator, PIC Simulator IDE) without using real PIC

and real electronics components.PIC Simulator IDE will explain in coming

example of Exp1_3.

Video => RealPicSimulator_Install.mp4

RealPicSimulator_Exp1_0.mp4

Project file to open with Real PIC Simulator => Exp1_0.rpp

Step 3(i) - If your PIC programmer is GTP-USB Board-

- Install WinPic800 only on Windows XP to burn(write) into PIC.

- Copy winpic800 folder to desktop.

- Then connect usb cable of programmer to computer and install driver.

- Then run winpic800.exe file.

Video => InstallWinPic800.avi

Step 3(ii) - If your PIC programmer is PicKit3 Device-

- Microsoft dotNet Framework 4 must be installed first .This exe file already

contain in that installation folder. If not, you can download from Microsoft.

- Then, install PicKit3 programmer application software on Windows XP or

Windows 7 to burn(write) into PIC.

Video => InstallPicKit3.mp4

Step 4 (i)- If your PIC programmer is GTP-USB Board-

- write that hex file to a PIC(pic16F84A) by using a PIC programmer device

and software. Notice Jumper position.

Page 12: PIC 16F84 Practical Ch1 to 3

YWT-12

Image => DeviceConnection.jpg

JumperConnection.jpg

DeviceConnection_ZIF.jpg

JumperConnection_ZIF.jpg

Video => DetectPIC16F84A.avi

BurnToPIC.avi

Step 4 (ii)- If your PIC programmer is PicKit3 Device -

- write that hex file to a PIC(pic16F84A) by using a PIC programmer device

and software. Notice Jumper position.

Image => DeviceConnectionPicKit3_1.jpg

DeviceConnectionPicKit3_2.jpg

DeviceConnectionPicKit3_3.jpg

JumperConnectionPicKit3.jpg

JumperConnectionPicKit3_2.jpg

Video => DetectPIC16F84AwithPicKit3.mp4

BurnToPICwithPicKit3.mp4

Note– In this video , we use GTP-USB Board and PicKit3 as PIC programmer

device and WinPIC800 and PicKit3 programmer application as PIC programmer

software. If you use another type of PIC programmer and programmer software,

step 3 and 4 may be different. You can buy pickit3 programmer from “Circuit

World Electronics shop”, Mandalay. Phone-09-6800396 .

Step 5 – Then, insert the PIC and required electronics components to project

board as shown in Fig1-7. The LED will be ON.

Image => BeforeProgramBurn.jpg

AfterProgramBurn.jpg

Making my GTP-USB PIC Programmer

If you want to burn GTP_USB.hex file to new PIC18F2550(24 pin) for your

new GTP-USB PIC programmer PCB board, change jumper position and burn.

Image => NewPICandBoard.jpg

BurnPic18F2550.jpg

BurnPic18F2550_JumperConnection.jpg

BurnPic18F2550_ZIF_JumperConnection.jpg

Page 13: PIC 16F84 Practical Ch1 to 3

YWT-13

Video => DetectPIC18F2550.avi

BurnPic18F2550.avi

If you want to burn into PIC16F877(40 pin),jumper position must be same

as in PIC18F2550(24 pin).

Image => BurnPic16F877.jpg

BurnPic16F877_JumperConnection.jpg

BurnPic16f877_ZIF.jpg

BurnPic16F877_ZIF_JumperConnection.jpg

Pic16F877withPicKit3JumperConnection

DetectPic16F877withPicKit3.jpg

Video => DetectPIC16F877.avi

If found 16F877 in detection, then burn into it.

Some available PIC and ZIF(Zero Insertion Force) sockets

Image => SomePIC.jpg

GTP-USB programmer and adaptor bottom view

Image => IMG_0362.jpg

IMG_0363.jpg

Circuit diagram of GTP_USB => USB_PIC_SCH.pdf

Example 1-1 => Example 1-1 uses the same circuit in example 1-0 (Fig 1-7). This

program will blink LED in every second. 0.5 second ON and 0.5 second OFF.

Fig 1-6

Exp1_1.c

330 Ohm Red LED

RB0

main()

TRISB = 0xFE ; while(1)

PORTB = 0x01; Delay_ms(500);

PORTB = 0x00; Delay_ms(500);

Page 14: PIC 16F84 Practical Ch1 to 3

YWT-14

Explanation

TRISB=0xFE; => put the hexa-decimal value of FE to TRISB register.

Hexa FE(0xFE) = Binary 1111 1110(0b11111110) = Decimal 254(254).

So you can replace TRISB=0xFE; with TRISB=0b11111110; (or) TRISB=254;

You can use windows 7 built-in calculator with programmer mode to change

hexa => binary,octa,decimal video => HexToOthersWithWin7Calc.mp4

Binary => hexa,octa,decimal video => BinToOthersWithWin7Calc.mp4

Decimal => binary, hexa, octa video => DecToOthersWithWin7Calc.mp4

According to this statement, last bit(bit 0) of TRISB register which is assigned to

RB0 is 0 and other bits(bit 1 to 7 which is assigned to RB1 to RB7) are 1.That

mean that this program declare RB0 pin as output and declare RB1 to RB7 pins

as input.

While(1) Process 1; Process 2; Process 3;etc.. => means that the program

lines between “ “ and “” will do repeat and repeat forever.

Eg. Will do =>Process 1; Process 2; Process 3;etc.. Process 1; Process 2;

Process 3; etc..Process 1; Process 2; Process 3;etc.. Process 1; Process 2;

Process 3;etc… until program stop.

PORTB=0x01; => put the hexa-decimal value of 01 to PORTB register.

Hexa 01(0x01) = Binary 0000 0001(0b00000001) = Decimal 1(1).

So you can replace PORTB=0x01; with PORTB=0b00000001; (or) PORTB=1;

This command made output pin RB0(last bit or bit 0 of PortB) to logic “1”.While

RB0 pin is “1”, that pin produce 5V.

Delay_ms(500); => the program will be in waiting state for 500 mili-seconds.

1000 mili-seconds = 1 seconds

PORTB=0x00; => put the hexa-decimal value of 00 to PORTB register.

Page 15: PIC 16F84 Practical Ch1 to 3

YWT-15

Hexa 01(0x00) = Binary 0000 0000(0b00000001) = Decimal 0(0).

So you can replace PORTB=0x00; with PORTB=0b00000000; (or) PORTB=0;

This command made output pin RB0(last bit or bit 0 of PortB) to logic “0”.While

RB0 pin is “0”, that pin produce 0V or connect internally with ground.

In Brief,

At the first of program, declare RB0 as output.

Step 1 to 4 are in while(1) loop. So, step 1 to 4 will do in sequence and repeatedly

forever.

Step 1 – RB0 become logic “1” and produce 5V.So LED will ON.

Step 2- wait for 500 mili-second (or) 0.5 second. During this time, the

program do nothing. So, LED will still ON.

Step 3 - RB0 become logic “0” and produce 0V.So LED will OFF.

Step 4- wait for 500 mili-second (or) 0.5 second. During this time, the

program do nothing. So, LED will still OFF.

So, while running this program, LED at RB0 will blink in every second.

Example 1-1 also use circuit diagram in Fig 1-7.

You can run Example 1-1 with Real PIC Simulator.

Video => RealPicSimulator_Exp1_1.mp4

RealPicSimulator_Exp1_1Osc.mp4 (with Oscilloscope)

Project file to open with Real PIC Simulator => Exp1_1.rpp

Exp1_1_Osc.rpp (with Oscilloscope)

You can run Example 1-1 on Project board with 9V battery and 7805 IC.

7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Page 16: PIC 16F84 Practical Ch1 to 3

YWT-16

This section shows Example 1-1 circuit with various color of LED.

Image => IMG_0206.jpg (Red LED with 330 Ohm, OFF)

IMG_0204.jpg (Red LED with 330 Ohm, ON)

Video => MVI_0207.avi (Red LED with 330 Ohm)

Image => IMG_0210.jpg (Green LED with 220 Ohm, OFF)

Video => MVI_0211.avi (Green LED with 220 Ohm)

Image => IMG_0208.jpg (Yellow LED with 330 Ohm, ON)

Video => MVI_0209.avi (Yellow LED with 330 Ohm)

Image => IMG_0216.jpg (Red Transparent LED with 150 Ohm, OFF)

IMG_0215.jpg (Red Transparent LED with 150 Ohm, ON)

Video => MVI_0218.avi (Red Transparent LED with 150 Ohm)

Image => IMG_0216.jpg (Orange Transparent LED with 150 Ohm, OFF)

IMG_0220.jpg (Orange Transparent LED with 150 Ohm, ON)

Video => MVI_0219.avi (Orange Transparent LED with 150 Ohm)

Image => IMG_0216.jpg (Yellow Transparent LED with 150 Ohm, OFF)

IMG_0221.jpg (Yellow Transparent LED with 150 Ohm, ON)

Video => MVI_0222.avi (Yellow Transparent LED with 150 Ohm)

Page 17: PIC 16F84 Practical Ch1 to 3

YWT-17

Image => IMG_0216.jpg (Green Transparent LED with 150 Ohm, OFF)

IMG_0223.jpg (Green Transparent LED with 150 Ohm, ON)

Video => MVI_0224.avi (Green Transparent LED with 150 Ohm)

Image => IMG_0229.jpg (Blue Transparent LED with 100 Ohm, OFF)

IMG_0226.jpg (Blue Transparent LED with 100 Ohm, ON)

Video => MVI_0225.avi (Blue Transparent LED with 100 Ohm)

Image => IMG_0229.jpg (Violet Transparent LED with 100 Ohm, OFF)

IMG_0230.jpg (Violet Transparent LED with 100 Ohm, ON)

Video => MVI_0231.avi (Violet Transparent LED with 100 Ohm)

Image => IMG_0216.jpg (Pink Transparent LED with 150 Ohm, OFF)

IMG_0233.jpg (Pink Transparent LED with 150 Ohm, ON)

Video => MVI_0232.avi (Pink Transparent LED with 150 Ohm)

Image => IMG_0229.jpg (White Transparent LED with 100 Ohm, OFF)

IMG_0234.jpg (White Transparent LED with 100 Ohm, ON)

Video => MVI_0235.avi (White Transparent LED with 100 Ohm)

Image => IMG_0214.jpg (White Short Transparent LED with 220 Ohm, OFF)

IMG_0213.jpg (White Short Transparent LED with 220 Ohm, ON)

Video => MVI_0212.avi (White Short Transparent LED with 220 Ohm)

Image => IMG_0201.jpg (RGB color Transparent LED with 330 Ohm, OFF)

Page 18: PIC 16F84 Practical Ch1 to 3

YWT-18

IMG_0202.jpg (RGB color Transparent LED with 330 Ohm, ON)

Video => MVI_0203.avi (RGB color Transparent LED with 330 Ohm)

You can run Example 1-1 on Project board with 5V power supply.

This section shows Example 1-1 circuit with various color of LED.

Image => IMG_0025.jpg (Red LED with 330 Ohm, OFF)

IMG_0181.jpg (Red LED with 330 Ohm, ON)

Video => MVI_0031.avi (Red LED with 330 Ohm)

Image => IMG_0033.jpg (Red Transparent LED with 150 Ohm, OFF)

Video => MVI_0034.avi (Red Transparent LED with 150 Ohm)

Image => IMG_0036.jpg (White Transparent LED with 100 Ohm, ON)

Video => MVI_0037.avi (White Transparent LED with 100 Ohm)

Image => IMG_0182.jpg (RGB color Transparent LED with 330 Ohm)

You can run Example 1-1 on PIC16F84 PCB board.

Video => MVI_0291.avi

Page 19: PIC 16F84 Practical Ch1 to 3

YWT-19

Example 1-2 => Exp1_2.c will blink two LED alternatively with 500 mili-seconds

delay. Exp1_2_2.c will "on" two LED(LED1&LED2=01,10,11,00) with 1000 mili-

seconds(1 second) delay.

Fig 1-8

Exp1_2.c Exp1_2_2.c

Fig 1-9 . Complete circuit diagram for example 1-2 and 1-2-2

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ

(or)

10KΩ

330

Ohm Red LED 2

330

OhmRed LED 1

330 Ohm Red LED 2

RB1

330 Ohm Red LED 1

RB0

main()

TRISB = 0xFC; while(1)

PORTB = 0x01; Delay_ms(500);

PORTB = 0x02; Delay_ms(500);

main()

TRISB=252; while(1)

PORTB=1; Delay_ms(1000); PORTB=2;

Delay_ms(1000); PORTB=3; Delay_ms(1000);

PORTB=0; Delay_ms(1000);

Page 20: PIC 16F84 Practical Ch1 to 3

YWT-20

You can run Example 1-2 with Real PIC Simulator.

Video => RealPicSimulator_Exp1_2.mp4

RealPicSimulator_Exp1_2Osc.mp4 (with Oscilloscope)

Project file to open with Real PIC Simulator => Exp1_2.rpp

Exp1_2Osc.rpp (with Oscilloscope)

You can run Example 1-2 on Project board with 9V battery and 7805 IC.

7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

This section shows Example 1-2 circuit with various color of LED.

Image => IMG_0237.jpg (Two Red LED with 330 Ohm)

Video => MVI_0238.avi (Two Red LED with 330 Ohm)

Image => IMG_0240.jpg ( Red(use 330 Ohm) and Green(use 220 Ohm) LED )

Video => MVI_0241.avi ( Red(use 330 Ohm) and Green(use 220 Ohm) LED )

You can run Example 1-2 on Project board with 5V power supply.

This section shows Example 1-2 circuit with various color of LED.

Image => IMG_0052.jpg ( Red(use 330 Ohm) and Green(use 220 Ohm) LED )

IMG_0053.jpg ( Red(use 330 Ohm) and Green(use 220 Ohm) LED )

Video => MVI_0054.avi ( Red(use 330 Ohm) and Green(use 220 Ohm) LED )

Page 21: PIC 16F84 Practical Ch1 to 3

YWT-21

Image => IMG_0043.jpg (Red(use 330 Ohm) and Yellow(use 330 Ohm) LED )

IMG_0048.jpg (Red(use 330 Ohm) and Yellow(use 330 Ohm) LED )

Video => MVI_0050.avi ( Red(use 330 Ohm) and Yellow(use 330 Ohm) LED )

Image => IMG_0039.jpg (Red(150 Ohm) and Yellow(150 Ohm) Transparent LED )

IMG_0040.jpg (Red(150 Ohm) and Yellow(150 Ohm) Transparent LED )

IMG_0041.jpg (Red(150 Ohm) and Yellow(150 Ohm) Transparent LED )

Video => MVI_0042.avi ( Red(150 Ohm) and Yellow(150 Ohm) Transparent LED )

You can run Example 1-2 on PIC16F84 PCB board.

Video => MVI_0292.avi

You can run Example 1-2-2 with Real PIC Simulator.

Video => RealPicSimulator_Exp1_2_2.mp4

Project file to open with Real PIC Simulator => Exp1_2_2.rpp

You can run Example 1-2-2 on Project board with 5V power supply.

Video => MVI_0055.avi ( Red(use 330 Ohm) and Green(use 220 Ohm) LED )

You can run Example 1-2-2 on PIC16F84 PCB board.

Video => MVI_0293.avi

Page 22: PIC 16F84 Practical Ch1 to 3

YWT-22

main()

char i,j;

TRISB = 0x00;

while(1)

j=1;

for(i=1;i<=8;i++)

PORTB = j;

j = j<<1;

Delay_ms(50);

Example 1_3 will run 8 LEDs from right to left(LED1 to LED8) by 1.

Fig 1-10

Explanation Exp1_3.c

At first of while loop, j=1; => j is 0000 0001, so only last bit (bit 0) will be “1”.

In for loop, i start with 1 and increase by 1 then end at 8.So this loop will do 8

times.

In for loop, put value of j to portB, so only RB0 will be ON because j is 00000001.

j=j<<1; => left shift j by 1 and override the new result to j. So, left shift 0000 0001

by 1 get 0000 0010. Then put this value to j. So, j’s current value is 0000 0010.

j=j<<1;(in binary) is same with j=jx2;(in decimal).

Delay_ms(50); => wait for 50 mili-seconds.

Then, repeat the first line of for loop with j value of 0000 0010. So, only RB1 will

be ON.then,left shift by 1, so j=00000100 ,50mili second delay,

Then repeat and this time, only RB2 will on.. till only RB7 will On and finish for

loop. Then repeat the first line of while loop … and while loop will do forever.

RB4

RB6

330 Ohm Red LED 1

RB0

330 Ohm

RB1

330 Ohm

RB2

330 Ohm

RB3

330 Ohm

330 Ohm

RB5

330 Ohm

330 Ohm

RB7

Red LED 2

Red LED 3

Red LED 4

Red LED 5

Red LED 6

Red LED 7

Red LED 8

Page 23: PIC 16F84 Practical Ch1 to 3

YWT-23

main()

char i,j;

TRISB = 0x00;

while(1)

j=1;

for(i=1;i<=8;i++)

PORTB = j;

j = j<<2;

Delay_ms(50);

main()

char i,j;

TRISB = 0x00;

while(1)

j=128;

for(i=1;i<=8;i++)

PORTB = j;

j = j>>1;

Delay_ms(50);

Example1_3_2.c will run 8 LEDs from right to left(LED1 to LED8) by 2.

Example1_3_3.c will run 8 LEDs from left to right(LED8 to LED1) by 1.

Exp1_3_2.c Exp1_3_3.c

Explanation

Exp1-3-2

j=1; => 0000 0001 in binary … j=j<<2; => left shift by 2 => 0000 0100

j=j<<2;(in binary) is same with j=jx4;(in decimal).

Exp1-3-3

j=128; => 1000 0000 in binary .. j=j>>1; => right shift by 1 => 0100 0000

j=j>>1;(in binary) is same with j=j/2;(in decimal).

Page 24: PIC 16F84 Practical Ch1 to 3

YWT-24

Fig 1-10 . Complete circuit diagram for example 1-3 and 1-3-2 and 1-3-3

You can run Example 1-3 with Real PIC Simulator.

Video => RealPicSimulator_Exp1_3.mp4

RealPicSimulator_Exp1_3Osc.mp4 (with Oscilloscope)

Project file to open with Real PIC Simulator => Exp1_3.rpp

Exp1_3Osc.rpp (with Oscilloscope)

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ

(or)

10kΩ

330

Ohm

330

Ohm

330

Ohm

330

Ohm

330

Ohm

330

Ohm

330

Ohm

Red LED 2

330

OhmRed LED 1

Red LED 3

Red LED 4

Red LED 5

Red LED 6

Red LED 7

Red LED 8

Page 25: PIC 16F84 Practical Ch1 to 3

YWT-25

PIC Simulator IDE Software

PIC Simulator IDE software is also a PIC simulator that can watch the changes of

Special Function Registers (SFRs) and General Purpose Registers (GPRs) of PIC

while running program in Hex file.

Special Function Registers and PIC’s internal hardware functions have exact

relations. For example, if you set TRISB register to 0 and PORTB register to 255,

all PortB pins(RB0 to RB7) will produce 5V.

You can see the value of TRISB and changing values of PORTB register in Hex

and Graphical form of bit by bit while running hex file in PIC simulator IDE.

PIC Simulator IDE installation video => PicSimulatorIDE_Install.mp4

Run Example 1_3 in PIC Simulator IDE video => PicSimulatorIDE_Exp1_3.mp4

You can run Example 1-3 on Project board with 9V battery and 7805 IC.

7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0070.jpg ( Red LED with 330 Ohm)

IMG_0077.jpg ( Red LED with 330 Ohm)

Video => MVI_0080.avi ( Red LED with 330 Ohm)

Image => IMG_0121.jpg ( color LED )

Video => MVI_0122.avi ( color LED )

You can run Example 1-3 on Project board with 5V power supply.

Image => IMG_0056.jpg ( Red LED with 330 Ohm-before connecting wires)

IMG_0059.jpg ( Red LED with 330 Ohm-before connecting wires)

IMG_0060.jpg ( Red LED with 330 Ohm)

Page 26: PIC 16F84 Practical Ch1 to 3

YWT-26

IMG_0064.jpg ( Red LED with 330 Ohm)

Video => MVI_0065.avi ( Red LED with 330 Ohm)

Image => IMG_0127.jpg ( color Transparent LED )

Video => MVI_0130.avi ( color Transparent LED )

You can run Example 1-3 on PIC16F84 PCB board.

Video => MVI_0294.avi ( color LED )

MVI_0295.avi ( with ZIF )

MVI_0296.avi ( Red LED )

You can run Example 1-3-2 with Real PIC Simulator.

Video => RealPicSimulator_Exp1_3_2.mp4

Project file to open with Real PIC Simulator => Exp1_3_2.rpp

You can run Example 1-3-2 on Project board with 9V battery and 7805 IC.

7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Video => MVI_0079.avi ( Red LED with 330 Ohm)

MVI_0123.avi ( color LED )

You can run Example 1-3-2 on Project board with 5V power supply.

Video => MVI_0066.avi ( Red LED with 330 Ohm)

MVI_0129.avi ( color LED )

Page 27: PIC 16F84 Practical Ch1 to 3

YWT-27

You can run Example 1-3-2 on PIC16F84 PCB board.

Video => MVI_0298.avi ( color LED )

MVI_0297.avi ( Red LED )

You can run Example 1-3-3 with Real PIC Simulator.

Video => RealPicSimulator_Exp1_3_3.mp4

Project file to open with Real PIC Simulator => Exp1_3_3.rpp

You can run Example 1-3-3 on Project board with 9V battery and 7805 IC.

7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Video => MVI_0075.avi ( Red LED with 330 Ohm)

MVI_0124.avi ( color LED )

You can run Example 1-3-3 on Project board with 5V power supply.

Video => MVI_0067.avi ( Red LED with 330 Ohm)

MVI_0128.avi ( color LED )

You can run Example 1-3-3 on PIC16F84 PCB board.

Video => MVI_0300.avi ( color LED )

MVI_0299.avi ( Red LED )

Page 28: PIC 16F84 Practical Ch1 to 3

YWT-28

CHAPTER (2)

INTPUT with Push Button

Example 2-1 and Example 2-2 with Fig 2-1.

Exp2_1.c

Fig 2-1 .s1 is normal =0, pressed =1 type

switch

Exp2_2.c

In Fig 2-1, Red LED will ON when RB0 pin is “1”(5V).

Before S1 press, RA0 is only connected to ground(0V) through 10K Ohm resistor,

so RA0 pin will be 0V(logic 0).

When S1 press, RA0 will connect 5V directly(virtually consider as through 0 Ohm

resistor) and connect to ground through 10K Ohm resistor .According to voltage

divider law , RA0 pin will be 5V(logic 1).

main()

TRISA = 1 ;

TRISB = 0 ;

while(1)

PORTB.F0=~PORTA.F0;

main()

TRISA = 1 ;

TRISB = 0 ;

while(1)

PORTB.F0=PORTA.F0;

330 Ohm

Red

LED

RB0

RA0

+5V

s1

10K

Ohm

Page 29: PIC 16F84 Practical Ch1 to 3

YWT-29

In Example 2-1 (when use with Fig 2-1), declare RA0 pin as input (by TRISA=1; )

and declare all Port B pins (RB0 to RB7) as output (by TRISB=0;).

Then set the PORTB.F0(bit 0 of Port B or RB0) to the value of PORTA.F0(bit 0 of

Port A or RA0) within continuously while loop. That mean that, when RA0 is 1,

RB0 will 1 and LED will ON. Otherwise , LED will be OFF. By using this program

with Fig 2-1, LED will only ON while push button S1 is pressed.

In Example 2-2 (when use with Fig 2-1), declare RA0 pin as input (by TRISA=1; )

and declare all Port B pins (RB0 to RB7) as output (by TRISB=0;).

Then set the PORTB.F0(bit 0 of Port B or RB0) to the opposite value of PORTA.F0

(bit 0 of Port A or RA0) within continuously while loop. That mean that, when RA0

is 0, RB0 will 1 and LED will ON. Otherwise , LED will be OFF. ( “~” means “apply

by Not Gate” ). By using this program with Fig 2-1, LED will only OFF while push

button S1 is pressed.

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ

(or)

10KΩ

330

OhmRed

LED

+5V

S1

10K

Ohm

Fig 2-2 .Complete circuit diagram for Example 2-1 and Example 2-2 with Fig 2-1.

Page 30: PIC 16F84 Practical Ch1 to 3

YWT-30

You can run Example 2-1 with Fig 2-1 in Real PIC Simulator.

Video => Exp2_1 with Fig2_1.mp4

Project file to open with Real PIC Simulator => Exp2_1 with Fig2_1.rpp

You can run Example 2-1 with Figure 2-1 on Project board with 9V battery and

7805 IC. 7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0243.jpg

Video => MVI_0245.avi

You can run Example 2-1 with Figure 2-1 on Project board with 5V power supply.

Image => IMG_0177.jpg

IMG_0178.jpg

Video => MVI_0179.avi

You can run Example 2-1 with Figure 2-1 on PIC16F84 PCB board.

Image => IMG_0301.jpg

Video => MVI_0304.avi

Page 31: PIC 16F84 Practical Ch1 to 3

YWT-31

You can run Example 2-2 with Fig 2-1 in Real PIC Simulator.

Video => Exp2_2 with Fig2_1.mp4

Project file to open with Real PIC Simulator => Exp2_2 with Fig2_1.rpp

You can run Example 2-2 with Figure 2-1 on Project board with 9V battery and

7805 IC. 7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0250.jpg

Video => MVI_0251.avi

You can run Example 2-2 with Figure 2-1 on Project board with 5V power supply.

Image => IMG_0174.jpg

IMG_0175.jpg

Video => MVI_0176.avi

You can run Example 2-2 with Figure 2-1 on PIC16F84 PCB board.

Video => MVI_0305.avi

Page 32: PIC 16F84 Practical Ch1 to 3

YWT-32

Example 2-1 and Example 2-2 with Fig 2-3.

Exp2_1.c

Ex2_2.c

Fig 2-3 . s1 is normal =1, pressed =0 type switch

In Fig 2-3, Red LED will ON when RB0 pin is “1”(5V).

Before S1 press, RA0 is only connected to 5V through 10K Ohm resistor, so RA0

pin will be 5V(logic 1).

When S1 press, RA0 will connect to ground(0V) directly(virtually consider as

through 0 Ohm resistor) and connect to 5V through 10K Ohm resistor .According

to voltage divider law , RA0 pin will be 0V(logic 0).

In Example 2-1 (when use with Fig 2-3, declare RA0 pin as input (by TRISA=1; )

and declare all Port B pins (RB0 to RB7) as output (by TRISB=0;).

Then set the PORTB.F0 (bit 0 of Port B or RB0) to the value of PORTA.F0 (bit 0 of

Port A or RA0) within continuously while loop. That means that, when RA0 is 1,

main()

TRISA = 1 ;

TRISB = 0 ;

while(1)

PORTB.F0=PORTA.F0;

330 Ohm

Red

LED

RB0

RA0

+5V

s1

10K

Ohm

main()

TRISA = 1 ;

TRISB = 0 ;

while(1)

PORTB.F0=~PORTA.F0;

Page 33: PIC 16F84 Practical Ch1 to 3

YWT-33

RB0 will 1 and LED will ON. Otherwise, LED will be OFF. By using this program

with Fig 2-3, LED will only OFF while push button S1 is pressed.

In Example 2-2 (when use with Fig 2-3), declare RA0 pin as input (by TRISA=1; )

and declare all Port B pins (RB0 to RB7) as output (by TRISB=0;).

Then set the PORTB.F0 (bit 0 of Port B or RB0) to the opposite value of PORTA.F0

(bit 0 of Port A or RA0) within continuously while loop. That means that, when

RA0 is 0, RB0 will 1 and LED will ON. Otherwise, LED will be OFF. ( “~” means

“apply by Not Gate” ). By using this program with Fig 2-3, LED will only ON while

push button S1 is pressed.

Fig 2-4 . Complete circuit diagram for Example 2-1 and Example 2-2 with Fig 2-3.

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10K Ohm

330

OhmRed

LED

+5V

10K

Ohm

S1

Page 34: PIC 16F84 Practical Ch1 to 3

YWT-34

You can run Example 2-1 with Fig 2-3 in Real PIC Simulator.

Video => Exp2_1 with Fig2_3.mp4

Project file to open with Real PIC Simulator => Exp2_1 with Fig2_3.rpp

You can run Example 2-1 with Figure 2-3 on Project board with 9V battery and

7805 IC. 7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0246.jpg

Video => MVI_0247.avi

You can run Example 2-1 with Figure 2-3 on Project board with 5V power supply.

Image => IMG_0170.jpg (Red LED-330 Ohm Resistor)

IMG_0155.jpg (Red Transparent LED-150 Ohm Resistor)

IMG_0156.jpg (Green Transparent LED-150 Ohm Resistor)

IMG_0160.jpg (Blue Transparent LED-100 Ohm Resistor)

IMG_0165.jpg (Green LED-220 Ohm Resistor)

Video => MVI_0169.avi (Red LED-330 Ohm Resistor)

You can run Example 2-1 with Figure 2-1 on PIC16F84 PCB board.

Image => IMG_0306.jpg

Video => MVI_0308.avi

Page 35: PIC 16F84 Practical Ch1 to 3

YWT-35

You can run Example 2-2 with Fig 2-3 in Real PIC Simulator.

Video => Exp2_2 with Fig2_3.mp4

Project file to open with Real PIC Simulator => Exp2_2 with Fig2_3.rpp

You can run Example 2-2 with Figure 2-3 on Project board with 9V battery and

7805 IC. 7805 IC is used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0252.jpg

Video => MVI_0249.avi

You can run Example 2-2 with Figure 2-3 on Project board with 5V power supply.

Image => IMG_0171.jpg

IMG_0172.jpg

Video => MVI_0173.avi

You can run Example 2-2 with Figure 2-3 on PIC16F84 PCB board.

Image => IMG_0306.jpg

Video => MVI_0307.avi

Page 36: PIC 16F84 Practical Ch1 to 3

YWT-36

Example 2-4.Toogle LED by S1 push button .

Fig 2-4 .

Exp2_3.c

In Fig 2-4, Red LED will ON when RB0 pin is “1”(5V).

Before S1 press, RA0 is only connected to 5V through 10K Ohm resistor, so RA0

pin will be 5V(logic 1).

When S1 press, RA0 will connect to ground(0V) directly(virtually consider as

through 0 Ohm resistor) and connect to 5V through 10K Ohm resistor .According

to voltage divider law , RA0 pin will be 0V(logic 0).

main()

TRISA = 1;

TRISB = 0;

PORTB=0;

while(1)

while(PORTA.F0==0)

while(PORTA.F0==0)

Delay_ms(100);

PORTB.F0=~PORTB.F0;

330 Ohm

Red

LED

RB0

RA0

+5V

s1

10K

Ohm

Page 37: PIC 16F84 Practical Ch1 to 3

YWT-37

Example 2-3 explanation

Declare RA0 pin as input by TRISA=1; command. Declare RB0 to RB7 pins

as output by TRISB=0; command. Then set PORTB’s initial value to 0 by

PORTB=0; , that means all PortB’s pins(RB0 to RB7) will be 0V at first.

This program has three while loop.

External while loop( while(1) ) will do continuously till the program run.

Middle while loop will do while RA0 pin(bit 0 of PortA) is 0V(logic 0) by the line of

“while(PORTA.F0==0)”. That means that this loop will do when you start to press

S1 push button.

Then the inner while loop will do until you release S1 push button because it will

also do if PORTA.F0 is 0. In this loop, wait 100 mili-second (0.1 second) by

Delay_ms(100); command. If you press S1 for 0.5 second and then release, this

internal loop will do 5 times and out to middle while loop because PORTA.F0 is

not equal to 1 now.

Then, the second command in the middle loop do. According to this command,

invert the current state of RB0(PORTB.F0) and then put to RB0. For example, if

current RB0 is logic 0(LED OFF), inverted value will be logic 1 and put that value

to RB0.So, RB0 is now logic 1(LED ON). If current RB0 is logic 1(LED ON),

inverted value will be logic 0 and put that value to RB0.So, RB0 is now logic

0(LED OFF).

Now, this middle while loop will also out to external while loop because current

value of RA0 is not equal to 0.

Then, go to first line of external while loop( while(1) ) and wait for the next time

of S1 press.This loop will do forever.

Page 38: PIC 16F84 Practical Ch1 to 3

YWT-38

Fig 2-5 . Complete circuit diagram for Example 2-3.

You can run Example 2-3 in Real PIC Simulator.

Video => RealPicSimulator_Exp2_3.mp4

Project file to open with Real PIC Simulator => Exp2_3.rpp

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10K Ohm

330

OhmRed

LED

+5V

10K

Ohm

S1

Page 39: PIC 16F84 Practical Ch1 to 3

YWT-39

You can run Example 2-3 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0252.jpg

Video => MVI_0255.avi

You can run Example 2-3 on Project board with 5V power supply.

Video => MVI_0164.avi (Red LED-330 Ohm Resistor)

MVI_0168.avi (Green LED-220 Ohm Resistor)

MVI_0154.avi (Red Transparent LED-150 Ohm Resistor)

MVI_0157.avi (Green Transparent LED-150 Ohm Resistor)

MVI_0161.avi (Blue Transparent LED-100 Ohm Resistor)

You can run Example 2-3 on PIC16F84 PCB board.

Image => IMG_0306.jpg

Video => MVI_0309.avi

Page 40: PIC 16F84 Practical Ch1 to 3

YWT-40

Example 2-4 .Press S1 to ON the LED and press S2 to OFF.

Fig 2-6.

Exp2_4.c

Fig 2-6 Explanation

While S1 is pressing, RA0 pin will “0”(0V) , otherwise “1”(5V).

While S2 is pressing, RA1 pin will “0”(0V) , otherwise “1”(5V).

While RB0 is being “1”(5V), LED will ON, otherwise OFF.

Example 2-4 Explanation

Declare the last two bits of PORTA(RA0 and RA1) as input by writing TRISA

register to 3(as binary-0b00000011) using the command-TRISA=0b00000011;

330 Ohm

Red

LED

RB0

RA0

+5V

S1

10K

Ohm

RA1

+5V

S2

10K

Ohm

main()

TRISA = 0b00000011;

TRISB = 0b11111110;

PORTB=0;

while(1)

while(PORTA.F0==0)

PORTB.F0=1;

while(PORTA.F0==0)

Delay_ms(100);

while(PORTA.F1==0)

PORTB.F0=0;

while(PORTA.F1==0)

Delay_ms(100);

Page 41: PIC 16F84 Practical Ch1 to 3

YWT-41

Declare the last bit of PORTB(RA0) as output and others in PORTB as input by

writing TRISB register to 254(as binary-0b11111110) using the command-

TRISB=0b11111110;

Set PORTB’s initial value to 0 to be 0V at all PORTB’s pins by PORTB=0;

Then, while(1) … loop will repeat continuously until the program stop.

In while(1) loop, there are two while loops. while(POTRA.F0==0) loop and

while(POTRA.F1==0) loop.

while(POTRA.F0==0) loop will do while RA0 is “0”(while S1 is pressed).In this

loop, set RB0 to “1”(to ON LED) and use other 0.1 second waiting while loop until

RA0 is “1”(when S1 is released).

while(POTRA.F1==0) loop will do while RA1 is “0”(while S1 is pressed).In this

loop, set RB0 to “0”(to OFF LED) and use other 0.1 second waiting while loop

until RA1 is “1”(when S1 is released).

Fig 2-7 . Complete circuit diagram for Example 2-4

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10K Ohm

330

OhmRed

LED

+5V

10K

Ohm

S1+5V

10K

Ohm

S2

Page 42: PIC 16F84 Practical Ch1 to 3

YWT-42

You can run Example 2-4 in Real PIC Simulator.

Video => RealPicSimulator_Exp2_4.mp4

Project file to open with Real PIC Simulator => Exp2_4.rpp

You can run Example 2-4 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0258.jpg

Video => MVI_0259.avi

You can run Example 2-4 on Project board with 5V power supply.

Image => IMG_0147.jpg

IMG_0148.jpg

IMG_0149.jpg

IMG_0150.jpg

Video => MVI_0151.avi

You can run Example 2-4 on PIC16F84 PCB board.

Image => IMG_0310.jpg

Video => MVI_0311.avi

Image => IMG_0312.jpg ( with Project Board )

Video => MVI_0313.avi ( with Project Board )

Page 43: PIC 16F84 Practical Ch1 to 3

YWT-43

CHAPTER (3)

PRODUCING SOUND

Frequency(f) is a number of cycles in one second. It's unit is Hertz(Hz).

Amplitude is a peak voltage. In sound wave,higher amplitude make louder sound.

In Fig3-1,the frequency(f) for that sine wave is 2Hz beause it contains 2

cycles in one second.The time for each cycle is called T and T=1/f .For this sine

wave,T for each cycle is ( T = 1/f = 1/2 = 0.5 second) 0.5 second.The frequency

range that can heard by human is from 20Hz to 20kHz.

Fig 3-1 .

PIC can generate square wave frequency with 5V amplitude as shown in Fig3-2.

The total time(T) for each cycle equal the sum of "on time" and "off time" of that

cycle. T=T1+T2=Ton+Toff=3ms+1ms =4ms. For frequency, f = 1/T = 1/4ms = 250

Hz.

Fig 3-2.

1st cycle 2ndcycle

Amplitude

(Voltage)

Time(second)0 0.5 1

T1 T2

0.5 second 0.5 second

Amplitude

(Voltage)

Time

(mili-second)1

T1 T2

4 mili-sec

4 5 8 90 2 3 6 7 10

0V

5V

4 mili-sec

Ton

3ms

1m

s

1st

cycle 2nd

cycle

To

ff

Page 44: PIC 16F84 Practical Ch1 to 3

YWT-44

So,we can hear that frequency by connecting with a speaker circuit as shown in

Fig3-3.

Fig 3-3.

If we want to produce 1kHz frequency(f) (with 50% duty cycle) for 2 second by PIC

, we need to calculate the time(T) for one cycle and also need to know how many

cycles(number of cycles-NC) are required for 2 seconds.

To calculate T, T = 1/f = 1/1k = 1ms.

Duty cycle = (Ton/T) *100% , if duty cycle is 50%, Ton =Toff = T/2 = 1ms/2 = 0.5ms

= 500 µs(micro-second).

For number of cycles (NC) for 2 seconds, NC = 2 second / T = 2sec/1msec = 2000

cycles.

To write a program, use 500 micro-second delay for Ton and Toff for one cycle

and need to produce 2000 cycles by looping as shown in Fig 3-4,Fig3-5 and

exp3_0.c.

Fig 3-4

PIC

8 Ohm

0.2 Watt

speaker

10µF

capacitor

Time-ms

(mili-second)

Amplitude

(Voltage)

Ton

5V, logic "1"

Toff

1ms 2ms0ms 2000ms

(2 seconds)

0V, logic "0"

.5m

s

1st

cycle

2nd

cycle

2000th

cycle

.5ms

Page 45: PIC 16F84 Practical Ch1 to 3

YWT-45

Example 3-0. Produce 1 KHz sound for 2 seconds.

Fig 3-5 .

Exp3_0.c

Fig 3-5 Explanation

The speaker will produce sound when RA3 pin produce some frequencies (by

changing RA3’s pin state- logic “0”(0V) and logic “1”(5V) a few hundreds or

thousands of times during per second) .

Example 3-0 Explanation

Declare i as integer by int i; and Declare RA3 as output pin by TRISA.F0 = 1;

Then, do for loop 2000 times (i=0 to 1999). Each time produce 1 frequency by RA3

pin. This program use RA3, but any I/O pin of PIC16F84(except RA4) can be used.

At each frequency, ON time(Ton) is 500 micro-second(0.5 mili-second) because

set RA3 to “1” by PORTA.F3=1; command and then wait 500 micro-second by

Delay_us(500); command.

After 500 micro-second of ON time, OFF time will occur for 500 micro-second (0.5

mili-second) because set RA3 to “0” by PORTA.F3=0; command and then wait 500

micro-second by Delay_us(500); command.

Each for loop produce a frequency and that frequency has 0.5 mili-second for ON

time(Ton) and 0.5 mili-second for OFF time(Toff). So,T=Ton+Toff = 0.5 + 0.5 = 1

mili-second.

RA3PIC

8 Ohm

0.2 Watt

speaker

10µF

capacitor

main()

int i;

TRISA.F3 = 0;

for (i=0;i<2000;i++)

PORTA.F3 = 1;

Delay_us(500);

PORTA.F3 = 0;

Delay_us(500);

Page 46: PIC 16F84 Practical Ch1 to 3

YWT-46

There are 2000 for loop, 1 mili-second x 2000 = 2000 mili-second = 2 seconds.

To calculate the frequency (f), (f=1/T ), f = 1/1 mili-second = 1000= 1 Kilo Hertz =

1KHz .So, this program produce 1KHz frequency for 2 seconds.

Fig 3-6 . Complete circuit diagram for Example 3-0, 3-1, 3-4 and 3-5

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10K Ohm

10µF

capacitor8 Ohm

0.2 Watt

speaker

Page 47: PIC 16F84 Practical Ch1 to 3

YWT-47

You can run Example 3-0 in Real PIC Simulator.

(Note - Real PIC Simulator version 1.1 has some sound error. Therefore, to run

sound simulations, Real PIC Simulator version 1.3 should be used.)

Video => RealPicSimulator_1.3_30dayTrial_Install.mp4

RealPicSimulator_Exp3_0.mp4

RealPicSimulator1.3_RunExp3_0.mp4

RealPicSimulator_1.3_Exp3_0Osc.mp4 ( with Oscilloscope)

RealPicSimulator1.3_RunExp3_0Osc.mp4( with Oscilloscope)

Project file to open with Real PIC Simulator => Exp3_0.rpp

Exp3_0Osc.rpp(with Oscilloscope)

You can run Example 3_0 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0269.jpg

Video => MVI_0268.avi

You can run Example 3_0 on Project board with 5V power supply.

Image => IMG_0131.jpg

Video => MVI_0133.avi

You can run Example 3_0 on PIC16F84 PCB board.

Image => IMG_0314.jpg

Video => MVI_0316.avi

Page 48: PIC 16F84 Practical Ch1 to 3

YWT-48

Example 3-1. Produce 500 Hz sound for the whole time.

Fig 3-5 .

Exp3_1.c

Complete circuit for this example is shown previously in Fig 3-6 .

Explanation

This example is similar to example 3-0.

Example 3-0 use 500 micro-second delay to get frequency of 1 KHz. But this

example use 1000 microsecond to get 500 Hz.

Example 3-0 use for loop for limited times. But this example use while(1) loop for

unlimited times or the whole time until the program stop.

You can run Example 3-1 in Real PIC Simulator.

(Note - Real PIC Simulator version 1.1 has some sound error. Therefore, to run

sound simulations, Real PIC Simulator version 1.3 should be used.)

Video => RealPicSimulator_Exp3_1.mp4

RealPicSimulator1.3_RunExp3_1.mp4

RealPicSimulator_Exp3_1Osc.mp4 ( with Oscilloscope)

Project file to open with Real PIC Simulator => Exp3_1.rpp

Exp3_1Osc.rpp(with Oscilloscope)

RA3PIC

8 Ohm

0.2 Watt

speaker

10µF

capacitor

main()

TRISA.F3 = 0;

While(1)

PORTA.F3 = 1;

Delay_us(1000);

PORTA.F3 = 0;

Delay_us(1000);

Page 49: PIC 16F84 Practical Ch1 to 3

YWT-49

You can run Example 3_1 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0269.jpg

Video => MVI_0270.avi

You can run Example 3_1 on Project board with 5V power supply.

Image => IMG_0131.jpg

Video => MVI_0135.avi

You can run Example 3_1 on PIC16F84 PCB board.

Image => IMG_0314.jpg

Video => MVI_0317.avi

Page 50: PIC 16F84 Practical Ch1 to 3

YWT-50

Example 3-2 . 1 KHz sound will be produced by speaker during the time while

press "s1" push button.

Fig 3-7 .

Exp3_2.c

Explanation

Declare RA3 pin as output by TRISA.F3=0; command and declare RA0 pin as

input by TRISA.F0=1; command.

Then, while(1) loop will do continuously until the program stop.

While you press s1 push button, RA0 will change to logic “0” and internal while

loop ( while(PORTA.F0) loop ) will do to produce 1 KHz sound from RA3 pin.

RA0

+5V

s1

10kO

RA3

8 Ohm

0.2 Watt

speaker

10µF

capacitor

main()

TRISA.F3 = 0;

TRISA.F0 = 1;

while(1)

while(PORTA.F0==0)

PORTA.F3 = 1;

Delay_us(500);

PORTA.F3 = 0;

Delay_us(500);

Page 51: PIC 16F84 Practical Ch1 to 3

YWT-51

Fig 3-8 . Complete circuit diagram for Example 3-2

You can run Example 3-2 in Real PIC Simulator.

(Note - Real PIC Simulator version 1.1 has some sound error. Therefore, to run

sound simulations, Real PIC Simulator version 1.3 should be used.)

Video => RealPicSimulator_Exp3_2.mp4

RealPicSimulator1.3_RunExp3_2.mp4

RealPicSimulator_Exp3_2Osc.mp4 ( with Oscilloscope)

RealPicSimulator1.3_RunExp3_2Osc.mp4 ( with Oscilloscope)

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10K Ohm

+5V

10K

Ohm

S1

10µF

capacitor8 Ohm

0.2 Watt

speaker

Page 52: PIC 16F84 Practical Ch1 to 3

YWT-52

Project file to open with Real PIC Simulator => Exp3_2.rpp

Exp3_2Osc.rpp(with Oscilloscope)

You can run Example 3_2 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0262.jpg

Video => MVI_0263.avi

You can run Example 3_2 on Project board with 5V power supply.

Image => IMG_0142.jpg

IMG_0143.jpg

Video => MVI_0144.avi

You can run Example 3_2 on PIC16F84 PCB board.

Image => IMG_0328.jpg

Video => MVI_0329.avi

Image => IMG_0326.jpg ( with Project Board )

Video => MVI_0327.avi ( with Project Board )

Page 53: PIC 16F84 Practical Ch1 to 3

YWT-53

Example 3-3. Creating 3 different sound using 3 push button.

Fig 3-9.

Exp3_3.c

Explanation

Declare last 3 bits of PORTA register (RA0, RA1 and RA2) as input and other

PORTA pins as output by TRISA=7; command( “7”in binary is 00000111).

Example 3-3 is similar to example 3-2.

main()

TRISA = 7;

while(1)

while(PORTA.F0==0)

PORTA.F3=1;

Delay_us(477);

PORTA.F3=0;

Delay_us(477);

while(PORTA.F1==0)

PORTA.F3=1;

Delay_us(425);

PORTA.F3=0;

Delay_us(425);

while(PORTA.F2==0)

PORTA.F3=1;

Delay_us(380);

PORTA.F3=0;

Delay_us(380);

RA0

+5V

S1

10kO

RA3

8 Ohm

0.2 Watt

speaker

10µF

capacitor

RA1

+5V

S2

10kO

RA2

+5V

S3

10kO

Page 54: PIC 16F84 Practical Ch1 to 3

YWT-54

Example 3-2 has one button in circuit and one while loop in while(1) loop to

produce one frequency while pressing that button.

But example 3-3 has three buttons in circuit and three while loop in while(1) loop

to produce three different frequencies while pressing each of three button.

Fig 3-10. Complete circuit for example 3-3

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10K Ohm

+5V

10K

Ohm

S1

+5V

10K

Ohm

S2

10µF

capacitor8 Ohm

0.2 Watt

speaker

+5V

10K

Ohm

S3

Page 55: PIC 16F84 Practical Ch1 to 3

YWT-55

You can run Example 3_3 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0281.jpg

Video => MVI_0282.avi

You can run Example 3_3 on Project board with 5V power supply.

Image => IMG_0146.jpg

IMG_0148.jpg

Video => MVI_0146.avi

You can run Example 3_3 on PIC16F84 PCB board.

Image => IMG_0330.jpg

IMG_0331.jpg

Video => MVI_0333.avi

Image => IMG_0335.jpg ( with Project Board )

Video => MVI_0336.avi ( with Project Board )

Page 56: PIC 16F84 Practical Ch1 to 3

YWT-56

Example 3-4. Producing long notes

for "Do" "Ray" "Mi" "Fa" "So" "La"

"Ti" “Do(high)”.

Fig 3-5.

Table for frequency of notes

Exp3_4.c

RA3PIC

8 Ohm

0.2 Watt

speaker

10µF

capacitor

N_C()

Note Sound Frequency(Hz)

Do 1047

N_D() Ray 1175

N_E() Mi 1319

N_F() Fa 1397

N_G() So 1568

N_A() La 1760

N_B() Ti 1976

N_C1() Do(high) 2093

N_S() no sound 0

void N_C(void);

void N_D(void);

void N_E(void);

void N_F(void);

void N_G(void);

void N_A(void);

void N_B(void);

void N_C1(void);

void N_S(void);

void main()

TRISA.F3=0;

N_C();N_C();N_C();

N_D();N_D();N_D();

N_E();N_E();N_E();

N_F();N_F();N_F();

N_G();N_G();N_G();

N_A();N_A();N_A();

N_B();N_B();N_B();

N_C1();N_C1();N_C1();

void N_C(void)

int i;

for (i=0;i<150;i++)

PORTA.F3=1;

Delay_us(477);

PORTA.F3=0;

Delay_us(477);

void N_D(void)

int i;

for (i=0;i<168;i++)

PORTA.F3=1;

Delay_us(425);

PORTA.F3=0;

Delay_us(425);

void N_E(void)

int i;

for (i=0;i<189;i++)

PORTA.F3=1;

Delay_us(379);

PORTA.F3=0;

Delay_us(379);

void N_F(void)

int i;

for (i=0;i<200;i++)

PORTA.F3=1;

Delay_us(358);

PORTA.F3=0;

Delay_us(358);

void N_G(void)

int i;

for (i=0;i<225;i++)

PORTA.F3=1;

Delay_us(318);

PORTA.F3=0;

Delay_us(318);

void N_A(void)

int i;

for (i=0;i<252;i++)

PORTA.F3=1;

Delay_us(284);

PORTA.F3=0;

Delay_us(284);

void N_B(void)

int i;

for (i=0;i<282;i++)

PORTA.F3=1;

Delay_us(253);

PORTA.F3=0;

Delay_us(253);

void N_C1(void)

int i;

for (i=0;i<300;i++)

PORTA.F3=1;

Delay_us(239);

PORTA.F3=0;

Delay_us(239);

void N_S(void)

int i;

for (i=0;i<567;i++)

PORTA.F3=0;

Delay_us(126);

Page 57: PIC 16F84 Practical Ch1 to 3

YWT-57

Explanation

This program uses 9 functions. These functions must be declare before main

program.

Void N_C(void) function produce the sound with 1047 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “Do” sound(Note C) of piano.

Void N_D(void) function produce the sound with 1175 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “Ray” (Note D)of piano.

Void N_E(void) function produce the sound with 1319 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “Mi” (Note E)of piano.

Void N_F(void) function produce the sound with 1397 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “Fa” (Note F)of piano.

Void N_G(void) function produce the sound with 1568 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “So” (Note G)of piano.

Void N_A(void) function produce the sound with 1760 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “La” (Note A)of piano.

Void N_B(void) function produce the sound with 1976 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “Ti” (Note B)of piano.

Void N_C1(void) function produce the sound with 2093 Hz frequency for 0.25

seconds at RA3 pin which is equivalent with “Do(High)” (Note C#)of piano.

Void N_S(void) function produce no sound( 0 Hz) for a very short period at RA3

pin. This function can be used as a break between two consecutive sound(notes).

The first “void” found in these functions means this function has no return value.

The last “void” found in these functions means that there is no need to use input

parameters to run that function.

When call these functions from main program, you can omit both of first “void”

word and last “void” word. For example, N_C(); , N_D(); , N_E(); , N_F(); , N_G(); ,

N_A(); , N_B(); , N_C1(); , N_S();

If long note of “C” take for 0.75 second, you can call N_C(); function 3 times

because it takes 0.25 second for N_C(); function 1 time. Eg - N_C(); N_C(); N_C();

If short note of “C” take for 0.5 second, you can call N_C(); function 2 times

because it takes 0.25 second for N_C(); function 1 time. Eg - N_C(); N_C();

Page 58: PIC 16F84 Practical Ch1 to 3

YWT-58

Complete circuit diagram for example 3-4 is shown in Fig 3-6 .

You can run Example 3_4 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0269.jpg

Video => MVI_0272.avi

You can run Example 3_4 on Project board with 5V power supply.

Image => IMG_0131.jpg

Video => MVI_0137.avi

You can run Example 3_4 on PIC16F84 PCB board.

Image => IMG_0314.jpg

Video => MVI_0318.avi

Page 59: PIC 16F84 Practical Ch1 to 3

YWT-59

Example 3-5 - Producing “Mya Nandar” song using piano key notes.

RA3PIC

8 Ohm

0.2 Watt

speaker

10µF

capacitor

void N_C(void);

void N_D(void);

void N_E(void);

void N_F(void);

void N_G(void);

void N_A(void);

void N_B(void);

void N_C1(void);

void N_S(void);

void main()

TRISA.F3=0;

N_F();N_F();N_F(); N_D();N_D();N_D();

N_A();N_A();N_A(); N_F();N_F();

N_E();N_E(); N_D();N_D();

N_D();N_D(); N_C();N_C();

N_B();N_B(); N_A();N_A();

N_S();N_S();N_S(); N_C();N_C();

N_D();N_D(); N_A();N_A();

N_C();N_C(); N_A();N_A();

N_G();N_G(); N_F();N_F();N_F();

N_G();N_G();N_G(); N_A();N_A();N_A();

N_C();N_C();N_C(); N_S();N_S();N_S();

N_E();N_E(); N_D();N_D();

N_C();N_C(); N_A();N_A();

N_G();N_G(); N_F();N_F();

N_F();N_F(); N_A();N_A();

N_E();N_E(); N_E();N_E();

N_E();N_E(); N_G();N_G();

N_G();N_G(); N_C();N_C();

N_F();N_F();N_F();

void N_C(void)

int i;

for (i=0;i<150;i++)

PORTA.F3=1;

Delay_us(477);

PORTA.F3=0;

Delay_us(477);

void N_D(void)

int i;

for (i=0;i<168;i++)

PORTA.F3=1;

Delay_us(425);

PORTA.F3=0;

Delay_us(425);

void N_E(void)

int i;

for (i=0;i<189;i++)

PORTA.F3=1;

Delay_us(379);

PORTA.F3=0;

Delay_us(379);

void N_F(void)

int i;

for (i=0;i<200;i++)

PORTA.F3=1;

Delay_us(358);

PORTA.F3=0;

Delay_us(358);

void N_G(void)

int i;

for (i=0;i<225;i++)

PORTA.F3=1;

Delay_us(318);

PORTA.F3=0;

Delay_us(318);

void N_A(void)

int i;

for (i=0;i<252;i++)

PORTA.F3=1;

Delay_us(284);

PORTA.F3=0;

Delay_us(284);

void N_B(void)

int i;

for (i=0;i<282;i++)

PORTA.F3=1;

Delay_us(253);

PORTA.F3=0;

Delay_us(253);

void N_C1(void)

int i;

for (i=0;i<300;i++)

PORTA.F3=1;

Delay_us(239);

PORTA.F3=0;

Delay_us(239);

void N_S(void)

int i;

for (i=0;i<567;i++)

PORTA.F3=0;

Delay_us(126);

Page 60: PIC 16F84 Practical Ch1 to 3

YWT-60

Explanation

Example 3-5 is similar to example 3-4. All functions and function declarations are

exactly the same. The difference is only in main program section. Both example

use RA3 pin as output(by TRISA.F0=3) and to connect to speaker via capacitor.

The main program section in Example 3-4 call functions to produce “Do” “Ray”

“Mi” “Fa” “So” “La” “Ti” “Do(high)” sound.

The main program section in Example 3-5 call functions to produce “Mya Nandar”

song.

Complete circuit diagram for example 3-5 is shown in Fig 3-6 .

You can run Example 3_5 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0269.jpg

Video => MVI_0275.avi

You can run Example 3_5 on Project board with 5V power supply.

Image => IMG_0131.jpg

Video => MVI_0140.avi

You can run Example 3_5 on PIC16F84 PCB board.

Image => IMG_0314.jpg

Video => MVI_0320.avi

Page 61: PIC 16F84 Practical Ch1 to 3

YWT-61

Example 3-6 – This example will do the following these 5 steps.

(1)produce notes for "Do" "Ray" "Mi" "Fa" "So" "La" "Ti" Do(high).

(2)make 5 seconds delay.

(3) produce “Mya Nandar” song using above notes.

(4) During the “ Mya Nandar” song -

While producing “Do”(note C) sound, LED at RB0 will be ON.

While producing “Ray”(note D) sound, LED at RB1 will be ON.

While producing “Mi”(note E) sound, LED at RB2 will be ON.

While producing “Fa”(note F) sound, LED at RB3 will be ON.

While producing “So”(note G) sound, LED at RB4 will be ON.

While producing “La”(note A) sound, LED at RB5 will be ON.

While producing “Ti”(note B) sound, LED at RB6 will be ON.

While producing “Do(High)”(note C#) sound, LED at RB7 will be ON.

(5)Blink all LEDs with 5 seconds ON and 4 seconds OFF until the power off.

Fig 3-11 .

RB4

RB6

330 Ohm Red LED 1

RB0

330 Ohm

RB1

330 Ohm

RB2

330 Ohm

RB3

330 Ohm

330 Ohm

RB5

330 Ohm

330 Ohm

RB7

Red LED 2

Red LED 3

Red LED 4

Red LED 5

Red LED 6

Red LED 7

Red LED 8

RA3PIC

8 Ohm

0.2 Watt

speaker

10µF

capacitor

Page 62: PIC 16F84 Practical Ch1 to 3

YWT-62

void N_C(void); // Do for 0.25 sec

void N_D(void); //Ray for 0.25 sec

void N_E(void); //Mi for 0.25 sec

void N_F(void); //Fa for 0.25 sec

void N_G(void); //So for 0.25 sec

void N_A(void); //La for 0.25 sec

void N_B(void); //Ti for 0.25 sec

void N_C1(void); //High Do for 0.25 sec

void N_S(void); // No Sound

void main()

TRISA.F3=0; //assign RA3 as output pin

TRISB=0; //assign RB0 to 7 as output pin

PORTB=0;

// start Do Ray Mi Fa So La Ti DoHIGH

N_C();N_C();N_C(); // Do for 0.75 seconds( 0.25 x 3 )

N_D();N_D();N_D(); // Ray for 0.75 seconds

N_E();N_E();N_E(); // Mi for 0.75 seconds

N_F();N_F();N_F(); // Fa for 0.75 seconds

N_G();N_G();N_G(); // So for 0.75 seconds

N_A();N_A();N_A(); // La for 0.75 seconds

N_B();N_B();N_B(); // Ti for 0.75 seconds

N_C1();N_C1();N_C1(); // Do HIGH for 0.75 seconds

//no sound for 5 seconds

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

//Start Mya Nandar(chos)

N_F();N_F();N_F(); N_D();N_D();N_D(); N_A();N_A();N_A();

N_F();N_F(); N_E();N_E(); N_D();N_D();

N_D();N_D(); N_C();N_C(); N_B();N_B(); N_A();N_A();

N_S();N_S();N_S(); N_C();N_C(); N_D();N_D();

N_A();N_A(); N_C();N_C(); N_A();N_A(); N_G();N_G();

N_F();N_F();N_F(); N_G();N_G();N_G(); N_A();N_A();N_A();

N_C();N_C();N_C();

N_S();N_S();N_S(); N_E();N_E(); N_D();N_D();

N_C();N_C(); N_A();N_A(); N_G();N_G(); N_F();N_F();

N_F();N_F(); N_A();N_A(); N_E();N_E();

N_E();N_E(); N_E();N_E(); N_G();N_G();

N_G();N_G(); N_C();N_C(); N_F();N_F();N_F();

// LEDs Blink

do

PORTB=255;

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

PORTB=0;

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

Delay_ms(1000);

while(1);

void N_C(void) // produce "Do" sound

int i;

PORTB.F0=1; // ON LED at RB0 pin

for (i=0;i<150;i++)

PORTA.F3=1;

Delay_us(477); //delay for 477 micro seconds

PORTA.F3=0;

Delay_us(477);

PORTB.F0=0; // OFF LED at RB0 pin

void N_D(void) // produce "Ray" sound

int i;

PORTB.F1=1; // ON LED at RB1 pin

for (i=0;i<168;i++)

PORTA.F3=1;

Delay_us(425);

PORTA.F3=0;

Delay_us(425);

PORTB.F1=0; // OFF LED at RB1 pin

void N_E(void) // produce "Mi" sound

int i;

PORTB.F2=1; // ON LED at RB2 pin

for (i=0;i<189;i++)

PORTA.F3=1;

Delay_us(379);

PORTA.F3=0;

Delay_us(379);

PORTB.F2=0; // OFF LED at RB2 pin

void N_F(void) // produce "Fa" sound

int i;

PORTB.F3=1; // ON LED at RB3 pin

for (i=0;i<200;i++)

PORTA.F3=1;

Delay_us(358);

PORTA.F3=0;

Delay_us(358);

PORTB.F3=0; // FF LED at RB3 pin

void N_G(void) // produce "So" sound

int i;

PORTB.F4=1; // ON LED at RB4 pin

for (i=0;i<225;i++)

PORTA.F3=1;

Delay_us(318);

PORTA.F3=0;

Delay_us(318);

PORTB.F4=0; // OFF LED at RB4 pin

void N_A(void) // produce "La" sound

int i;

PORTB.F5=1; // ON LED at RB5 pin

for (i=0;i<252;i++)

PORTA.F3=1;

Delay_us(284);

PORTA.F3=0;

Delay_us(284);

PORTB.F5=0; // OFF LED at RB5 pin

void N_B(void) // produce "Ti" sound

int i;

PORTB.F6=1; // ON LED at RB6 pin

for (i=0;i<282;i++)

PORTA.F3=1;

Delay_us(253);

PORTA.F3=0;

Delay_us(253);

PORTB.F6=0; // OFF LED at RB6 pin

void N_C1(void) // produce "Do(high)" sound

int i;

PORTB.F7=1; // ON LED at RB7 pin

for (i=0;i<300;i++)

PORTA.F3=1;

Delay_us(239);

PORTA.F3=0;

Delay_us(239);

PORTB.F7=0; // OFF LED at RB7 pin

void N_S(void) // make a break (or) no sound

int i;

for (i=0;i<567;i++)

PORTA.F3=0;

Delay_us(126);

Page 63: PIC 16F84 Practical Ch1 to 3

YWT-63

Explanation

This example combine example 3-4 and example 3-5 . And add some Delays and

modified N_C(), N_D(), N_E(), N_F(), N_G(), N_A(), N_B(), N_C1() functions to ON

corresponding LED at the beginning of the function and to OFF that LED at the

end of the function.

You can see comments to know the concept of lines in program. Comments are

used by programmers( start with “//” ) to note about something. You can write

comment by start with “//” and then follow any words as you like and these

comments will not take effect to the program’s working sequences.

The purpose of comments written in Exp3_6.c are only to understand the

program and you can omit that comments when you write again Exp3_6.c in

mikroC to compile.

You can run Example 3-6 in Real PIC Simulator.

(Note - Real PIC Simulator version 1.1 has some sound error. Therefore, to run

sound simulations, Real PIC Simulator version 1.3 should be used.)

Video => RealPicSimulator_Exp3_6.mp4

RealPicSimulator1.3_RunExp3_6.mp4

Project file to open with Real PIC Simulator => Exp3_6.rpp

You can run Example 3_6 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0081.jpg (Red LED-330 Ohm resistor)

IMG_0111.jpg (color LED)

MG_0113.jpg (color LED)

IMG_0115.jpg (color LED)

Video => MVI_0088.avi (Red LED-330 Ohm resistor)

MVI_0119.avi (color LED)

Page 64: PIC 16F84 Practical Ch1 to 3

YWT-64

You can run Example 3_6 on PIC16F84 PCB board.

Image => IMG_0314.jpg

Video => MVI_0321.avi

Page 65: PIC 16F84 Practical Ch1 to 3

YWT-65

Example 3-7 is the binary up / down counter .When you press S1 push button,

current binary value will increase by 1 and “Do” sound will be produced from

speaker connected with RA3 via 10uF capacitor. The resulted binary value will be

shown with 8 LED connected to RB0 to RB7.When you press S2 push button,

current binary value will increase by 1 and “Mi” sound will be produced from

speaker connected with RA3 via 10uF capacitor. The resulted binary value will be

shown with 8 LED connected to RB0 to RB7.

10kΩ

RA0

+5V

S1

(Up)

RA1

S2

(Down)

10kΩ

+5V

RA3

8 Ohm

0.2 Watt

speaker

10µF capacitor

RB4

RB6

330 Ohm Red LED 1

RB0

330 Ohm

RB1

330 Ohm

RB2

330 Ohm

RB3

330 Ohm

330 Ohm

RB5

330 Ohm

330 Ohm

RB7

Red LED 2

Red LED 3

Red LED 4

Red LED 5

Red LED 6

Red LED 7

Red LED 8

Fig 3-12 .

void N_C(void);

void N_E(void);

void main()

int i=0;

TRISA = 3;

TRISB = 0;

PORTA.F0=0;

PORTA.F1=0;

PORTB=0;

while(1)

PORTB=i ;

if (PORTA.F0==1)

i++;

if (i>255) i=255;

N_C();

if (PORTA.F1==1)

i--;

if (i<0) i=0;

N_E();

Delay_ms(100);

void N_C(void)

int i;

for (i=0;i<150;i++)

PORTA.F3=1;

Delay_us(477);

PORTA.F3=0;

Delay_us(477);

void N_E(void)

int i;

for (i=0;i<189;i++)

PORTA.F3=1;

Delay_us(379);

PORTA.F3=0;

Delay_us(379);

Page 66: PIC 16F84 Practical Ch1 to 3

YWT-66

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84 (or) PIC 16F84A

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

22pF22pF

4MHz

Crystal

+5V

+5V

4.7K Ohm

(or)

10k Ohm

Red LED 2

330

OhmRed LED 1

Red LED 3

Red LED 4

Red LED 5

Red LED 6

Red LED 7

Red LED 8

10µF

capacitor8 Ohm

0.2 Watt

speaker

330

Ohm

330

Ohm

330

Ohm

330

Ohm

330

Ohm

330

Ohm

330

Ohm+5V

10K

Ohm

S1

(Up)

+5V

10K

Ohm

S2

(Down)

Fig 3-13 . Complete circuit diagram for example 3-7

You can run Example 3-7 in Real PIC Simulator.

(Note - Real PIC Simulator version 1.1 has some sound error. Therefore, to run

sound simulations, Real PIC Simulator version 1.3 should be used.)

Video => RealPicSimulator_Exp3_7.mp4

Project file to open with Real PIC Simulator => Exp3_7.rpp

Page 67: PIC 16F84 Practical Ch1 to 3

YWT-67

You can run Example 3_7 on Project board with 9V battery and 7805 IC. 7805 IC is

used to convert 9V from 9V battery to 5V to supply PIC circuit.

Image => IMG_0090.jpg (Red LED-330 Ohm resistor)

IMG_0092.jpg (Red LED-330 Ohm resistor)

IMG_0098.jpg (color LED)

IMG_0104.jpg (color LED)

Video => MVI_0094.avi (Red LED-330 Ohm resistor)

MVI_0107.avi (color LED)

You can run Example 3_7 on PIC16F84 PCB board.

Image => IMG_0322.jpg

Video => MVI_0323.avi

Image => IMG_0324.jpg ( with Project Board )

Video => MVI_0325.avi ( with Project Board )

Page 68: PIC 16F84 Practical Ch1 to 3

YWT-68

22pF22pF

4MHz

Crystal

+5V

+5V

4.7KΩ+

5V

Gn

d

3x4 Project Board

3x3 Project Board

+5V

USB connector

for 5V power

Screw

connector

330Ω RGB

LED

100Ω Blue

LED

100Ω Violet

LED

150Ω Pink

LED

Green

LED

Ground

Jumper

1 2 3 4 5 6 7 8 9

18 17 16 15 14 13 12 11 10

PIC 16F84A (or) PIC 16F84

RA2 RA3 RA4 MCLR VSS RB0 RB1 RB2 RB3

RB7 RB6 RB5 RB4RA1 RA0 OSC1 OSC2 VDD

RA1RA0 RA2 RA3 RA4

connector

connector connector

150Ω

150Ω

150Ω

Yellow

LED

Orange

LED

Red

LED

+5V in/out

Gnd

150Ω

+5V

10µF

capacitor

8 Ω speaker

10kΩ

10kΩ

S2S1

Up

Down

Fig 3-14 - Example 3_6 on PIC16F84 PCB board