Microcontrollers: Architecture, Instructions, and Programming

Introduction

A microcontroller is a closed system that contains a fully programmable digital system on a single integrated circuit. It is designed to control a specific task that cannot be changed and includes a CPU, memory, program data, clock, peripheral input/output, etc.

PIC16F84A Internal Architecture

The PIC16F84A microcontroller has the following internal architecture:

  • Program memory: Flash ROM
  • Data memory: Divided into two areas (RAM and EEPROM)
  • 8-bit ALU and work register W
  • Two ports for communication with the outside world: PORTA (5-bit) and PORTB (8-bit)
  • 13-bit program counter, allowing for 4K words of memory
  • Harvard architecture (system is segmented)
  • RISC architecture (reduced instruction set computer)
  • Orthogonal instruction format
  • Banked register architecture

Organization of Memory

Program Memory: Contains the instructions that control the application. It is non-volatile, meaning that it is maintained even if power is lost.

RAM Data Memory: Stores variables and data. It is volatile, so the data is erased when power is lost.

EEPROM Memory: Non-volatile data storage area, so the information is retained after a program reset. It can be read and written to.

The Program Counter

The program counter is an internal register used to direct the control program to the instructions stored in program memory. It contains the address of the next instruction to be executed and is automatically incremented so that the natural sequence of program execution is linear.

Data Memory

RAM is divided into two parts:

Special Function Registers (SFRs): These are the first registers, each serving a specific purpose in the control of the microcontroller.

General Purpose Registers (GPRs): These are general-purpose registers that can be used to store temporary data while the program is running.

The data memory has two memory banks, Bank 0 and Bank 1:

  • SFR registers are grouped between addresses 00h to 0BH in Bank 0 and 80h to 8BH in Bank 1.
  • GPR registers consist of 68 positions from Bank 0, since Bank 1 is mapped onto Bank 0.

Registers Related to Ports

PORTA: Located at address 05h in Bank 0. Input/output port with five bits (RA4: RA0). Can be read or written to as a register. The direction of the pins is controlled by the TRISA register.

PORTB: Located at address 06h in Bank 0. Input/output port with eight bits (RB7: RB0). Can be read or written to as a register. The direction of the pins is controlled by the TRISB register.

TRISA: Located at address 85H in Bank 1. Port A configuration register. A 0 in a bit sets it as an output pin, and a 1 sets it as an input.

TRISB: Located at address 86h in Bank 1. Port B configuration register. A 0 in a bit sets it as an output pin, and a 1 sets it as an input.

Work Register W: The main register that participates in most instructions. It is located within the CPU.

Status Register (STATUS): Located at address 03h in Bank 0 or 83H in Bank 1. One of the most important registers. The bits of this register indicate the status of the last arithmetic or logic operation, the cause of reset, and the bank select bit for the data memory. The bits in the status register are called flags or banners.

Machine Language

Machine language is the only language that microcontrollers understand. It is composed of zeros and ones from the binary system.

Assembly Language

Due to the difficulty of using machine language, assembly language is used. Assembly language uses mnemonics, which are groups of alphanumeric characters that represent the commands and tasks to be performed with each statement.

Assembler Program

An assembler program is a software that is responsible for translating the alphanumeric mnemonics and instructions written in assembly language by the user into machine code to be executed by the microcontroller.

Code of Operation

The second column in an assembly language instruction, which specifies the operation to be performed by the microcontroller. The instruction is usually a microcontroller instruction that is directly translated into machine code by the assembler.

Operands

The third column in an assembly language instruction, which contains the operands for the instruction field. It may contain one or more operands separated by commas, and depending on the instruction, these operands may be numbers, labels, or addresses representing constants.

EQU

An allocation directive. EQU allocations go to the start of the program.

ORG

This directive tells the assembler program the memory address from which it must assemble the source code instructions. That is, the address in program memory where these instructions are to be stored is determined by the expression of the directive.

CONFIG

This directive tells the configuration chosen for the configuration process of the microcontroller. In this case, there is no code protection, no watchdog is enabled, the Power-Up reset is enabled, and the quartz crystal oscillator is used.

Machine Cycle

The basic unit of time used by the microcontroller. For the PIC16F84, it is 4 clock cycles. Therefore, the time it takes to execute a machine cycle is four times the period of the oscillator. Most instructions require one machine cycle to execute, except for jumps, which require two.

Delay

Microcontrollers often need to generate timeouts, called delays. A delay can be achieved by a subroutine, based on repeating instructions as many times as necessary. Since the execution time of each instruction is known, the only thing to do is to calculate the initial value of the R_ContA register. To achieve this, use the formula T = 4xToscxPrescx (256-Vtmr0).

