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

Microcontroller Lecture

The document provides an introduction to microcontrollers, including: 1) A microcontroller is a single-chip computer used to control embedded systems. It contains a processor, memory, and peripherals on a single chip. 2) Microcontrollers are classified based on bus width, instruction set, and memory structure. Common types include 8-bit, 16-bit, and 32-bit microcontrollers. 3) Memory architecture can be Harvard (separate program and data memory spaces) or Princeton (common memory space). Common microcontroller families include 8051, PIC, AVR, and ARM.

Uploaded by

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

Microcontroller Lecture

The document provides an introduction to microcontrollers, including: 1) A microcontroller is a single-chip computer used to control embedded systems. It contains a processor, memory, and peripherals on a single chip. 2) Microcontrollers are classified based on bus width, instruction set, and memory structure. Common types include 8-bit, 16-bit, and 32-bit microcontrollers. 3) Memory architecture can be Harvard (separate program and data memory spaces) or Princeton (common memory space). Common microcontroller families include 8051, PIC, AVR, and ARM.

Uploaded by

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

Introduction to Microcontroller

Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1
General microcontroller
1. Microcontroller definition
A microcontroller is a small, low-cost, and self-contained computer on a chip that can be used as an
embedded system.
A microcontroller is a single chip and it is denoted with μC or uC.
An alternate name of the microcontroller is the embedded controller.
Note:
Embedded system:
An embedded system is a microprocessor-based computer hardware system with software that is
designed to perform a dedicated function, either as an independent system or as a part of a
large system. At the core is an integrated circuit designed to carry out computation for real-time
operations.
A combination of computer hardware and software where software is embedded into hardware is
called an embedded system.

2. Short description of microcontroller


It is a compressed microcomputer used to control the embedded system functions in robots, office
machines, motor vehicles, home appliances & other electronic gadgets.
The different components used in a microcontroller are a processor, peripherals, & memory.

3. Classification
The microcontrollers are characterized regarding bus-width, instruction set, and memory
structure.

For the same family, there may be different forms with different sources/manufacturer.

At present, there are different microcontrollers types that are existing in the market like 4-bit, 8-bit, 64-
bit & 128-bit.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3

Microcontrollers Types According to the Number of Bits


The bits in the microcontroller are 8-bits, 16-bits, and 32-bits microcontroller.

In an 8-bit microcontroller, the point when the internal bus is 8-bit then the ALU performs
the arithmetic and logic operations. The examples of 8-bit microcontrollers are Intel
8031/8051, PIC1x, and Motorola MC68HC11 families.

The 16-bit microcontroller performs greater precision and performance as compared to


the 8-bit. For example, 8-bit microcontrollers can only use 8 bits, resulting in a final range
of 0×00 – 0xFF (0-255) for every cycle. In contrast, 16-bit microcontrollers with their bit
data width have a range of 0×0000 – 0xFFFF (0-65535) for every cycle.

A longer timer’s most extreme worth can likely prove to be useful in certain applications
and circuits. It can automatically operate on two 16 bit numbers. Some examples of the
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4
16-bit microcontrollers are 16-bit MCUs are extended 8051XA, PIC2x, Intel 8096, and
Motorola MC68HC12 families.

The 32-bit microcontroller uses the 32-bit instructions to perform the arithmetic and logic
operations. These are used in automatically controlled devices including implantable
medical devices, engine control systems, office machines, appliances, and other types of
embedded systems. Some examples are Intel/Atmel 251 family, PIC3x.

Microcontrollers Types According to Memory Devices


The memory devices are divided into two types, they are

• Embedded memory microcontroller


• External memory microcontroller
Embedded Memory Microcontroller: When an embedded system has a microcontroller
unit that has all the functional blocks available on a chip is called an embedded
microcontroller. For example, 8051 having program & data memory, I/O ports, serial
communication, counters and timers and interrupts on the chip is an embedded
microcontroller.

External Memory Microcontroller: When an embedded system has a microcontroller


unit that has not all the functional blocks available on a chip is called an external memory
microcontroller. For example, 8031 has no program memory on the chip is an external
memory microcontroller.

Microcontrollers Types According to Instruction Set


CISC: CISC is a Complex Instruction Set Computer. It allows the programmer to use one
instruction in place of many simpler instructions.

RISC: The RISC stands for Reduced Instruction set Computer, this type of instruction sets
reduces the design of microprocessor for industry standards. It allows each instruction to
operate on any register or use any addressing mode and simultaneous access of program
and data.

Example for CISC and RISC

CISC: Mov AX, 4 RISC: Mov AX, 0


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5
Mov BX, 2 Mov BX, 4

ADD BX, AX Mov CX, 2

Begin ADD AX, BX

Loop Begin

From the above example, RISC systems shorten execution time by reducing the clock
cycles per instruction, and CISC systems shorten execution time by reducing the number
of instructions per program. The RISC gives a better execution than the CISC.

Microcontrollers Types According to Memory Architecture


The memory architecture of microcontroller are two types, they are namely:

• Harvard memory architecture microcontroller


• Princeton memory architecture microcontroller
Harvard Memory Architecture Microcontroller: The point when a microcontroller unit
has a dissimilar memory address space for the program and data memory, the
microcontroller has Harvard memory architecture in the processor.

