You are on page 1of 34

AE2B34MIK

Microcontrollers

Lecture 1
About

Lectures: Vladimír Janíček


(janicev@fel.cvut.cz, C3-s142b)

Exercises: Tomáš Teplý


(teplyt1@fel.cvut.cz, A3-216)

Support: http://moodle.fel.cvut.cz
References
• Every book about C programming language
• http://www.microchip.com
• http://www.mikroe.com
• Robert B. Reese: Microprocessors From Assembly Language to C Using the
PIC18Fxx2, Da Vinci Engineering Press, Hingham, Massachusetts, 2005
PIC 18F45K22
• 8-bit RISC
• 32 kB program memory (FLASH)
• 1536 B data memory RAM
• 1024 B data memory EEPROM
• Up to 16 MIPs (fOSC 0 - 64MHz)
• I2C, SPI, serial port (USART)
• 10bit ADC (30 channels)
• 5bit DAC
• 6 modules CCP (Capture/Compare)
• PWM 1,2 or 4 outputs
• 2 comparators
• 7 timers
• 75 instruction set
What’s inside?

Block schema of PIC16F887


Mikroelektronika EasyPIC v7
History

1971: Intel ® has developed a 4-bit computer - four integrated circuits. One of the circuits
was the 4004, which served to decoding and execution of instructions (the central
processing unit, or CPU), while other circuits have served as memory and input/output (I/O)
peripherals. This circuit was called the microprocessor (μP). The 4004 is so referred to as
the first microprocessor in the world. Since then, the development in this area was divided
into two paths:
Microprocessors (μP) - the trend towards an increase in performance, the number of transistors, the
width of the bus, etc. Programs and data stored on external storage devices are connected to the
microprocessor and other auxiliary I/O circuits. Among the biggest producers include AMD, IBM, Intel ...
In the field of portable devices rule the ARM derivates (Mediatek, Qualcomm, Samsung, TI, ...).
Microcontrollers (μC) - microcontrollers are characterized by the fact that they have the program data
stored in its internal memory, integrated in a single package together with the necessary I/O circuits.
There are plenty of families of microcontrollers, which are available at different width of the bus in the
range of 8 to 32-bit. Individual families vary widely in the number and combination of peripherals,
memory size, etc. Among the world's largest manufacturer of microcontrollers include Analog Devices,
Atmel, Freescale (formerly Motorola), Microchip, NXP (Philips), ST Microelectronics, Texas Instruments,
and many more.
μC architecture
• This chapter will address the basic concepts of microcontrollers architecture. We
will focus on the Microchip PIC18F45K22 that will be used in the exercises.
• Individual concepts and part of the μC will be described here only briefly. Most will
be discussed in more details in other chapters.
PIC18F45K22 blocks without peripherals
μC architecture - ALU
• ALU (Aritmetic/Logic Unit) is used to perform arithmetic and logical operations.
• The left input to the ALU consists of a multiplexer 1 of 2, which selects between
the W register and the data from Instruction register (BITOP) that are directly part
of the instruction.
• On the right input may be data from the data memory (RAM) or the data from the
W register.
• The output is connected to the data bus again, do the result of the operation can
be saved to the W register or other registry in the RAM.

c: c=a+b
asm:
MOVF b, 0
ADDWF a, 0
MOVWF c

ALU (Aritmetic/Logic Unit)


μC architecture - ALU
• After the processing of the data in the ALU occurs in the majority of the
instructions a change of the flag bits in the STATUS register. We talk about flags
also.
• The basic symptoms are:
– Carry (C)– transfer to a higher-order beyond the scope of the registry
– Zero (Z)– the result of the operation is equal to zero

• Other symptoms can include:


– Negative (N) – indicates the operation, that the result is negative (MSB=1).
– Digit Carry (DC) – a carry-out from the 4th low-order bit of the result has
occured.
– Overflow (OV) – indicates the overrun into the sign (7th) bit., i.e., that a
change has occurred in the highest bit.

• W register (WREG), called sometimes as “working”, the register has some special
features beyond the standard registers (see ALU and instruction set).
μC architecture - PROGRAM MEMORY
• PIC18 family uses Harvard architecture, which uses a separate program and data
memory. In a lot of third-party μC, however, Von Neumann architecture is used –
like μC core ARMv6-M (Cortex-M0, M0 + M1).

