Module 1
Module 1
Microcontrollers
PIC16F917 Microcontrollers
Memory organization and registers
I/O Ports
Programming PIC16F917
Microcontroller’s Internal Oscillator
Timers
CPU: is the component that executes the firmware and controls all
the microcontroller’s other components.
#
aximum
OTHEmeny
= Goto initialize
ORG OXOS
I code
here
initialize
-
this
not use
will
=
we here
not sun start
- code
need
to
i every
either
27 Data Memory :
RAM
Organization
Bank
the
↑configuratoamsden ess
[Link]
programming-in-assembly
A
oh
O
o
0
Ag
5151FFh
new
=
S
↑
20
1st p
e
xf -
>
[Link]
© 2025 Basim Hafidh 13
(which assign Bitd
clean FirstBank
0x03 (example (
& ↑
11
BCF Status RPO
,
The register file of the PIC is made of 352 bytes (registers) in total.
Each register can be accessed either directly through RP1, RP0, (to
specify the bank) and the op-code address field to specify the
address in that bank.
F File registen
=
accumulator
w= Work registen
Example (Direct Addressing mode)
If RP1 and RP0 are set to 0, then
at the address of
the W and the content of thefile
ADDWF 0x7F => add the content
register
will add the content of the accumulator (working register) to the content
of the register in address 0x7F in bank 0 of the data memory and store
the result back in address 7Fh.
addressnk
Ba
this
of
BGF Status ,
I RP O
to
MOLW zoh Always have
use the work registen
FSR
MOU WE O When you want to
INL INDFE
access
to the file
egiste
a
~
INDF =
indirect file register
painten
↓
to FSR is called
INDF
The register assigned
litend to work register
we
[Link]
© 2025 Basim Hafidh 17
Example (Clearing registers in addresses 20h to 2Fh of
Bank 0)
C
GOTO NEXT ;no clear next
CONTINUE ;yes continue
BTFS
test =
Bit
instruction
check if it's zenoifyes goto next if 1 ship next
SFRs are special purpose registers used by the CPU and peripheral
functions to control the desired operation of the microcontroller.
Setting TRISA<5> to 1,
configures PORTA<5> as an input bit.
Setting TRISA<4> to 0,
configures PORTA<4> as an output bit.
can
work as digital
intput = digital/andog
only
output = digital
>
-
can be digital
on andog
ANSEL Register
3
ponte
Setting RA5 as an analog input pin. Iwe wanted to
↑
use a intput pin as
digital no need
registen
;Select Bank 1 to set the digital
BSF STATUS,RP0 ; or BANKSEL TRISA
BCF STATUS,RP1
BSF TRISA,TRISA5 ; input (set to 1 = intput)
&
Syntax: Instruction_symbol F , d or
Instruction_symbol F
F represents the address of the file register to be used. The value of F
ranges from 0x00 to 0x7F.
d is the destination designator which specifies where the result of the
operation is to be placed
d = 0 ⇒ the result is placed in W
d = 1 ⇒ the result is placed in the File register F.
The default value of d is 1.
Examples
© 2025 Basim Hafidh 29
1. Byte Oriented Instructions halfcanny bit =
c =
DC canny
:
canny
bit
bit from 3 to Y
2 = Zeno
Instruction
Cycle (not neces
I
need any destin
,
cleanfile registen don't ,
clean accumulator
check not
(if we want to
if file on
MH2, do nothing)
(0 Sys
.
if we use8
&
Syntax: Instruction_symbol F , b
F represents the address of the file register to be used.
b represents the bit number to operate on, ranging from 0 (LSB) to 7
(MSB)
Examples
Syntax: Instruction_symbol k or
Instruction_symbol
k represents the literal (either a constant value or a label)
(k)
= result in the accumulator
timen is a counter
werse timen
=>
to
generate delay
Y XY oo 0 0
© 2025 Basim Hafidh 36
Microcontroller’s Internal Oscillator
Example
Setting bits 6,5,4, of OSCCON to 1,0,1, respectively, sets the internal
oscillation to 2 MHz.
initialize value -
the timen
of
1 (FOIF)
initialize counter Flagchange from O to
Delay >
Figure 2: Configuration of functionality of Timer0. The timing diagram in the middle assumes that
Prescaler = 2 and the initial value ©in2025
TMR0 is 0xAB.
Basim Hafidh 39
Fosc is the PIC’s main oscillator’s frequency.
TMR0 gets incremented automatically after each falling edge of the clock it
is connected to, regardless of what the firmware (CPU) is doing.
When TMR0 reaches 0xFF it rolls back to 0x00 at the next incremental tick.
The value of the prescaler is determined by bits PS2, PS1, and PS0 of
OPTION_REG, which correspond to OPTION_REG<2:0>. presalen formula
The prescaler possible values are described as follows: 24 x2
+ 1
=
2n
1 O O O O *
Y Y
41
always © 2025 Basim Hafidh -
presalen
Example I
grequency is MH2/Example
I
8
2. Calculate the timer’s initial value (to be stored in TMR0) using (1).
Remarks
If the calculated initial value turns out to be non-integer, it is an indication
that the exact target delay cannot be generated.
In that case, the best one can do is to generate a delay as close as possible to
the target delay by rounding the timer’s initial value to the nearest integer.
This leads to a timing error (i.e., difference between the generated and the target
delay). © 2025 Basim Hafidh 44
The time (Delay) taken to roll back to 0x00 is
(1)
Fpi Fpo
The smallest possible value of the Tpo is when the prescaler value is
chosen as (2).
⇒ Smallest possible Tpo in this case is 2 x Tpi = 2 x 0.5 = 1 µsec
The idea here is to force the last transition to correspond to: “0xFF →
0x00” so that the flag T0IF is set to 1 as soon as the target time (3 µsec) is
elapsed.
1 2 3
0xFD → 0xFE → 0xFF → 0x00 (T0IF =1) → ………..
© 2025 Basim Hafidh 48
3. Is it possible to generate a delay of 4.7µsec? What’s the initial value
to set TMR0 to? What’s the resultant error in the generated delay?
Thus, TMR0 must be initialized to 0xFB so that the last timer transition is
“0xFF → 0x00”
1 2 3 4 5
TMR0: 0xFB → 0xFC → 0xFD → 0xFE → 0xFF → 0x00 (T0IF =1) → →
It is also recommended (for the sake of this course) to turn OFF the
comparators by initializing SFR CMCON0 to 0x07. Comparators will
not be used in the course, but they are enabled by default in
PIC16F917.
Write a program so that every time the tactile switch SW2 on the
mechatronics board is pressed, LED (D0) is toggled (on/off).
SW2 (input) can be connected to pin RA0 of the PIC, which will have to
be configured as a digital input pin since the signal connected to SW2 is
digital
LED D0 (output) can then be connected to pin RD7 of the PIC, which
will have to be configured as a digital output pin.
Vcc
RA0
RD7
SW2
PIC16F917
D0
If pressed, If released,
toggle LED start timer0
If pressed
within 16 ms
16 ms elapsed while
switch not pressed
Ideal pushbutton