Professional Documents
Culture Documents
Microprocessor Vs Microcontroller
2/18/2012
y Microprocessor, by itself is useless. y It does not have Memory & I/O Ports on the chip
itself.
y Must have the External Peripherals to interact with
2/18/2012
fixed amount of RAM, ROM, I/O Ports, Timers, & Serial Communication channel on the chip itself.
8051 Architecture
2/18/2012
Features of 8051
y y y y y y y y
8051 is an 40 pin IC. 8051 is an 8-bit Microcontroller. 128 Bytes of RAM. 4KBytes of Internal ROM. 4 I/O Ports (P0, P1, P2 & P3). Two 16-bit Timers (T0 & T1). Supports 5+1 Interrupts. Supports One Serial Communication Channel.
2/18/2012
Cont
y It has 4 Register Banks (Bank0 y 16-bit Program Counter. y 8-bit Stack Pointer. y 16-bit DPTR (Data Pointer). y External Code and Data Memory up to 64KB. y 8-bit PSW (Program Status Word).
Bank 3)
2/18/2012
2/18/2012
2/18/2012
Pin Description
RESET
which is used reset the microcontroller s internal registers and ports upon starting up. y A high on this pin for two machine cycles while the oscillator is running resets the device.
2/18/2012 MTS Technologies Ltd
XTAL1: Input to the inverting oscillator amplifier and input to the internal clock operating circuit. XTAL2: Output from the inverting oscillator amplifier.
oscillator amplifier hence we need to only connect a crystal at these pins to provide clock pulses to the circuit.
2/18/2012 MTS Technologies Ltd
Port 0
y PORT P0 can be used as a general purpose 8 bit
port when no external memory is present, but if external memory access is required then PORT P0 acts as a multiplexed address and data bus that can be used to access external memory in conjunction with PORT P2. y P0 doesn t have internal Pull-up resistors, because to access external memory devices. y If we want to use Port0 as I/O Port, we need to connect external pull-up resisters to P0.
2/18/2012
Port 1
y Port 1 is an 8-bit bidirectional I/O Port with
internal pull-ups.
Port 2
y Port 2 is an 8-bit bi-directional I/O port with
internal pull-ups. y Port 2 also used as the high-order address byte during accessing external memory that use 16-bit addresses.
2/18/2012
Port 3
y Port 3 is an 8-bit bi-directional I/O port with
EA (External Access) Pin: This is active Low signal. When we are accessing any external memory this pin should be low. y EA should be connected to VCC for internal program executions. PSEN Pin: (Program Store Enable) This is an active low pin. To Store our program into the Chip, we need to make this pin Low.
2/18/2012
differentiate between that we will use this ALE Pin. y If ALE pin is high means we are sending the value from Port0 is for address.
2/18/2012
Machine cycle
y A machine cycle consists of a sequence of 6
states, numbered S1 through S6. y Each state is divided into a Phase1 half and a Phase2 half. y Each state time lasts for two oscillator periods. y Thus a machine cycle takes 12 oscillator periods or 1s if the oscillator frequency is 12MHz.
2/18/2012
y In the case of Crystal Frequency = 12 MHz, the machine cycle frequency is 1MHz (12MHz / 12). y Machine cycle Time Period is 1 S (1 / 1MHz). y In case of Crystal Frequency = 11.0592MHz, the machine cycle frequency is 921.6KHz (11.0592MHz / 12). y Machine Cycle Time Period is 1.085 S (1 / 921.6 KHz).
2/18/2012
RAM, although only the first 128 bytes are available for general use by the programmer. y The first 128 bytes of RAM (from 00h to 7Fh) are called the Direct Memory, and can be used to store data.
2/18/2012 MTS Technologies Ltd
upper area of addressable memory, from address 80h to FFh. y The 8051 is a flexible microcontroller with a relatively large number of modes of operations. y Your program may inspect and/or change the operating mode of the 8051 by manipulating the values of the 8051's Special Function Registers (SFRs).
2/18/2012
SFR Table
YELLOW Background are I/O Ports SFR PINK Background are Control SFR RED Background are Other SFR
2/18/2012 MTS Technologies Ltd
bit registers, R0 to R7. y This means that there are essentially 32 available general purpose registers, although only 8 (one bank) can be directly accessed at a time. y To access the other banks, we need to change the current bank number in the flag status register.
2/18/2012
2/18/2012
Immediate Addressing Register Addressing Direct Addressing Indirect Addressing External Direct Code Indirect
MOV A,#20H MOV A,R0 MOV A,30H MOV A,@R0 MOVX A,@A+DPTR MOVC A,@A+DPTR
value to be stored in memory immediately follows the operation code in memory. y The instruction itself dictates what value will be stored in memory. MOV A,#20h y This instruction uses Immediate Addressing because the Accumulator will be loaded with the value that immediately follows.
2/18/2012
the data to be manipulated. MOV A,R0 MOV R2,A y The source &destination registers must match in size. y If the source is an 8-bit register, the destination should be 8-bit register. MOV DPTR,#2456H MOV A,DPL
2/18/2012 MTS Technologies Ltd
value to be stored in memory is obtained by directly retrieving it from another memory location. MOV A,30h y This instruction will read the data out of Internal RAM address 30 (hexadecimal) and store it in the Accumulator.
2/18/2012
addressing is a very powerful addressing mode which in many cases provides an exceptional level of flexibility. MOV A,@R0 y This instruction causes the 8051 to analyze the value of the R0 register. y The 8051 will then load the accumulator with the value from Internal RAM which is found at the address indicated by R0.
2/18/2012
External Direct
y External Memory is accessed using a suite of instructions which use "External Direct" addressing. y Because it appears to be direct addressing, but it is used to access external memory rather than internal memory. y There are only two commands that use External Direct addressing mode:
MOVX A,@DPTR MOVX @DPTR,A
y Both commands utilize DPTR. y In these instructions, DPTR must first be loaded with the address of external memory that you wish to read or write.
2/18/2012 MTS Technologies Ltd
Code Indirect
This addressing mode is used to access the data from the code (Program) memory. MOVC A,@A+DPTR If we want to use this addressing mode make sure that the accumulator should be clear before calling this function.
2/18/2012
2/18/2012
ACALL ADD/ADDC ANL CJNE CLR CPL DA DEC DIV DJNZ INC JB JNB
y y y y y y y y y y y y y
JC JNC JZ JNZ LCALL LJMP MOV MOVC MOVX MUL NOP ORL POP
PUSH RET RETI RL RLC RR RRC SETB SJMP SUBB SWAP XCH XRL
2/18/2012
2/18/2012
the bits of that port to 1. y To make any port pin as input port we need to make that pin as 1. mov p1,#0ffh setb p2.5 To make port1 as i/p. To make p2.5 as i/p.
2/18/2012
2/18/2012
Summary of Topics
2/18/2012
2/18/2012
Example:
Void main (void) { while (1) //This is Super Loop. { P1=0xAA; delay (20); P1=0x55; delay(20); } } //This Program continuously toggles the Port1.
2/18/2012 MTS Technologies Ltd
2/18/2012
2/18/2012
Address of Ports:
y P0 is at address 0x80 y P1 is at address 0x90 y P2 is at address 0xA0 y P3 is at address 0xB0
2/18/2012
SFR16
y Some 8051 derivatives use two SFRs with
consecutive addresses to specify 16-bit values. y For example, the 8052 uses addresses 0xCC and 0xCD for the low and high bytes of timer/counter 2. y The Cx51 compiler provides the sfr16 data type to access 2 SFRs as a 16-bit SFR.
2/18/2012
Memory Models
Different types are: TINY SMALL MEDIUM COMPACT LARGE HUGE
2/18/2012
2/18/2012
segments will be placed in the 8051 s internal memory. y Total RAM 128 bytes (8051/31) y COMPACT- variables are stored in paged memory addressed by ports 0 and 2. Indirect addressing opcodes are used. On-chip registers are still used for localsand parameters. y Total RAM 256 bytes off-chip, 128 or 256 bytes on-chip.
2/18/2012
memory addressed by @DPTR. y On-chip registers are still used for locals and parameters. y Total RAM up to 64KB, 128 or 256 bytes on-chip.
2/18/2012
2/18/2012
SBIT
y To write a single pin, we can make use of an sbit
variable in keil(c51). y Syntax: sbit asd = P1^0; y Here, P1^0 is a single bit assigned to asd.
2/18/2012
segment) and ends at 07fH (127 decimal). y This RAM can be used for program variables. y It is directly addressable, so that instructions like MOV A, x are usable.
2/18/2012
Memory Configurations....
y A second memory area exists between 80H and 0FFH
which is only indirectly addressable and is prefixed by I: and known as IDATA. y It is only accessible via indirect addressing (MOV A, @Ri) y It effectively overlays the directly addressable sfr area.
2/18/2012
Memory Configurations....
y A third memory space, the CODE segment, also
starts at zero, but this is reserved for the program. y It typically runs from C:0000 to C:0FFFFH (65536 bytes) but as it is held within an external EPROM, it can be any size up to 64KB (65536 bytes). y The CODE segment is accessed via the program counter (PC) for opcode fetches and by DPTR for data.
2/18/2012
Memory Configurations....
y A fourth memory area is also off-chip, starting at X:0000. y This exists in an external RAM device and, like the C:0000 segment, can extend up to X:0FFFFH (65536 bytes). y The X: prefix implies the external XDATA segment. y The 8051 s only 16 bit register, the DPTR (data pointer) is used to access the XDATA. y Finally, 256 bytes of XDATA can also be addressed in a paged mode. y Here an 8-bit register (R0) is used to access this area, termed PDATA.
2/18/2012
C51 Variables
y bit =1 bit 0-1 y char =8 bits 0 - +/- 127 y unsigned char =8 bits 0 - 255 y int =16 bits 0 - +/-32768 y unsigned int =16 bits 0 - 65535 y long =32 bits 0 - +/- 2.147483648x109 y unsigned long =32 bits 0 - 4.29496795x109 y float =32 bits +/-1.176E-38 to +/-3.4E+38 y pointer =24/16/8 bits Variable address
2/18/2012 MTS Technologies Ltd
modules, each with a unique function, so a software system is built from a number of discrete tasks. y In the electronic case, each module is designed and perfected individually and then finally assembled into a complete working machine.
2/18/2012
2/18/2012
Example:
#include<reg51.h> void main (void) { P0 = 0x12 & 0x34; P1 = 0x23 | 0x56; P2 = 0x45 ^ 0x 41; P3 = ~P2; P1 = 0x62 >> 5; P3 = 0x45 << 6; }
2/18/2012 MTS Technologies Ltd
types. y 8051 does not support signed arithmetic. y Extra code is required to manipulate signed data. y This reduces your program speed and increases the program size.
2/18/2012
The typedef keyword allows us to provide aliases for data types. We can use these aliases in place of the original types.
Example: typedef unsigned int tint; y If you want to use unsigned int, we can directly use tint.
2/18/2012
Syntax: XBYTE [loc]. y loc is an address in the range 0x0000- 0xffff y XBYTE is part of the absacc.h header file.
2/18/2012
Example:
#include <reg51.h> #include <absacc.h> void main() { unsigned char i; XBYTE[0]='A'; XBYTE[1]='B'; XBYTE[2]='C'; XBYTE[3]='D'; XBYTE[4]='E'; XBYTE[5]='F'; for (i=0;i<5;i++) { P1 = XBYTE [i]; } }
2/18/2012 MTS Technologies Ltd
Explanation
y In the above example, I have stored six alphabets
2/18/2012
2/18/2012
0 1 2 3 4
2/18/2012
Example:
#include<reg51.h> sbit switch1 = P1^0; sbit switch2 = P1^2; sbit led = P1^5; void timer0(void) interrupt 1 { switch2=~switch2; } void main() { switch1 = 1; TMOD = 0x02; TH0 = 0x43; IE = 0x82; while(1) { TR0 = 1; led = switch1; } } MTS Technologies Ltd
2/18/2012
Explanation:
y In the above example, the value corresponding switch1 is
represented by LED.
y Timer0 is enabled by using IE register. y When ever timer0 interrupt is generated then only the
switch2 is toggled.
2/18/2012
_____ _______
2/18/2012
Timer/Counter 0 y Timer/Counter 1
y
y Timer is used to generate some delay. y Counter is used to count external events occurred
2/18/2012
y These are 16-bit Timers/ Counters. y These are accessed as Low byte (TLx) and High
byte (THx).
y For Timer0, TH0 and TL0. y For Timer1, TH1 and TL1.
2/18/2012
TIMER 0
TH0
TL0
TIMER 1
TH1
TL1
2/18/2012
operation of both timers. y Each bit of the SFR gives the microcontroller specific information concerning how to run a timer. y The higher four bits (bits 4 through 7) are related to Timer 1 where as the lower four bits (bits 0 through 3) perform the exact same functions, but for timer 0.
2/18/2012 MTS Technologies Ltd
GATE: y When this bit is set the timer will start through external Hardware only. y When this Gate bit is 0, the timer will run through software only. C/T: (Counter/ Timer) y When this bit is 0, the timer/counter is used as Timer to generate delay. y When this bit is 1, the timer/counter is used as Counter to count external events.
2/18/2012 MTS Technologies Ltd
M1 0 0 1 1
M0 0 1 0 1
Mode 0 1 2 3
Description 13-bit Timer 16-bit Timer 8-Auto Reload Split Timer Mode
2/18/2012
development.
2/18/2012
0 and causes THx to be incremented by 1. y Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. y If you set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles.
2/18/2012 MTS Technologies Ltd
What is that?
y When a timer is in mode 2, THx holds the
"reload value" and TLx is the timer itself. y Thus, TLx starts counting up. y When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of mode 1), it will be reset to the value stored in THx.
2/18/2012 MTS Technologies Ltd
becomes two separate 8-bit timers. y That is to say, Timer 0 is TL0 and Timer 1 is TH0. y Both timers count from 0 to 255 and overflow back to 0. y All the bits that are related to Timer 1 will now be tied to TH0.
2/18/2012 MTS Technologies Ltd
2/18/2012
TF (Timer Flag)
y TF0 for Timer Flag 0 y TF1 for Timer Flag 1 y Initially TFx=0, When THx and TLx roll over from 0000
2/18/2012
Counter
y Counter is used to count external events occurred in the physical world. y When the timer is used as a counter, it is a pulse outside of the 8051 that increments the TH, TL. y When the C/T=1, the counter counts up as pulses are fed from
y y
Timer 0 input pin (Pin 14, P3.4). Timer 1 input pin (Pin 15, P3.5).
2/18/2012
2/18/2012
2/18/2012
of the 8051. y Other one is connected to ground. y When the switch is pressed we will get a 0 signal at the port pin.
2/18/2012
2/18/2012
Example:
y Let us assume that,
Program:
ORG 00h SETB P2.5 BACK:JB P2.5,ON CLR P1.0 SJMP BACK ON:SETB P1.0 SJMP BACK END
an LED is connected to P1.0 and a switch is connected to P2.5 of the 8051. y Our aim is, when we press the switch then only the LED will glow.
2/18/2012
2/18/2012
LCD Interfacing
y Generally we will use 2x16 LCD.
2/18/2012
(Enable)
2/18/2012
y Pin 7
y
If we are not connect the pins 15 & 16, the back light of LCD will be off.
2/18/2012
LCD Registers
y RS (Register Select)
y If RS = 0, the instruction command code is selected,
2/18/2012
LCD. y R/W = 0, input allows the user to send data to the LCD.
y E (Enable):
y A High-to-Low pulse must be applied to this pin in
order to transmit the data to LCD. y The Pulse must be a minimum of 450nS.
2/18/2012
Initialization of 2 lines, 5x7 matrix. To clear the display screen. To increment the cursor to Right side. Display on, cursor blinking. Force cursor to beginning of 1st line. Force cursor to beginning of 2nd line.
In this way there are so many commands to initialize the LCD. These are some use full commands.
2/18/2012 MTS Technologies Ltd
MOV P2,#38H ACALL COMMAND MOV P2,#01H ACALL COMMAND MOV P2,#06H ACALL COMMAND MOV P2,#0EH ACALL COMMAND MOV P2, #80H ACALL COMMAND
2/18/2012 MTS Technologies Ltd
MOV P2,# H ACALL DISPLAY MOV P2,# A ACALL DISPLAY MOV P2,# I ACALL DISPLAY BACK: SJMP BACK
COMMAND: CLR P1.0 CLR P1.1 SETB P1.2 NOP CLR P1.2 ACALL DELAY RET
DISPLAY: SETB P1.0 CLR P1.1 SETB P1.2 NOP CLR P1.2 ACALL DELAY RET
DELAY: MOV R0,#255 REP: MOV R1,#255 DJNZ R1,$ DJNZ R0,REP RET
END
2/18/2012
lcd_command(0x06); delay(250); lcd_command(0x01); delay(250); lcd_command(0x80); delay(250); lcd_data ('U'); delay(250); lcd_data ('T'); delay(250); lcd_data ('L'); delay(250); } void delay (unsigned int value) { int i, j; for (i=0; i<1275; i++) for (j=0; j<value; j++); }
2/18/2012 MTS Technologies Ltd
//SHIFT CURSOR TO RIGHT //CLEAR THE LCD //FORSE THE CURSOR AT //THE BEGINNING OF //FIRST LINE // SEND Character TO THE LCD
//DELAY SUBROUTINE
void lcd_command (unsigned char value) //COMMAND SUBROUTINE { DATA = value; // SEND VALUE TO PORT rs = 0; //MAKING RS PIN AS LOW rw = 0; //MAKING RW PIN AS HIGH e = 1; delay(20); e = 0; //MAKING E PIN HIGH TO LOW return; } void lcd_data (unsigned char value) { DATA = value; rs = 1; rw = 0; e = 1; delay(20); e = 0; return; }
2/18/2012 MTS Technologies Ltd
//DATA SUBROUTINE
2/18/2012
Serial Communication
y There are two types of Communication.
y
2/18/2012
Basics of Communication
instead of 8-bit data lines in parallel communication. y At the transmitter bytes of data is converted to serial bits using a parallel-in-serial-out register. y At the receiver end serial data is packed into bytes by serial-in-parallel-out register.
2/18/2012 MTS Technologies Ltd
Serial Communication
y Serial Communication uses two methods:
y y
Synchronous Asynchronous
y Synchronous: Transfers a block of data at a time. y Asynchronous: Transfers single byte at a time. y There are special IC s made by different
manufacturers for serial data communication. y UART (Universal Asynchronous Receiver Transmitter) y USART (Universal Synchronous Asynchronous Receiver Transmitter.
2/18/2012
stop bits. y Start bit is always a Low (0), and Stop bit is always a High (1). y Baud Rate: The rate of data transfer in serial data communication is stated as bps or Baud Rate.
2/18/2012
bits of byte is transferred one after the other in a timed sequence on a single wire. y Serial communication is often used to transmit or receive data from the embedded processor.
2/18/2012
RS232 Standards
y To
allow compatibility among data communication equipment made by various manufacturers, an interfacing standard called RS232 was set by the Electronics Industries Association (EIA) in 1960. y In RS232, a 1 is represented by -3 to -25V, while a 0 bit is +3 to +25V, making -3 to +3V undefined. y But, 8051 microcontrollers voltage levels are TTL compatible.
2/18/2012
this reason, to connect any RS232 to microcontroller system, we must use voltage converters. y MAX232 is the IC used to convert the TTL Levels into RS232 voltage levels and vice versa. y These MAX232 IC s are commonly referred as line drivers.
2/18/2012
MAX232
y The MAX232 has two sets of line drivers for transferring data. y The line drivers used for TxD are called T1 & T2. y The line drivers used for RxD are referred as R1 & R2. y In many applications only one of each is used. y T1 and R1 are used together for TxD and RxD of the 8051 and the second set is left open.
2/18/2012
2/18/2012
2/18/2012
8051 system with out any error, we must make sure that the baud rate of the 8051 system matches the baud rate of the PC s COM port. Some of the baud rates supported by PC BIOS are listed in the table.
2/18/2012
many different baud rates. y The baud rate in the 8051 is programmable. This is done with the help of Timer 1. y The 8051 divides the crystal frequency by 12 to get the machine cycle frequency. y The 8051 serial communication UART circuitry divides the machine cycle frequency of 921.6 KHz by 32 once more before it is used by Timer1 to set the baud rate. y When timer1 is used to set the baud rate it must be programmed in Mode 2, that is 8-bit, auto reload.
2/18/2012 MTS Technologies Ltd
Baud Rate
TH1 (Decimal)
TH1 (HEX)
-3 -6 -12 -24
FD FA F4 E8
SBUF Register
y SBUF is an 8-bit register used solely for serial
communication in the 8051. y To transmit any data from 8051 via TxD line, it must be placed in the SBUF register. y Similarly, SBUF holds the byte of data when it is received by the 8051 RxD line. Example:
MOV SBUF, #D MOV SBUF, A MOV A, SBUF
2/18/2012
;load SBUF=44H, ASCII for D. ;Copy Accumulator into SBUF. ;Copy SBUF into Accumulator.
stop bit, and data bits of data framing, among other things. y SM0, SM1: These two bits determine the framing of data by specifying the number of bits per character, and the start and stop bits.
2/18/2012
SM0 SM1 0 0 Serial Mode 0 0 1 Serial Mode 1 1 0 Serial Mode 2 1 1 Serial Mode 3 Serial Mode 0: 9-bit data with variable baud rate. Serial Mode 1: 8-bit data, 1 start, 1 stop bit with variable baud rate. Serial Mode 2: 9-bit data with fixed baud rate. Serial Mode 3: 8-bit data with fixed baud rate.
Out of these 4 modes, we will use only Mode 1.
2/18/2012 MTS Technologies Ltd
using the 8051 in a multiprocessor environment. y REN (Receive Enable): When the REN bit is high, it allows the 8051 to receive data on RxD pin of 8051. If we want the 8051 to both transfer and receive data, REN must be set to 1. By making REN=0, the receiver is disabled.
2/18/2012
and 3.
y We will make TB8 and RB8 as 0, since it is not used
in our applications.
character, it raises the TI flag to indicate that it is ready to transfer another byte. y This bit is raised by the beginning of the Stop bit.
2/18/2012
y RI (Receive Interrupt):
y When the 8051 receives data serially via RxD, it
gets rid of start and stop bits and places the byte in the SBUF register. y Then it raises the RI flag bit to indicate that a byte has been received and should be picked up before it lost.
2/18/2012
Transmitting data to PC: y Send data to the serial buffer. y Monitor the TI flag. Receiving data from PC: y Monitor the RI flag. y Receive data from serial buffer.
2/18/2012 MTS Technologies Ltd
Example:
Transmitting:
ORG 00h MOV TMOD,#20H MOV SCON,#50H MOV TH1,#0FDH SETB TR1 MOV SBUF,#S HERE:JNB TI,HERE CLR TI BACK:SJMP BACK END
Receiving:
ORG 00h MOV TMOD,#20H MOV SCON,#50H MOV TH1,#0FDH SETB TR1 HERE:JNB RI,HERE MOV A,SBUF MOV P2,A CLR RI BACK:SJMP BACK END
2/18/2012
Example:
#include<reg51.h> void main (void)
{
TMOD =0X20; SCON = 0X50; TH1 = 0XFD; TR1 = 1; WHILE(1) { SBUF = A ; WHILE(TI==0); TI=0; } }
2/18/2012 MTS Technologies Ltd
2/18/2012