You are on page 1of 115

Course On

Microcontrollers
By
Md. Ariful Hoque Shuhan
mdshuhan@yahoo.com
Mobile: 01678142459
&
Abdur Rahman Noble
arnoble072@gmail.com
Mobile:01670225958

What is a Microcontroller?
Mini-Computer

Microprocessor
The Brains
Arithmetic Logic Unit (ALU)
Control Unit

Program/

Data Storage
Peripherals (Input/Output)
Low-Cost

History
The

first microprocessor was developed by


what was then a small company called Intel
(short for Integrated Electronics) in the early
1970s.
The chipset was a success and within a short
while Intel developed a general purpose 4 bit
microprocessor called the 4004.
In 1974 the more powerful second
generation microprocessor (the 8008)
wasannounced fabricated as a single chip.

Why Important?
Embedded

Inside:

Automotive systems
Airplanes
Toys
Medical Devices
Furniture

Billions

of units

Available uC in
Bangladesh

AT89C51
AT89C52
AT89S51
AT89S52 ATMEGA-8
AT89C2051 AT89C4051 AT89C55WD
AT89S8252 AT90S2313 AT90S8515
AT90S8535 ATMEGA-16
ATMEGA-32 ATMEGA-64 ATMEGA-88
ATTINY-13
ATTINY-15 ATTINY-26 AT89C51ED2
AT89C51RD2 AT91RM9200
AT91SAM256 ATMEGA-128 ATMEGA162
W78E052B40DL

Available uC in
Bangladesh
PIC16F72
PIC16F74
PIC16F77
PIC10F200
PIC12F629
PIC16C54C
PIC16C65B
PIC16F676
PIC18F452
PIC16F628A
PIC16F873A
PIC16F877A
PIC18F4550

PIC16F73
PIC16F76
PIC16F88
PIC12F508
PIC12F675
PIC16C57C
PIC16C923
PIC16F84A
PIC16C622A
PIC16F648A
PIC16F876A
PIC18F2550

PIC16F690
20-Pin Flash-Based, 8-Bit
CMOS Microcontrollers with
nanoWatt Technology

Wide

operating voltage range (2.0V-5.5V)


17 I/O pins and 1 input only
Two 8-bit Timer, One 16-bit Timer
12-channel, 10-bit ADC
Programmable Serial USART
Internal Calibrated RC Oscillator
DC 20 MHz oscillator/clock input
Software selectable frequency range of 8
MHz to 32 kHz

Pin
Diagram

Pin Out And Data Sheet

What do I need to start?


To start C programming language on
Microchip PIC Microcontroller you need
these following tools:
Down

load the latest Microchip


MPLAB IDE
The MPLAB IDE is already come with
the HITECH PICC-Lite C compiler
The PIC16F690 datasheet
Microchip Programmer.
Trainer Board

Software Installation
1.
2.
3.

MPLab
Programmer
Software
Proteus

Install Proteus for


simulation

When Licence requires, Browse to


Many Crack> Sonsivri_LICENCE

Open it, select it and


install it

When installation is completed, run Crack


Proteus 7.2 SP6 PRO v1.1 and select the
folder where the proteus is installed.

Open MPLAB

Start -> All Programs ->


Microchip -> MPLAB IDE ver 8.60
-> MPLAB IDE

Create New Project

Project -> Project Wizard from MPLAB IDE and


this will launch the project wizard, just continue with
Next

Give Project Name

Creating the .C file

File -> New and save it as test.c

END OF CLASS -1

I/O Port
For

Port A, B, C there are control


bytes TRISx.
Setting a TRISx bit (= 1) will
make the corresponding PORTx
pin an input
Clearing a TRISx bit (= 0) will
make the corresponding PORTx
pin an output

TRISA and PORTA

TRISB and PORTB

TRISC and PORTC

Write the first code and


simulate
#include<pic.h>

#include<htc.h>
void delay(int x)
{
int y;
for(y=0;y<=6500;y++)
{
}
}
void main()
{
TRISC=0X00;
TRISA0=0;
TRISB=0X00;
while(1)
{
PORTC=1;
RA0=1;

PORTB=1;
delay(800);
PORTC=0;
RA0=0;
PORTB=255;
delay(800);
}
}

