You are on page 1of 12

BFM3333 Microcontroller System

Lab 01 Report - Introduction to Microcontroller


HO JUN FU (FB14059) & CHIN KUO HAO (FB14071)
Faculty of Manufacturing Engineering
Universiti Malaysia Pahang
Pekan, Malaysia
Abstract This is an introduction to microcontroller
system and embedded device which later will expose to
microcontroller concept and design of embedded device
which include design fundamental, peripheral interfacing
technique, serial communication subsystem, interrupt
subsystem, and timing subsystem.

I. INTRODUCTION
A microcontroller is a self-supporting single chip
processor with all constituent subsystems of an ordinary
desktop computer. A microcontroller is basically a
computer, which is in much smaller size. Microcontroller
is a specific purpose computer which run specific
program and commands, unlike the general purpose
computer that we use daily. Microcontroller is an
integrated device with features of central processing unit
(CPU), ROM (read-only-memory), RAM (random access
memory), registers, input output ports (such as UARTs),
peripherals (such as timers and event counters), analog-todigital converter/ digital-to-analog converter, clock and
others serial communications interfaces (I2C, Controller
Area Network). Microcontroller is usually embedded
inside a device which require moderate amount of local
intelligence within given application. Microcontroller is
employed to control the functions, actions and features of
the device. Microcontroller is programmed to run specific
program and is dedicated to specific task. This specific
program is saved and stored in the ROM of the
microcontroller. [2]
II. OBJECTIVE
A. Able to understand the specification of
Microcontroller, Peripheral Interface Controller
(PIC), circuit and wiring of the PIC.
B. Able to understand the basic concept of integrating
between the PICBasicPro, Proteus and PICKit2 with
SK40C and UIC00B.

III. LAB EXERCISES


A. Logic Input/ Output
There are few definitions and configurations is needed
for this section. DEFINE OSC is required to define the
synchronization of most of PBPs time dependent
functions for example pause, to the oscillator that feed the
PIC chip. If OSC in not defined, these functions will
assume to be 4 mHz. By defining DEFINE OSC 20, the
oscillator will be set to frequency of 20 mHz.
TRISx is to initialize and configure the input and
output of the PORTx. For example, TRISB =
%00000011 is to configuring PORTB as the input and
output, 0 for output and 1 for input which means
PORTB.0 and PORTB.1 are configured as input while
PORTB.2 to PORTB.7 are configured as outputs. % is
used for binary definition while $ is used for
hexadecimal.
PORTx is to initialize the output value of PORTx.
PORTB = $00 is defined in hexadecimal to set all the
pins of PORTB initially as low. PORTB = %00000000
is used for binary.
MAINLOOP is where the programming codes are
inserted to be compiled. Using IF and THEN as the
selection statements for how the PORT or pins works
logically. ENDIF is required every time if the function
IF is used so that the system will know when to end the
selection statements.
PAUSE x is the milliseconds in which the program
will pause. For example, the LED will light for 500
milliseconds for PAUSE 500.
GOTO MAINLOOP function as a loop to the
functions in MAINLOOP. GOTO MAINLOOP
function to start back the program from the
MAINLOOP. This ensure that the program will run
endlessly until it is stop externally or reset.

1) LED and Switches Logic.


i. Produce a circuit when SW1 is ON, LED1
should be ON.
Code:
DEFINE OSC 20
TRISB = %00000011
PORTB = $00
MAINLOOP
IF PORTB.0 = 0 THEN
PORTB.6 = 1
ELSE
PORTB.6 = 0
ENDIF
PAUSE 1000
GOTO MAINLOOP
Explanation:
The frequency of the oscillator is set to 20 mHz
and PORTB is configured as the input and output for the
program. In this case, PORTB.0 and PORTB.1 are set as
input while PORTB.2 to PORTB.7 are set as outputs. In
the mainloop, it stated that PORTB.6 will be high when
PORTB.0 is low (active low) or else PORTB.6 will
remain low. In this case, PORTB.0 is SW1 while
PORTB.6 is LED1. As SW1 is pressed (ON), PORTB.0 is
low (active low), LED1 will lights up. The program is
paused for 1000 milliseconds if the selection statements is
performed, which means LED1 will light up for 1000
milliseconds. Then the statements in mainloop will start
all over again.
Simulation using Proteus:

Fig. 1. LED and Switches Logic (i) when switch is OFF.

Fig. 2. LED and Switches Logic (i) when switch is ON.

ii. Produce a circuit such that when SW1 and SW2