Program memory
• as the name implies, in this memory is stored the control program of the µC. This
software is also called as firmware.
• Program memory of PIC18 class uses 21bit bus width, which allows to address up
to 2 MB (221) of memory. It is a nonvolatile type of memory – it stores the data
independent on the supply voltage. It is a Flash memory ('F'-Flash). Be careful,
however, there are µC with OTP memories (One Time Programmable) that can be
programmed only once (e.g.. PIC18C).
• Program memory will be addressed through Program Counter or in the case of
write/read through special instruction it is addressed by Table pointer.
μC architecture - PROGRAM MEMORY
• The program counter (PC) is a
pointer to the program memory and
its content is the address of the
instruction that will be made in the
following step. For PIC18 it is 21bit
long and it is divided into three
registers – PCL(8b), PCH(8b) and
PCU(5b).
• Stack – or tray of return addresses
(Return Address Stack). It is used to
temporarily store the addresses of
instructions to be executed in the
next step when you call a subroutine
(Call), or when interrupt occurs - it
stores the current value of the PC. Program memory
The pointer to that position is called
the Stack Pointer.
μC architecture – DATA MEMORY
• Data memory is memory that is used to store the values of variables, results of
arithmetic and logical operations, etc.
• PIC18 uses 12 bits for addressing. Therefore, you can address up to 4096 bits of
memory.
• Data memory is volatile. If you lose power its content will be lost.
• The data space is divided between the two types of registers:
– GPR-General Purpose Register, these are the General registers for general
use.
– SFR-Special Function Register, i.e. registers with the assigned special
functions.
• Data space can be addressed either directly or indirectly through so-called
indirect addressing.
μC architecture – DATA MEMORY
• Due to the fact that the part of the instructions may contain only 8bit long address
of the desired registry in RAM, but to addressing the entire RAM we need 12 bit
pointer, you need to use one more SFR register in which the remaining four
address bits are stored. This register is called the BSR (Bank Select Register).
Addressing scheme is shown in Figure.
• In code of the instruction are saved the lower 8 bits of the address, the top 4 are
atored in the BSR register.

Obr. 4 – Adresování RAM[2]


μC architecture – DATA MEMORY
• Lower 8 bits of the address that are part of the instruction, can address 256 variables
(registers) with the addresses from 00 to FFh (0-255). The value in the BSR registry
indicates the number of so-called bank in which we can address only these 256 bits
range.
• If we go on with only 256 variables, then we could ignore the contents of the BSR.
However, complications arose with access to SFR, which is in Bank 15.
• To simplify access to RAM so-called RAM Access bit called 'a‘ is used. When you set
this bit to a value of '0' (it is a part of the instructions, which contain address of RAM)
address space reduction occurs on 8 bits (the value of the BSR is ignored) that
addresses 0-7Fh are reserved for variables and the range of 80h-FFh is dedicated to
SFR.

SFR block schema


μC architecture
DATA MEMORY
μC architecture - PERIPHERALS
Microcontrollers can be equipped with various peripherals. Combined, it often
predetermines the final use of the microcontroller. The most common peripherals
include the following:

• IO Ports
• Timers
• Communication bus (UART, I2C, SPI, CAN, USB, Ethernet, I2S)
• ADC, DAC, comparators
• PWM, QEI (Quadrature Encoder Interface)
• LCD driver
μC architecture
PORTS
• Input pins (metal connections)
are used for supplying the
power, for connection of
supporting components and
circuitry (crystal, capacitors,
...) and for communication
purposes. These pins are
grouped together (according
to architecture after eight /
sixteen), and these grouped
pins are then referred to as
input output ports (I/O
ports).

• PIC18F45K22 includes five


ports (A, B, C, D, E)
μC architecture - SFR
For setting the basic properties of ports five SFR registers are used.

