Unit-4.ppt Microcontroller
Unit-4.ppt Microcontroller
Functional block diagram Instruction format addressing modes Interrupt structure Timer I/O ports Serial communication.
Microprocessor vs Microcontroller
Microprocessor CPU is stand-alone, RAM, ROM, I/O, timer are separate designer can decide on the amount of ROM, RAM and I/O ports. expansive versatility general-purpose Microcontroller CPU, RAM, ROM, I/O and timer are all on a single chip fix amount of on-chip ROM, RAM, I/O ports for applications in which cost, power and space are critical single-purpose
Embedded System
Embedded system means the processor is embedded into that application. An embedded product uses a microprocessor or microcontroller to do one task only. In an embedded system, there is only one application software that is typically burned into ROM. Example-printer, keyboard, video game player
INTRODUCTION
MCS-51 family, originally designed by Intel in the 1980s Used in a large percentage of embedded systems Includes several on-chip peripherals, like timers and counters 128 bytes of on-chip data memory and up to 4K bytes of on-chip program memory
FEATURES
8-bit CPU optimized for control applications Extensive Boolean processing (single-bit logic) capabilities 64K Program Memory address space 64K Data Memory address space Up to 4K bytes of on-chip Program Memory 128 bytes of on-chip Data RAM 32 bi-directional and individually addressable I/O lines Two 16-bit timer/counters 6-source/5-vector interrupt structure with two priority levels
Block Diagram
External interrupts Interrupt Control On-chip ROM for program code
Timer/Counter
On-chip RAM
Timer 1 Timer 0
Counter Inputs
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
Address/Data
8051
Vcc P0.0(AD0 P0.1(AD1) ) P0.2(AD2 P0.3(AD3) ) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.7(A15) P2.6(A14 P2.5(A13 ) P2.4(A12 ) P2.3(A11 ) P2.2(A10) ) P2.1(A9) P2.0(A8)
Architecture
Memory Organization Program Status Word Interrupt Structure Port Structures Timer/Counters Reset
Memory Organization
Logical separation of program and data memory
Separate address spaces for Program (ROM) and Data (RAM) Memory Allow Data Memory to be accessed by 8-bit addresses quickly and manipulated by 8-bit CPU
Program Memory
Only be read, not written to The address space is 16-bit, so maximum of 64K bytes Up to 4K bytes can be on-chip (internal) of 8051 core PSEN (Program Store Enable) is used for access to external Program Memory
Memory Organization
Data Memory
Includes 128 bytes of on-chip Data Memory which are more easily accessible directly by its instructions There is also a number of Special Function Registers (SFRs) Internal Data Memory contains four banks of eight registers and a special 32-byte long segment which is bit addressable by 8051 bitinstructions External memory of maximum 64K bytes is accessible by movx
Memory Organization
Internal Data Memory, 128 bytes
The Program Status Word (PSW) contains several status bits that reflect the current state of the CPU. It contains the Carry bit, the Auxiliary Carry (for BCD operations), the two register bank select bits, the Overflow flag, a parity bit, and two user-definable status flags.
The Carry bit, other than serving the functions of a Carry bit in arithmetic operations, also serves as the Accumulator for a number of Boolean operations. the Auxiliary Carry for BCD operations. The bits RS0 and RS1 are used to select one of the four register banks shown below. A number of instructions refer to these RAM locations as R0 through R7. The selection of which of the four banks is being referred to is made on the basis of the bits RS0 and RS1 at execution time. The parity bit reflects the number of 1s in the Accumulator: P = 1 if the Accumulator contains an odd number of 1s, and P = 0 if the Accumulator contains an even number of 1s. Two bits in the PSW are uncommitted and may be used as general purpose status flags.
Interrupt Structure
The 8051 provides 4 interrupt sources
Two external interrupts Two timer interrupts
Port Structures
The 8051 contains four I/O ports All four ports are bidirectional Each port has SFR (Special Function Registers P0 through P3) which works like a latch, an output driver and an input buffer Both output driver and input buffer of Port 0 and output driver of Port 2 are used for accessing external memory Accessing external memory works like this Port 0 outputs the low byte of external memory address (which is time-multiplexed with the byte being written or read) Port 2 outputs the high byte (only needed when the address is 16 bits wide) Port 3 pins are multifunctional. The alternate functions are activated with the 1 written in the corresponding bit in the port SFR
Read-Modify-Write Feature
When reading a port some instructions read the latch and others read the pin The instructions that read the latch rather than the pin are the ones that read a value (possibly change it), an then rewrite it to the latch are called read-modify-write instructions
Timer/Counters
The 8051 has two 16-bit Timer/Counter registers Timer 0 Timer 1 Both can work either as timers or event counters Both have four different operating modes from which to select.
Mode 0 (13-bit Timer) Mode 1 (16-bit Timer) Mode 2 (8-bit Timer with Auto-Reload) Mode 3 (Two 8-bit Timers)
Instruction Set
Optimized for 8-bit control applications Fast addressing modes for accessing internal RAM in order to facilitate byte operations on small data structures Good for systems that require a lot of Boolean processing because of its extensive support for one-bit variables as a separate data type
Indirect Addressing
The instruction specifies a register which contains the address of the operand The address register for 8-bit addresses can be R0 or R1 of the selected bank, or the Stack Pointer The address register for 16-bit addresses can only be 16-bit data pointer register, DPTR Both internal and external RAM can be indirectly addressed
Immediate Constants
The value of a constant follows the opcode MOV A, #10 loads the Accumulator with the decimal number 10
Indexed Addressing
Only Program Memory can be accessed and it can be a read Used for reading look-up tables in Program Memory and case jump instruction
Arithmetic Instructions Logical Instructions Data Transfers Lookup Tables Boolean Instructions Jump Instructions
8051 Registers
Rn Register R7-R0 of the currently selected Register Bank. direct 8-bit internal data locations address. This could be an Internal Data RAM location (0127) or a SFR [i.e., I/O port, control register, status register,etc.(128 255)]. @Ri 8-bit internal data RAM location (0-255) addressed indirectly through register R1or R0. #data 8-bit constant included in instruction. #data 16 16-bit constant included in instruction. addr 16 16-bit destination address. Used by LCALL and LJMP. A branch can be anywhere within the 64K byte Program Memory address space.
addr 11 11-bit destination address. Used by ACALL and AJMP. The branch will be within the same 2K byte page of program memory as the first byte of the following instruction.
rel Signed (twos complement) 8-bit offset byte. Used by SJMP and all conditional jumps. Range is -128 to +127 bytes relative to first byte of the following instruction.
bit Direct Addressed bit in Internal Data RAM or Special Function Register.
ARITHMETIC OPERATIONS
ADD A,Rn ADD A,direct ADD A,@Ri ADD A,#data ADDC A,Rn ADDC A,direct ADDC A,@Ri ADDC A,#data SUBB A,Rn SUBB A,direct SUBB A,@Ri SUBB A,#data INC A INC Rn INC direct INC @Ri DEC A DEC Rn DEC direct DEC @Ri INC DPTR MUL AB DIV AB DA A Add register to Accumulator Add direct byte to Accumulator Add indirect RAM to Accumulator Add immediate data to Accumulator Add register to Accumulator with Carry Add direct byte to Accumulator with Carry Add indirect RAM to Accumulator with Carry Add immediate data to Acc with Carry Subtract Register from Acc with borrow Subtract direct byte from Acc with borrow Subtract indirect RAM from ACC with borrow Subtract immediate data from Acc with borrow Increment Accumulator Increment register Increment direct byte Increment direct RAM Decrement Accumulator Decrement Register Decrement direct byte Decrement indirect RAM Increment Data Pointer Multiply A & B Divide A by B Decimal Adjust Accumulator
LOGICAL OPERATION
ANL A,Rn ANL A,direct ANL A,@Ri ANL A,#data ANL direct,A ANL direct,#data ORL A,Rn ORL A,direct ORL A,@Ri ORL A,#data ORL direct,A ORL direct,#data XRL A,Rn XRL A,direct XRL A,@Ri XRL A,#data XRL direct,A XRL direct,#data AND Register to Accumulator AND direct byte to Accumulator AND indirect RAM to Accumulator AND immediate data to Accumulator AND Accumulator to direct byte AND immediate data to direct byte OR register to Accumulator OR direct byte to Accumulator OR indirect RAM to Accumulator OR immediate data to Accumulator OR Accumulator to direct byte OR immediate data to direct byte Exclusive-OR register to Accumulator Exclusive-OR direct byte to Accumulator Exclusive-OR indirect RAM to Accumulator Exclusive-OR immediate data to Accumulator Exclusive-OR Accumulator to direct byte Exclusive-OR immediate data to direct byte
LOGICAL OPERATION
CLR A Clear Accumulator CPL A Complement Accumulator RL A Rotate Accumulator Left RLC A Rotate Accumulator Left through the Carry RR A Rotate Accumulator Right RRC A Rotate Accumulator Right through the Carry SWAP A Swap nibbles within the Accumulator
Data Transfers
MOV A,Rn MOV A,direct MOV A,@Ri MOV A,#data MOV Rn,A MOV Rn,direct MOV Rn,#data MOV direct,A MOV direct,Rn MOV direct,direct MOV direct,@Ri MOV direct,#data MOV @Ri,A MOV @Ri,direct Move register to Accumulator Move direct byte to Accumulator Move indirect RAM to Accumulator Move immediate data to Accumulator Move Accumulator to register Move direct byte to register Move immediate data to register Move Accumulator to direct byte Move register to direct byte Move direct byte to direct Move indirect RAM to direct byte Move immediate data to direct byte Move Accumulator to indirect RAM Move direct byte to indirect RAM
Data Transfers
MOV @Ri,#data MOV DPTR,#data16 MOVC A,@A+DPTR MOVC A,@A+PC MOVX A,@Ri MOVX A,@DPTR MOVX @Ri,A MOVX @DPTR,A PUSH direct POP direct XCH A,Rn XCH A,direct XCH A,@Ri XCHD A,@Ri Move immediate data to indirect RAM Load Data Pointer with a 16-bit constant Move Code byte relative to DPTR to Acc Move Code byte relative to PC to Acc Move External RAM (8-bit addr) toAcc Move External RAM (16-bit addr) to Acc Move Acc to External RAM (8-bit addr) Move Acc to External RAM (16-bitaddr) Push direct byte onto stack Pop direct byte from stack Exchange register with Accumulator Exchange direct byte with Accumulator Exchange indirect RAM with Accumulator Exchange low-order Digit indirect RAM with Acc
PROGRAM BRANCHING
ACALL LCALL RET RETI AJMP addr11 LJMP addr16 SJMP rel Short JMP @A+DPTR JZ rel JNZ rel CJNE A,direct,rel CJNE A,#data,rel CJNE Rn,#data,rel CJNE @Ri,#data,rel DJNZ Rn,rel DJNZ direct,rel NOP addr11 Absolute Subroutine Call addr16 Long Subroutine Call Return from Subroutine Return from interrupt Absolute Jump Long Jump Jump (relative addr) Jump indirect relative to the DPTR Jump if Accumulator is Zero Jump if Accumulator is Not Zero Compare direct byte to Acc and Jump if Not Equal Compare immediate to Acc and Jump if Not Equal Compare immediate to register and Jump if Not Equal Compare immediate to indirect and Jump if Not Equal Decrement register and Jump if NotZero Decrement direct byte and Jump ifNot Zero No Operation
Lookup Tables
Timer/Counters
8051 has two 16-bit Timer/Counter registers Timer/Counter 0 Timer/Counter 1 These registers can be used as timers or as event counters When operating as a timer, the timer/counter runs for a programme length of time, then issues an interrupt request. When operating as a counter, the timer/counter counts negative transitions on an external pin. After a preset number of counts, the counter issues an interrupt request. Both registers have additional four operating modes
Timer/Counter Modes
The selection for Timer or Counter is done by control bits C/T in the TMOD register Both Timer/Counters have four operating modes, which Modes 0, 1 and 2 are the same for both Timer/Counters, Mode 3 is different Modes are selected by bit pairs (M1, M0) in TMOD SFR Another SFR used for work with Timer/Counters is TCON containing flag (TFx) and control (TRx) bits
TMOD REGISTER
T CON REGISTER
Timer 0
Timer 0 functions as either a timer or event counter in four modes of operation. Timer 0 is controlled by the four lower bits of the TMOD register (see Table 2-5) and bits 0, 1, 4 and 5 of the TCON register (see Table 2-3). TMOD register selects the method of timer gating (GATE0), timer or counter operation (T/C0#) and mode of operation (M10 and M00). The TCON register provides timer 0 control functions: overflow flag (TF0), run control bit (TR0), interrupt flag (IE0) and interrupt type control bit (IT0). For normal timer operation (GATE0= 0), setting TR0 allows TL0 to be incremented by the selected input. Setting GATE0 and TR0 allows external pin INT0# to control timer operation.
Timer 0 overflow (count rolls over from all 1s to all 0s) sets TF0 flag, generating an interrupt request.
It is important to stop timer/counter before changing mode.
Mode 0 configures timer 0 as a 13-bit timer which is set up as an 8-bit timer (TH0 register) and the lower five bits of the TL0 register. The upper three bits of TL0 register are indeterminate and should be ignored. Pre scalar overflow increments the TH0 register. As the count rolls over from all 1s to all 0s, it sets the timer interrupt flag TF0. The counted input is enabled to the Timer when TR0 = 1 and either GATE = 0 or INT0 = 1. (Setting GATE = 1 allows the Timer to be controlled by external input INT0, to facilitate pulse width measurements). TR0 is a control bit in the Special Function register TCON, GATE is in TMOD. Mode 0 operation is the same for Timer 0 as for Timer 1. Substitute TR0, TF0 and INT0 for the corresponding Timer 1 signals. There are two different GATE bits, one for Timer 1 (TMOD.7) and one for Timer 0 (TMOD.3).
Mode 2 configures timer 0 as an 8-bit timer (TL0 register) that automatically reloads from the TH0 register. TL0 overflow sets TF0 flag in the TCON register and reloads TL0 with the contents of TH0, which is preset by software. When the interrupt request is serviced, hardware clears TF0. The reload leaves TH0 unchanged. The next reload value may be changed at any time by writing it to the TH0 register. Mode 2 operation is the same for Timer/Counter 1.
Mode 3 configures timer 0 so that registers TL0 and TH0 operate as separate 8-bit timers. This mode is provided for applications requiring an additional 8-bit timer or counter. TL0 uses the timer 0 control bits C/T0# and GATE0 in the TMOD register, and TR0 and TF0 in the TCON register in the normal manner. TH0 is locked into a timer function (counting FPER /6) and takes over use of the timer 1 interrupt (TF1) and run control (TR1) bits. Thus, operation of timer 1 is restricted when timer 0 is in mode 3.
Interrupt
8051 provides 4 interrupt sources
2 external interrupts 2 timer interrupts
They are controlled via two SFRs, IE and IP Each interrupt source can be individually enabled or disabled by setting or clearing a bit in IE (Interrupt Enable). IE also exists a global disable bit, which can be cleared to disable all interrupts at once
Interrupt
Each interrupt source can also be individually set to one of two priority levels by setting or clearing a bit in IP (Interrupt Priority) A low-priority interrupt can be interrupted by high-priority interrupt, but not by another lowpriority one A high-priority interrupt cant be interrupted by any other interrupt source If interrupt requests of the same priority level are received simultaneously, an internal polling sequence determines which request is serviced, so within each priority lever there is a second priority structure
Interrupt
This internal priority structure is determined by the polling sequence, shown in the following table
External Interrupts
External interrupts ~INT0 and ~INT1 have two ways of activation
Level-activated Transition-activated
This depends on bits IT0 and IT1 in TCON The flags that actually generate these interrupts are bits IE0 and IE1 in TCON On-chip hardware clears that flag that generated an external interrupt when the service routine is vectored to, but only if the interrupt was transition-activated When the interrupt is level-activated, then the external requesting source is controlling the request flag, not the on-chip hardware
Handling of Interrupts
When interrupt occurs (or correctly, when the flag for an enabled interrupt is found to be set 1), the interrupt system generates an LCALL to the appropriate location in Program Memory, unless some other conditions block the interrupt Several conditions can block an interrupt An interrupt of equal or higher priority level is already in progress The current (polling) cycle is not the final cycle in the execution of the instruction in progress The instruction in progress is RETI or any write to IE or IP registers If an interrupt flag is active but not being responded to for one of the above conditions, must be still active when the blocking condition is removed, or the denied interrupt will not be serviced Next step is saving the registers on stack. The hardware-generated LCALL causes only the contents of the Program Counter to be pushed onto the stack, and reloads the PC with the beginning address of the service routine In some cases it also clears the flag that generated the interrupt, and in other cases it doesnt. It clears an external interrupt flag (IE0 or IE1) only if it was transition-activated.
Handling of Interrupts
Having only PC be automatically saved gives programmer more freedom to decide how much time to spend saving other registers. Programmer must also be more careful with proper selection, which register to save The service routine for each interrupt begins at a fixed location. The interrupt locations are spaced at 8-byte interval, beginning at 0003H for External Interrupt 0, 000BH for Timer 0, 0013H for External Interrupt 1 and 001BH for Timer 1, shown in the following tables
Execution of service routine continues from that location until the end, that is until it encounters RETI. RETI instruction does two things It informs the processor that this interrupt Routine is finished Secondly, reloads the PC from the top bytes from the stack
Reset
The reset input is RST pin To accomplish a reset the RST pin must be held high for at least two machine cycles In the response on the RST signal, CPU generates an internal reset The external reset signal is asynchronous to the internal clock In the internal reset algorithm, 0s are written to all the SFRs except the port latches and Stack Pointer The port latches are initialized to FFH and Stack Pointer to 07H Driving ALE and PSEN pins to 0 while reset is active could cause the device to go into an indeterminate state The internal RAM is not affected by reset. On power up the RAM content is indeterminate
MAX233 MAX233 performs the same job as the MAX232 eliminates the need for capacitors much more expensive than the MAX232
Figure
(a) Inside MAX232 (b) its Connection to the 8051 (Null Modem)
Figure
(a) Inside MAX233 (b) Its Connection to the 8051 (Null Modem)
Baud rate in the 8051 serial communications of the 8051 with the COM port of the PC must make sure that the baud rate of the 8051 system matches the baud rate of the PC's COM port Baud rate in the 8051 baud rate in the 8051 is programmable done with the help of Timer 1 relationship between the crystal frequency and the baud rate in the 8051 8051 divides the crystal frequency by 12 to get the machine cycle frequency XTAL = 11.0592 MHz, the machine cycle frequency is 921.6 kHz 8051's UART divides the machine cycle frequency of 921.6 kHz by 32 once more before it is used by Timer 1 to set the baud rate 921.6 kHz divided by 32 gives 28,800 Hz Timer 1 must be programmed in mode 2, that is 8-bit, auto-reload
With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates. (a) 9600 (b) 2400 (c) 1200
Write a program to transfer the message "YES" serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.
PROGRAMMING IN ASSEMBLY
Importance of the TI flag
check the TI flag bit, we know whether can transfer another byte TI flag bit is raised by the 8051 TI flag cleared by the programmer writing a byte into SBUF before the TI flag bit is raised, may lead to loss of a portion of the byte being transferred
Program the 8051 to receive bytes of data serially, and put them in P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit.