Beruflich Dokumente
Kultur Dokumente
Introduction Block Diagram and Pin Description of the 8051 Registers Memory mapping in 8051 Stack in the 8051 I/O Port Programming Timer Serial Communication Interrupt
CPU: Central Processing Unit I/O: Input /Output Bus: Address bus & Data bus Memory: RAM & ROM Timer Interrupt Serial Port Parallel Port
Microcontroller
A smaller computer On-chip RAM, ROM, I/O ports... ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X
Microprocessors vs Microcontrollers
Microprocessor
CPU is stand-alone, RAM, ROM, I/O , timer have to be added externally. designer can decide on the amount of ROM, RAM and I/O ports. general-purpose ExampleIntels x86, Motorolas 680x0
Microcontroller
CPU, RAM, ROM, I/O and timer are all on a single chip fix amount of on-chip ROM, RAM, I/O ports Single-purpose for applications in which cost, power and space are critical ExampleIntels 8051, Motorolas 6811, Zilog Z80
CISC vs RISC
Complex instruction set computer Large number of complex instructions, take more clock cycles to execute. Variable length instructions hence difficult to implement pipelining. Supports memory to memory addressing modes.
Software design is easy. Ex:Intel 80X86,
Reduced instruction set computer Instructions are few and simple, majority of them are executed in a single clock cycle Majority of the instructions are of same length and pipelining is effectively achieved. All operations are register-to-registers. Load-Store architecture. Software design is complex. Programmers have to effectively utilize the available instructions to complete the task. Ex:ARM,Atmel Avr,Pic
The CISC approach attempts to minimize the number of instructions per program, sacrificing the number of cycles per instruction. RISC does the opposite, reducing the cycles per instruction at the cost of the number of instructions per program.
Consists of a single linear memory to store both data and the code Common data and address bus,hence at any given time either data or code can be fetched. Commonly found in processors Ex:Intel 80X86,
Consists of separate memories to store data and code Two different data and address buses,hence data and code can be accessed simultaneously. Commonly found in controllers Ex:Intel 8051,
ROM(read only memory):Nonvolatile memory used to store the instructions. PROM(Programmable ROM): Type of ROM memory which is programmed after manufacturing. EPROM(Erasable Programmable ROM):Once programmed , an EPROM can be erased by exposing to UV-rays and can be reprogrammed. EEPROM(Electrically Erasable Programmable ROM):EEPROM can be erased and reprogrammed by applying the electrical voltage. FLASH: It is a specific type of EEPROM memory that is erased and reprogrammed in large blocks and is comparitively faster than EEPROM.
8031
NO
128 2 32 1 6
Block Diagram
External interrupts Interrupt Control On-chip ROM for program code
Timer/Counter
On-chip RAM
Timer 1 Timer 0
Counter Inputs
CPU
Serial Port
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
Address/Data
the controller. Upon applying a high pulse to this pin the controller will reset and starts the execution from first(from memory location 0000h), this is often referred as power on reset(POR).
ACC B PSW SP
DPTR RAM are all zero.
00 00 00 07
0000
A Pin of Port1
Read latch
TB2
Vcc
Load(L1)
P1.X
Clk Q
P1.X pin M1
P0.x
8051 IC
Read latch
A Pin of Port0
TB2
D Q
P1.X M1
Clk Q
P0.X pin
P0.0 DS5000 P0.1 P0.2 8751 P0.3 P0.4 8951 P0.5 P0.6 P0.7
Port
R4
R5 R6 R7
8k
30H 2FH
Bit-Addressable RAM
0FFFH 1FFFH 20H 1FH
8751 AT89C51
8752 AT89C52
Atmel Corporation
08H 07H
Register Bank 0
00H
Stack of 8051
Stack is a part of RAM used by CPU to store the information. This information could be data or address. Usually it is used to store the return address in case of call instruction. The register used to access the stack is called SP (stack pointer) . The stack pointer in the 8051 is only 8 bits wide, which means that it can take value 00 to FFH. When 8051 powered up, the SP register contains value 07. Instructions used to access the stack memory are PUSH & POP. The storing of a CPU register in the stack is called a PUSH. SP is pointing to the last used location of the stack(top of stack).As we push data onto the stack, the SP is incremented by one. Loading the contents of the stack back into a CPU register is called a POP. With every pop, the top byte of the stack is copied to the register specified by the instruction and the stack pointer is decremented once
Programming Environment
Addressing modes
Different ways of accessing the data is known as addressing modes. Immediate addressing mode: In this mode the data is provided within the instruction itself.(#) indicates immediate mode. Ex: mov a,#20h, add a,#30h Register addressing mode: Here both the source and destination are cpu registers. Ex: mov a,r0 mov r0,a add a,r0. Direct addressing mode: Here the data is stored in the memory whose address will be provided in the instruction itself. Ex: mov a, addr mov a,50h mov 40h,a Indirect addressing mode: Here the data is stored in the memory whose address will be stored in one of the cpu registers(R0,R1,DPTR) Ex: mov a,@r0 mov @r1,a mov a,@dptr. Here @ indicates indirect mode.
An assembly language instruction consist of four fields. Label: mnemonic operands ;comment Label: This field is used to refer a particular line of code, usually used in branching and call instruction to jump to a particular label. Mnemonic: Specifies the operation like add, mov, mul etc. Operands: An entity on which the operation is performed. Here the number of operands depends on the type of instruction. Comment:Used to document the program, these are written at the end of the line following a semicolon(;) and are ignored by assembler. 8051 instructions are grouped into 5 groups 1)Data transfer instructions 2)Arithmetic instructions 3)Logical instructions 4)Branching instructions 5)Boolean instructions
Register mode
Mov A, Rn Mov Rn, A
Direct mode
Mov A, addr Mov addr, A Mov Rn, addr Mov addr, Rn
Indirect mode Mov A, @Ri Mov @Ri, A Mov @Ri, #data Mov @Ri, addr Mov A, @DPTR Mov @DPTR, A Movc A, @A+DPTR Movc A, @A+PC Movx A, @DPTR Movx A, @Ri Movx @DPTR, A Movx @Ri, A
Arithmetic instructions
Immediate mode
ADD A, #data SUBB A, #data ADDC A, #data SUBB A, #data
Direct mode
ADD A, addr SUBB A, addr INC addr DEC addr
Register mode
ADD A, B ADD A, Rn ADDC A, B SUBB A, B MUL AB DIV AB INC A INC Rn (n=0-7) DEC
Indirect mode
ADD A, @Ri ADD A, @DPTR ADDC A, @Ri INC @Rn (n=0 or 1) DEC @Rn
Logical instructions
Immediate mode
ANL A, #data ORL A, #data XRL A, #data
Direct mode
ANL A, Addr ORL A, Addr XRL A, Addr ANL Addr, A ORL Addr, A XRL Addr, A
Register mode
ANL A, Rn ORL A, Rn XRL A, Rn RL A RR A RLC A RRC A SWAP A
Indirect mode
ANL A, @Rn (n=0 or 1) ORL A, @Rn XRL A, @Rn
Conditional Jump
CJNE A, #data, rel
CJNE Rn, #data, rel CJNE @Ri, #data, rel CJNE A, Addr, rel JZ/JNZ reladdr JC/JNC reladdr JB/JNB bitaddr, reladdr DJNZ Rn, reladdr DJNZ direct, reladdr
8051 timer/counter
Timers/Counters Programming
The 8051 has 2 timers(Timer0 & Timer1),they can be used as
1. 2. Timers to generate delays or. Event counters to count events happening outside the microcontroller.These events could represent the number of people passing through an entrance, or the number of wheel rotations, or any other event that can be converted to pulses.
Both Timer 0 and Timer 1 are 16 bits wide,Since 8051 has an 8-bit architecture, each16-bits timer is accessed as two separate registers of low byte and high byte.These can be accessed like any other 8-bit registers. MOV TL0,#26h MOV TH0,#50h MOV TL1,#0ABh MOV TH1,#0FFh
Timers/Counters Programming
Once the timer is started it increments for every clock cycle until it reaches FFFFh and for the next clock it rolls to 0, raising the timer overflow flag(TF0=1).
TMOD Register
Both timers 0 and 1 use the same register called TMOD (timer mode), to set the various timer operation modes.TMOD is a 8-bit register.The lower 4 bits are for Timer 0 & the upper 4 bits are for Timer 1.In each case,the lower 2 bits are used to set the timer mode & the upper 2 bits to specify the operation.
M1 M0 0 0 0 1 1 0
Mode 0 1 2
Operating Modes 13-bit timer mode 16-bit timer mode 8-bit auto reload
8-bit THx + 5-bit TLx (x= 0 or 1) 8-bit THx + 8-bit TLx 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows.
TMOD Register
Timer modes
TCON Register
TCON :Timer control register: Upper nibble for timer/counter, lower nibble for interrupts. TR (run control bit) TR0 for Timer/counter 0; TR1 for Timer/counter 1. TR is set by programmer to turn timer/counter on/off. TR=0: off (stop) TR=1: on (start) TF (timer flag, control flag) TF0 for timer/counter 0; TF1 for timer/counter 1. TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000 from FFFFH, the TF is set to 1. If we enable interrupt, TF=1 will trigger ISR.
IE1
(LSB) IT0
TR0=1
4. Keep monitoring the timer flag (TF) to see if it is raised, Get out of the loop when TF becomes high. here: JNB TF0,here while(!TF0); or while(TF0==0); 5. Stop the timer & clear the overflow flag for next operation CLR TR0 TR0=0 CLR TF0 TF0=0 7. Go back to Step 2 to load TH and TL again.
The timer works with a clock frequency of 1/12 of the XTAL frequency; therefore, we have
11.0592 MHz / 12 = 921.6 kHz as the timer frequency. As a result, each clock has a period T = 1/921.6kHz = 1.085us. In other words, Timer increments for every1.085 us resulting in delay = number of counts 1.085us. STEPS: Assume that XTAL = 11.0592 MHz . And we know desired delay how to find the values for the TH,TL ? 1. Divide the delay by 1.085 s and get n. 2. Perform 65536 n 3. Convert the result of Step 2 to hex (yyxx ) 4. Set TH = yy and TL = xx.
Program to generate 500hz square wave on P2.0 Assuming XTAL = 11.0592 MHz, 8051
2ms P2.0
1ms
1 / 500 = 2ms.
Delay: MOV TH0,#0FCh MOV TL0.#66h SETB TR0 Here: JNB TF1, Here CLR TR0 CLR TF0 RET END
Serial Communication
Serial Communication
Computers transfer data in two methods:
Parallel Often 8 or more lines are used to transfer the data in chunks of bytes. Hence a lot of data is transmitted. Used for short distance communication. Expensive Ex: Computer to Printer Serial Here the data is transmitted bit by bit on a single wire. Used for long distance communication. Cheaper compared to parallel. Ex: Internet.
Asynchronous : Here both transmitter and receiver generate their own clock(Baud-rate).
Serial Communication
Basics of serial communication:
At the transmitting end, the byte of data must be converted to serial bits using parallel-in-serial-out shift register. serial data out At the receiving end, there is a serial-in-parallel-out shift register to receive the serial data and pack them into byte. Parallel data in
Shift Register
Clock
Shift Register
Clock
serial data in
Parallel data out
When the distance is short, the digital signals can be transferred as it is on a simple wire and requires no modulation. Ex: keyboard,mouse If data is to be transferred on the telephone line, it must be converted from 0s and 1s to audio tones, this conversion is performed by a device called a modem, Modulator/demodulator. Ex: internet
Serial Communication
Packaging (Framing):
Block-oriented data transfers use the synchronous method where as asynchronous serial data communication is widely used for characteroriented transmissions. Each character is placed in between start and stop bits, this is called framing The start bit is always one bit, but the stop bit can be one or two bits. Start and Stop Bits: The start bit is always a 0 (low) and the stop bit(s) is 1 (high)
RS232 Standard
Introduced in 1960 by Electronics Industries Association (EIA). Logic 1 is represented by -3 to -25 volt Logic 0 is represented by 3 to 25 volt To Connect TXD to RXD and RXD to TXD from pc to 8051 we must use max232 to convert signal from TTL level to RS232 level
1 2 3 4 5 6 7 8 9
Serial Communication
Baud Rate and Bit Rate:
Baud Rate: It is defined as the number of signal changes per second. In modems there are occasions where a single change of signal transfers several bits of data. Bit Rate: It is defined as the number of bits transferred per second. Both baud rate and bit rate are measured in bits per second(bps). To allow the data transfer between two systems(8051 & PC) without any error, the baud rate of two systems should match. Some of the standard baud rates supported by IBM PC:
Serial Communication
Baud Rate Generation:
Serial Communication
SBUF Register
SBUF is an 8-bit register used solely for serial communication. For a byte of data to be transferred via theTxD line, it must be placed in the SBUF register the moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD line. SBUF holds the byte of data when it is received by 8051 RxD line. When the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data received, and then placing it in SBUF. MOV MOV MOV SBUF,#A SBUF,A A,SBUF ;load SBUF=41H(65), ASCII of A ;copy accumulator into SBUF ;copy SBUF into accumulator
SM0
SM1
SM2
REN
TB8
RB8
operation shift register 8-bit data 1 start and 1 stop bit. 9 bit UART 9 bit UART
TI
RI
1 1
0 1
2 3
SM2 : used for multi processor communication. REN : receive enable (by software enable/disable).
Serial Communication
Steps to transfer character bytes serially:
1. Configure Timer1 in mode2(8-bit auto-reload) to generate the baud rate. TMOD=0x20; 2. The TH1 is loaded with one of the values to set baud rate for serial data transfer. TH1=0xFD; or TH1=-3; for 9600 baud rate. 3. Configure SCON register in mode1(8-bit data, 1-start and 1-stop bit). SCON=0x50;
Serial Communication
Program to transfer letter A serially at 9600 baud, continuously.
MOV TMOD,#20H ; MOV TH1,# -3 ; MOV SCON,#50H ; SETB TR1 ; AGAIN: MOV SBUF,#A ; HERE: JNB TI,HERE ; CLR TI ; SJMP AGAIN ;
Void main() { TMOD=0x20; TH1=0xfd; SCON=0x50; TR1=1; while(1) { SBUF=A; while(TI==0); TI=0; } }
timer 1,mode 2(auto reload) 9600 baud rate 8-bit, 1 stop, REN enabled start timer1 to generate the baud rate letter A to be transmitted wait till data is transmitted clear TI for next char keep sending A
Serial Communication
Steps to Receive and transfer character bytes serially:
1. Configure Timer1 in mode2(8-bit auto-reload) to generate the baud rate. 2. The TH1 is loaded with one of the values to set baud rate for serial data transfer. 3. Configure SCON register in mode1(8-bit data, 1-start and 1-stop bit). 4. Start timer1 to generate the baud rate.
Serial Communication
Program to receive and transfer characters serially at 9600 baud, continuously.
MOV TMOD,#20H ; MOV TH1,# -3 ; MOV SCON,#50H ; SETB TR1 ; HERE: JNB RI,HERE; CLR RI ; MOV A, SBUF; AGAIN: MOV SBUF, A ; WAIT : JNB TI,WAIT ; CLR TI ; SJMP HERE; Void main() { char ch; TMOD=0X20; TH1=0XFD; SCON=0X50; TR1=1; while(1) { while(RI==0); RI=0; ch=SBUF; SBUF=ch; while(TI==0); TI=0; } } timer 1,mode 2(auto reload) 9600 baud rate 8-bit, 1 stop, REN enabled start timer1 to generate the baud rate wait till data is received clear RI to receive next char copy the received char into accumlator. transmit the received char. wait till data is transmitted. clear TI for next char continue the process
Interrupts Programming
Interrupts Programming
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
Polling:
The microcontroller continuously monitors the status of the device to check whether it needs service and provides the service if required. After that it moves on to monitor the next device until every one is serviced. The polling method is not efficient, since it wastes much of the controllers time by polling devices that do not need service.
Interrupts:
Here the device itself informs (interrupts) the controller whenever it needs the service. Each device can get the attention of the controller based on the priority. Where as Polling uses Round-Robin fashion hence it is not possible to assign the priority.
Interrupts Programming
Classification of interrupts: Interrupts can be classified as
For every interrupt, there must be an interrupt service routine (ISR), or interrupt handler.
When an interrupt is invoked, the microcontroller runs the interrupt service routine. For every interrupt, there is a fixed location in memory that holds the address of its ISR. The group of memory locations set aside to hold the addresses of ISRs is called interrupt vector table.
Interrupts Programming
Six interrupts are allocated as follows:
Reset : power-on- reset. Two interrupts are set aside for the timers one for timer 0 and one for timer 1. Two interrupts are set aside for hardware external interrupts. P3.2 and P3.3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2). Serial communication has a single interrupt that belongs to both receive and transfer.
Interrupt Vectors:
Each interrupt has a specific place in code memory where program execution (interrupt service routine) begins.
Interrupts Programming
Interrupt Enable (IE) register
Upon reset, all interrupts are disabled (masked), meaning that none will be responded to by the microcontroller if they are activated. The interrupts must be enabled by software in order for the microcontroller to respond to them. There is a register called IE (interrupt enable) that is responsible for enabling (unmasking)
Interrupts Programming
By bit operation
SETB SETB SETB SETB SETB SETB EA ET0 ET1 EX0 EX1 ES SETB SETB SETB SETB SETB SETB IE.7 IE.1 IE.3 IE.0 IE.2 IE.4 EA=1 ET0=1 ET1=1 EX0=1 EX1=1 ES=1 ;Enable ;Enable ;Enable ;Enable ;Enable ;Enable All Timer0 ovrf Timer1 ovrf INT0 INT1 Serial port
ORG
0000H LJMP MAIN ORG 000BH LJMP T0ISR ORG 001BH LJMP T1ISR ORG MAIN: 0030H MOV MOV MOV MOV MOV SETB SETB MOV SJMP T0ISR: MOV MOV CPL RETI MOV MOV CPL RETI END
Asm program to generate 500hz and 2khz square waves on P1.0 & P1.1
8051
P1.1
0.5ms
0.25ms
TMOD,#11H TH0,#0FCh TL0,#066h TH1,#0FFh TL1,#019h TR0 TF1 IE,#8AH $ TH0,#0FCh TL0,#066h P1.0
2ms
1ms
P1.0
T1ISR:
Notice that There is no need for a CLR TFx instruction in timer ISR ,8051 automatically clears the TF internally upon jumping to ISR.
C program to generate 500hz and 2khz square waves on P1.0 & P1.1 simultanously
#include<reg51.h> Sbit Sqr1=P1^0; Sbit sqr2=P1^1; Void timer0_isr() interrupt 1 { sqr1=!sqr1; TH0=0xFC; TL0=0x66; } Void timer1_isr() interrupt 3 { sqr2=!sqr2; TH1=0xFF; TL1=0x19; } Void main() { TMOD=0x11; TH0=0xFC; TL0=0x66; TH1=0xFF; TL1=0x19; IE=0x8A; while(1); }
Timer calculation for 500hz: 1. T = 1 / 500 = 2 ms, period of square wave. 2. 1 / 2 of it for the high and low portion of the pulse is 1 ms. 3. 1ms / 1.085 us = 9216 and 65536 9216 = 56320 in decimal, and in hex it is DC00H. 4. TL = 00 and TH = DC (hex).
Timer calculation for 2khz: 1. T = 1 / 2000 = .5 ms, period of square wave. 2. 1 / 2 of it for the high and low portion. ie pulse width=0.25 ms. 1. 0.25ms / 1.085 us = 9216 and 65536 9216 = 56320 in decimal, and in hex it is DC00H. 2. TL = 00 and TH = DC (hex).
Led Interfacing
LED (Light emitting diode): It is a type of diode which emits the light whenever it is connected in forward bias.Led can be Connected in two fashions ie.common anode & common cathode. Common cathode: Here all the cathode terminals of leds are shorted(made common) and connected to GND, so to turn on a particular led a logic 1(5V) has to be applied.
VCC(5v)
GND(0v)
Common cathode
Common anode.
Common anode: Here all the anode terminals of leds are shorted(made common) and connected to VCC, so to turn on a particular led a logic 0(0V) has to be applied.
void delay(unsigned int n) { while(n--); } void main() { while(1) { LED=0xff ; //turn on the leds delay(50000);
LED=0x00 ; //turn off the leds delay(50000); }
he x 3f 30 5b 4f 66 6d
Common Anode
Common Cathode.
0110
0111 1000 1001
a f g a
01111101
00000111 01111111 01101111
a b f g d b f a g d
7d
07 7f 6f
b
e
d
e
d
LCD Interfacing
1. 2. Liquid Crystal Displays (LCDs) cheap and easy way to display all letters of alphabet, greek letters, punctuation marks, mathematical symbols etc. Available in Various configurations (1 line by 20 char upto 8 lines X 80 chars ).
8 data pins D7:D0 RS: Data or Command Register Select R/W: Read or Write E: Enable (Latch data)
3.
Pinout
Microcontroller
E
R/W
RS DB7DB0
communications bus
Used to latch the data present on the data pins. Bi-directional data/command pins. Alphanumeric characters are sent in ASCII format.
8
LCD controller
LCD Module
LCD Interfacing
LCD Commands
4.
Send the ASCII value of data to lcd databus. databus=dat; Select the Data Register. RS=1; Write operation. RW=0; High-to-Low pulse at the enable pin. EN=1 delay(); EN=0;