is ON, LED is ON.
Code:
DEFINE OSC 20
TRISB = %00000011
PORTB = $00
MAINLOOP
IF PORTB.0 = 0 AND PORTB.1 = 0 THEN
PORTB.6 = 1
ELSE
PORTB.6 = 0
ENDIF
PAUSE 1000
GOTO MAINLOOP
Explanation:
The oscillator is set to frequency of 20 mHz and
PORTB is configure as the input and output for this
program. PORTB.0 and PORTB.1 are set as input while
PORTB.2 to PORTB.7 are set as outputs. In mainloop, if
both PORTB.0 and PORTB.1 both are low (active low),
PORTB.6 will be high or else PORTB.6 will remain low.
In this case, PORTB.0 is SW1 and PORTB.1 is SW2,
while PORTB.6 is LED1. As SW1 and SW2 are pressed
(ON), PORTB.0 is low (active low), LED1 will lights up.
The program will pause for 1000 milliseconds if the
selective statements is executed, which means LED1 will
light up for 1000 milliseconds. Then the statements in
mainloop will start all over again.

Simulation using Proteus:

Fig. 6. LED and Switches Logic (ii) when SW1 and SW2 are ON.
Fig. 3. LED and Switches Logic (ii) when SW1 and SW2 are OFF.

iii. Produce a circuit such that when either of SW 1


or SW2 is ON, LED1 is ON.
Code:
DEFINE OSC 20
TRISB = %00000011
PORTB = $00
MAINLOOP
IF PORTB.0 = 0 OR PORTB.1 = 0 THEN
PORTB.6 = 1
ELSE
PORTB.6 = 0

Fig. 4. LED and Switches Logic (ii) when only SW1 is ON.

ENDIF
PAUSE 1000
GOTO MAINLOOP
Explanation:

Fig. 5. LED and Switches Logic (ii) when only SW2 is ON.

The oscillator is set to frequency of 20 mHz and


PORTB is configure as the input and output for this
program. PORTB.0 and PORTB.1 are set as input while
PORTB.2 to PORTB.7 are set as outputs. In mainloop, if
PORTB.0 or PORTB.1 are low (active low), then
PORTB.6 will be high or else PORTB.6 will remain low.
In this case, PORTB.0 is SW1 and PORTB.1 is SW2,
while PORTB.6 is LED1. As SW1 or SW2 is pressed
(ON), PORTB.0 is low (active low), LED1 will lights up.
The program will pause for 1000 milliseconds if the
selective statements is executed, which means LED1 will
light up for 1000 milliseconds. Then the statements in
mainloop will start all over again.

Simulation using Proteus:


iv. Produce a circuit such that when SW1 is ON,
LED1 and LED2 are ON.
Code:
DEFINE OSC 20
TRISB = %00000011
PORTB = $00
MAINLOOP
IF PORTB.0 = 1 THEN
PORTB.6 = 1
PORTB.7 = 1

Fig. 7. LED and Switches Logic (iii) when SW1 and SW2 are OFF.

ELSE
PORTB.6 = 0
PORTB.7 = 0
ENDIF
PAUSE 1000
GOTO MAINLOOP
Explanation:

Fig. 8. LED and Switches Logic (iii) when SW1 is ON and SW2 is OFF.

The oscillator is set to frequency of 20 mHz and


PORTB is configure as the input and output for this
program. PORTB.0 and PORTB.1 are set as input while
PORTB.2 to PORTB.7 are set as outputs. In mainloop, if
PORTB.0 is low (active low), then PORTB.6 and
PORTB.7 will be high or else they will remain low. In this
case, PORTB.0 is SW1 while PORTB.6 is LED1 and
PORTB.7 is LED2. As SW1 is pressed (ON), PORTB.0 is
low (active low), LED1 and LED2 will lights up. The
program will pause for 1000 milliseconds if the selective
statements is executed, which means LED will light up for
1000 milliseconds. Then the statements in mainloop will
start all over again.
Simulation using Proteus:

Fig. 9. LED and Switches Logic (iii) when SW1 is OFF and SW2 is ON.

Fig. 10. LED and Switches Logic (iv) when SW1 is OFF.

PORTB.1 is low (active low), LED2 will lights up. The


program will pause for 1000 milliseconds if the selective
statements is executed, which means the LED will light
up for 1000 milliseconds. Then the statements in
mainloop will start all over again.
Simulation using Proteus:

Fig. 11. LED and Switches Logic (iv) when SW1 is ON.

v. Produce a circuit when SW1 is ON, LED1 is ON