Princeton Memory Architecture Microcontroller: The point when a microcontroller has


a common memory address for the program memory and data memory, the
microcontroller has Princeton memory architecture in the processor.

Microcontrollers Types

There are different microcontroller types like 8051, PIC, AVR, ARM,

AVR Microcontrollers
AVR microcontroller is developed by Alf-Egil Bogen and Vegard Wollan from Atmel
Corporation. The AVR microcontrollers are modified Harvard RISC architecture with
separate memories for data and program and the speed of AVR is high when compared to
8051 and PIC. The AVR stands for Alf-Egil Bogen and Vegard Wollan’s RISC processor.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 6

Atmel AVR Microcontroller

Classification of AVR Controllers

AVR Microcontrollers are classified into three types:

i) TinyAVR – Less memory, small size, suitable only for simpler applications.
ii) MegaAVR – These are the most popular ones having a good amount of memory (up
to 256 KB), the higher number of inbuilt peripherals, and suitable for moderate to
complex applications.
iii) XmegaAVR – Used commercially for complex applications, which require large
program memory and high speed.
Features of AVR Microcontroller

• 16KB of In-System Programmable Flash memory


• 512B of In-System Programmable EEPROM
• 16-bit Timer with extra features
• Multiple internal oscillators
• Internal, self-programmable instruction flash memory up to 256KB
• In-system programmable using ISP, JTAG or high voltage methods
[Note: ISP, JTAG are PCB testing method]
• Optional boot code section with independent lock bits for protection
• Synchronous/asynchronous serial peripherals (UART/USART)
[Note: USART — a Universal Synchronous/Asynchronous Receiver/Transmitter]
• Serial peripheral interface bus (SPI)
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 7
• Universal serial interface (USI) for two/three-wire synchronous data transfer
• Watchdog timer (WDT)
• Multiple power-saving sleep modes
• 10-bit A/D Converters, with a multiplex of up to 16 channels
• CAN and USB controller support
• Low-voltage devices operating down to 1.8v
[Note: TTL - LVTTL conversion/level shifter 5V, 3.3V,2.5V,1.8V ]
[LVTTL: Low voltage Transistor Transistor Logic]
There are many AVR family microcontrollers, such as ATmega8, ATmega16, and so on.
Here, we discussing the ATmega328 microcontroller. The ATmega328 and ATmega8 are
pin-compatible ICs but functionally they are different. The ATmega328 has a flash
memory of 32kB, where the ATmega8 has 8kB. Other differences are extra SRAM and
EEPROM, the addition of pin change interrupts, and timers. Some of the features of
ATmega328 are:
[ SRAM : static random access memory]
[ EEPROM : electrically erasable programmable read-only memory]
[ Flash Ram : Non volatile ram]
Features of ATmega328
• 28-pin AVR microcontroller
• Flash program memory of 32kbytes
• EEPROM data memory of 1kbytes
• SRAM data memory of 2kbytes
• I/O pins are 23
• Two 8-bit timers
• A/D converter
• Six-channel PWM
• Inbuilt USART
• External Oscillator: up to 20MHz

Pin Description of ATmega328


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 8
It comes in 28 pin DIP, shown in the figure below:

AVR Microcontrollers Pin Diagram

Vcc: Digital supply voltage.

GND: Ground.

Port B: Port B is an 8-bit bi-directional I/O port. The Port B pins are tri-stated when a reset
condition becomes active or one, even if the clock is not running.
[ Tri-state : Can be isolated from bus]
Port C: Port C is a 7-bit bi-directional I/O port with internal pull-up resistors.

PC6/RESET

Port D: It is an 8-bit bi-directional I/O port with internal pull-up resistors. The output buffers
of Port D consist of symmetrical drive characteristics.

AVcc: AVcc is the supply voltage pin for the ADC.

AREF: AREF is the analog reference pin for the ADC.

Applications of AVR Microcontroller


There are many applications of AVR microcontrollers; they are used in home automation,
touch screen, automobiles, medical devices, and defense.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 9

MSP Microcontroller
A microcontroller like MSP430 is a 16-bit microcontroller. The term MSP is the acronym of
“Mixed Signal Processor”. This microcontroller family is taken from Texas Instruments and
designed for low cost as well as low power dissipation systems. This controller includes a
16-bit data bus, addressing modes-7 with reduced instructions set, which permits a
denser, shorter, programming code used for quick performance.
This Microcontroller is one kind of integrated circuit, used to execute the programs to
control other machines or devices. It is one kind of a micro-device, used to control other
machines. The features of this microcontroller are normally obtainable with other kinds of
the microcontroller.
• Complete SoC like ADC, LCD, I/O ports, RAM, ROM, UART, watchdog timer, basic
timer, etc. [SoC : System of chip]
• It uses one external crystal and an FLL (frequency-locked loop) oscillator mainly derives
all inner CLKs
• Power utilization is low like 4.2 nW only for each instruction
• Stable generator for the most frequently used constants like –1, 0, 1, 2, 4, 8
• Typical high speed is 300 ns for each instruction like 3.3 MHz CLK
• Addressing modes are 11 where the seven addressing modes are used for source
operands & four addressing modes are used for destination operand.
• RISC architecture with 27 core instructions
Real-time capacity is full, stable, and nominal system CLK frequency is obtainable after 6-
clocks only once the MSP430 is restored from low-power mode. For the main crystal, no
waiting to start stabilize & oscillation.