TRIS (TRIState)
• Used to select the input or output function of one bus (A,B,C…).
• TRISx (TRISA, TRISB, ...). Log. 1 symbolizes the input function, log. 0 states for
output.
PORT
The recent logic levels on each of the inputs can be read through the registers.
Ex. PORTx (PORTA, PORTB, ...)
LAT (Latch)
To write the logic levels on output of pins.
Ex. LATx (LATA, LATB, ...)
ANSEL (Analog Select Register)
To select between analog and digital functions of the buses.
Ex. ANSELx (ANSELA, ANSELB, ...)
SLRCON (SLEW RATE CONFIGURATION)
Speed selection changes on output pins. You can choose standard speed or about
1/10 of the speed because of the limitations of EMI (Electromagnetic
Interference).
μC architecture
I/O PIN
μC architecture
Example: We want to set the port (B) as the output and the output to write
the value of 11110000b.

Assembler C
MOVLW b’00000000’
MOVWF ANSELB,0 ANSELB = 0;
MOVLW b’00000000’
MOVWF TRISB,0 TRISB = 0;
MOVLW b’11110000’
MOVWF LATB,0 LATB = 0b11110000;
μC architecture - PORTS
“Read Modify Write” Problem

For writing data to or reading data from the respective ports we use PORT and LAT
registers. Under certain circumstances, we can only use the registers PORTx, but there
may be situations where a simple use of PORT can lead to erroneous result.

When data changes on the ports a sequence called "Read-Modify-Write“ is used. This
means that the internal registry first reads the current value of the registers (port),
then changes to a new value, and this new value is written back to the registry in
memory of µC. During the reading of the actual voltage level read the µC on input
pins of the µC and not the value that is stored in the register PORTB. An error that may
occur in this case, will be described in the following example.
μC architecture - PORTS
Erroneous write example
In the program we want to perform the following sequence of instructions, which
changes in the first step the PIN "0“ state on the PORTB on the log. 1, and in the next
step pin "1" on PORTB from a log. 0 to the log. 1.
On PIN "0" of the PORTB bus we have connected capacitor, see figure.

PORTB.B0 = 1;
PORTB.B1 = 1;

µC with connected capacitor


Operation RMW on PORTB The progress of the first instruction on PORTB. B0 = 1
Operation RMW on PORTB
The progress of the second instruction PORTB. B1 = 1 with error generation
μC architecture - PORTS
HOW TO DO IT CORRECTLY…

1. Use LAT register for writing output data.

2. In showed example merge the two lines into one instruction PORTB = 0b00000011.

3. Insert a time delay between the first and the second line to let enough time for the
value at the output to be settling down.
BUTTONS

T1 < 1ms (typical bounce time)


T2 < 60/600/2 = 0,05s (typical hold time)
T1 < T3 < T2 ~ 10-30 ms (repeat check time)
INSTRUCTION SET

Instruction groups:
– „Byte-oriented“ operations
– „Bit-oriented“ operations
– Operactions with numbers
– Control operations

A total of 75 instructions, the vast majority will be performed within one


instruction cycle (4 cycles of the oscillator).
„Byte-oriented“
operations
ADDWF f,d,a

Operands:
• f – registry name (variables) we work
with
• d – selects a destination where the
result of the operation is to be saved
(f, w)
• a – „RAM access bit“

Example:
We want to add values in the registers "W"
and the "reg". We will store the result in
the register W. At the end:
W = 0xD9
REG = 0xC2
„Bit-oriented“
operations
BCF f,b,a

Operands:
• f – registry name (variables) we
work with
• b – selected bit of f register, (0
≤ b ≤ 7)
• a – „RAM access bit“

Example:
We want to set the 5th bit of “reg”
register to the value „0“.
Operations
with
numbers
MOVLW k

Operands:
k – a numeric value that
will be saved to the
register W
At start:
W = 0xC3
Example: At the end:
We want to store the W = 0xFF
value of 0x5A to the
register W.
Control
operations
GOTO n

Operands:
n – label

Example:
We want to jump to the label „THERE“
in the program.

PC (Program Counter) changes its value


to the position of the label „THERE“.
REFERENCES AND LINKS
[1] http://www.microchip.com
[2] Robert B. Reese: Microprocessors From Assembly Language to C Using the
PIC18Fxx2, Da Vinci Engeneering Press, Hingham, Massachusetts, 2005
[3] http://www.mikroe.com
[4] http://www.picaxe.com
[5] http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/

You might also like