and LED2 is OFF & when SW2 is ON, LED2
should be ON and LED1 is OFF.
Code:
Fig. 12. LED and Switches Logic (v) when SW1 and SW2 are OFF.

DEFINE OSC 20
TRISB = %00000011
PORTB = $00
MAINLOOP
IF PORTB.0 = 0 THEN
PORTB.6 = 1
PORTB.7 = 0
ELSEIF PORTB.1 = 0 THEN
PORTB.6 = 0
PORTB.7 = 1
ELSE
PORTB.6 = 0
PORTB.7 = 0

Fig. 13. LED and Switches Logic (v) when SW1 is ON and SW2 is
OFF.

ENDIF
PAUSE 5
GOTO MAINLOOP
Explanation:
The oscillator is set to frequency of 20 mHz and
PORTB is configure as the input and output for this
program. PORTB.0 and PORTB.1 are set as input while
PORTB.2 to PORTB.7 are set as outputs. In mainloop, if
PORTB.0 is low (active low), then PORTB.6 will be high
while PORTB.7 will remain low, else if PORTB.1 is low
(active low), then PORTB.6 will be low and PORTB.7
will be high. In this case, PORTB.0 is SW1 and PORTB.1
is SW2 while PORTB.6 is LED1 and PORTB.7 is LED2.
As SW1 is pressed (ON), PORTB.0 is low (active low),
LED1 will lights up, else if SW2 is pressed (ON),

Fig. 14. LED and Switches Logic (v) when SW1 is OFF and SW2 is
ON.

2) LED Sequential

Fig. 15. LED Sequential (i) when SW1 is OFF.

i. Produce a circuit when SW1 is ON, LED1


should ON for 5 seconds then LED2 ON.
Code:
DEFINE OSC 20
INCLUDE "modedefs.bas"
TRISB = %00000011
PORTB = $00
MAINLOOP:
IF PORTB.0 = 0 THEN
PORTB.6 = 1
Fig. 16. LED Sequential (i) when SW1 is ON.

PAUSE 5000
WHILE PORTB.6 = 1
PORTB.6 = 0
PORTB.7 = 1
WEND
ENDIF
GOTO MAINLOOP
Explanation:
The oscillator is set to frequency of 20 mHz and
PORTB is configure as the input and output for this
program. PORTB.0 and PORTB.1 are set as input while
PORTB.2 to PORTB.7 are set as outputs. In mainloop, if
PORTB.0 is low (active low), PORTB.6 will be high for
5000 milliseconds (5 seconds) and then PORTB.7 will be
high. In this case, PORTB.0 is SW1 while PORTB.6 is
LED1 and PORTB.7 is LED2. As SW1 is pressed (ON),
PORTB.0 is low (active low), LED1 will lights up for
5000 milliseconds and then LED2 will lights up. Then the
statements in mainloop will start all over again after the
execution.

Fig. 17. LED Sequential (i) after SW1 is ON for 5 seconds.

LED Sequantial (i) UML Flowchart:


START

Switch HIGH

Simulation using Proteus:


NO

Statement
Perform
YES
LED1 HIGH

After 5 seconds
LED1 HIGH
LED2 LOW

END

ii.

Produce a circuit when SW1 is ON,


LED1 and LED2 should be ON for 5
seconds then LED2 ON.

Code:
DEFINE OSC 20
INCLUDE "modedefs.bas"
TRISB = %00000011
PORTB = $00
MAINLOOP:
IF PORTB.0 = 0 THEN
PORTB.6 = 1
PORTB.7 = 1

Fig. 18. LED Sequential (ii) when SW1 is OFF.

PAUSE 5000
WHILE PORTB.7 = 1
PORTB.6 = 0
WEND
ENDIF
GOTO MAINLOOP
Explanation:
The oscillator is set to frequency of 20 mHz and
PORTB is configure as the input and output for this
program. PORTB.0 and PORTB.1 are set as input while
PORTB.2 to PORTB.7 are set as outputs. In mainloop, if
PORTB.0 is low (active low), PORTB.6 and PORTB.7
will be high for 5000 milliseconds (5 seconds) and then
PORTB.7 will continue to be high while PORTB.6 will be
low. In this case, PORTB.0 is SW1 while PORTB.6 is
LED1 and PORTB.7 is LED2. As SW1 is pressed (ON),
PORTB.0 is low (active low), both LED1 and LED2 will
light up for 5000 milliseconds and then LED1 will turn
off while LED2 will continue to lights up. Then the
statements in mainloop will start all over again after the
execution.

Fig. 19. LED Sequential (ii) when SW1 is ON.

Simulation using Proteus:

