Sunteți pe pagina 1din 26

EN2852 Applied Electronics

4. Microcontrollers

By: Dr. S. Thayaparan


Slide contents are extracted from the following source of origin :
1.
2.
3.
4.
5.
6.
7.

http://en.wikipedia.org/wiki/Microcontroller
http://www.engr.sjsu.edu/bjfurman/courses/ME106/ME106pdf/MicroC_overview.pdf
http://www.mikroe.com/eng/chapters/view/64/chapter-1-introduction-to-microcontrollers/
http://www.mstracey.btinternet.co.uk/pictutorial/picmain.htm
http://www.codeproject.com/Articles/29528/Programming-the-PIC-Microcontroller-in-C-using-PIC
http://www.mikroe.com/eng/chapters/view/4/chapter-3-i-o-ports/
http://www.mikroe.com/eng/chapters/view/2/chapter-1-pic16f887-microcontroller-device-overview/

Applied Electronics Course Content


1.
2.
3.
4.

Op Amps
Sensors & Transducers
Electronic Instrumentation Systems
Microcontrollers

 Lectures are: 1.5 Hrs/Week


 Evaluation is : 60% Exam
40% CA
2

4.0 Microcontrollers
1.
2.
3.
4.
5.
6.

Introduction to Microcontrollers
Components of A Microcontroller
Microcomputer Architecture
Binary and Hexadecimal Numbers
Basic DSP Operations
Programming Microcontrollers

A PIC 18F8720 microcontroller


in an 80-pin TQFP package

Intel P8051 microcontroller

4.1 Introduction to Microcontrollers


 A microcontroller (sometimes abbreviated C, uC or MCU) is a small
computer on a single integrated circuit (IC) containing a processor core,
memory, and programmable input/output peripherals
 Microcontrollers are used in automatically controlled products and
devices, such as automobile engine control systems, implantable medical
devices, remote controls, office machines, appliances, power tools, toys
and other embedded systems.
 The first single-chip microprocessor was the 4-bit Intel 4004 released in
1971
 Even though there is a large number of different types of microcontrollers
and even more programs created for their use only, all of them have many
things in common.
 If you learn to handle one of them you will be able to handle them all.

4.1 Introduction to Microcontrollers


 A typical scenario on the basis of which it all functions is as follows:
1.
2.

3.

4.

5.

Power supply is turned off and everything is stillthe program is loaded


into the microcontroller, nothing indicates what is about to come
Power supply is turned on and everything starts to happen at high speed.
The control logic unit keeps everything under control. It disables all
other circuits except quartz crystal to operate. While the preparations
are in progress, the first milliseconds go by.
Power supply voltage reaches its maximum and oscillator frequency
becomes stable. Special Function Registers (SFR) are being filled with bits
reflecting the state of all circuits within the microcontroller. All pins are
configured as inputs. The overall electronics starts operation in rhythm
with pulse sequence. From now on the time is measured in S or nS.
Program Counter is set to zero. Instruction from that address is sent to
instruction decoder which recognizes it, after which it is executed with
immediate effect.
The value of the Program Counter is incremented by 1 and the whole
process is repeated...several million times per second.
5

4.1 Introduction to Microcontrollers

4.1 Introduction to Microcontrollers

4.1 Introduction to Microcontrollers

PIC16F887 Block Diagram

4.1 Introduction to Microcontrollers


Read Only Memory (ROM)
ROM is a type of memory used to permanently save the program.
ROM size depends on the size of the program.
ROM can be built in the microcontroller or added as an external chip.
If ROM is external, the microcontroller is cheaper and the program can be
considerably longer.
The internal ROM is usually smaller and more expensive, but leaves more
pins available for connecting to peripheral environment.
The typical size of ROM ranges from 512B to 64KB.
Random Access Memory (RAM)
RAM is used for temporary storing data and intermediate results created
and used during the operation of the microcontrollers.
The content of this memory is cleared once the power supply is off.
For example, if the program performs an addition, it is necessary to have a
register standing for the sum.
The typical size of RAM goes up to a few KBs.
9

4.1 Introduction to Microcontrollers


Electrically Erasable Programmable ROM (EEPROM)
The EEPROM is a special type of memory not contained in all
microcontrollers.
Its contents may be changed during program execution (similar to RAM ),
but remains permanently saved even after the loss of power (similar to
ROM).
It is often used to store values, created and used during operation (such as
calibration values, codes, values to count up to etc.), which must be saved
after turning the power supply off.
A disadvantage of this memory is that the process of programming is
relatively slow and it is measured in miliseconds.

10

4.2 Components of A Microcontroller

11

4.3 Microcomputer Architecture

12

4.3 Microcomputer Architecture

i8051 microarchitecture

13

4.4 Binary and Hexadecimal Numbers


 Microcontrollers are fundamentally digital and use binarylogic
 Digital : Two states: high and low, 1 or 0, on or off
Often 5V or 0V
 One binary digitis called a bit
 It can take on two possible states: 1or 0
 Four binary digits is called a nibble
 Eight binary digits is called a byte

14

4.4 Binary and Hexadecimal Numbers

15

4.4 Binary and Hexadecimal Numbers

16

4.4 Binary and Hexadecimal Numbers

17

