You are on page 1of 68

YWT-1

PIC 16F84(A) Practical


Part(1) Basic
Introduction

To view the following file names with underline, press mouses 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
10F capacitor x 1

Optional (not important)


Transparent Red LED
Transparent Orange LED
Transparent Yellow LED
Transparent Green LED
Transparent Blue LED
Transparent Violet LED
Transparent Pink LED
Transparent RGB color LED
Transparent White LED
Transparent White Short LED
Yellow LED
Green LED

100 resistor( Watt)


150 resistor( Watt)
220 resistor( Watt)
330 resistor( Watt)

x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1

x3
x5
x2
x2

5V Power Adaptor (USB phone charger) x 1


USB cable( to use with phone charger) x 1

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)

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

YWT-4

7805

Vin
+
9V(or)12V _

Vout
+
Gnd

5V
_

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)

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

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 builtin (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

22pF

22pF

+5V
4MHz
Crystal

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K
(or)
10K

+5V

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

YWT-7

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


Screw
connector

+5V in/out

330

+5V
Gnd

RGB
LED

3x3 Project Board


22pF

22pF

150

Pink
LED

100

Violet
LED

100

Blue
LED

150

Green
LED

+5V
4MHz
Crystal

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

+5V

PIC 16F84A (or) PIC 16F84


Gnd

USB
connector
For
5V power

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

connector
150 Yellow
LED

3x4 Project Board


RA0

RA1

RA2

RA3

RA4

150 Orange
LED

Ground
Jumper

4.7K

+5V

connector

connector
150

Red
LED

+5V

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.

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

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

TRISA
0x85

RA1 RA0 (LSB)

RA4

PORTA
0x05

RA3 RA2

RA3 RA2 RA1

RA0 (LSB)

5V

5V

0V

5V

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

TRISB
0x86

PORTB
0x06

RB6 RB5

RB4 RB3 RB2 RB1

RB0(LSB)

RB7

RB6

5V

5V

0V

0V

5V

5V

RB5 RB4

RB3 RB2 RB1 RB0(LSB)

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

YWT-10

Example 1-0 => After burning Exp1_0.hex to PIC, PortBs RB0 pin will be assign
as output pin and produce 5V( logic 1 ).So, the red LED will be ON.
main()
{
RB0
330 Ohm

TRISB=0;

Red LED

PORTB=1;
}

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 LEDs 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.
22pF

22pF

+5V
4MHz
Crystal
18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K
(or)
10K

+5V

330
Ohm

Red
LED

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

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.

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

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.

RB0
330 Ohm

Red LED

Fig 1-6
Exp1_1.c

main()
{
TRISB = 0xFE ;
while(1)
{
PORTB = 0x01;
Delay_ms(500);
PORTB = 0x00;
Delay_ms(500);
}
}

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.

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.

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)

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)

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

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 miliseconds(1 second) delay.
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);
}
}

RB0
330 Ohm

Red LED 1

330 Ohm

Red LED 2

main()
{
TRISB = 0xFC;
while(1)
{
PORTB = 0x01;
Delay_ms(500);
PORTB = 0x02;
Delay_ms(500);
}
}

RB1

Fig 1-8
Exp1_2.c

22pF

Exp1_2_2.c

22pF

+5V
4MHz
Crystal
18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K

330
Ohm Red LED 2

(or)
10K

+5V

330
Red LED 1
Ohm

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

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 )

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

YWT-22

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

RB0

RB4
330 Ohm

Red LED 1

RB1

330 Ohm

Red LED 5

RB5
330 Ohm

Red LED 2

330 Ohm

Red LED 3

RB2

330 Ohm

Red LED 6

330 Ohm

Red LED 7

RB6

RB3

RB7
330 Ohm

Red LED 4

330 Ohm

Red LED 8

Fig 1-10
Explanation

main()
{
char i,j;
TRISB = 0x00;
while(1)
{
j=1;
for(i=1;i<=8;i++)
{
PORTB = j;
j = j<<1;
Delay_ms(50);
}
}
}
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, js 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.

YWT-23

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.
main()
{
char i,j;
TRISB = 0x00;
while(1)
{
j=128;
for(i=1;i<=8;i++)
{
PORTB = j;
j = j>>1;
Delay_ms(50);
}
}
}

main()
{
char i,j;
TRISB = 0x00;
while(1)
{
j=1;
for(i=1;i<=8;i++)
{
PORTB = j;
j = j<<2;
Delay_ms(50);
}
}
}
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).

YWT-24

22pF

22pF

330
Ohm

Red LED 8

330
Ohm

Red LED 7

330
Ohm

Red LED 6

330
Ohm

Red LED 5

330
Ohm

Red LED 4

330
Ohm

Red LED 3

