final 8051 printout

Upload: arun-jyothi

Post on 14-Apr-2018

226 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/27/2019 Final 8051 Printout

    1/78

  • 7/27/2019 Final 8051 Printout

    2/78

    IntroductionBlock Diagram and Pin Description of the 8051

    Registers

    Memory mapping in 8051

    Stack in the 8051

    I/O Port ProgrammingTimer

    Serial Communication

    Interrupt

    Contents:

  • 7/27/2019 Final 8051 Printout

    3/78

    CPU: Central Processing Unit

    I/O: Input /Output

    Bus: Address bus & Data bus

    Memory: RAM & ROM

    Timer

    Interrupt

    Serial Port

    Parallel Port

    Necessary tools for processor/controller

  • 7/27/2019 Final 8051 Printout

    4/78

    Stand alone general purpose processors No RAM, ROM, I/O on CPU chip itself

    ExampleIntels x86, Motorolas 680x0

    Microprocessors & Microcontrollers

    A smaller computer

    On-chip RAM, ROM, I/O ports...

    ExampleMotorolas 6811, Intels 8051,Zilogs Z8 and PIC 16X

    Microprocessor

    Microcontroller

  • 7/27/2019 Final 8051 Printout

    5/78

    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

    Microprocessors vs Microcontrollers

  • 7/27/2019 Final 8051 Printout

    6/78

    Complex instruction set computer

    Large number of complex

    instructions, take more clockcycles 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 asingle 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 tocomplete 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 thecycles per instruction at the cost of the number of instructions per program.

    CISC vs RISC

  • 7/27/2019 Final 8051 Printout

    7/78

  • 7/27/2019 Final 8051 Printout

    8/78

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

    emory un : Memory is any physical device to store data.

  • 7/27/2019 Final 8051 Printout

    9/78

    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 unit2. availability of software development tools

    assemblers, debuggers, C compilers, emulator, simulator,

    technical support

    3. wide availability and reliable sources of the microcontrollers.

    Three criteria in chossing a Microcontroller

  • 7/27/2019 Final 8051 Printout

    10/78

    8051 Basic Component

    Feature 8031 8051 8052 8751 8951ROM NO 4kB 8kB 4kB UV Eprom 4KB FLASRAM (Bytes) 128 128 256 128 128TIMERS 2 2 3 2 2I/O PINS 32 32 32 32 32SERIAL PORTS 1 1 1 1 1

    INTERRUPT 6 6 8 6 6SOURCES

    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

    l k

  • 7/27/2019 Final 8051 Printout

    11/78

    CPU

    On-chip

    RAM

    On-chip

    ROM for

    program

    code

    4 I/O Ports

    Timer 0

    Serial

    PortOSC

    Interrupt

    Control

    External interrupts

    Timer 1

    Timer/Counter

    Bus

    Control

    TxD RxDP0 P1 P2 P3

    Address/Data

    Counter

    Inputs

    Block Diagram

  • 7/27/2019 Final 8051 Printout

    12/78

    8051 Block diagram

    Pi Di f 8051

  • 7/27/2019 Final 8051 Printout

    13/78

    Pin Diagram of 8051

    Pi D i ti f 8051

  • 7/27/2019 Final 8051 Printout

    14/78

    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.

    GND

    ground(0v)

    XTAL1 & XTAL2: A quartz crystal

    oscillator is connected accross XTAL1

    &XTAL2, which provides the clock source for the controller.

    Pin Description of 8051

    Pi D i ti f 8051

  • 7/27/2019 Final 8051 Printout

    15/78

    DPTR 0000

    Reset ValueRegister

    RAM are all zero.

    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 location0000h), this is often referred as power on reset(POR).

    SP 07PSW 00

    B 00

    ACC 00PC 0000

    Pin Description of 8051

    Reset values of some 8051 Registers

  • 7/27/2019 Final 8051 Printout

    16/78

  • 7/27/2019 Final 8051 Printout

    17/78

    8051 IC

    D Q

    Clk Q

    Vcc

    Load(L1)

    Readlatch

    Read pin

    Write to latch

    Internal CPU

    bus

    M1

    P1.X

    pinP1.X

    TB1

    TB2

    P0.x

    A Pin of Port1

    W itin 1 t O tp t Pin P1 x

  • 7/27/2019 Final 8051 Printout

    18/78

    Writing 1 to Output Pin P1.x

    Writing 0 to Output Pin P1.x

    Reading high at Intput Pin P1 x

  • 7/27/2019 Final 8051 Printout

    19/78

    Reading high at Intput Pin P1.x

    Reading low at Intput Pin P1.x

  • 7/27/2019 Final 8051 Printout

    20/78

    D Q

    Clk Q

    Read latch

    Read pin

    Write to latch

    Internal CPUbus

    M1

    P0.X

    pinP1.X

    TB1

    TB2

    P0.0P0.1P0.2P0.3P0.4P0.5P0.6P0.7

    DS5000

    8751

    8951

    Vcc10 K

    Port

    0

    A Pin of Port0

    Port0 with Pull Up Resistors

  • 7/27/2019 Final 8051 Printout

    21/78

    A

    B

    R0

    R1

    R3

    R4

    R2

    R5

    R7

    R6

    DPH DPL

    PC

    DPTR

    Some 8051 16-bit RegisterSome 8-bit Registers of

    the 8051

    CY AC F0 RS1 RS0 OV _ P

    PSW:Program Status Word

    Programmers Model Of 8051

  • 7/27/2019 Final 8051 Printout

    22/78

    ROM memory map in 8051

    family

    0000H

    0FFFH

    0000H

    1FFFH

    8751

    AT89C518752

    AT89C52

    4k 8k

    AtmelCorporation

    7FH

    30H

    2FH

    20H

    1FH

    17H10H

    0FH

    07H

    08H

    18H

    00H

    Register Bank 3

    Bit-Addressable RAM

    Scratch pad RAM

    Register Bank 2

    Register Bank 1

    Register Bank 0

    RAM memory map in

    8051 family

    Memory mapping of 8051

    SFR(special function re isters

  • 7/27/2019 Final 8051 Printout

    23/78*indicates Bit addressable

    SFR(special function registers

    Stack of 8051

  • 7/27/2019 Final 8051 Printout

    24/78

    Stack is a part of RAM used by CPU to store the information. This information could be dataor 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 toFFH. 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. Withevery pop, the top byte of the stack is copied to the register specified by the

    instruction and the stack pointer is decremented once

    Stack of 8051

    Programming Environment

  • 7/27/2019 Final 8051 Printout

    25/78

    Programming Environment

  • 7/27/2019 Final 8051 Printout

    26/78

    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.

    Addressing modes

    8051 I t ti S t

  • 7/27/2019 Final 8051 Printout

    27/78

    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 inbranching 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

    D t t f i t ti

  • 7/27/2019 Final 8051 Printout

    28/78

    Immediate mode

    MovA, #dataMov B, #data

    Mov Rn, #data

    Mov DPTR, #data(16bit)

    Direct modeMovA, addr

    Mov addr, A

    Mov Rn, addr

    Mov addr, Rn

    Register modeMovA, Rn

    Mov Rn, A

    Indirect mode

    MovA, @RiMov @Ri, AMov @Ri, #dataMov @Ri, addr

    Mov A, @DPTRMov @DPTR, AMovc A, @A+DPTRMovc A, @A+PC

    Movx A, @DPTRMovx A, @RiMovx @DPTR, AMovx @Ri, A

    Data transfer instructions

    A ith ti i t ti

  • 7/27/2019 Final 8051 Printout

    29/78

    Immediate modeADDA, #data

    SUBB A, #data

    ADDC A, #data

    SUBB A, #data

    Direct modeADDA, addr

    SUBB A, addr

    INC addr

    DEC addr

    Register modeADD A, B

    ADD A, Rn

    ADDC A, B

    SUBB A, B

    MUL AB

    DIV AB

    INC A

    INC Rn (n=0-7)

    DEC

    Indirect modeADDA, @Ri

    ADD A, @DPTR

    ADDCA, @RiINC @Rn (n=0 or 1)

    DEC @Rn

    Arithmetic instructions

    L i l i t ti

  • 7/27/2019 Final 8051 Printout

    30/78

    Immediate mode

    ANL A, #dataORL A, #data

    XRLA, #data

    Direct modeANL A, Addr

    ORL A, Addr

    XRL A, Addr

    ANL Addr, A

    ORL Addr, A

    XRL Addr, ARegister modeANL A, RnORL A, Rn

    XRL A, Rn

    RL A

    RR ARLC A

    RRC A

    SWAP A

    Indirect modeANL A, @Rn (n=0 or 1)

    ORL A, @Rn

    XRL A, @Rn

    Logical instructions

    P t l/B hi i t ti

  • 7/27/2019 Final 8051 Printout

    31/78

    Conditional JumpCJNE A, #data, rel

    CJNE Rn, #data, relCJNE @Ri, #data, rel

    CJNE A, Addr, rel

    JZ/JNZ reladdrJC/JNC reladdr

    JB/JNB bitaddr, reladdr

    DJNZ Rn, reladdr

    DJNZ direct, reladdr

    Unconditional JumpJMP @A+DPTR

    SJMP reladdr

    AJMP codeaddr

    LJMP codeaddr

    LCALL codeaddr

    ACALL codeaddr

    RET

    RETI

    Program control/Branching instructions

  • 7/27/2019 Final 8051 Printout

    32/78

  • 7/27/2019 Final 8051 Printout

    33/78

    8051

    timer/counter

    Tim s/C nt s P mmin

  • 7/27/2019 Final 8051 Printout

    34/78

    The 8051 has 2 timers(Timer0 & Timer1),they can be used as

    1. Timers to generate delays or.

    2. Event counters to count events happening outside the microcontroller.Theseevents could represent the number of people passing through an entrance, orthe number of wheel rotations, or any other event that can be converted topulses.

    Registers Used in Timer/Counter

    1. TH0, TL0, TH1, TL1 (To hold the timer value)

    2. TMOD (Timer mode register)3. TCON (Timer control register)

    Both Timer 0 and Timer 1 are 16 bits wide,Since 8051 has an 8-bitarchitecture, each16-bits timer is accessed as two separate registers of lowbyte and high byte.These can be accessed like any other 8-bit registers.

    MOV TL0,#26hMOV TH0,#50h

    MOV TL1,#0ABh

    MOV TH1,#0FFh

    Timers/Counters Programming

    Timers/Counters Pro rammin

  • 7/27/2019 Final 8051 Printout

    35/78

    Timers/Counters Programming

    Timer/Counter Block Diagram

  • 7/27/2019 Final 8051 Printout

    36/78

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

    Timer/Counter Block Diagram

  • 7/27/2019 Final 8051 Printout

    37/78

  • 7/27/2019 Final 8051 Printout

    38/78

    Timer modes

  • 7/27/2019 Final 8051 Printout

    39/78

    Timer modes

    Mode1: 16-bit mode

    Mode0: 13-bit mode

    Mode2: 8-bit autoreload.

    TCON Register

  • 7/27/2019 Final 8051 Printout

    40/78

    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.

    TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

    Timer 1 Timer0 for Interrupt

    (MSB) (LSB)

    TCON Register

    Steps for timer programming

  • 7/27/2019 Final 8051 Printout

    41/78

    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.

    Steps for timer programming

    Timer delay calculation

  • 7/27/2019 Final 8051 Printout

    42/78

    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 n3. Convert the result of Step 2 to hex (yyxx )4. Set TH = yy and TL = xx.

    Timer delay calculation

    Program to generate 500hz square wave on P2.0

  • 7/27/2019 Final 8051 Printout

    43/78

    g m g q .

    1ms

    2ms

    8051

    P2.0

    Assuming XTAL = 11.0592 MHz,

    Solution:1. The period of the square wave1 / 500 = 2ms.2. The high or low portion of thesquare wave = 1 ms.3. 1ms / 1.085 s = 9214. 65536 921 = 64614 in decimal= FC66 in hex.5. TL1 = 0x66h and TH1 = 0xFCh.

    Program to generate 500hz square wave on P2.0

  • 7/27/2019 Final 8051 Printout

    44/78

    g g q

    ORG 0000HMOV TMOD,#01H

    Back: SETB P2.0LCALL delayCLR P2.0LCALL DelaySJMP Back

    Delay: MOV TH0,#0FCh

    MOV TL0.#66hSETB TR0

    Here: JNB TF1, HereCLR TR0CLR TF0RET

    END

    #includeSbit 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();

    }

    }

  • 7/27/2019 Final 8051 Printout

    45/78

    Serial

    Communication

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    46/78

    Serial Communication

    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.

    Computers transfer data in two methods:

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    47/78

    Serial CommunicationModes of communication

    Simplex: Here the data is transmitted in only one

    direction. Ex: PrinterHalf 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 aclock. 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

  • 7/27/2019 Final 8051 Printout

    48/78

    Serial CommunicationBasics of serial communication: At the transmitting end, the byte of data

    must be converted to serial bits usingparallel-in-serial-out shift register.

    At the receiving end, there is aserial-in-parallel-out shift register

    to receive the serial data

    and pack them into byte.

    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 amodem, Modulator/demodulator. Ex: internet

    Shift Register Clock

    Shift Register Clock

    Parallel data in

    serial data out

    serial data in

    Parallel data out

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    49/78

    Serial CommunicationPackaging (Framing):

    Block-oriented data transfers use the synchronous method where as

    asynchronous serial data communication is widely used for character-oriented 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 St d d

  • 7/27/2019 Final 8051 Printout

    50/78

    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 levelto RS232 level

    1 DCD

    2 RD

    3 TD

    4 DTR

    5 GND

    6 DSR

    7 RTS

    8 CTS

    9 RI

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    51/78

    Serial CommunicationBaud 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 transfersseveral 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 anyerror, the baud rate of two systems should match.

    Some of the standard baud rates supported by IBM PC:

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    52/78

    Serial CommunicationBaud Rate Generation:

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    53/78

    Serial CommunicationSBUF 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 SBUF,#A ;load SBUF=41H(65), ASCII of A

    MOV SBUF,A ;copy accumulator into SBUF

    MOV A,SBUF ;copy SBUF into accumulator

    S i l t l (SCON) R i t

  • 7/27/2019 Final 8051 Printout

    54/78

    Serial control (SCON) Register

    SM2: used for multi processor communication.

    REN: receive enable (by software enable/disable).

    TB8 : transmit bit 8.RB8 : receive bit 8.

    TI : transmit interrupt flag, set by HW once data is transmitted, and cleared by SW

    RI : receive interrupt flag, set by HW after data is received ,and cleared by SW

    SM0 RITIRB8TB8RENSM2SM1

    7 6 5 4 3 2 1 0

    SM0 SM1 MODE operation transmit rate0 0 0 shift register fixed (xtal/12)0 1 1 8-bit data 1 start variable (timer1)

    and 1 stop bit.1 0 2 9 bit UART fixed (xtal/32 or xtal/64)1 1 3 9 bit UART variable (timer1)

    SM0: mode specifier

    SM1: mode specifier

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    55/78

    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

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    56/78

    Program to transfer letter A serially at 9600 baud, continuously.MOV TMOD,#20H ; timer 1,mode 2(auto reload)

    MOV TH1,# -3 ; 9600 baud rate

    MOV SCON,#50H ; 8-bit, 1 stop, REN enabledSETB TR1 ; start timer1 to generate the baud rate

    AGAIN: MOV SBUF,#A ; letter A to be transmitted

    HERE: JNB TI,HERE ; wait till data is transmitted

    CLR TI ; clear TI for next char

    SJMP AGAIN ; keep sending A

    Void main()

    {

    TMOD=0x20;

    TH1=0xfd;

    SCON=0x50;

    TR1=1;

    while(1)

    {

    SBUF=A;

    while(TI==0);

    TI=0;

    }

    }

    Serial Communication

    Serial Communication

  • 7/27/2019 Final 8051 Printout

    57/78

    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

    Ser a ommun cat on

  • 7/27/2019 Final 8051 Printout

    58/78

    Program to receive and transfer characters serially at 9600 baud, continuously.MOV TMOD,#20H ; timer 1,mode 2(auto reload)

    MOV TH1,# -3 ; 9600 baud rate

    MOV SCON,#50H ; 8-bit, 1 stop, REN enabled

    SETB TR1 ; start timer1 to generate the baud rate

    HERE: JNB RI,HERE; wait till data is receivedCLR RI ; clear RI to receive next char

    MOV A, SBUF; copy the received char into accumlator.

    AGAIN: MOV SBUF, A ; transmit the received char.

    WAIT : JNB TI,WAIT ; wait till data is transmitted.

    CLR TI ; clear TI for next char

    SJMP HERE; continue the process

    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;

    }

    }

    Ser a ommun cat on

  • 7/27/2019 Final 8051 Printout

    59/78

    Interrupts Programming

    Interrupts Programming

  • 7/27/2019 Final 8051 Printout

    60/78

    n rrup r gr mm ng An in terruptis 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 in terrupt service rout in e

    (ISR) orin terrupt 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 checkwhether 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

  • 7/27/2019 Final 8051 Printout

    61/78

    p g mm gClassification 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 bydisabling 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.

  • 7/27/2019 Final 8051 Printout

    62/78

    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 onthe 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 servicesubroutine until it reaches the last instruction of the subroutine which isRETI (return from interrupt)

    Upon executing the RETI instruction, the microcontroller returns to theplace where it was interrupted. First, it gets the program counter (PC)address from the stack by popping the top two bytes of the stack intothe PC and then it starts to execute from that address

    Interrupts Programming

  • 7/27/2019 Final 8051 Printout

    63/78

    p g gSix 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

  • 7/27/2019 Final 8051 Printout

    64/78

    p g g

    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 aregister called IE (interrupt enable) that is responsible for enabling (unmasking)

    Interrupt Enable (IE) register

    Interrupts Programming

  • 7/27/2019 Final 8051 Printout

    65/78

    p g gBy bit operation

    SETB EA EA=1 ;Enable AllSETB ET0 ET0=1 ;Enable Timer0 ovrf

    SETB ET1 ET1=1 ;Enable Timer1 ovrfSETB EX0 EX0=1 ;Enable INT0SETB EX1 EX1=1 ;Enable INT1SETB ES ES=1 ;Enable Serial port

    By mov instructionMOV IE, #10010110B;

    SETB IE.7

    SETB IE.1

    SETB IE.3

    SETB IE.0

    SETB IE.2

    SETB IE.4

    Asm program to generate 500hz and 2khz square waveson P1 0 & P1 1

  • 7/27/2019 Final 8051 Printout

    66/78

    on P1.0 & P1.1ORG 0000H

    LJMP MAINORG 000BH

    LJMP T0ISR

    ORG 001BHLJMP T1ISR

    ORG 0030HMAIN: MOV TMOD,#11H

    MOV TH0,#0FChMOV TL0,#066h

    MOV TH1,#0FFhMOV TL1,#019hSETB TR0SETB TF1MOV IE,#8AHSJMP $

    T0ISR: MOV TH0,#0FChMOV TL0,#066hCPL P1.0RETI

    T1ISR: MOV TH1,#0FFhMOV TL1,#19hCPL P1.1

    RETIEND

    0.25ms

    0.5ms

    1ms

    2ms

    P1.1

    8051

    P1.0

    Notice that

    There is no need for a CLR TFxinstruction in timer ISR ,8051

    automatically clears the TF internally

    upon jumping to ISR.

    C program to generate 500hz and 2khz square waves onP1 0 & P1 1 simultanously

  • 7/27/2019 Final 8051 Printout

    67/78

    P1.0 & P1.1 simultanously

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

  • 7/27/2019 Final 8051 Printout

    68/78

    Led Interfacing

  • 7/27/2019 Final 8051 Printout

    69/78

    g

    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.

    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 & commoncathode.

    Common cathode: Here all the cathode terminals of leds

    areshorted(made common) and connected to GND, so to

    turn on a particular led a logic 1(5V) has to be applied.

    GND(0v)

    Common cathode Common anode.

    VCC(5v)

    Program to blink the Led continuously

  • 7/27/2019 Final 8051 Printout

    70/78

    g y#include

    #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 ledsdelay(50000);

    }

    }

    Seven Segment Display

  • 7/27/2019 Final 8051 Printout

    71/78

    g p y1. It is composed of 8 LEDs arranged as a

    rectangle.

    2. Segments are marked with the lettersfrom 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 segmentcode 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/27/2019 Final 8051 Printout

    72/78

    a

    b

    c

    f

    ed

    f

    e

    a

    b

    e

    g

    d

    a

    b

    c

    g

    d

    b

    c

    fg

    a

    c

    fg

    d

    a

    c

    f

    e

    g

    d

    a

    b

    c

    a

    b

    c

    f

    e

    g

    d

    a

    b

    c

    fg

    d

    BCD p g f e d c b a

    7_seg

    hex

    0000 0 0 1 1 1 1 1 1 3f

    0001 0 0 1 1 0 0 0 0 30

    0010 0 1 0 1 1 0 1 1 5b

    0011 0 1 0 0 1 1 1 1 4f

    0100 0 1 1 0 0 1 1 0 660101 0 1 1 0 1 1 0 1 6d

    0110 0 1 1 1 1 1 0 1 7d

    0111 0 0 0 0 0 1 1 1 07

    1000 0 1 1 1 1 1 1 1 7f

    1001 0 1 1 0 1 1 1 1 6f

    7-segment displays come in 2 configurations:

    Common cathode: Logic 1 (5v) should

    be provided to turn on a particularsegment.

    Common Anode Common Cathode.

    Common anode: Logic 0 (0v) should be

    provided to turn on a particular segment.

    Program to display decimal upcounter(0-9)

  • 7/27/2019 Final 8051 Printout

    73/78

    #include

    #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

  • 7/27/2019 Final 8051 Printout

    74/78

    g g p y

    LCD Interfacing

  • 7/27/2019 Final 8051 Printout

    75/78

    1. Liquid Crystal Displays (LCDs)

    2. cheap and easy way to display all letters of

    alphabet, greek letters, punctuation marks,

    mathematical symbols etc.3. Available in Various configurations (1 line by

    20 char upto 8 lines X 80 chars ).

    g

    E

    R/W

    RS

    DB7DB0

    LCD

    controller

    communications

    bus

    Microcontroller

    8

    LCD Module

    Pinout8 data pins D7:D0RS: Data or Command Register Select

    R/W: Read or WriteE: Enable (Latch data)

    RS Register SelectRS = 0 Command RegisterRS = 1 Data Register

    R/WRead/WriteR/W = 0 writeR/W= 1 Read

    E EnableUsed to latch the data present on the datapins.

    D0 D7Bi-directional data/command pins.

    Alphanumeric characters are sent in ASCIIformat.

    LCD Interfacing

  • 7/27/2019 Final 8051 Printout

    76/78

    g

    LCD Commands

  • 7/27/2019 Final 8051 Printout

    77/78

    Program to display hello, world on lcd

  • 7/27/2019 Final 8051 Printout

    78/78

    Steps to send command to lcd:1. Send the command to lcd databus.

    databus=cmd;

    2. Select the command Register.RS=0;

    3. Write operation.

    RW=0;

    4. High-to-Low pulse at the enable pin.

    EN=1

    delay();

    EN=0;

    Steps to send Data to lcd:1. Send the ASCII value of data to lcd databus.

    databus=dat;

    2. Select the Data Register.

    RS=1;

    3. Write operation.RW=0;

    4. High-to-Low pulse at the enable pin.

    EN=1

    delay();

    EN 0

    #include

    #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