The core instructions were combined using special features to make the program easy
within the MSP430 microcontroller using assembler otherwise in C to provide outstanding
functionality as well as flexibility. For instance, even by using a low instruction count, the
microcontroller is capable of following approximately the whole instruction set.

Hitachi Microcontroller
Hitachi microcontroller belongs to the H8 family. A name like H8 is used within a large 8-
bit, 16-bit & 32-bit family of microcontrollers. These microcontrollers were developed
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 10
through Renesas Technology. This technology was founded in Hitachi semiconductors, in
the year 1990.

Motorola Microcontroller
Motorola microcontroller is an extremely incorporated microcontroller, used for data
handling process with high-performance. The unit of this microcontroller uses a SIM
(System Integration Module), TPU (Time Processing Unit) & QSM (Queued Serial
Module).

4. Architecture
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 11

The microcontroller’s basic structure includes following different components.


CPU
The microcontroller is called a CPU device, used to carry & decode the data & finally
completes the allocated task effectively.
By using a central processing unit, all the microcontroller components are connected to
a particular system.
Instruction fetched through the programmable memory can be decoded through the
CPU.
Memory
In a microcontroller, the memory chip stores all the data as well as programs.
Microcontrollers are designed with some amount of RAM/ROM/flash memory to store
the program source code.
system bus
A bus is a pathway for digital signals to rapidly move data.
There are three internal buses associated with processors: the data bus, address bus,
and control bus.
Together, these three make up the “system bus.
The system bus is an internal bus, intended to connect the processor with internal
hardware devices, and is also called the “local” bus, Front Side Bus.
system bus comprises of following buses:
a) Address bus carries memory addresses from the processor to other
components such as primary storage and input/output devices.
b) Data bus carries the data between the processor and other components.
c) Control bus carries control signals from the processor to other
components.

I/O Ports
Basically, these ports are used to interface with different appliances like LEDs, LCDs,
printers, etc.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
12
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5. Function(ADC/Timer/Interupt/PWM/USB/serial communication)
Microcontroller performs special functions described below.

Timers
A microcontroller includes timers otherwise counters.
These are used to manage all the operations of timing and counting in a
microcontroller.
The main function of the counter is to count outside pulses whereas the operations
which are performed through timers are clock functions, pulse generations,
modulations, measuring frequency, making oscillations, etc.
Interpret Control
This controller is employed to give delayed control to a running program &
interpretation is either internal otherwise external.
ADC (Analog to Digital Converter)
ADC is the acronym of analog to digital converter.
The main function of ADC is to change the signals from analog to digital. For ADC, the
required input signals are analog and the production of a digital signal is used in
different digital applications like measurement devices
DAC (Digital to Analog Converter)
The acronym of DAC is digital to analog converter, used to perform reverse functions
to ADC.
Generally, this device is used to manage analog devices such as DC motors, etc.
Serial Ports
Serial ports are used to provide serial interfaces between microcontroller as well as a
variety of other peripherals which support serial port communication.
USB Port
USB (Universal Serial Bus)
It is the most common connection used to connect a microcontroller to different
devices such as digital cameras, printers, scanners and external hard drives etc.
USB TRANSFER SPEEDS
USB 1.0 Supports data transfer rates of:
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 13
5 Mega bits per second Mbps (low speed), used for Human Input Devices e.g

keyboard, mouse, joysticks etc
• 12 Mbps (high speed) used for printers, scanners etc
USB 2.0 known as hi-speed USB and capable of supporting a transfer rate up to
480Mbps.
USB 3.0 Also known as super-speed USB and supports transfer rates up to
5.0 gigabits per second (Gbps).

Special Functioning Block


Some special microcontrollers designed for special devices like robots, space systems
include a special function block.
This block has extra ports to carry out some particular operations.

6. Purpose/Application
There are many applications of microcontrollers; they are used in home automation,
touch screen, automobiles, medical devices, and defense.

Microcontrollers are mainly used for embedded devices, in contrast to the


microprocessors which are utilized in personal computers otherwise other devices.
These are mainly used in different appliances like implantable medical devices, power
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 14
tools, engine control systems in automobiles, machines used in offices, appliances
controlled through remote, toys, etc. The main applications of microcontrollers types
include the following.

Automobiles

Hand-held metering systems

Mobile Phones

Computer Systems

Security Alarms

Appliances

Current meter

Cameras

Micro Oven

Measurement Instruments

Devices for process control

Used in metering & measurement devices, voltmeter, measuring rotating objects

Controlling Devices

Industrial instrumentation devices

Instrumentation devices in Industries

Light Sensing

Safety devices

Process control devices

Controlling devices

Fire detection
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 15
Temperature sensing

Mobile Phones

Auto Mobiles

Washing Machines

Cameras

Security Alarms

7. Process of using(Programming/Burning)
Microcontroller programming means coding of a microcontroller for different purposes
in a special software. There are lots of software in which we can write different codes
for microcontroller and IC Chips..

