Microcontroller:: Chapter-8: Microcontrollers
Microcontroller:: Chapter-8: Microcontrollers
1. What is Microcontroller?
Microcontroller:
The microcontroller is the electronic device. It has processing unit along with fixed sized ROM,
RAM, and other required peripheral components. These all the components are embedded on the
single chip. As it has all the components required to process and store data, it is also called
as minicomputer or computer on the single chip.
Below is the block diagram of the architecture of microcontroller.
1) Havard Architecture: In Havard architecture separate storage and signal buses are provided for different set
of instructions and data. This architecture has the entire data storage within the CPU and there is no access
available for instruction storage as data.
This architecture provides simultaneous access to an instructions and data stored inside internal buses of
microcontroller.
Let's see the block diagram of Havard Architecture:
2) Von Neumann Architecture: This architecture of microcontroller was proposed by scientist John Von
Neumann. In this architecture for both instruction and data a single data path or bus is present.
Therefore the CPU performs a single operation at a time. It either performs Read/Write operation on data, or
fetches a set of instruction from memory. Hence instruction fetch and a data transfer operation cannot occur
simultaneously by using a common bus.
Let's see the block diagram of Von Neumann Architecture:
1) AVR: AVR microcontroller is developed by Atmel service provider, AVR architecture is based on the Harvard
architecture. It is based on Reduced Instruction Set Computers (RISC). AVR is not an acronym it is just a name
given to the RISC architecture based microcontroller.
2) PIC: PIC microcontroller is also a Harvard architecture based controller. PIC is an acronym stands for
"Peripheral Interface Controller". This type of microcontroller supports programming in C, Assembly and BASIC
C.
3) Hitachi: Hitachi microcontroller belongs to H8 family of the controller. H8 is name used in large family of 8-
bit, 16-bit and 32-bit microcontroller developed by Renesas Technology, founded in an early 1990s within Hitachi
semiconductors.
4) Motorola: Motorola microcontroller is highly integrated microcontroller that is used for high-performance data
manipulation operation. This microcontroller unit uses a System Integration Module (SIM), Time Processing Unit
(TPU) and Queued Serial Module (QSM).
5. PIC and PIC16f877a Introduction
PIC Introduction:
Peripheral Interface Controllers (PIC) is one of the advanced microcontrollers developed by microchip
technologies. These microcontrollers are widely used in modern electronics applications. A PIC controller
integrates all type of advanced interfacing ports and memory modules. These controllers are more advanced than
normal microcontroller like 8051. The first PIC chip was announced in 1975 (PIC1650). As like normal
microcontroller, the PIC chip also combines a microprocessor unit called CPU and is integrated with various
types of memory modules (RAM, ROM, EEPROM ,etc.), I/O ports, timers/counters, communication ports, etc.
All PIC microcontroller family uses Harvard architecture. This architecture has the program and data accessed
from separate memories so the device has a program memory bus and a data memory bus (more than 8 lines in
a normal bus). This improves the bandwidth (data throughput) over traditional von Neumann architecture where
program and data are fetched from the same memory (accesses over the same bus). Separating program and data
memory further allows instructions to be sized differently than the 8-bit wide data word. Now we will move to
PIC16F877A.
PIC16F877A Introduction:
Microcontroller PIC16F877A is one of the PICMicro Family microcontroller which is popular at this moment,
start from beginner until all professionals. Because very easy using PIC16F877Aand use FLASH memory
technology so that can be write-erase until thousand times. The superiority this RISC Microcontroller compared
to with other microcontroller 8-bit especially at a speed of and his code compression.
The 16F877A is a capable microcontroller that can do many tasks because it has a large enough programming
memory (large in terms of sensor and control projects) 8k words and 368 Bytes of RAM. This is enough to do
many different projects.
6. Features of PIC16F877A
The PIC16F877A CMOS FLASH-based 8-bit microcontroller is upward compatible with the PIC16C5x,
PIC12Cxxx and PIC16C7x devices. It features 200 ns instruction execution, 256 bytes of EEPROM data
memory, self programming, an ICD, 2 Comparators, 8 channels of 10-bit Analog-to-Digital (A/D) converter, 2
capture/compare/PWM functions, a synchronous serial port that can be configured as either 3-wire SPI or 2-wire
I2C bus, a USART, and a Parallel Slave Port.
High-Performance RISC CPU
Lead-free; RoHS-compliant
Operating speed: 20 MHz, 200 ns instruction cycle
Operating voltage: 4.0-5.5V
Industrial temperature range (-40° to +85°C)
15 Interrupt Sources
35 single-word instructions
All single-cycle instructions except for program branches (two-cycle)
Special Microcontroller Features
Flash Memory: 14.3 Kbytes (8192 words)
Data SRAM: 368 bytes
Data EEPROM: 256 bytes
Self-reprogrammable under software control
In-Circuit Serial Programming via two pins (5V)
Watchdog Timer with on-chip RC oscillator
Programmable code protection
Power-saving Sleep mode
Selectable oscillator options
In-Circuit Debug via two pins
Peripheral Features
33 I/O pins; 5 I/O ports
Timer0: 8-bit timer/counter with 8-bit prescaler
Timer1: 16-bit timer/counter with prescaler
Can be incremented during Sleep via external crystal/clock
Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler
Two Capture, Compare, PWM modules
16-bit Capture input; max resolution 12.5 ns
16-bit Compare; max resolution 200 ns
10-bit PWM
Synchronous Serial Port with two modes:
SPI Master
I2C Master and Slave
USART/SCI with 9-bit address detection
Parallel Slave Port (PSP)
8 bits wide with external RD, WR and CS controls
Brown-out detection circuitry for Brown-Out Reset
Analog Features
10-bit, 8-channel A/D Converter
Brown-Out Reset
Analog Comparator module
2 analog comparators
Programmable on-chip voltage reference module
Programmable input multiplexing from device inputs and internal VREF
Comparator outputs are externally accessible
7. Different Applications of Microcontroller
Microcontroller is termed as “Computer-on-a-Chip“. It is named so, because not only the CPU, but
RAM, ROM, I/O ports, Timer/Counter, Serial I/Os all are put together on a single microcontroller
chip. Microcontrollers are task specific and are essentially used for making Embedded Systems.
The below image shows very few examples for the application of microcontrollers in our day-to-day
used products. Actually wherever we need electronic automation, we use microcontroller. The
application area of microcontrollers is so vast, probably we won’t be able to finish up listing them
all. Here we will categorize different application areas of microcontrollers:
Figure: Application of microcontroller
i. Consumer Electronics Products:
Toys, Cameras, Robots, Washing Machine, Microwave Ovens etc. [any automatic home
appliance]
ii. Instrumentation and Process Control:
Oscilloscopes, Multi-meter, Leakage Current Tester, Data Acquisition and Control etc.
iii. Medical Instruments:
ECG Machine, Accu-Check etc.
iv. Communication:
Cell Phones, Telephone Sets, Answering Machines etc.
v. Office Equipment:
Fax, Printers etc.
vi. Multimedia Application:
Mp3 Player, PDAs etc.
vii. Automobile:
Speedometer, Auto-breaking system etc.
8. Differentiate between CISC and RISC
CISC is a Complex Instruction Set Computer. It is a computer that can address a large number of
instructions.
In the early 1980s, computer designers recommended that computers should use fewer instructions
with simple constructs so that they can be executed much faster within the CPU without having to
use memory. Such computers are classified as Reduced Instruction Set Computer or RISC.
The following points differentiate a CISC from a RISC –
CISC RISC
Many addressing modes causing complex Few addressing modes, fix instruction
instruction formats. format.
Memory Organization
I/O Ports
The PIC microcontroller consists of 5-ports, namely Port A, Port B, Port C, Port D and Port E.
BUS
BUS is used to transfer & receive the data from one peripheral to another. It is categorized into two
types like data bus and address.Data Bus is used to transfer or receive the data.
BUS
The address bus is used to transfer the memory address from the peripherals to the central processing
unit. Input/Output pins are used to interface the exterior peripherals; both the UART & USART are
serial communication protocols, used to interface with serial devices such as GPS, GSM, IR,
Bluetooth, etc.
A/D Converters
A/D converter is used to convert analog voltage values to digital voltage values. An A/D module in
Peripheral Interface Controller comprises of 5-inputs for 28-pin devices & 8-inputs for 40-pin
devices. The operation of the A/D converter is controlled by special registers like ADCON0 &
ADCON1. The upper and lower bits of the converter are stored in registers like ADRESH and
ADRESL. In this process, it needs 5V of an analog reference voltage.
General-purpose registers: General-purpose registers are used for storing temporary data and
results created during operation. For example, if the program performs a counting (for example,
counting products on the assembly line), it is necessary to have a register which stands for what we
in everyday life call “sum”. Since the microcontroller is not creative at all, it is necessary to specify
the address of some general purpose register by 1, after each product passes through a sensor, should
be created. Therefore, the microcontroller can execute that program because it now knows what and
where the sum which must be incremented is. Similar to this example, each program variable must
be reassigned some of general-purpose register.
SFR Registers: Special purpose registers are also Ram memory locations, but unlike general-
purpose registers, their purpose is predetermined during manufacturing process and cannot be
changed. Since their bits are physically connected to particular circuits on the chip (A/D converter,
serial communication module, etc.), any change of their contents directly affects the operation of
microcontroller or some of its circuits. For example, by changing TRISA register, the function of
each port A pin can be changed in a way it acts as input or output. Another feature of these memory
locations is that they have their names (registers and their bits), which considerably facilities program
writing. Since high-level programming language can use the list of all registers with their exact
addresses, it is enough to specify the register’s name in order to read or change its contents.
Von-Neumann architecture supports simple hardware. It allows the use of a single, sequential
memory. Today's processing speeds vastly outpace memory access times, and we employ a very fast
but small amount of memory (cache) local to the processor.
Harvard Architecture
The Harvard architecture offers separate storage and signal buses for instructions and data. This
architecture has data storage entirely contained within the CPU, and there is no access to the
instruction storage as data. Computers have separate memory areas for program instructions and data
using internal data buses, allowing simultaneous access to both instructions and data.
Programs needed to be loaded by an operator; the processor could not boot itself. In a Harvard
architecture, there is no need to make the two memories share properties.
Single memory to be shared by both code Separate memories for code and data.
and data.
Processor needs to fetch code in a separate Single clock cycle is sufficient, as
clock cycle and data in another clock cycle. separate buses are used to access code and
So it requires two clock cycles. data.
Higher speed, thus less time consuming. Slower in speed, thus more time-
consuming.
Simple in design. Complex in design.
You can use conditional compilation to select particular sections of code to compile while
excluding other sections. All compiler directives must be completed in the source file in which
they begun.
For example:
#DEFINE extended_format
First, #IFDEF checks if flag is defined by means of #DEFINE. If so, only block of code will be
compiled. Otherwise, alternate block of code in #ELSE (if any) will be compiled. #ENDIF ends
the conditional sequence. The result of the preceding scenario is that only one section of code
(possibly empty) is passed on for further processing. The processed section can contain
further conditional clauses, nested to any depth; each #IFDEF must be matched with a
closing #ENDIF.
Here is an example:
Unlike #IFDEF, #IFNDEF checks if flag is not defined by means of #DEFINE, thus producing the
opposite results.
Include Directive #I
The #I parameter directive instructs mikroBasic PRO for PIC to include the named text file in
the compilation. In effect, the file is inserted in the compiled text right after the #I
filename directive. If filename does not specify a directory path, then, in addition to searching
for the file in the same directory as the current unit, mikroBasic PRO for PIC will search for file
in order specified by the search paths.
To specify a filename that includes a space, surround the file name with quotation marks: #I
"My file".
There is one restriction to the use of include files: An include file can't be specified in the
middle of a statement part. In fact, all statements between the begin and end of a statement
part must exist in the same source file.
EEPROM Registers
The below table shows the registers associated with PIC16F877A UART.
Register Description
EECON1 EEPROM read/Write Control register
EECON2 Used to execute special instruction sequence(0x55-0xAA) during write
EEDATA Holds the data to be Written/Read to/from EEPROM.
EEADR Hold the Eeprom memory address from where the data needs to be read/written.
1. Check the WR bit to see if a write is in progress and wait till it becomes zero.
2. Write the address to EEADR. Make sure that the address is not larger than the memory size
of the device.
3. Write the 8-bit data value to be programmed in the EEDATA register.
4. Clear the EEPGD bit to point to EEPROM data memory.
5. Set the WREN bit to enable program operations.
6. Disable interrupts (if enabled).
7. Write 55h to EECON2
8. Write AAh to EECON2
9. Set the WR bit
10. Restore the Interrupts.
11. Clear the WREN bit to disable program operations.
Steps For EEPROM Read
1. Check the WR bit to see if a write is in progress and wait till it becomes zero.
2. Write the address to EEADR from where the data needs to be read. Make sure that the
address is not larger than the memory size of the device.
3. Set the RD bit to start the read operation
4. Read the data from the EEDATA register.
Reference: https://exploreembedded.com/wiki/PIC_Internal_Eeprom
16. Data types in MikroC for Microcontroller:
There are several types of data that can be used in C programming language. A table below shows
the range of values which these data can have when used in their basic form.
Data type Description Size (number of bits) Range of values
char Character 8 0 to 255
int Integer 16 -32768 to 32767
float Floating point 32 ±1.17549435082 ·10-38 to
±6.80564774407 ·1038
double Double precision 32 from ±1.17549435082 ·10-
38
floating point to ±6.80564774407 ·1038
By adding prefix (qualificator) to any data type, the range of its possible values changes as well as
the number of memory bytes needed.
Data type Data type with Size (number of bits) Range
prefix
char signed char 8 -128 to 128
int unsigned int 16 0 to 65535
short int 8 0 to 255
signed short int 8 -128 to 127
long int 32 0 to 4294967295
signed long int 32 -2147483648 to
2147483647
17. Operators, Operations and Expressions in MikroC for Microcontroller:
An operator is a symbol denoting particular arithmetic, logic or some other operation. There are more than
40 operations available in C language, but at most 10-15 of them are used in practice. Every operation is
performed upon one or more operands which can be variables or constants. Besides, every operation features
priority execution and associativity as well.
ARITHMETIC OPERATORS
Arithmetic operators are used in arithmetic operations and always return positive results. Unlike unary
operations being performed upon one operand, binary operations are performed upon two operands. In other
words, two numbers are required to execute a binary operation. For example: a+b or a/b.
OPERATOR OPERATION
+ Addition
- Subtraction
* Multiplication
/ Division
% Reminder
ASSIGNMENT OPERATORS
There are two types of assignments in C language:
Simple operators assign values to variables using the common '=' character. For example: a = 8
Compound assignments are specific to C language and consist of two characters as shown in the table.
An expression can be written in a different way as well, but this one provides more efficient machine code.
OPERATOR EXAMPLE
Expression Equivalent
+= a += 8 a=a+8
-= a -= 8 a=a-8
*= a *= 8 a=a*8
/= a /= 8 a=a/8
%= a %= 8 a=a%8
INCREMENT AND DECREMENT OPERATORS
Increment and decrement by 1 operations are denoted by '++' and '--'. These characters can either precede or
follow a variable. In the first case (++x), the x variable will be first incremented by 1, then used in
expression. Otherwise, the variable will be first used in expression, then incremented by 1. The same applies
to the decrement operation.
RELATIONAL OPERATORS
Relational operators are used in comparisons for the purpose of comparing two variables which can be
integers ( int ) or floating point numbers (float). If an expression evaluates to true, a 1 is returned. Otherwise,
a 0 is returned. This is used in expressions such as ‘if the expression is true then...’
0 1 1
1 0 1
1 1 1
OPERATOR LOGICAL NOT
! Operand1 Result
0 1
1 0
BITWISE OPERATORS
Unlike logic operations being performed upon variables, the bitwise operations are performed upon single
bits within operands. Bitwise operators are used to modify the bits of a variable. They are listed in the table
below:
19. Interrupt:
An interrupt is a signal to the processor emitted by hardware or software indicating an event that
needs immediate attention. Whenever an interrupt occurs, the controller completes the execution of
the current instruction and starts the execution of an Interrupt Service Routine (ISR) or Interrupt
Handler. ISR tells the processor or controller what to do when the interrupt occurs. The interrupts
can be either hardware interrupts or software interrupts.
Hardware Interrupt
A hardware interrupt is an electronic alerting signal sent to the processor from an external device,
like a disk controller or an external peripheral. For example, when we press a key on the keyboard
or move the mouse, they trigger hardware interrupts which cause the processor to read the keystroke
or mouse position.
Software Interrupt
A software interrupt is caused either by an exceptional condition or a special instruction in the
instruction set which causes an interrupt when it is executed by the processor. For example, if the
processor's arithmetic logic unit runs a command to divide a number by zero, to cause a divide-by-
zero exception, thus causing the computer to abandon the calculation or display an error message.
Software interrupt instructions work similar to subroutine calls.
What is Polling?
The state of continuous monitoring is known as polling. The microcontroller keeps checking the
status of other devices; and while doing so, it does no other operation and consumes all its processing
time for monitoring. This problem can be addressed by using interrupts.
In the interrupt method, the controller responds only when an interruption occurs. Thus, the
controller is not required to regularly monitor the status (flags, signals etc.) of interfaced and inbuilt
devices.
Interrupts v/s Polling
Here is an analogy that differentiates an interrupt from polling −
Interrupt Polling
An interrupt is like a shopkeeper. If one The polling method is like a salesperson. The
needs a service or product, he goes to him salesman goes from door to door while requesting
and apprises him of his needs. In case of to buy a product or service. Similarly, the controller
interrupts, when the flags or signals are keeps monitoring the flags or signals one by one for
received, they notify the controller that they all devices and provides service to whichever
need to be serviced. component that needs its service.