0% found this document useful (0 votes)
11 views

Module1- pres

The document provides an overview of microcontroller programming and interfacing, focusing on the PIC16F917 microcontroller. It covers key concepts such as memory organization, I/O ports, programming instructions, and the internal oscillator, as well as the use of timers for precise timing operations. The course emphasizes the practical application of these concepts in electronic devices and includes examples of programming in assembly language.

Uploaded by

aaronknapper2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module1- pres

The document provides an overview of microcontroller programming and interfacing, focusing on the PIC16F917 microcontroller. It covers key concepts such as memory organization, I/O ports, programming instructions, and the internal oscillator, as well as the use of timers for precise timing operations. The course emphasizes the practical application of these concepts in electronic devices and includes examples of programming in assembly language.

Uploaded by

aaronknapper2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

ELG 4159

Integrated Control Systems


Module 1 – Microcontroller Programming
and Interfacing
By : Basim Hafidh Notes from
Dr. Wail Gueaieb
Outline

 Microcontrollers
 PIC16F917 Microcontrollers
 Memory organization and registers
 I/O Ports
 Programming PIC16F917
 Microcontroller’s Internal Oscillator
 Timers

2
Microcontrollers

 A microcontroller is a single Very-Large-Scale-Integration


chip that contains many digital circuits that perform
arithmetic, logic, communication and control functions.

 Microcontrollers are used in almost every electronic device


nowadays:
 dish washers, fax machines, TV sets, etc.

 Figure 1 shows a block diagram of a typical microcontroller.

 A typical microcontroller is composed of a CPU, a RAM, a


ROM, digital and analog I/O ports, a serial communication
interface, timers, and A/D and D/A converters.

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.

 RAM: is a volatile memory and is used to store some values on


which the firmware depends.

 CPU: is the component that executes the firmware and controls all
the microcontroller’s other components.

 I/O ports: allows data to be transferred to and from the


microcontroller using external pins on the IC (Integrated Circuit).
This data can be, for instance, the state of an external switch, a
sensory data, a control voltage to be transferred to an actuator, etc.
An Analog I/O port is an I/O port carrying an analog signal, whereas
a digital I/O port is an I/O port carrying a digital (binary) signal.

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

 A/D Converter: is an IC that converts an external analog voltage


(from a sensor, for instance) to a digital value that can be processed
or stored by the microcontroller.

 D/A Converter: is an IC that allows the microcontroller to convert a


digital signal to an analog one (that can be transferred to an
actuator, for example).

 Timers: are usually used to help creating delays or ensure that


certain events occur at precise time intervals (e.g., reading sensory
data).
6
 There are many types of microcontrollers:
 Microchip’s PIC,
 Motorolla’s HCS12,
 Intel’s 8086,
 etc.
 In this course, the focus will be most on the Microchip PIC
microcontrollers due to its
 Wide acceptance in the industry
 Abundant information resources
 Versatile capabilities
 Ease of use
 Upward firmware compatibility and pin compatibility
 Relatively low cost
 …….
 The board that is used in this course labs is the Mechatronics board
from Microchip, which carries PIC16F917.
 Microchip uses “PIC” to refer to its line of microcontrollers.
7
PIC16F917 Microcontrollers

 The microcontroller adopted in the course is PIC16F917.

 Most PIC16 microcontrollers have very similar architectures.

 The figure in [PC16F917 DataSheet, 2007, page 2] shows block


diagram of the PIC16F917.

 PIC16F917 is an 8-bit microcontroller that can hold up to 8Kx14


instructions (each instruction is composed of 14 bits).

 Its internal clock is typically driven at a speed of 8 MHz (software


controllable).

 Most instructions take 4 clock cycles to be executed.

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.

 Example (Direct Addressing mode)


If RP1 and RP0 are set to 0, then
ADDWF 0x7F
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.

14
Indirect addressing mode

 The other method of accessing a GPR or SFR is the indirect


addressing method. This method is based on a special register
called “INDF”.

 INDF is a logical (not physical) 9-bit register obtained by


concatenating a physical 8-bit File Select Register (FSR) and the
IRP bit (STATUS<7>). In this case, bit IRP and FSR<7> specify the
bank number and FSR<6:0> specify the address to be accessed in
that bank.

 Indirect addressing mode is commonly used when the programmer


prefer to store the address of the register to be accessed in a
register instead of explicitly specifying it in the program instruction.

 More details about indirect addressing mode are in [PIC16F917


DataSheet, 2007, page 41].
15
16
https://www.mikroe.com/ebooks/pic-microcontrollers-programming-in-assembly 17
 Example (Clearing registers in addresses 20h to 2Fh of
Bank 0)

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.

 There are two types of SFRs:


 Core SFRs are SFRs associated to the "Core" functions of the microcontroller.
 Peripheral SFRs are SFRs associated to the operation of the peripheral features
