Sie sind auf Seite 1von 78

Contents:

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

Necessary tools for processor/controller

CPU: Central Processing Unit I/O: Input /Output Bus: Address bus & Data bus Memory: RAM & ROM Timer Interrupt Serial Port Parallel Port

Microprocessors & Microcontrollers


Microprocessor Stand alone general purpose processors No RAM, ROM, I/O on CPU chip itself ExampleIntels x86, Motorolas 680x0

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.

Von-Neumann vs Harvard Architecture

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,

Memory unit: Memory is any physical device to store data.

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.

Three criteria in chossing a Microcontroller


1. meeting the computing needs of the task efficiently and cost effectively speed, the amount of ROM and RAM, the number of I/O ports and timers, size, packaging, power consumption easy to upgrade cost per unit 2. availability of software development tools assemblers, debuggers, C compilers, emulator, simulator, technical support 3. wide availability and reliable sources of the microcontrollers.

8051 Basic Component


8 bit controller 4KB of internal ROM and 128 bytes of RAM memory. Four 8bit I/O ports(P0,P1,P2,P3). Two 16 timers/counters. One serial port(UART). 8bit data bus and 16bit address bus hence can access 64k of external memory. 6 Interrupts

8051 family of controllers


Feature ROM RAM (Bytes) TIMERS I/O PINS SERIAL PORTS
INTERRUPT SOURCES

8031
NO

128 2 32 1 6

8051 4kB 128 2 32 1 6

8052 8kB 256 3 32 1 8

8751 4kB UV Eprom 128 2 32 1 6

8951 4KB FLASH 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

8051 Block diagram

Pin Diagram of 8051

Pin Description of 8051


P0: Can be used as 8bit port or multiplexed lower 8bit data and address bus. P1:Can be used as simple 8bit I/O port. P2: Can be used as 8bit port or higher 8bit address bus. P3:It is multifunctional port used as normal port or as special port for serial communication, interrupts, timers and for external read write signals. VCCVcc provides supply voltage(+5v) to the chip. GNDground(0v) XTAL1 & XTAL2: A quartz crystal oscillator is connected accross XTAL1 &XTAL2, which provides the clock source for the controller.

Pin Description of 8051


RESET: It is an active high (normally low) input signal used to reset

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).

Reset values of some 8051 Registers


Register PC Reset Value 0000

ACC B PSW SP
DPTR RAM are all zero.

00 00 00 07
0000

Pin Description of 8051


EA : External access is an input pin either connected to VCC or GND. If EA is connected to GND then the controller fetches the code from the external memory else from the internal ROM memory. PSEN: Program store enable is the output pin connected to the OE pin of the ROM memory in which the Program is stored. ALE: Address Latch enable is output pin used to demultiplex the lower 8bit address and data bus.

A Pin of Port1

Read latch
TB2

Vcc
Load(L1)

Internal CPU bus Write to latch

P1.X
Clk Q

P1.X pin M1

TB1 Read pin

P0.x
8051 IC

Writing 1 to Output Pin P1.x

Writing 0 to Output Pin P1.x

Reading high at Intput Pin P1.x

Reading low at Intput Pin P1.x

Read latch

A Pin of Port0
TB2
D Q

Internal CPU bus Write to latch

P1.X M1
Clk Q

P0.X pin

TB1 Read pin

Port0 with Pull Up Resistors


Vcc 10 K

P0.0 DS5000 P0.1 P0.2 8751 P0.3 P0.4 8951 P0.5 P0.6 P0.7

Port

Programmers Model Of 8051


Some 8-bit Registers of the 8051
A B R0 R1 R2 R3 PC

Some 8051 16-bit Register


DPTR DPH
DPL

R4
R5 R6 R7

PSW:Program Status Word


CY AC F0 RS1 RS0 OV _ P

Memory mapping of 8051


ROM memory map in 8051 family
4k
0000H 0000H

RAM memory map in 8051 family


7FH

Scratch pad RAM

8k
30H 2FH

Bit-Addressable RAM
0FFFH 1FFFH 20H 1FH

8751 AT89C51

Register Bank 3 Register Bank 2 Register Bank 1