Microcontrollers were originally programmed only in assembly language, but


various high-level programming languages, such as C, Python and JavaScript, are
now also in common use to target microcontrollers and embedded systems.

• write program code on your computer.


• compile the code with a compiler for the microcontroller you are using.
• upload the compiled version of your program, generally hexfile, to
your microcontroller.

Intel hexadecimal object file format, Intel hex format or Intellec Hex is a file format
that conveys binary information in ASCII text form. It is commonly used for
programming microcontrollers, EPROMs, and other types of programmable logic
devices.

8. Advantages
The advantages of microcontrollers types include the following.
Dependable
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 16
Reusable
Energy-efficient
Cost-effective
Reusable
It requires less time to operate
These are flexible & very small
Because of their high integration, its size & cost of the system can be decreased.
Interfacing of the microcontroller is easy with additional ROM, RAM & I/O ports.
Many tasks can be performed, so the human effect can be reduced.
It is simple to use, troubleshooting & maintaining the system is simple.
It works like a microcomputer without any digital parts

9. Disadvantages
The disadvantages of the microcontrollers types include the following.

Programming Complexity

Electrostatic Sensitivity

Interfacing with high-power devices cannot possible.

Its structure is more complex as compared with microprocessors.

Generally, it is used in microdevices

It simply performs incomplete no. of executions simultaneously.

It is generally used in micro equipment

It has a more complex structure as compared to a microprocessor

The microcontroller cannot interface a higher power device directly

It only performed a limited number of executions simultaneously


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 17

Specific microcontroller(PIC18f4550):
1. Pin configuration

PIC18F4550 is a 8-bit and 40-Pin PIC Microcontroller which belongs to PIC18


Family and has a Program Memory of 32KB, RAM of 2048
Bytes, EEPROM Memory of 256Bytes and CPU Speed of 12MIPS. It is mostly
used in embedded projects and is quite used in Engineering Projects. Few of its
main features are:
• It has five Ports on it starting from Port A to Port E.
• It has below mentioned Ports for Data Communication.
• USB Port
• Serial Port
• I2C Port
• It also has interrupt functionality in it and we can place any kind of interrupt in it.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 18
• Here's the Pin Diagram of PIC18F4550:

PIC18F4550 Compiler

Their official Compiler is available online at Microchip website and can be used free
of cost.

MikroC Pro for PIC Compiler which you can download from their official site and it has
a demo version but you have to buy the full version.

In addition CCS C compiler is also available.


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 19

All the Grounded lines are shown in black color and Power Lines with +5V are shown
with Red Lines.
We also have Crystal Oscillator for frequency here connected to Pin # 13 & Pin # 14.
Now, let's have a look at PIC18F4550 Ports:
Port A: It has 6 Pins in total starting from Pin # 2 to Pin # 7.
Port B: It has 8 Pins in total starting from Pin # 33 to Pin # 40.
Port C: It has 8 Pins in total. It's pins are not aligned together. ...
Port D: It has 8 Pins in total.
Port E: It has 3 Pins in total starting from Pin # 8 to Pin
2. Architecture
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 20

3. Function(ADC/Timer/Interupt/PWM/USB/serial communication)
PIC18F4550 USB Communication
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 21
Here's the main reason of PIC18F4550's popularity, it supports USB Communication.
Here's the simplest circuit diagram for PIC18F4550 USB Communication,

So, we can do the PIC18F4550 USB Communication by using below two Pins:
Pin # 23: (D-)
Pin # 24: (D+)

We can also do the PIC18F4550 Serial Communication.


PIC18F4550 Serial Communication Pins are shown in below figure:
You can see in the figure that below two pins are used for PIC18F4550 Serial
Communication:
Pin # 25 (TX).
Pin # 26 (RX).
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 22

Successive Approximation ADC


The successive approximation ADC generates a series of digital codes each
corresponding to a fixed analog level with an internal counter to compare with the
analog signal under conversion. The generation is stopped when the analog level
becomes larger than the analog signal. The digital code corresponding to the analog
level is the desired digital representation of the analog signal.

The performance of the ADC is measured using the resolution and speed as
parameters. Resolution is nothing but the number of levels or intervals which can be
divided from a certain analog input range which is expressed in bits. The resolution of
an n-bit ADC is 1/(2^n). For example, an ADC with 10-bit resolution as one in PIC
18F4550 can have 2^10 (1024) intervals for the range of analog input signal.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 23

ADC in PIC Microcontroller (PIC18F4550)


The ADC in PIC18F4550 is a successive approximation ADC with a resolution of 10
bits. The resolution indicates how much the reference voltage can be divided. For a 10
bit resolution ADC, it is possible to divide up to 1024 (2^10) voltages. So for a 5V
reference voltage, the minimum voltage will be 5/1024 = 4.8mV. This means 4.8mV in
the analog pin will be detected as 1 and 9.6mV will be 2 (10 in binary). It can’t detect
the difference between 1mV and 3mV since both will be detected as 0. This is where
the resolution comes into the picture. The more the resolution is, ADC will be better at
detecting the small voltage changes.

PIC18F4550 has 13 channels which mean 13 analog input signals can be converted
simultaneously using the module. 8 choosable clock inputs are available for conversion
and the module can be configured in auto triggering mode.

