08.601 MBSD Module 1
08.601 MBSD Module 1
08.601 MBSD Module 1
To make a complete microcomputer, memories like ROM and RAM, memory decoders, oscillator, IO devices, serial and parallel ports, etc. are required. A very large system can be configured around CPU as the application demands, since the flexibility of microprocessors they are referred as General Purpose Processor.
Page 1
2. Microcontroller True computer on a chip, also referred as System on a Chip Uses all features of microprocessor with peripherals like RAM, ROM, Ports, clock circuit, etc. General block diagram of microcontroller:
Similar to microprocessor, microcontroller is also general purpose device, but not very flexible as microprocessor. Block diagram of system using microcontroller:
Page 2
Microcontroller itself is a system An eg. Comparison between Z80 microprocessor and 8051 microcontroller: Features 8085 8051 Pins 40 40 Address lines 16 16 Data lines 8 8 Interrupt lines 6 2 IO lines 0 32 8-bit registers 20 34 16-bit registers 4 2 Stack size 64kB 128B Internal ROM 0 4kB Internal RAM 0 128B External 64kB 128kB Memory Flags 6 4 Timer 0 2 Parallel ports 0 4 Serial ports 0 1 The main disadvantage of microcontrollers is less number of instruction sets, but can be overcome by writing programs by high level programming languages.
Page 3
2. 8-bit microcontrollers
Simple applications to high speed machine control Different families for each type
Page 4
4. 32-bit microcontrollers Applications like Robots, high intelligent instruments, avionics, image processing, etc. Some features of Intels 80960 microcontrollers are:
5. Embedded Processors Microcontrollers are inadequate for complicated tasks, can be overcome by embedded processors RISC and CISC processors High end processors Integrating more functions into the chip, Instruction Set Architecture (ISA) model Some processors: 1. Intels X86 processors 2. Macintosh Power PC 604,603, 620 etc.
Page 5
Page 6
Page 7
- External interrupt 0
- External data memory write strobe - External data memory read strobe
5. 6.
Program Store Enable Dedicated control signal To enable external program memory, usually connected to Output Enable (OE) pin of external EPROM During fetch state this signal becomes low and the binary opcodes (programs) are read from external EPROM During the execution of a program from internal ROM, this pins remains high ALE is used to de-multiplex address and data bus of port 0 during external memory operation. The ALE signal pulses 1/6th of on-chip oscillator frequency
Page 9
7.
For 8031/8032 this pin must be tied low The pin is also served as programming voltage (Vpp) for internal EPROM For flash Vpp is +12V For EPROM Vpp is +21V
8. RST Master reset When this pin is high for 2 machine cycles, 8051 internal registers are loaded with appropriate values for an orderly system start-up Some register values are: PC = 0000H SP = 07H DPTR = 0000H A = 00H B = 00H PSW = 00H P0 = FFH P1 = FFH P2 = FFH P3 = FFH
Page 10
9. XTAL1 and XTAL2 On-chip oscillator inputs Typically connected to driven by a crystal (stable, but costly) or ceramic resonators (poor stability, but economic).
Page 11
Where C1 and C2 are stabilizing capacitors and C1 = C2 = 30pF 10pF for crystals or 40pF 10pF for ceramic resonators External clock driving also possible and circuit diagram is shown below:
State is the basic time period for microcontroller for discrete operations like fetching, decoding, executing, etc. One state form two pulse periods. To complete most operations, microcontroller require at least six states and is referred as one machine cycle ALE signal is also shown above Typical frequency range 0 to 24MHz Practical frequency range is 1MHz to 24MHz; since some internal registers are dynamic and causes data loses at low frequencies 8051 Memory organization
Department of ECE, VKCET Page 12
Internal RAM 128 byte internal RAM is organized as three groups: 1. A total of 32 bytes of working registers from address 00H to 1FH and are set aside for four register banks and the stack 2. A total of 16 bytes bit-addressable locations from address 20H to 2FH and the address range of bit area are 00H to 7FH 3. A total of 80 bytes general purpose area from 30H to 7FH can be used for read/write operation and is also referred as scratch pad.
Internal ROM
One bank can be used at a time and can be switched between each using PSW Bank 0 is default register bank and default stack memory starts from Bank 1
Page 13
8051 has on-chip 4k byte ROM to hold program (code) and is organized with address 0000H to 0FFFH
PC of 8051 hold 16 bit data, then the addressing capacity is 64k byte. So if the address is more than 4k byte, microcontroller access external code memory by enabling 8051 Registers The 8051 registers are classified into general purpose and special function registers (SFRs) 32 general purpose registers (working registers) 32 byte registers (00H to 1FH) arranged as 4 banks of internal RAM. Other locations can be used as direct address registers (20H to 7FH) Special Function Registers (SFR) Three categories: a) For data manipulation b) Memory pointer c) Control registers
Page 14
Accumulator (A) register hold one operand as well as result during mathematical operations in CPU and also used to transfer data between 8051 and external memory. It is 8-bit bit-addressable register B register is used to hold one operand and result during multiplication and division operation
Page 15
Page 16
Page 17
Port 1:
Dual function: IO lines and bidirectional data bus and lower order address bus for external memory To use as input line, a 1 must write to corresponding port 0 latch to turn off two output transistors, which provide high impedance state to pin To use as output line, a 0 logic will appear due to the on state of lower transistor. But for logic 1 external pull-up resistor is required Control logic turn on/off output transistors according to external memory access Dedicated IO lines with internal FET pull up , hence faster than port 0 pins Input and output operations are similar to port 0 Dual function: IO lines and higher order address bus for external memory For input operation no need to write 1 to latch Output operation is similar to other ports Have internal FET pull up
Port 2:
Page 18
Timing diagram:
Page 19
Examples.
Examples:
Page 20
Examples:
Examples:
Page 21
Different types: 1. Arithmetic instructions 2. Logical instructions 3. Data transfer instructions 4. Bit oriented instructions 5. Program branching instructions Arithmetic instructions: 1. ADD A, source A = A + source, source may be immediate, direct, register or register indirect address operand. CY, OV and AC flags affect. Examples: ADD A,#90H ADD A,R7 ADD A,37H ADD A,@R1 2. ADDC A, source A = A + CY + source, similar to ADD instruction, but carry flag status will added to the result. CY, OV and AC flags affect. 3. DA A Decimal adjust for addition, it adjust A content to BCD after ADD or ADDC operation. Only CY flag affect. Example: ADD A,R0 DA A 4. SUBB A, source A = A CY source, source may be immediate, direct, register or register indirect address operand. CY, OV and AC flags affect. Examples: SUBB A,#90H
Department of ECE, VKCET Page 23
Page 24
4. PUSH direct Move the content in direct internal RAM address to stack, where address defined in SP Example: PUSH E0H 5. POP direct Move the content from stack to direct internal RAM address, where address defined in SP Example: POP E0H 6. XCH A, source Exchange A with source. Source may be any register, direct address or indirect address Examples: XCH A,R0 XCH A,80H XCH A,@R0 7. XCHD A, source Exchange lower nibble of A with indirect address RAM content Example: XCHD A,@R0 Bit oriented instructions: 1. CLR C Clears the carry flag 2. CLR bit Clears the direct bit Examples: CLR ACC.0 CLR EA CLR 00 3. SETB C Sets the carry flag 4. SETB bit Sets the direct bit Examples: SETB ACC.0 SETB EA SETB 00 5. CPL C Complements the carry flag 6. CPL bit Complements the direct bit
Department of ECE, VKCET Page 26
7. ANL C, bit AND direct bit to the carry flag Examples: ANL C,PSW.0 ANL C,01 8. ANL C,/bit AND complements of direct bit to the carry flag Examples: ANL C,/PSW.0 ANL C,/01 9. ORL C,bit OR direct bit to the carry flag Examples: ORL C,ACC.0 10.ORL C,/bit OR complements of direct bit to the carry flag Example: ORL C,/00 11. MOV C,bit Moves the direct bit to the carry flag Example: MOV C,00 12. MOV bit,C Moves the carry flag to the direct bit Example: MOV 02,C Program branching instructions: Different types:
Branching instructions replace the content of the PC with a new program address causes program execution from a new location. The difference of the new address from the current address in PC is called range and there tree types of ranges for branching instructions: 1. Relative range: +127 to -128 bytes from the instruction following the branch instruction 2. Short Absolute range: Same of 2k byte from the instruction following the branch instruction
Page 27
anywhere in program memory Relative range: Only one byte of data (either positive or negative) is fro branching address The program with relative branching instructions can be located anywhere in the program memory The only disadvantage is address range is limited to +127 to 128 Short absolute range: Gives the paging concept Program memory ranges from 0000H to FFFFH; 8051 program memory is arranged as 2k byte pages of 32 numbers (total 64k byte) The hexa-decimal address of each page is shown below:
The upper five bits of the address hold page number and lower eleven bits hold the address within each page. Branching instructions ends at X7FF or XFFF and starts with X000 or X800, then branching will be in same page Similar to relative addressing; fewer bytes are needed and have longer programming distances than relative addressing.
Page 28
Where radd is relative address and is generated by: radd = target address current address, if target address is more than current address, radd is positive ranges from 01H to 7FH . If target is less than current address, radd is negative ranges from 80H to FFH and is in 2s complement from
Page 29
Unconditional jumps: Doesnt test any bit or bytes, always jump to any address specified Instructions:
Page 30
Interrupt and returns: Interrupt causes a hardware generated call Interrupt subroutine addresses are:
Assembly language programming: Programming by mnemonics: codes and abbreviations easily to remember. Eg: ADD , MOV, JMP etc.. Assembler translates assembly language to opcode (machine code or operation code) Eg: ADD A, R0 has opcode 28 Assembly languages are low level languages deals with structure of CPU; knowledge of internal architecture is must C, C++, BASIC etc are high level language; no need to have the knowledge of internal structure of CPU Compiler translates high level language to opcodes Structure of 8051 ASSEMBLY LANGUAGE
Department of ECE, VKCET Page 31
Eg: MAIN:
MOV DPTR,#4500H
C1 U1
Vcc 33nF
R2
10k 19 18 39 38 37 36 35 34 33 32 21 22 23 24 25 26 27 28 10 11 12 13 14 15 16 17
R3
10k Output
X1
CRYSTAL
XTAL1 XTAL2
C2 RESET C3
10uF 33nF
RST
P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7 P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15 P3.0/RXD P3.1/TXD P3.2/INT0 P3.3/INT1 P3.4/T0 P3.5/T1 P3.6/WR P3.7/RD
INPUT
R1
8.2k
29 30 31
PSEN ALE EA
1 2 3 4 5 6 7 8
Page 33
If P0.1 = 1? Yes Set P0.0 as 1 Wait 0.5ms Clear P0.0 Wait 0.5ms
No
Assembly language program: To wait during execution either software delay or hardware delay is required. Hardware delay is provided by timer/counter module. At this level software delay can be used and design for standard delay may be 100us, 1ms, 1s etc In the given program all delay are multiples of 250us, so a 250us delay is suitable. Consider a subroutine: delay_us: MOV R0,#N ;1 MC (machine cycle) execution time loop: DJNZ R0,loop ;2 MC x N execution time RET ;2 MC execution time For the above instructions total time for execution is Texecution = 3MC +(2N)MC If clock frequency is 12MHz, one clock period is T = 1/12M sec and 1MC = 12T = 1us Then Texecution = (3 + 2N)us. If Texecution = 250us, N = 123.5 To get accurate output N value should be an integer, the add a NOP instruction to the code;
Department of ECE, VKCET Page 34
MOV R0,#N ;1 MC (machine cycle) DJNZ R0,loop ;2 MC x N NOP ;1 MC RET ;2 MC Then Texecution = (4 + 2N)us. For Texecution = 250us, N= 123 = 7BH Calling this routine causes a software delay; killing the time:
Final program: MAIN: REPEAT: ORG 0 CLR P0.0 SETB P0.1 MOV C,P0.1 JNC SEVENTYFIVE SETB P0.0 LCALL delay_us LCALL delay_us CLR P0.0 LCALL delay_us LCALL delay_us SJMP REPEAT SETB P0.0 LCALL delay_us LCALL delay_us LCALL delay_us CLR P0.0 LCALL delay_us SJMP REPEAT MOV R0,#7BH ; Count value for 250us DJNZ R0,loop NOP RET END
SEVENTYFIVE:
delay_us: loop:
Page 35
IDL Set to 1 activate idle mode and only exit in interrupt or system reset PD Set to 1 activates power down mode and only exit in reset or interrupt (Both facilities is only available in CMOS versions 89C51/80C51) GF1 & GF0 are general purpose flag bits SMOD is used to set baud rate for Serial port
Page 37
Page 38
Page 39