Open ISIS Professional from


Proteus Program

Draw the circuit

Specify HEX file to


Microcontroller

Simulate the Circuit with


Microcontroller Program

Output current caution


Ensure

that the output current <20mA

Maximum output current sunk by any I/O


pin .................................................................................................... 25
mA
Maximum output current sourced by any I/O
pin .............................................................................................. 25 mA
Maximum current sunk by PORTA, PORTB and PORTC
(combined) ............................................................ 200 mA
Maximum current sourced PORTA, PORTB and PORTC
(combined) ............................................................ 200 mA

Input

Input Arrangement
For

hardware side, use 1uf-10uf


Capacitor across the push button
if the input source is a push
button.
Solve the de-bouncing problem
by programming accordingly.

Class work
Write

a program that

takes input from PC1 and PA0


Output LED at PC2 is light while any
of the input is high
Output LED as PC3 is light while both
of the inputs are high
Simulate

it and run it to the


Microcontroller

Basics Of Display Driver


BCD

to 7-segment Decoder
LCD Driver
5X7 Dot Matrix Driver

Display Driver 7447


Display test and blank input
are active-low so they should be
high for normal operation.

If the blank input is low the


display will be blank when the
count input is zero (0000).

END OF CLASS 3

Analog to Digital
Converter
10-bit

Resolution
13 - 260 s Conversion Time
12 Multiplexed Single Ended
Input Channels
0 - VCC ADC Input Voltage
Range
Selectable 2.56V ADC
Reference Voltage

Block Diagram

Related Pins

Configuration Sequence
Enable

ADC
Analog input channel declaration
Channel Selection
Clock Selection
Start a conversion
Wait until the conversion finishes
When the conversion is done, read
the 8 bit LSB from ADRESL and 2 bit
MSB from ADRESH byte.
Concatenate them to get 10 bit
conversion

Configuration Bytes
There are four registers available
to control the
functionality of the A/D module:
1. ANSEL
2. ANSELH
3. ADCON0
4. ADCON1

Enabling ADC

Analog input channel


declaration
The

ANS<11:0> bits (ANSEL<7:0>


and ANSELH<3:0>) and the
TRISA<4,2:0>, TRISB<5:4> and
TRISC<7:6,3:0>> bits control the
operation of the A/D port pins.
Set the corresponding TRISx bits to
1 to set the pin output driver to its
high-impedance state.
Likewise, set the corresponding
ANSx bit to disable the digital input
buffer.

CHANNEL SELECTION
There

are fourteen analog


channels
The CHS<3:0> bits
(ADCON0<5:2>) control which
channel is connected to the
sample and hold circuit.

VOLTAGE REFERENCE
There

are two options for the voltage


reference to the A/D converter: either
VDD is used or an analog voltage
applied to VREF is used.
The VCFG bit (ADCON0<6>)controls
the voltage reference selection.
If VCFG is set, then the voltage on the
VREF pin is the reference;otherwise,
VDD is the reference.

Voltage Reference

CONVERSION CLOCK
The

source of the conversion


clock is software selectable via
the ADCS bits (ADCON1<6:4>).

STARTING A CONVERSION
The

A/D conversion is initiated by


setting the GO/DONE bit
(ADCON0<1>).
When the conversion is
complete, the A/D module:
Clears the GO/DONE bit

CONVERSION OUTPUT
The

A/D conversion can be


supplied in two formats: Left or
right justified.
The ADFM bit (ADCON0<7>)
controls the output format.

END OF CLASS 4

Interrupt
Basics

of Interrupt

Interrupted operation while normal


operation is going on
Very good option for taking input

Interrupt Basic
So far functions were called from the main
program or other functions. The interrupt
functions is called by the hardware itself and
operates by following steps:
After the interrupt function is loaded into the
memory, the function waits for a moment for
the interrupt to occur;
When the interrupt has occurred, the
operating system stops the execution of the
main function and free itself to perform the
interrupt function;
After the execution of the interrupt function,
the operating system continues to run the
main function from the place it stopped
before the interrupt has occurred.