Other terminology usually appears while using ADC is the reference voltage. It
specifies the minimum and maximum voltage range of analog input. PIC18F4550 has
two reference voltage inputs, Vref- and Vref+. Vref- is the minimum input voltage of
analog input and Vref+ is the maximum voltage. That means if Vref- is applied to the
pin for conversion, the digital value after conversion will be 0 and for the Vref+, this will
be the maximum value (1023 for 10-bit resolution). The PIC18F4550 pins for Vref+ is
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 24
RA3 and Vref- is RA2. In normal cases, we set Vref- as 0 and Vref+ as 5V. The digital
value is calculated based on the following equation.

Digital value = [Analog value/ (Vref+ - Vref-)] * (2^resolution)

For Vref+ = Vdd, Vref- = Vss and 10 bit resolution


Digital vaue = [Analog value/ 5*1024]

Examples: for 0V -> Digital value = 0


for 2.49V -> Digital value = 511 (0b0111111111 in binary)
for 4.99V -> Digital value = 1023 (0b1111111111 in binary)
Another term useful while using ADC is acquisition time. It is the time required to fully
charge the charge holding capacitor to the input channel voltage level. In layman’s
terms, it is the time required to wait before starting conversion and after applying input
in the analog port pin. It depends on the holding capacitor value, an input impedance of
the analog source, Vdd, conversion error, temperature etc. Based on the commonly
used values, it is calculated as 2.45 us. What this means that we should start the
conversion only after applying input analog and passing 2.45 us.
A/D conversion clock is the analog to digital conversion time per bit and denoted as
Tad. A/D conversion requires 11 Tad per 10-bit conversion. There are seven possible
options for Tad.

• 2 Tosc
• 4 Tosc
• 8 Tosc
• 16 Tosc
• 32 Tosc
• 64 Tosc
• Internal RC Oscillator

Tad must be greater than minimum Tad specified and must be as short as possible.
From PIC18F4550 datasheet, it is 0.7 µs (page 400, table 28-29). So for a 20MHz
PIC18F4550,

Fosc = 20 MHz
Tosc = 1/20 MHz = 50ns

If we select 16 Tosc
16 Tosc = 16x 50ns = 0.8 us
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 25
which is more than 0.7 us(minimum requirement)
xxxxxxxxxxxxxxxxxxx

PIC18F4550 ADC
Introduction
• When we interface sensors to the microcontroller, the output of the sensor many of the times is analog
in nature. But microcontroller processes digital signals.
• Hence we use ADC in between sensors and microcontrollers. It converts an analog signal into digital
and gives it to the microcontroller.
• There are a large number of applications of ADC like in a biometric application, Environment
monitoring, Gas leakage detection, etc.

So now let’s see ADC in PIC18F4550.

• PIC18f4550 has inbuilt 10-bit 13 channel ADC.


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 26
• 13-channels of ADC are named as AN0-AN12. It means we can connect 13 different sensors at the
same time.
• 10-bit ADC means:
• It will give digital count in the range of 0-1023 (2^10).
• Digital Output value Calculation

• To keep things simple, let us consider that Vref is 5V,

• For 0Vin digital o/p value = 0

• For 5Vin digital o/p value = 1023 (10-bit)

• For 2.5Vin digital o/p value = 512 (10-bit)


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 27
ADC Registers of PIC18F4550

ADRESH (High byte) and ADRESL (Low byte) Registers are used in combination to store the
converted data i.e. digital data. But the data is only 10-bit wide, so the remaining six bits are not
used.

ADCON0: A/D Control Register 0

CHS3:CHS0: Analog Channel Select Bits

CHS3:CHS0 Channels Channel Name

0000 Channel 0 AN0

0001 Channel 1 AN1

0010 Channel 2 AN2

0011 Channel 3 AN3

0100 Channel 4 AN4

0101 Channel 5 AN5

0110 Channel 6 AN6

0111 Channel 7 AN7

1000 Channel 8 AN8

1001 Channel 9 AN9

1010 Channel 10 AN10

1011 Channel 11 AN11

1100 Channel 12 AN12


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 28
GO/DONE’: A/D Conversion Status Bit

When ADON=1

1= A/D conversion is in progress

0= A/D is idle

ADON: A/D Conversion Enable/ON bit

1= A/D conversion is ON (Start conversion).

0= A/D conversion is disabled.

ADCON1: A/D Control Register 1

VCFG1: Voltage Reference Configuration bit

1= Vref –

0= Vss

VCFG0: Voltage Reference Configuration bit

1= Vref +

0= Vdd

PCFG3:PCFG0: A/D Port Configuration Bits:

As the ADC pins in PIC18F4550 are multiplexed with many other functions. So these bits are
used to de-multiplex them and use them as an analog input pin.

xxxxxxxxxxx
The microcontroller PIC18F4550 has 13 ADC (Analog to Digital Converter) channels.

The resolution of the PIC18F4550 ADC is 10-bit, which means the analog value after conversion is stored as a
10-bit number that varies from 0 to 1023 (0x3FF).

