Module1- pres
Module1- pres
Microcontrollers
PIC16F917 Microcontrollers
Memory organization and registers
I/O Ports
Programming PIC16F917
Microcontroller’s Internal Oscillator
Timers
2
Microcontrollers
3
Figure 1: Block diagram of a mechatronic system.
4
ROM: is a non volatile memory and is used to store the program to
be executed. This program is referred to as the firmware.
CPU: is the component that executes the firmware and controls all
the microcontroller’s other components.
5
Serial Communication Interface: is a special I/O port that uses a
serial communication protocol. There are various serial
communication protocols including :
SPI (serial peripheral interface),
I2C (Inter-integrated circuit),
USART (Universal Synchronous Asynchronous Receiver Transmitter).
8
9
10
Program Memory Organization
PIC16F917 has an 8kx14 program memory (Flash) [PIC16F917
DataSheet, 2007, page 23].
11
https://www.mikroe.com/ebooks/pic-microcontrollers-
programming-in-assembly
12
Data Memory Organization
The data memory is partitioned into 4 banks.
Each bank extends from address 00 to 7Fh (total of 128 bytes).
Each bank contains General Purpose Registers (GPRs) and Special Function Registers
(SFRs).
The first portion of each bank is composed of SFRs followed by GPRs.
Some frequently used SFRs from one bank are mirrored in another bank for
code reduction and quicker access (no need to switch banks).
The bank selection bits RP0 and RP1, which correspond to bits 5 and 6 of
the STATUS register, respectively. So to access bank 2, the programmer
must set RP1 = 1 and RP0 = 0.
The data memory map of PIC16F917 is shown in [PIC16F917 DataSheet,
2007, page 26].
https://www.mikroe.com/ebooks/pic-microcontrollers-programming-in-assembly
13
General Purpose Register File
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.
14
Indirect addressing mode
18
Special function registers (SFRs)
SFRs are special purpose registers used by the CPU and peripheral
functions to control the desired operation of the microcontroller.
19
I/O Ports
20
I/O Ports
Setting TRISA<5> to 1,
configures PORTA<5> as an input bit.
Setting TRISA<4> to 0,
configures PORTA<4> as an output bit.
24
Analog Inputs
25
26
Example
Setting RA5 as an analog input pin.
27
Programming PIC16F917
28
Byte Oriented Instructions
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
29
Byte Oriented Instructions
30
Bit Oriented Instructions
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
31
Bit Oriented Instructions
32
Literal and Control Instructions
Syntax: Instruction_symbol k or
Instruction_symbol
k represents the literal (either a constant value or a label)
Examples
33
Literal and Control Instructions
34
Remarks about Microchip’s assembler
35
Microcontroller’s Internal Oscillator
Example
Setting bits 6,5,4, of OSCCON to 1,0,1, respectively, sets the internal
oscillation to 2 MHz.
36
37
Timers
The PIC16F917 comes with two 8-bit timers:
Timer0 and Timer2, which count from zero to 255 (= 28 - 1).
and one 16-bit timer:
Timer1, which counts from zero to 216 - 1.
38
TMR0 - The 8-bit timer
The SFR associated with Timer0 is called TMR0.
When the PIC’s internal oscillator is used as the clock source for the timer, it
will have the following configuration:
Figure 2: Configuration of functionality of Timer0. The timing diagram in the middle assumes that
Prescaler = 2 and the initial value in TMR0 is 0xAB. 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>.
40
41
Example
Configure the internal oscillator to 8MHz, timer 0 to 128 Prescaler.
Initialize the timer to zero, and check out the T0IF.
42
Timer0 Calculations
The behavior of Timer 0 is controlled by 3 parameters (i.e., 3 degrees of
freedom): Fosc , the timer’s prescaler and initial value. These parameters
are determined as follows:
1. Determine a suitable combination of Fosc and prescaler, such that
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).
43
The time (Delay) taken to roll back to 0x00 is
(1)
44
Example
Fpi Fpo
45
Remarks
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.
46
Case Study
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.
47
References
48