+5V
4MHz
Crystal
18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K
(or)
10k

+5V

330
Ohm

Red LED 2

330
Red LED 1
Ohm

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)

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 PICs 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)

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 )

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 )

YWT-28

CHAPTER (2)
INTPUT with Push Button
Example 2-1 and Example 2-2 with Fig 2-1.

+5V

s1

main()
{
TRISA = 1 ;
TRISB = 0 ;
while(1)
{
PORTB.F0=PORTA.F0;
}
}

Exp2_1.c

RA0

RB0
330 Ohm
Red
LED

10K
Ohm

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


switch

main()
{
TRISA = 1 ;
TRISB = 0 ;
while(1)
{
PORTB.F0=~PORTA.F0;
}
}

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

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.

+5V
22pF

22pF

S1
+5V
4MHz
Crystal
18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

10K
Ohm

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K
(or)
10K

+5V

330
Ohm

Red
LED

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

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

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

YWT-32

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


+5V

10K
Ohm

main()
{
TRISA = 1 ;
TRISB = 0 ;
while(1)
{
PORTB.F0=PORTA.F0;
}
}

Exp2_1.c
RA0

RB0
330 Ohm
Red
LED

s1

main()
{
TRISA = 1 ;
TRISB = 0 ;
while(1)
{
PORTB.F0=~PORTA.F0;
}
}

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,

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.

+5V
22pF

22pF

10K
Ohm

+5V
4MHz
Crystal

S1

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K Ohm
(or)
10K Ohm

+5V

330
Ohm

Red
LED

Fig 2-4 . Complete circuit diagram for Example 2-1 and Example 2-2 with Fig 2-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

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

YWT-36

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


+5V

main()
{
TRISA = 1;
TRISB = 0;
PORTB=0;
while(1)
{
while(PORTA.F0==0)
{
while(PORTA.F0==0)
{

10K
Ohm

RA0

Delay_ms(100);

RB0

330 Ohm
Red
LED

PORTB.F0=~PORTB.F0;

s1

}
}
}

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

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 PORTBs initial value to 0 by
PORTB=0; , that means all PortBs 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.

YWT-38

+5V
22pF

22pF

10K
Ohm

+5V
4MHz
Crystal

S1

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K Ohm
(or)
10K Ohm

+5V

330
Ohm

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

Red
LED

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

YWT-40

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

+5V

+5V

10K
Ohm

10K
Ohm

RA0

RA1

RB0
330 Ohm
S1

S2

Red
LED

Fig 2-6.

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);
}
}
}
}

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;

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 commandTRISB=0b11111110;
Set PORTBs initial value to 0 to be 0V at all PORTBs 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).

+5V
22pF

22pF

10K
Ohm

+5V
4MHz
Crystal

+5V

S1

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

10K
Ohm

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

S2
4.7K Ohm
(or)
10K Ohm

+5V

330
Ohm

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

Red
LED

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 )

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.
Amplitude
(Voltage)
1st cycle

2ndcycle

Time(second)

0.5

T1
T2
0.5 second 0.5 second

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.
Amplitude
(Voltage)
1stcycle

5V

3ms

1ms Toff

Ton

2ndcycle

0V
0 1 2 3 4 5 6 7 8 9 10
T1
T2
4 mili-sec
4 mili-sec

Fig 3-2.

Time
(mili-second)

YWT-44

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

10F
capacitor

PIC
8 Ohm
0.2 Watt
speaker

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.
Amplitude
(Voltage)
1st
cycle

2000 th
cycle

2nd
cycle

5V, logic "1"

.5ms

.5ms

Ton Toff

0V, logic "0"


0ms

Time-ms
(mili-second)
1ms

2ms

Fig 3-4

2000ms
(2 seconds)

YWT-45

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

10F
capacitor

PIC

RA3
8 Ohm
0.2 Watt
speaker

Fig 3-5 .

main()
{
int i;
TRISA.F3 = 0;
for (i=0;i<2000;i++)
{
PORTA.F3 = 1;
Delay_us(500);
PORTA.F3 = 0;
Delay_us(500);
}
}
Exp3_0.c

Fig 3-5 Explanation


The speaker will produce sound when RA3 pin produce some frequencies (by
changing RA3s 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.

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.

22pF

22pF

+5V
4MHz
Crystal
18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K Ohm
(or)
10K Ohm

8 Ohm
0.2 Watt
speaker

10F
capacitor
+5V

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

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

YWT-48

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

10F
capacitor

PIC

RA3
8 Ohm
0.2 Watt
speaker

main()
{
TRISA.F3 = 0;
While(1)
{
PORTA.F3 = 1;
Delay_us(1000);
PORTA.F3 = 0;
Delay_us(1000);
}
}

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)

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

YWT-50