Reset

Reset causes reinitialization of the microcontroller, starting from scratch. In this state, most of the microcontroller’s internal devices take on a known state. The microcontroller needs a reset pin to restart the system, called Master Clear. Resetting is done by applying a logic low.

Call and Return

: The instruction that causes the execution call is transferred to the subroutine. The return statement causes a return to the main program, you must remember the location of the statement following the call. Subroutines: A set of instructions that can be accessed from anywhere in the main program. It is an applet that uses the principal when needed. Advantages to the program as modular, reducing programming time and error detection, code easier to interpret. Even often need more than one subroutine, thus providing libraries of subroutines called libraries. Instructions sum: addlw k: Adds the contents of the log W with literal k. Stores the result in W. If there is carrying the flag C is set to 1. Add f, d: Adds the contents of W register the contents of register f. Stores the result in W if d = 0 and log f if d = 1. If there is carrying the Trafalgar is set to 1. Instructions subtraction: sublw k: Subtraction in 2’s complement the contents of the constant k less than the W register contents and stores the result in W. subwf f, d: complement Resta 2 f log contents in the register unless W. Stores the result in W if d = 0 and f if d = 1. Increase and decrease: dec f, d: The contents of register f is decremented by one. Stores the result in W if d = 0 and log f if d = 1. Trafalgar activates the Z 1 if the result of the operation is 0. Inc f d: The contents of register f is increased by one unit. W stores the result if d = 0 and log f if d = 1. The Z flag is triggered to 1 if the result of the operation is 0. Instructions logical: andlw k: Performs the logical AND operation between the contents of register W and k. contante Stores the result in W. The flag is triggered to 1 if the result of the operation is 0. Andwf f, d: Performs the logical AND operation between the W register contents and the contents of register f. Stores the result in W if d = 0 and log f if d = 1. The Z flag is triggered to 1 if the result of the operation is 0. Swapf f, d: the four bit more weight f register are exchanged with the four least significant bits of the same record f. If d = 0 the result is stored in W, and if d = 1 in f. xorlw k: Performs the exclusive OR function between the contents of register W and k contante of 8 bits. The result is stored in W, and Z is activated Trafalgar 1 if the result of the operation is 0. Xorwf f, d: performs the function exclusive OR between the contents of W register and the contents of register f. Stores the result in f if d = 1 and W if d = 0. The Z flag is triggered to 1 if the result of the operation is 0. Sleep: To save power the PIC have sleep instruction that happens to idle or low power consumption. Main clock is stopped and frozen its associated circuits. Btfsc f, b: This instruction can act in two ways: If the bit number b f is a registration statement that follows it is running normally, if the bit number b registration f is 0 the statement following it ignores and skips; btfss f, b: This instruction operates in a manner contrary to the above: If bit registration number f b is 0, the statement following it is executed normally; If bit registration number f b is 1 the statement following it is ignored and skipped.Ties or loops: program fragments that are repeated a finite or infinite number of times. Infinite Loop: it is an unconditional jump to an earlier position of the program formed a bond of infinite repetition. Battery: Memory is an area which is separated both program memory and the data within the microcontroller. Its structure is the LIFO type as the last data that is saved is the first out. N Structured Programming: division of the major program modules or procedures that perform a specific task within the program and running one after another linearly and with a minimal amount of hops. The timer0: a timer is implemented by means of a meter that determines a precise time between when the value is loaded and the time at which the overflow occurs. As an accountant : its mission is to count the number of external events represented by impulses. As timer: used to determine specific time intervals. These pulses have a duration of a cycle machine. Frequency Divider (Prescaler): it is sometimes necessary to control long times and increase the duration of the pulses that increase the TMR0. This is used the Prescaler, a programmable circuit that divides the frequency used. CIP has two timers, the timer0 (principal) and the Watchdog that monitors do not crash the program. INTCON: is a register located at address 0BH bank 0 and doubled in a bank 8BH. OPTION: record timer0 governs behavior. Watchdog: a 8-bit timer aim to generate a general reset when it overflows its own. His timing is independent of the main oscillator of the microcontroller and is based on an internal RC network sets an oscillation period of 18 ms. This period may be extended by making use of the Prescaler. Polling: technique used to read input to the microcontroller which cyclically checks the status of the corresponding entries. Disruptions: a mechanism by which an event can interrupt the execution of a program at any time . Since then a jump to a subroutine of attention to the disruption attending the event and returns immediately implementing the program. To produce an interruption should be timer0 TOIE and GIE bits of the INTCON register to 1.