MICROCONTROLLER
MICROCONTROLLER
MICROCONTROLLER
Introduction
EMT355/3 Microcontroller
Lecturers
Computers are in
here...
Single-functioned
Executes a single program, repeatedly.
Tightly-constrained
Low cost, low power, small, fast, etc.
Reactive and real-time
Continually reacts to changes in the system’s
environment.
Must compute certain results in real-time without
delay.
Design Constraints
Limited Power
Most embedded systems operate within battery
operated equipment.
Limited Memory
An embedded system is constrained to a finite and
small amount of memory.
Real-Time
Most embedded systems are used in a control function.
They are expected to sense the real-time status of the
system in which they are housed and produce real-time
responses.
Definition of Embedded
System
Processor
Memory
Provide storage for the software and data.
Peripherals
Software
Initialization and Configuration.
Operating System.
The application software.
Error handling.
Debug and maintenance support.
Embedded System
Microcontroller
General-purpose
microprocessors Microcontroller
Must add RAM, ROM, I/O The fixed amount of on-chip
ports, and timers externally to ROM, RAM, and number of I/O
make them functional
ports makes them ideal for many
Make the system bulkier and applications in which cost and
much more expensive space are critical
Have the advantage of
versatility on the amount of In many applications, the space
RAM, ROM, and I/O ports it takes, the power it consumes,
and the price per unit are much
more critical considerations than
the computing power
Microcontrollers for
Embedded Systems
Home
•Appliances, intercom, telephones, security systems, garage door openers, answering
machines, fax machines, home computers, TVs, cable TV tuner, VCR, camcorder,
remote controls, video games, cellular phones, musical instruments, sewing machines,
lighting control, paging, camera, pinball machines, toys, exercise equipment
Office
•Telephones, computers, security systems, fax machines, microwave, copier, laser printer,
color printer, paging
Automotive
•Trip computer, engine control, air bag, ABS, instrumentation, security system, transmission
control, entertainment, climate control, cellular phone, keyless entry
x86 PC Embedded
Applications
8-bit microcontrollers
Motorola’s 6811
Intel’s 8051
Zilog’s Z8
Microchip’s PIC
There are also 16-bit and 32-bit
microcontrollers made by various chip
makers
Criteria for Choosing a
Microcontroller
Other
interrupts
8051 CPU
From Crystal
Oscillator or RC ALE /PSEN P3 P2 P1 P0 TxD RxD
network (Address/data)
8051 Family
“n-bit” – the “n” refers to the data bus width of the CPU, and is
the maximum width of data it can handle at a time
PCs with 64-bit microprocessors are becoming common
Over 55% of all processors sold per year are 8-bit processors,
which comes to over 3 billion of them per year!*
8-bit microcontrollers are sufficient and cost-effective for many
embedded applications
More and more advanced features and peripherals are
added to 8-bit processors by various vendors
8-bit MCUs are well-suited for low-power applications that use
batteries
*Note: Statistics from Embedded.com Article ID# 9900861, Dec 2002
Eg: Remote
Control Car
Antenna Antenna
Front Electric
Forward RF RF Receiver Microcontroller Motor (Left/Right)
Microcontroller
Transmitter
Reverse
To other
peripherals
8051 -
architecture
Inside the 8051
Registers
Program Counter
Flag Bits / PSW (program status word)
Register
Register Banks and Stack
Registers
The Stack Pointer, like all registers except DPTR and PC,
may hold an 8-bit (1-byte) value.
The Stack Pointer is used to indicate where the next
value to be removed from the stack should be taken
from.
When you push a value onto the stack, the 8051 first
increments the value of SP and then stores the value at
the resulting memory location.
When you pop a value off the stack, the 8051 returns the
value from the memory location indicated by SP, and
then decrements the value of SP.
…
C – Loop Control
C – Basic Program Statements
Link Section
Definition Section
Main function
OUTPUT:
This is a C basic program
Sum of two numbers : 2
C – Data Types
(or)
const data_type *variable_name;
C – Variable
Arithmetic operators
These are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus
Assignment operators
These are used to assign the values for the variables in C programs.
Relational operators
These operators are used to compare the value of two variables.
Logical operators
These operators are used to perform logical operations on the
given two variables.
…
Eg:
int a[10]; // integer array
char b[10]; // character array i.e. string
int array[2][2] = {10,20,30,40}; //2D array
C – String
char string[20] = { ‘f’ , ’r’ , ‘e’ , ‘s’ , ‘h’ , ‘2’ , ‘r’ , ‘e’ , ‘f’ , ’r’ , ‘e’ , ‘s’
, ‘h’ , ‘\0’};
(or)
char string[20] = “fresh2refresh”;
(or)
char string [] = “fresh2refresh”;
”
THE END
Important to know: C – Syntax - Create function - Compile -
Execute
C Programming in
8051
Lecture: 3 hours
Assembly Code C Code
Code
.asm .c
Generation
Flow Assembler Compiler
Machine Code
.hex
Why Program 8051 in C?
C programming is less time consuming, but has larger hex file size
The reasons for writing programs in C
It is easier and less time consuming to write in C than Assembly
C is easier to modify and update
You can use code available in function libraries
C code is portable to other microcontroller with little of no
modification
Data Type
#include <8051.h>
void main(void){
unsigned char mynum[]="012345ABCD";
unsigned char z;
for (z=0;z<=10;z++)
P1 = mynum[z];
}
Eg:
//Toggle P1 forever
#include <8051.h>
void main(void){
for (;;){
P1 = 0x55;
P1 = 0xAA;
}
}
Signed char
//Singed numbers
#include <8051.h>
void main(void){
char mynum[] = {+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1 = mynum[z];
}
Unsigned and Signed int
void main(void){
unsigned int z;
for (z=0;z<=50000;z++){
MYBIT = 0;
MYBIT = 1;
//or /* BIT Register- P1 */
//P1_0 = 0; //__sbit __at (0x90) P1_0 ;
//P1_0 = 1; //__sbit __at (0x91) P1_1 ;
} //__sbit __at (0x92) P1_2 ;
} //__sbit __at (0x93) P1_3 ;
…
Bit and sfr
Logical operators
AND (&&), OR (||), and NOT (!)
Bit-wise operators
AND (&), OR (|), EX-OR (^), Inverter (~), Shift
Right (>>), and Shift Left (<<)
These operators are widely used in software
engineering for embedded systems and
control
…
Eg:
#include <8051.h>
void main(void){
P0 = 0x35 & 0x0F; //ANDing
P1 = 0x04 | 0x68; //ORing
P2 = 0x54 ^ 0x78; //XORing
P0 = ~0x55; //inversing
P1 = 0x9A >> 3; //shifting right 3
P2 = 0x77 >> 4; //shifting right 4
P0 = 0x6 << 4; //shifting left 4
}
Eg:
#include <8051.h>
void MSDelay(unsigned int); …
void main(void){ void MSDelay(unsigned int
P0 = 0x55; itime){
P2 = 0x55; unsigned int i,j;
while (1){ for (i=0;i<itime;i++)
P0 = ~P0; for (j=0;j<1275;j++);
P2 = P2^0xFF; }
MSDelay(250);
}
}
Eg:
#include <8051.h>
__bit membit;
void main(void){
while (1){
membit = P1_0; //get a bit from P1.0
P2_7 = ~membit; //invert it and send
//it to P2.7
}
}
Eg:
9. Write an 8051 C program to read the P1.0 and P1.1 bits and
issue an ASCII character to P0 according to the following
table.
P1.1 P1.0
0 0 send ‘0’ to P0
0 1 send ‘1’ to P0
1 0 send ‘2’ to P0
1 1 send ‘3’ to P0
Solution:
#include <8051.h>
void main(void){
unsigned char z;
z = P1;
z = z&0x3;
…
…
…
switch (z) {
case(0): {
P0=‘0’;
break;
}
case(1): {
P0=‘1’;
break;
}
case(2): {
P0=‘2’;
break;
}
case(3): {
P0=‘3’;
break;
}
}
}
Packed BCD to ASCII
Conversion
Packed BCD
In packed BCD, a single byte has two BCD
number in it, one in the lower 4 bits, and one in
the upper 4 bits
Eg: 0101 1001 is packed BCD for 59H
#include <8051.h>
void main(void){
unsigned char x,y;
unsigned char mybyte = 0x29;
#include <8051.h>
void main(void){
unsigned char bcdbyte;
unsigned char w =‘4’;
unsigned char z =‘7’;
w = w & 0x0F;
w = w << 4;
z = z & 0x0F;
bcdbyte = w | z;
P1= bcdbyte;
}
Checksum Byte in ROM
(c) If the second byte 62H has been changed to 22H, show
how checksum detects the error.
25H
+ 22H Adding the series of bytes including the
+ 3FH checksum byte shows that the result is not
+ 52H zero, which indicates that one or more bytes
+ E8H have been corrupted.
1C0H (dropping the carry, we get C0H)
Eg:
#include <8051.h>
void main(void){
unsigned char mydata[]= {0x25,0x62,0x3F,0x52};
unsigned char sum = 0;
unsigned char x;
unsigned char chksumbyte;
for (x=0;x<4;x++){
P2 = mydata[x];
sum= sum + mydata[x];
P1 = sum;
}
chksumbyte = ~sum + 1;
P1 = chksumbyte;
}
Eg:
#include <8051.h>
void main(void){
unsigned char mydata[]={0x25,0x62,0x3F,0x52,0xE8};
unsigned char chksum = 0;
unsigned char x;
For (x=0;x<5;x++)
chksum = chksum + mydata[x];
if (chksum == 0)
P0='G';
else
P0='B';
}
Binary (hex) to Decimal
and ASCII Conversion
#include <8051.h>
void main(void){
unsigned char x,binbyte,d1,d2,d3;
binbyte = 0xFD;
x = binbyte / 10;
d1 = binbyte % 10;
d2 = x % 10;
d3 = x / 10;
P0 = d1;
P1 = d2;
P2 = d3;
}
RAM Data Space Usage
by 8051 C Compiler
#include <8051.h>
void main(void){
unsigned char mynum[] = "ABCDEF"; //RAM space
unsigned char z;
for (z=0;z<=6;z++)
P1 = mynum[z];
}
RAM Space
#include <8051.h>
void main(void){
unsigned char mydata[100]; //RAM space
unsigned char x,z=0;
for (x=0;x<100;x++){
z--;
mydata[x] = z;
P1 = z;
}
}
Eg:
#include <8051.h>
void main(void){
static __code unsigned char mynum[]="ABCDEF";
(a)
#include <8051.h>
void main(void){
P1 =‘H’;
P1 =‘E’;
Short and simple, but the
P1 =‘L’;
individual characters are
P1 =‘L’;
embedded into the program
P1 =‘O’;
and it mixes the code and data
}
together
… Use the RAM data space to store
array elements, therefore the
(b) size of the array is limited
#include <8051.h>
void main(void){
unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0;z<=5;z++)
P1 = mydata[z];
} Use a separate area of the code
space for data. This allows the size of
(c) the array to be as long as you want if
#include <8051.h> you have the on-chip ROM.
void main(void){
static __code unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0;z<=5;z++)
P1 = mydata[z]; However, the more code space
} you use for data, the less space
is left for your program code.
ANY Questions
THE END
Important to know: Code 8051 using C - Data type - Logical operators -
BCD to ASCII conversion