Fig. 20. LED Sequential (ii) after SW1 is ON for 5 seconds.

LED Sequantial (ii) UML Flowchart:


START

Switch HIGH

NO

Statement
Perform
YES
LED1 HIGH
LED2 HIGH

After 5 seconds
LED1 HIGH
LED2 LOW

ENDIF
IF PORTB.1 = 0 THEN
PORTD = %10000000
PAUSE 100
PORTD = %10000000 >> 1
PAUSE 100
PORTD = %01000000 >> 1
PAUSE 100
PORTD = %00100000 >> 1
PAUSE 100
PORTD = %00010000 >> 1
PAUSE 100
PORTD = %00001000 >> 1
PAUSE 100
PORTD = %00000100 >> 1
PAUSE 100
PORTD = %00000010 >> 1
PAUSE 100
PORTD = %00000001 >> 1
PAUSE 100
ENDIF

END

PAUSE 100
3) LED Running Light
The LED running light is designed using to 8
LEDs.
Code:
DEFINE OSC 20
TRISB = %00000011
TRISD = %00000000
PORTD = $00
PORTB = $00
MAINLOOP
IF PORTB.0 = 0 THEN
PORTD = %00000001
PAUSE 100
PORTD = %00000001 << 1
PAUSE 100
PORTD = %00000010 << 1
PAUSE 100
PORTD = %00000100 << 1
PAUSE 100
PORTD = %00001000 << 1
PAUSE 100
PORTD = %00010000 << 1
PAUSE 100
PORTD = %00100000 << 1
PAUSE 100
PORTD = %01000000 << 1
PAUSE 100
PORTD = %10000000 << 1
PAUSE 100

GOTO MAINLOOP
Explanation:
The oscillator is set to frequency of 20 mHz, and
PORTB and PORTD are configured as the input and
output for this program. PORTB.0 and PORTB.1 are set
as input while PORTD are set as outputs. In the mainloop,
if PORTB.0 is low (active low), PORTDs output will be
00000001 which means that the PORTD.0 will be high
while others are low for 100 milliseconds. Then PORTDs
output will change to 00000010 which means PORTD.1
will be high while others are low. The 1 will shift from
PORTD.0 to PORTD.7 one by one from LED1 to LED8
and the << means that the1 will shift to the left by 1
bit. Else if PORTB.1 is low (active low), PORTDs output
will be 1000000 which means that the PORTD.7 will be
high while others are low for 100 milliseconds. Then
PORTDs output will change to 01000000 which means
PORTD.1 will be high while others are low. The 1 will
shift from PORTD.7 to PORTD.0 one by one from LED8
to LED1 and the >> means that the 1 will shift to the
right by 1 bit. In this case, the LED will light up one after
the other one for 100 milliseconds. After execution, the
program will reset itself and wait for input for the next
execution.
Simulation using Proteus:

Fig. 21. LED Running Light when SW1 and SW2 are OFF.

Fig. 24. LED Running Light when SW1 is OFF and SW2 is ON.

Fig. 22. LED Running Light when SW1 is ON and SW2 is OFF.

Fig. 25. LED Running Light when SW1 is ON, SW2 is OFF. The LED
is light up one after another from LED8 to LED1.

LED Running Light UML Flowchart:


START

Switch1 HIGH

Switch2 HIGH

LED1 HIGH

LED8 HIGH

LED2 HIGH

LED7 HIGH

LED3 HIGH

LED6 HIGH

LED4 HIGH

LED5 HIGH

LED5 HIGH

LED4 HIGH

LED6 HIGH

LED3 HIGH

LED7 HIGH

LED2 HIGH

Fig. 23. LED Running Light when SW1 is ON, SW2 is OFF. The LED
is light up one after another from LED1 ro LED8.

LED1 HIGH

LED8 HIGH
END

IN VAR BYTE
TRISA = %11111111
TRISB = %00000000
ADCON1 = 0
PORTB = $00
mainloop
ADCIN 0,IN
IF IN = 0 then
PORTB = 0
ELSEIF IN > 0 and IN < 32 then
PORTB = 1
ELSEIF IN >= 32 and IN < 64 then
PORTB = 3
ELSEIF IN >= 64 and IN < 96 then
PORTB = 7
ELSEIF IN >= 96 and IN < 128 then
PORTB = 15
ELSEIF IN >= 128 and IN < 160 then
PORTB = 31

ELSEIF IN >= 192 and IN < 224 then


PORTB = 127
ELSEIF IN >= 224 THEN
PORTB = 255
ENDIF
GOTO MAINLOOP

>224
>85.96
255
1111 1111