(LCD, I/O ports, A/D conversion, etc.)

 More details about the core SFRs can be found in [PIC16F917


DataSheet, 2007, pages 24–39].

19
I/O Ports

20
I/O Ports

 PIC16F917 has five 8-bit I/O ports:


 PORTA, PORTB, PORTC, PORTD and PORTE. Only bits 0 to 2 are
implemented in PORTE though.

 Every bit of an I/O port corresponds to an I/O pin on the PIC.


Example
PORTA<2> corresponds to pin RA2. See [PIC16F917 DataSheet,
2007, page 16].

 A TRIS register is associated to each port to configure it as an input


or output port.
 TRISA for PORTA, TRISB for PORTB, etc.

 Setting a certain bit in a TRIS register to 1 (or 0) makes the


corresponding bit in the I/O port associated to that TRIS register an
input (or output) bit.
21
22
https://www.mikroe.com/ebooks/pic-microcontrollers-programming-in-assembly
23
Example:

 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

 In PIC16F917 all output pins have to be digital.

 All input pins, however, have to be digital but eight.


 In case RA5 (PORTA<5>), RA<3:0> (PORTA<3:0>), or RE<2:0>
(PORTE<2:0>), are configured as input pins, they can either be
configured as digital or analog input pins.
 This is configured through register ANSEL: 0 for digital and 1 for analog.

 More details about I/O ports are on [PIC16F917 DataSheet, 2007,


page 43]

25
26
Example
 Setting RA5 as an analog input pin.

 Setting RE1 as a digital output pin.

27
Programming PIC16F917

 PIC16F917 will be programmed using assembly language.

 The instruction set used is detailed in Section 17 [PIC16F917


DataSheet, 2007, page 241].

 Table 17.2 [PIC16F917 DataSheet, 2007, page 242] summarizes


the instruction set.

 There are three main instruction categories:


 Byte-oriented instructions.
 Bit-oriented instructions.
 Literal and Control instructions.

28
Byte Oriented Instructions

 Byte oriented instructions operate on bytes (not bits), and involve


File registers and the accumulator W.

 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

 Bit oriented instructions operate on one-bit of the register rather than


the whole register.

 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

 Literal and Control Instructions involve the use of literals (constants


or labels).

 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

 By default, Microchip’s assembler assumes that numbers are


presented in a hexadecimal format unless otherwise specified. For
instance, the following instructions are equivalent:
MOVLW 12 ≡ MOVLW 12h ≡ MOVLW 0x12 ≡ MOVLW d‘18’ ≡
MOVLW b‘00010010’

 Instruction symbols (e.g., MOVWF, INCF) are case-insensitive,


whereas labels are case-sensitive.

35
Microcontroller’s Internal Oscillator

 Microcontrollers, in general, can be driven through an internal or


external oscillator (clock), which determines the execution speed of
the firmware.
 The internal oscillator of the PIC16F917 is controlled by the SFR
“OSCCON” (Oscillator Control Register).
 The internal oscillator frequency ranges from 31 KHz up to 8 MHz
depending on the values of OSCCON<6:4>.
 More details about the OSCCON SFR is on [PIC16F917 DataSheet,
2007, page 87].

 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 T0IF bit (which is INTCON<2>) is automatically set to 1 when TMR0


rolls over from 0xFF (255) to 0x00.

 The prescaler controls the frequency at which TMR0 is incremented.

 The value of the prescaler is determined by bits PS2, PS1, and PS0 of
OPTION_REG, which correspond to OPTION_REG<2:0>.

 The prescaler possible values are described as follows:

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

is maximized while keeping its TMR_Max_Ticks = 28 = 256.

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)

TMR_Max_Ticks ≡ max possible number of ticks = 28 = 256


TMR_Initial_Value ≡ Initial value in TMR0

44
Example

Fpi Fpo

Assume that the PIC main oscillator is running at 8 MHz


1. Can you create a delay of 0.7µsec?
2. With this configuration (Fosc = 8 MHz, prescaler = 2) is it possible
to generate a delay of 3µsec?
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?

45
Remarks

 Timer0 is always enabled in PIC16F917. It cannot be disabled.

 To set Timer 0 prescaler value, it is recommended (for the sake of


this course) to initialize OPTION_REG to 10000xxx, where xxx
depends on the prescaler value of your choice.

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

 More details on TMR0 are on Section 5 of [PIC16F917 DataSheet,


2007, pages 99].

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

[PIC16F917 DataSheet, 2007] PIC16F917 DataSheet (2007).


PIC16F913/914/916/917/946 Data Sheet.
Microchip Technology Inc., f edition.
http://ww1.microchip.com/downloads/en/DeviceDoc/41250F.pdf

48

You might also like