The analog reference voltage is software selectable to either the device’s positive and negative supply
voltage(VDD and VSS) or the voltage level on the RA3/AN3/VREF+ and RA2/AN2/VREF-/CVREF pins.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 29
The A/D converter has a unique feature of being able to operate while the device is in Sleep mode.

To operate in Sleep, the A/D conversion clock must be derived from the A/D’s internal RC oscillator.

Timer
PIC18F4550 Timer

Introduction

Timers in microcontrollers are used for introducing delay, counting events, generating
waveforms, and also for PWM generation.

Delays in a microcontroller can be induced by either of the two ways –

1. Provide delay using a software (through code using loops). But, a delay provided in this
way forces the microcontroller to put all its resources for the processing of the loops, and
thus blocks the code execution.
2. Providing delay using the timer in the microcontroller. In this approach, the delay is
provided by loading a count in a timer. So when a timer interrupt is generated current
execution will move to the ISR to serve the task. But when there is no timer interrupt it will
perform another task. Therefore, we can say it is a kind of non-blocking function.

Let us discuss timers in the PIC18F4550 microcontroller.

PIC18F4550 has 4 in-built timers

• Timer0 : 16-bit
• Timer1 : 16-bit
• Timer2 : 8-bit
• Timer3 : 16-bit

Out of the four timers in PIC18F4550, we will be discussing here Timer1. The working of the rest
of the timers is the same. To generate a time delay using Timer1, we have to calculate a count of
the desired time.

How to calculate count for Timer?


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 30
Let’s take an example. Suppose we have to generate a delay of 1 ms having 8 MHz oscillator
frequency of PIC18F4550. To do this, we need to find the period of the instruction cycle.

Fosc = 8 MHz

PIC18F4550 timer uses frequency as follows:

FTIMER = 8 MHz / 4 = 2 MHz

This 2 MHz frequency is fed to the PIC timer.

Then, to find the period which will be taken by the timer to increment the count,

Period = 1 / 2 MHz = 0.5 us

Therefore, after each 0.5 us time elapses, the timer value will get incremented by 1.

To find the count needed for a delay of 1 ms,

Count = Desired Delay / Timer Period

Count = 1 ms / 0.5 us = 2000

Before we load this count in the timer, we first need to subtract it from 65536. The reason we
need to subtract it is that the timer is 16-bit. Therefore,

65536 – count = 65536 – 2000 = 63536

Converting this count into hex we get 0xF830. Load this hex value in the Timer1 i.e. TMR1
register. TMR1 is a 16-bit register that is used to load the count for Timer1.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 31

Timer1 Interrupt

The TMR1 register gets incremented from this loaded value to the highest value i.e. 0xFFFF. After
an overflow occurs in the register, it resets to 0 and the TMR1IF overflow interrupt flag gets set.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 32

TMR1H and TMR1L registers can also be individually used to load the count in Timer1. These are
both 8-bit registers.

To use Timer1 in PIC18F4550, Control and Status registers are used. Let us understand more
about these registers of Timer1 in PIC18F4550.

T1CON: Timer1 Control Register

• It is used to configure Timer1 and also to start the Timer1.

RD16: 16-bit read/write mode enable bit

1 = Enable read/write of Timer1 in one 16-bit register which is TMR1.

0 = Enable read/write of Timer1 in two 8-bit registers which are TMR1H and TMR1L

T1RUN: Timer1 system clock status bit

1 = Device clock is derived from the Timer1 oscillator

0 = Device clock is derived from another source; it may be an internal oscillator or external
crystal oscillator.

T1CKPS1: T1CKPS0: Timer1 input clock pre-scale bits

These bits divide the clock by a value before it is applied to the Timer1 as a clock.

11 = 1 : 8 pre-scale value

10 = 1 : 4 pre-scale value

01 = 1 : 2 pre-scale value

00 = 1 : 1 pre-scale value

T1OSCEN : Timer1 oscillator enable bit

1 = Enable Timer1 oscillator.

0 = Disable Timer1 oscillator.


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 33
T1SYNC : Timer1 external clock input synchronization bit

When TMR1CS = 0

Timer1 uses an internal clock oscillator. So this bit is ignored.

When TMR1CS = 1

1 = Do not synchronize external clock input.

0 = Synchronize external clock input.

TMR1CS : Timer1 clock source select bit

1 = External clock from RC0 / T1OSO / T13CKl

0 = Internal clock (Fosc / 4)

This bit is generally used for counting events. To use Timer1 as counter, set TMR1CS = 1.

TMR1ON: Timer1 ON bit

1 = Start Timer1.

0 = Stop Timer1.

After Timer1 initializes, it continuously monitors the TMR1IF interrupt flag which gets set when
the Timer1 overflows. TMR1IF is located in the PIR1register as shown below.

PIR1: Peripheral Interrupt Register

TMR1IF: Timer1 Overflow Interrupt Flag

0 = Timer1 register overflow not occurred

1 = Timer1 register overflow occurred

TMR2IF: Timer2 Overflow Interrupt Flag

CCP1IF : Capture / Compare Interrupt Flag

SSPIF: Master Synchronous Serial Port Interrupt Flag

RCIF and TXIF: These are Serial Communication Interrupt Flag

ADIF: A / D Converter Interrupt Flag


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 34
SPPIF: Streaming Parallel Port Read/Write Interrupt Flag