The standard memory for 1 input is usually 8 bits


which are 28 = 256, where the range starts from 0 to 255.
Besides that, we also define the clock and delay period for
the ADC as well and we did set the PORTA input by
declaring TRISA = 255. In the mainloop, ADCIN 0, IN
is included where 0, IN representing the range of input
< 0 to IN > where the value of IN depends on the
potentiometer percentage.
By taking the bytes into account as input value
from potentiometer, IN is declared as the representative
for the input value. When the knob of potentiometer is
fully turned to the side where ground is connected, the IN
shows reading of 0. This indicates that no input and as a
result all LED does not light. When the knob is turned
slightly and the value is within the range of 1 to 32, then
only LED1 will light up. The rest will be follow the result
as shown in Table 1 above. Until the knob is fully turned
to the side where VDD is connected, all the LEDs will
light up.
The difference between 8-bit ADC and 10-bit
ADC is the degree of precision of the devices. The greater
the number of bits of precision, the greater the ability to
disambiguate the smallest differences in the measured
values. A 10-bit ADC can output 1024 distinct values,
which is from the range of 0 to 1023, whereas an 8-bit
ADC can output only 256 distinct values with the range
from 0 to 255.
Simulation using Proteus:

ELSEIF IN >= 160 and IN < 192 then


PORTB = 63

193 - 223
127
0111 1111

Code:
DEFINE OSC 20
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

72.99

161 - 192
63
0011 1111

59.23

129 -160
31
0001 1111

50.49

97 - 128
15
0000 1111

39.15

65 - 96
7
0000 0111

26.01

33 - 64
3

LED (binary)

0000 0011

15.12

1 32
1

5.04

LED (decimal)

0000 0001

Maximum
potentiometer
reading (%)

IN(byte)

TABLE I.

ADC is the short form of Analog Digital Converter. In


electronics, ADC is a system that converts an analog
signal, such as sound picked up by a microphone, light
entering a digital camera, into a digital signal. In this
question, we are required to convert analog data from
potentiometer.

Explanation:

0000 0000

B. Analog Input
1) Read analog input using potentiometer and show
the result of ADC conversion using 8 LEDs.

PORTB = 127

Fig. 26. High Resistant Potentiometer.

ELSEIF T >= 70 then


PORTB = 255

'hot

ENDIF
GOTO MAINLOOP

Explanation:

Fig. 27. Low Resistant Potentiometer.

2) Case Study
A client asked you to develop a temperature monitoring
system using PIC microcontroller to monitor the
temperature inside a room. The system must indicate
when reaches high temperature, red LED must be on. And
when the temperature is below low level, yellow light is
turn on. Green LED will light at ideal room temperature.

Firstly, we define the sample time of 50 s for


ADC. Then, we define 2 variable bytes which is T and
AnVal. So from the mainloop, we set that the
temperature when less than 10C, the LED1 will be light
up. Once the temperature increases to more than 10 or
equal, the LED2 will be light up too. Then, the next
LED3, LED4, LED5, LED6, LED7 and LED8 will light
up once it reaches the temperature which set at 20C,
25C, 30C, 40C, 55C and 70C respectively.
Simulation using Proteus:

Code:
DEFINE ADC_SAMPLEUS 50
T VAR BYTE
AnVal VAR BYTE
TRISA = %11111111
TRISB = %00000000
ADCON1 = 0
PORTB = $00
Fig. 28. At ideal room temperature.

MAINLOOP
ADCIN 0,AnVal
T = AnVal*500/255
IF T < 10 then
PORTB = 1

'cold

ELSEIF T >= 10 and T < 20 then


PORTB = 3

'cold

ELSEIF T >= 20 and T < 25 then


PORTB = 7

'cold

ELSEIF T >= 25 and T < 30 then


PORTB = 15

'normal

Fig. 29. At high temperature.

REFERENCES
ELSEIF T >= 30 and T < 40 then
PORTB = 31

'normal

ELSEIF T >= 40 and T < 55 then


PORTB = 63

'hot

ELSEIF T >= 55 and T < 70 then

'hot

[1]
[2]
[3]

[4]

BFM3333 Microcontroller System Sessio 2016/17 Lab Sheet.


Microchip Technology Inc. PIC16F87XA Data Sheet.
Steven F. Barrett, Daniel J. Pack (2006). Microcontroller
Fundamental for Engineers and Scientists (Ed. First). United States
of America. Morgan & Claypool Publishers.
Timothy D. Green (2008). Embedded Systems Programming with
the PIC16F877 (Ed.Second).

You might also like