Professional Documents
Culture Documents
Microcontrollers
Lecture 1
About
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?
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
• 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.
• 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).
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;
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
Instruction groups:
– „Byte-oriented“ operations
– „Bit-oriented“ operations
– Operactions with numbers
– Control operations
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.