1. Sample projects
(Circuit,programming,Simulation)

Project 1:
Question:
Build a microcontroller project which converts analogue voltage into
equivalent binary/decimal number and display in LCD display unit.
----------------
This project converts analogue voltage into binary/decimal number.
Converted number is shown in LCD Display.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
A potentiometer is used to insert variable input voltage(0V-5V).

Program:
// PIC18F4550 ADC example with CCS C compiler

//LCD module connections


#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <18F4550.h>
#device ADC=10
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
#include <lcd.c>

unsigned int16 i;
void main(){
setup_oscillator(OSC_8MHZ);
lcd_init(); // Initialize LCD module
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 35
setup_adc(ADC_CLOCK_DIV_8); // Set ADC conversion time to 8Tosc
setup_adc_ports(AN0); // Configure RA0 (AN0) as analog
set_adc_channel(0); // Select channel 0 input
delay_ms(100); // Wait 100ms
lcd_gotoxy(3, 1); // Go to column 3 row 1
lcd_putc("ADC reading:");
while(TRUE){
i = read_adc();
lcd_gotoxy(7, 2); // Go to column 7 row 2
printf(lcd_putc,"%4Lu",i); // Write i with 4 numbers max
delay_ms(10); // Wait 10ms
}
}
------------------------------ xxxxxxxxxxxxx------------------------------------------

Circuit and simulation

-------------------xxxxxxxxxxxxxxxx-------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 36

Project 2:
Question:
Build a microcontroller project which converts analogue voltage into
equivalent binary/decimal number and display in LCD display unit.
Reference voltages should be used to have higher precision.
--------------------
This project converts analogue voltage into binary/decimal number.
Converted number is shown in LCD Display.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
A potentiometer is used to insert variable input voltage(0V-5V).
2 reference pins are used to limit the conversion voltage and variable precision.

Program:
// PIC18F4550 ADC example with CCS C compiler

//LCD module connections


#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <18F4550.h>
#device ADC=10
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
#include <lcd.c>

unsigned int16 i;
void main(){
setup_oscillator(OSC_8MHZ);
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 37
lcd_init(); // Initialize LCD module
setup_adc(ADC_CLOCK_DIV_8); // Set ADC conversion time to 8Tosc

// setup_adc_ports(AN0); // Configure RA0 (AN0) as analog


setup_adc_ports(AN0_TO_AN1, VREF_VREF); // Configure RA0 (AN0) as analog with reference voltage

set_adc_channel(0); // Select channel 0 input


delay_ms(100); // Wait 100ms
lcd_gotoxy(3, 1); // Go to column 3 row 1
lcd_putc("ADC reading:");
while(TRUE){
i = read_adc();
lcd_gotoxy(7, 2); // Go to column 7 row 2
printf(lcd_putc,"%4Lu",i); // Write i with 4 numbers max
delay_ms(10); // Wait 10ms
}
}
------------------------------ xxxxxxxxxxxxx------------------------------------------

Circuit and simulation


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 38
-------------------------xxxxxxxxxxxxxxxxxxxx----------------------

Project 3:
Question:
Build a microcontroller project where an LED blinks at a regular
interval.
--------------------
An LED blinks at a regular interval.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.

Program:
// PIC18F4550 ADC example with CCS C compiler

#include <18F4550.h>

#fuses NOMCLR INTRC_IO


#use delay(clock=8000000)

void main()
{

set_tris_b(0x00); // set output mode


output_b(0x00); //All pins of PORTE LOW (VSS)

setup_wdt(WDT_ON);
setup_wdt(WDT_1S);

while(TRUE)
{
output_toggle(PIN_B0); // Toggle output pin RB0
delay_ms(500);
restart_wdt();
}
}
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 39

Circuit and simulation

-------------------xxxxxxxxxxxxxxxx----------------------------

Project 4:
Question:
Build a microcontroller project showing bit wise data reading and
writing.
--------------------
Press switches and LEDs are use.
When a switch is pressed, corresponding LED is lit.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 40

Program:
// PIC18F4550 ADC example with CCS C compiler
#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
short azbit1, azbit2 ;

void main()
{
set_tris_b(0x00); // set output mode
output_b(0x00); //All pins of PORTE LOW (VSS)
set_tris_d(0xFF); // set Input mode
setup_wdt(WDT_ON);
setup_wdt(WDT_1S);

while(TRUE)
{

azbit1 = input(PIN_D0);
azbit2 = input(PIN_D1);

if( azbit1==1)
{
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}

if( azbit2==1)
{
output_high(PIN_B1);
}
else
{
output_low(PIN_B1);
}

// output_toggle(PIN_B0); // Toggle output pin RB0


delay_ms(500);
restart_wdt();
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 41
}
}

--------------xxxxxxxxxxxxxxx----------------------

Circuit and simulation

-----------------------------xxxxxxxxxxxxxxxxxx---------------------------

Project 5:
Question:
Build a microcontroller based project showing byte wise data
reading and writing.
--------------------
Band-switches and Band-LEDs are use.
Port D is read and written in port D
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 42
Program:
// PIC18F4550 ADC example with CCS C compiler

#include <18F4550.h>