4.5 Basic DSP Operations


DSP : Digital Signal Processing
Everything that we do in analog domain can be done in digital domain
Example 1: Signal Amplification
Example 2: Filtering (Low pass, band pass, high pass)
Some things very difficult to do in analog domain can be easily done in digital
domain
Example 1: Finding the average value
Example 2: Finding the maximum value
DSP has a very wide scope. We will only have a very brief look
In digital domain, we work with fixed point numbers and variables
Each variable/value is represented by a fixed number of bits
Example 1: Interger 8 bits (signed & unsigned)
Example 2: Long Integer 16 bits (signed & unsigned)
Example 3: Float 32 or 64 bits (signed)
In micro controllers, floating point variables are not common. We usually
have to use integers
18

4.5 Basic DSP Operations


In simple micro-controllers, numbers are 8 bit values.
How we interpret the numbers is up to us.
Example 1: 1111 1111 = 255 (unsigned interpretation)
Example 2: 1111 1111 = -128 (signed interpretation)
Example 3: 1000 0000 = 128
Example 4: 1000 0000 = 0.5
We can also use two 8-bit variables to create 16-bit variable in our program
It is important to keep the same interpretation throughout our algorithm
Addition, substraction and multiplication
Example 1: after each adc operation do {
new_sig = sig_adc1 + sig_adc2;
}
Example 2: after each adc operation do {
new_sig = sig_adc1 - sig_adc2;
}
Example 3: after each adc operation do {
new_sig = sig_adc1 * 2;
}

19

4.6 Programming Microcontrollers


Example: 1 (For a pic micro-controller)
Operation : Smoothing (Low Pass Filtering)
function (adc_complete) {
val0 = 0;
val1 = 0;
sum = 0;
sum = sum + val0 + val1;
new_val = sum/3;
val0 = val1;
val1 = new_adc;
}
Example: 2 (For a pic micro-controller)
Operation : High Pass Filtering
- Substract the original with the LPF component.
20

4.6 Programming Microcontrollers


Example: 2
int ledPin = 13;

// LED connected to digital pin 13

void setup()
{
pinMode(ledPin, OUTPUT);
}

// sets the digital pin as output

void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000);
// waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000);
// waits for a second
}
21

4.6 Programming Microcontrollers

Pin-outs of the PIC 16F84


MCLR - Used to erase the memory locations
inside the PIC (i.e. When re-program it). In
normal use it is connected to VDD.
INT - an input pin can be monitored. If it goes
high, we can do restart, stop or any other
function.

RA0-4 & RB0-7 : RA & RB are


bidirectional ports. Can be
configured as an input or an
output.
The number following RA is the
bit number (0 to 4). Each bit can
be configured as Input or Output.
VDD & VSS - Power supply pins
VDD (+5V), VSS (0V)
OSC1/CLKIN & OSC2/CLKOUT
The microcontroller has some
kind of timing.
T0CK1: This is another clock
input, which operates an internal
timer. It operates in isolation to
the main clock.
22

4.6 Programming Microcontrollers


#include <htc.h>
#include <pic.h>
#include <delay.c>
main()
{
TRISB=0;
for(;;)
{
RB0=1;
DelayMs(250);
RB0=0;
Delayms(250);
}
Note:
}

TRISB = %00000000
TRISB = %11111111
PORTB = %11111111
PORTB.0[i] = 1

->
->
->
->

set all PORTB pins as output


set all PORTB pins as input
make all PORTB pins high
make pin number i high

// declare portb as output


// do forever
// LED ON
// delay for 0.25 seconds
// LED OFF

A.
B.
C.
D.

PORTB : I/O port name


TRISA, TRISB: data-direction register name
These names are common to most PIC micros.
When power up, all the I/O pins are configured as inputs.
23

4.6 Programming Microcontrollers

24

4.6 Programming Microcontrollers

Port A and TRISA Register

ANSEL register :
determine whether the pins will act as analog inputs or digital inputs/outputs.
RA0 = AN0 (determined by bit ANS0 of the ANSEL register);
RA1 = AN1 (determined by bit ANS1 of the ANSEL register);
RA2 = AN2 (determined by bit ANS2 of the ANSEL register);
RA3 = AN3 (determined by bit ANS3 of the ANSEL register);
25

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.

#include <htc.h>
#include <pic.h>
#include <delay.c>
int j;
// general global variables
void Wait_These_Seconds(float s) {
// only to 3 decimals
int milli_Equiv;
// milli second equivalent integer
milli_Equiv=1000*s;
for(j=1; j<=milli_Equiv; j++) { DelayMs(1); }
}
main() {
TRISB=0xF0;
// RB4:RB7 are input ; 11110000 in binary
for(;;) {
if(RB7==1) {
RB0=1;
// LED ON
Wait_These_Seconds(1.8);
// delay for 1.8 seconds
RB0=0;
// LED OFF
Wait_These_Seconds(0.9);
}
if(RB7==0) {
// interrupt is connecting RB7 to ground
RB0=1;
// LED ON
Wait_These_Seconds(0.7);
// delay for 0.7 seconds
RB0=0;
// LED OFF
Wait_These_Seconds(0.1);
}
}
}

4.6 Programming Microcontrollers

26

S-ar putea să vă placă și