8752 AT89C52

18H 17H 10H 0FH

Atmel Corporation

08H 07H

Register Bank 0
00H

SFR(special function registers

* indicates Bit addressable

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

8051 Instruction Set

Data transfer instructions


Immediate mode
Mov A, #data Mov B, #data Mov Rn, #data Mov DPTR, #data(16bit)

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

Program control/Branching instructions


Unconditional Jump
JMP @A+DPTR SJMP reladdr AJMP codeaddr LJMP codeaddr LCALL codeaddr ACALL codeaddr RET RETI

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.

Registers Used in Timer/Counter


1. 2. 3. TH0, TL0, TH1, TL1 (To hold the timer value) TMOD (Timer mode register) TCON (Timer control register)

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

Timer/Counter Block Diagram

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.

Split timer mode

TMOD Register

GATE: Every timer has a mean of starting and stopping.


GATE=0 Internal control The start and stop of the timer are controlled by way of software. GATE=1 External control Timer/counter is enabled only while the INT pin is high and the TR control pin is set (TR).

C/T: Timer or counter selected .


Cleared for timer operation (input from internal system clock). Set for counter operation (input from Tx input pin).

Mode0: 13-bit mode

Timer modes

Mode1: 16-bit mode

Mode2: 8-bit autoreload.

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.

(MSB) TF1 TR1 Timer 1

TF0 TR0 Timer0

IE1

IT1 IE0 for Interrupt

(LSB) IT0

Steps for timer programming


1.Configure the TMOD register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or 1) is selected. MOV TMOD,#01h TMOD=0x01 2. Load registers TL and TH with initial count value. MOV TH0,#12 TH0=0x01 MOV TL0,#34 TL0=0x34 3. Start the timer. SETB TR0

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.

Timer delay calculation

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

Solution: 1. The period of the square wave

2ms P2.0
1ms

1 / 500 = 2ms.

2. The high or low portion of the square wave = 1 ms.


3. 1ms / 1.085 s = 921 4. 65536 921 = 64614 in decimal = FC66 in hex.

5. TL1 = 0x66h and TH1 = 0xFCh.

Program to generate 500hz square wave on P2.0


ORG Back: 0000H MOV TMOD,#01H SETB P2.0 LCALL delay CLR P2.0 LCALL Delay SJMP Back #include<reg51.h> Sbit sqr_wave=P2^0; Void delay() { TH0=0xFC; TL0=0x66; TR0=1; while(!TF0); TR0=0; TF0=0; } Void main() { TMOD=0x11; while(1) { sqr_wave=1; delay(); sqr_wave=0; delay(); } }

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.

Serial Communication Modes of communication


Simplex: Here the data is transmitted in only one
direction. Ex: Printer Half Duplex :Here the data is transmitted in both the directions but not at the same time. Ex: Walkie-Talkie Full Duplex: Here the data is transmitted in both the directions at the same time. Ex: mobile

Serial data communication uses two methods:


Synchronous: Here both transmitter and receiver are synchronized with respect to a clock. It is used to transmit a block of data.

Asynchronous : Here both transmitter and receiver generate their own clock(Baud-rate).

Universal Serial Protocols:


USRT: Universal Synchronous Receiver Transmitter. UART: Universal Asynchronous Receiver Transmitter.

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

DCD RD TD DTR GND DSR RTS CTS RI

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

Serial control (SCON) Register


7 6 5 4 3 2 1 0

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

SM0 : mode specifier SM1 : mode specifier

SM0 SM1 MODE 0 0 0 0 1 1

transmit rate fixed (xtal/12) variable (timer1)

1 1

0 1

2 3

fixed (xtal/32 or xtal/64) variable (timer1)

SM2 : used for multi processor communication. REN : receive enable (by software enable/disable).

TB8 : transmit bit 8.


RB8 : receive bit 8. TI RI : transmit interrupt flag, set by HW once data is transmitted, and cleared by SW : receive interrupt flag, set by HW after data is received ,and cleared by SW

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;

4. Start timer1 to generate the baud rate. TR1=1;