#fuses NOMCLR INTRC_IO


#use delay(clock=8000000)
//short azbit1, azbit2 ;
int8 i;
//byte b1;

void main()
{

set_tris_b(0x00); // set output mode


output_b(0x00); //All pins of PORTE LOW (VSS)
set_tris_d(0xFF); // set Input mode

setup_wdt(WDT_ON);
setup_wdt(WDT_1S);

while(TRUE)
{

i=input_d();

delay_ms(10);
output_b(i); //All pins of PORT b

// output_toggle(PIN_B0); // Toggle output pin RB0


delay_ms(200);
restart_wdt();
}
}
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 43

Circuit and simulation

-----------------xxxxxxxxxxxxx------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 44
Project 6:
Question:
Build a microcontroller based project do develop a sine wave
inverter. Use internal interrupt and PWM.
--------------------
Internal interrupts are used. Timer2 is used to generate PWM. Sine function is used to
generate sinusoidal pulse width modulation.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.

Program:
// PIC18F4550 ADC example with CCS C compiler
#include <code6.h>
#fuses MCLR
#FUSES WDT // Watch Dog Timer
#FUSES HS //Highspeed Osc > 4mhz
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=20000000)

#include <lcd.c> // it is required for clock purpose/ LCD

long int init_time=0 ; // present time, max time, timer initial number
short mode;
int8 i_az=0;

int8 sin_table1[105];//Store 0~180 degree sine lookup table


#define PI 3.14159265
#define Step_delay 1 //micro second
#INCLUDE <MATH.H>

// for interrupt timer 0 --- start


#int_RTCC
void RTCC_isr(void)
{
set_timer0(init_time);
set_pwm2_duty(5); // TEMP
restart_wdt();

i_az = -1;
mode = ~mode; // Toggle mode variable
if(mode == 1)
{
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 45
output_high(PIN_B6); //
output_low(PIN_B5); //
}
else
{
output_low(PIN_B6); //
output_high(PIN_B5); //
}
}

// for interrupt timer 0 --- end

// interupt timer2 ============= start


#int_TIMER2
void TIMER2_isr(void)
{
i_az= i_az + 1;

set_pwm2_duty(sin_table1[i_az]);
if(i_az >= 101) i_az = 100;
}

// interupt timer2 ============= End

void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
init_time=64286; // 34.286, 65536 -1250
set_timer0(init_time);
enable_interrupts(INT_RTCC);

//////// fill sine lookup table //////////////////////


sin_table1[0]=0;
for (i_az = 0; i_az <=100; i_az++)
{
sin_table1[i_az]=floor(100*sin(i_az * PI /100));

for (i_az = 40; i_az <=60; i_az++)


{
sin_table1[i_az]=100;

}
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 46
sin_table1[0]=0;
sin_table1[1]=0;

sin_table1[97]=0;
sin_table1[98]=0;
sin_table1[99]=0;
sin_table1[100]=0;
sin_table1[101]=0;
sin_table1[102]=0;

//////// main loop ///////////////////////////////////


//set_pwm2_duty(50);

clear_interrupt(INT_TIMER2); // Clear Timer2 interrupt flag bit

setup_timer_2(T2_DIV_BY_1,0x64,1); // 99 --> 100

setup_ccp2(CCP_PWM);

enable_interrupts(INT_TIMER2); // Enable Timer2 interrupt

enable_interrupts(GLOBAL);

set_tris_e(0xF0);
set_tris_b(0x0F);
output_e(0x00); //All pins of PORTE LOW (VSS)

lcd_init(); // Initialize LCD module

setup_wdt(WDT_ON);
setup_wdt(WDT_1S);

} // End main

Circuit and simulation


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 47

The circuit diagram of sine wave inverter


Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 48

Generated Sine wave(Yellow) and other required waves


---------------------xxxxxxxxxxxxxxxxxx-------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 49
Project 7:
Question:
Build a microcontroller based project TO SHOW EXTERNAL
INTERRUPT.
--------------------
External interrupts are used.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.

Program:
// PIC18F4550 example with CCS C compiler
#include <18F4550.h>
#fuses NOMCLR
#FUSES WDT // Watch Dog Timer
#FUSES HS //Highspeed Osc > 4mhz
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=20000000)

// for External interrupt 0 --- start


#INT_EXT
void EXT_ISR(void)
{
output_low(PIN_B3); // output low pin RB0
output_high(PIN_B4); // output high pin RB1

// for External interrupt 0 --- end

void main()
{
setup_oscillator(OSC_8MHZ); // Set the internal oscillator to 8MHz

set_tris_b(0x01); // set output mode


output_b(0x01); //All pins of PORTE LOW (VSS)
clear_interrupt(INT_EXT); // Clear external interrupt flag bit

enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
enable_interrupts(GLOBAL);
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 50
while(TRUE)
{
output_low(PIN_B3); // output low pin RB3

delay_ms(500);
restart_wdt();
output_high(PIN_B3); // output low pin RB3
delay_ms(800);
output_low(PIN_B4);
restart_wdt();
}
}
Circuit and simulation

---------------------------xxxxxxxxxxxxxxxx-----------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
51
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

########################################################
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 52
Square wave inverter

You might also like