Interrupt Pins

Interrupts

Interrupt Configuration
RA2/INT

Must be configured as Digital I/O


Interrupt Mode selection
Initializing the flag bit
Enable External Interrupt
Enable Global interrupt
In

the Interrupt service routine, clear flag bit


every time.

RA2/INT pin as digital IO


Configure

TRISA2=1
ANS2=0 for digital input

Mode Selection
INTERRUPT

External interrupt on
RA2/INT pin is edge-triggered;
either rising if the INTEDG bit
(OPTION_REG<6>) is set,
or falling, if the INTEDG bit is clear.

Initializing Flag bit


When

a valid edge appears on the


RA2/INT pin, the INTF bit
(INTCON<1>) is set.

So

initialize the INTF=0

Enable External Interrupt

Enable Global Interrupt

Software handling of INTF


The

INTF bit must be cleared in software in


the Interrupt Service Routine before reenabling this interrupt.

void interrupt test(void) // Interrupt function definition


{
if (INTF==1)
{
INTF=0;
..
}
}

END OF CLASS 5

Timer/ Counter
3

timers/Counters
Two 8-bit timers and One 16-bit
timer
Easily selectable clock source

How timer works?

Its

a 8 or 16 bit register. Its value


increases with each clock cycle. So,
bigger the no of bits, the bigger the
range of counting.
By setting modes, we can calculate
intervals or generate events at
regular interval.

Timer0 Diagram

Timer Configuration
Clock

selection
Mode Selection
Interrupt configuration

Clock Input Pins


The

external
clock source
must be at least
2.5 less than the
C clock source.

Clock Source
The

internal clock can be used as


the clock source. For timers, the
internal clock can be pre-scaled.
There is option for external clock
source also.

Clock Selection and


Configuration

Clock Pre-scalar Selection

Timer Interrupt
Configuration

Timer1

END OF CLASS 6

USART

Universal Synchronous and Asynchronous serial


Receiver and Transmitter

Full Duplex Operation


(Independent Serial Receive
and Transmit Registers)
Asynchronous or
Synchronous Operation
Master or Slave Clocked
Synchronous Operation

Parallel and Serial


Communication

Frame Format of Serial


Communication

Two Types of serial


communication
Synchronous

Clock source is synchronized


Data is synchronized with clock
i.e. a common clock source for the
communicating devices is necessary
Asynchronous

No common clock source is required


Clock is recognized by the receiver
automatically

Serial Mode Selection

Clock Generation

Example of calculating the


Baud Rate

PORT Configuration
In

order to configure pins


RB6/SCK/SCL and RB7/TX/CK as the
Universal Synchronous
Asynchronous Receiver Transmitter:
SPEN (RCSTA<7>) bit must be set
(= 1),
TRISB<6> bit must be set (= 1),
and
TRISB<7> bit must be set (= 1).

Transmitter Block Diagram

The Operational Basics


The

heart of the transmitter is the


Transmit (serial) Shift Register (TSR).
The shift register obtains its data from
the read/write transmit buffer, TXREG.
TheTXREG register is loaded with data
in software.
The TSR register is not loaded until the
Stop bit has been transmitted from the
previous load.
As soon as the Stop bit is transmitted,
the TSR is loaded with new data from
the TXREG register (if available).

To set up an Asynchronous
Transmission:
1.

Initialize the SPBRGH:SPBRG


registers for the appropriate baud
rate.
2. Enable the asynchronous serial
port by clearing bit SYNC and
setting bit SPEN.
3. Enable Transmitter by Setting
the TXEN

Transmission

Stating The Transmission


Writing

TXREG starts the


transmission if the shift register is
empty

USART Receiver Block


Diagram

Receiver Enabling

Checking the receiver


status

Copy the data to


microcontroller
Copy

RCIF.

the REGREG and clear the

END OF CLASS 7
6 February 2010

CLASS 8

Analog Comparator

Comparator Enabling

Negative Channel
Selection

Positive Channel Selection

Comparator Output

The

C1OUT is the output register


of Analog Comparator.

Output Pin Configuration

END OF CLASS 8

You might also like