Example 3-2 . 1 KHz sound will be produced by speaker during the time while
press "s1" push button.
+5V

10kO
10F
capacitor
RA3

RA0
8 Ohm
0.2 Watt
speaker

Fig 3-7 .

s1

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);
}
}
}
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.

YWT-51

+5V
22pF

22pF

10K
Ohm

+5V
4MHz
Crystal

S1

18

17

RA1

RA0

16

15

14

13

OSC1

OSC2

VDD

RB7

12
RB6

11

10

RB5

RB4

PIC 16F84 (or) PIC 16F84A

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K Ohm
(or)
10K Ohm

8 Ohm
0.2 Watt
speaker

10F
capacitor
+5V

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)

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 )

YWT-53

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

+5V

+5V

10kO

+5V

10kO

RA0

10kO

RA1

S1

RA2

S2

10F
capacitor
RA3
8 Ohm
0.2 Watt
speaker

Fig 3-9.

S3

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);
}
}
}
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( 7in binary is 00000111).
Example 3-3 is similar to example 3-2.

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.

+5V

10K
Ohm

22pF

22pF

+5V
4MHz
Crystal

+5V
S1

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

10K
Ohm

PIC 16F84 (or) PIC 16F84A


+5V
S2

10K
Ohm

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K Ohm
(or)
10K Ohm

+5V

S3
8 Ohm
0.2 Watt
speaker

10F
capacitor

Fig 3-10. Complete circuit for example 3-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 )

YWT-56

Example 3-4. Producing long notes


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

10F
capacitor

PIC

RA3
8 Ohm
0.2 Watt
speaker

Fig 3-5.

Note

Sound

Frequency(Hz)

N_C()

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

Table for frequency of notes

Exp3_4.c

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);
}
}

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();

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

YWT-59

Example 3-5 - Producing Mya Nandar song using piano key notes.
10F
capacitor

PIC

RA3
8 Ohm
0.2 Watt
speaker

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);
}
}

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

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.
10F
capacitor

PIC

RA3
8 Ohm
0.2 Watt
speaker

RB0

RB4
330 Ohm

Red LED 1

RB1

330 Ohm

Red LED 5

RB5
330 Ohm

Red LED 2

330 Ohm

Red LED 3

RB2

330 Ohm

Red LED 6

330 Ohm

Red LED 7

RB6

RB3

RB7
330 Ohm

Red LED 4

Fig 3-11 .

330 Ohm

Red LED 8

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); }
}

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 programs 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)

YWT-64

You can run Example 3_6 on PIC16F84 PCB board.

Image => IMG_0314.jpg


Video => MVI_0321.avi

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.
void N_C(void);
void N_E(void);
+5V

+5V
S1
(Up)

S2
(Down)

RA1

RA0

10k

10k

10F capacitor
RA3

8 Ohm
0.2 Watt
speaker

RB0

RB4
330 Ohm

Red LED 1

RB1

330 Ohm

Red LED 5

RB5
330 Ohm

Red LED 2

330 Ohm

Red LED 3

RB2

330 Ohm

Red LED 6

330 Ohm

Red LED 7

RB6

RB3

RB7
330 Ohm

Red LED 4

Fig 3-12 .

330 Ohm

Red LED 8

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);
}
}

YWT-66

+5V
S1
(Up)

22pF

22pF

330
Ohm

Red LED 8

330
Ohm

Red LED 7

330
Ohm

Red LED 6

330
Ohm

Red LED 5

330
Ohm

Red LED 4

330
Ohm

Red LED 3

+5V
4MHz
Crystal

+5V

10K
Ohm

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

S2
(Down)

PIC 16F84 (or) PIC 16F84A

10K
Ohm

RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

4.7K Ohm
(or)
10k Ohm

8 Ohm
0.2 Watt
speaker

10F
capacitor

+5V

330
Ohm

Red LED 2

330
Ohm

Red LED 1

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

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 )

YWT-68

+5V in/out

Screw
connector

330

+5V
Gnd

RGB
LED

3x3 Project Board


22pF

22pF

150

Pink
LED

100

Violet
LED

100

Blue
LED

150

Green
LED

+5V
4MHz
Crystal

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1

OSC2

VDD

RB7

RB6

RB5

RB4

+5V

PIC 16F84A (or) PIC 16F84


Gnd

USB connector
for 5V power
RA2

RA3

RA4

MCLR

VSS

RB0

RB1

RB2

RB3

connector
150 Yellow
LED

10F
capacitor

3x4 Project Board


RA0

RA1

RA2

RA3

RA4
10k
150 Orange
LED

10k

Ground
Jumper

4.7K

+5V

connector

connector
150

8
speaker

+5V

S2
S1
Down
Up

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

Red
LED

You might also like