5. Load the character byte to be transferred serially into SBUF register. SBUF=A; or SBUF=65 ;SBUF is loaded with the ascii value of A. 6. Wait till data is transmitted(ie Wait till TI becomes 1). here: JNB TI, here or while(!TI) or while(TI==0); 7.Clear TI and go to step 5 to transfer the next byte.

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.

5. Wait till data is received (ie Wait till RI becomes 1).


6. Clear RI and copy the received data from SBUF into accumlator. 7. Load the received data into SBUF register for transmission. 8. Wait till data is transmitted(ie Wait till TI becomes 1). 9.Clear TI and go to step 5 to continue the process.

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.

Interrupts vs. Polling


A single microcontroller can serve several devices. There are two ways to do that:

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

1: Maskable and Non-Maskable interrupts


Maskable interrupts : The service can be delayed or rejected by the controller just by disabling the interrupts. Non-Maskable interrupts : The service cannot be rejected but it may be delayed depending on the priority of the other interrupts.

2: Vectored and non-vectored interrupts.


Vectored interrupts : The address of the interrupt service routine is hardwired or fixed. Non- Vectored interrupts : Here the device has to provide the address of the service routine.

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.

Steps in executing an interrupt


Upon activation of an interrupt, the microcontroller goes through the following steps
It finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack It also saves the current status of all the interrupts internally (i.e: not on the stack) It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of the ISR. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it and starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt) Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted. First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack into the PC and then it starts to execute from that address

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

By mov instruction MOV IE, #10010110B;

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:

TH1,#0FFh TL1,#19h P1.1

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.

Program to blink the Led continuously


#include<reg51.h> #define LED P2

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); }

Seven Segment Display


1. It is composed of 8 LEDs arranged as a rectangle.
2. Segments are marked with the letters from a to g, plus dp, as shown in figure. 3. Here the chars are displayed just by turning on/off the respective leds. To display a particular digit a seven segment code has to be generated and sent to the port as shown in the figure.To display 5 we have to turn ON a,c,d,f,g and OFF b,e,dp(Assuming the segment to be of common cathode). 4. Useful for displaying the digits 0 through 9, and some characters.

Seven Segment Display Code for common cathode


7-segment displays come in 2 configurations:

Common cathode: Logic 1 (5v) should be provided to turn on a particular segment.

BCD 0000 0001 0010 0011 0100 0101

pgfedcba 7_seg 001111 11 00110000 0101101 1 010011 11 011001 10 01101101

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

Common anode: Logic 0 (0v) should be provided to turn on a particular segment.


a f b f e e d d a g b a g b c f g b c d f a g

e
d

e
d

Program to display decimal upcounter(0-9)


#include<reg51.h> #define seven_segment P2
void main() { char a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f}; unsigned int i,j; while(1) { for(i=0;i<=9;i++) { seven_segment=a[i]; for(j=0;j<35000;j++); //delay } } }

Connecting 4-Seven Segment Display

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

RS Register Select R/W Read/Write E Enable D0 D7


R/W = 0 write R/W= 1 Read

RS = 0 Command Register RS = 1 Data Register

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

Program to display hello, world on lcd


#include<reg51.h> #define databus P2 sbit rs= P0^0; sbit rw= P0^1; sbit en= P0^2; void lcd_cmd( char cmd) { unsigned int i; databus=cmd; rs=0; rw=0; en=1; for(i=0;i<250;i++); void main() en=0; { } char a[]={"hello,world"},i; void lcd_dat( char dat) lcd_cmd(0x38); { lcd_cmd(0x0e); unsigned int i; lcd_cmd(0x01); databus=dat; lcd_cmd(0x80); rs=1; rw=0; while(a[i]) en=1; lcd_dat(a[i++]); for(i=0;i<250;i++); en=0; while(1); } }

Steps to send command to lcd:


1. 2. 3. 4. Send the command to lcd databus. databus=cmd; Select the command Register. RS=0; Write operation. RW=0; High-to-Low pulse at the enable pin. EN=1 delay(); EN=0;

Steps to send Data to lcd:


1.
2. 3.

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;

Das könnte Ihnen auch gefallen