8051 microcontroller-day2

66
March 13, 2022 1 WELCOME TO TRAINING PROGRAMME ON 8051 MICROCONTOLLER & APPLICATIONS September 1-2,2008 DEPARTMENT OF ECE MVGR COLLEGE OF ENGINEERING CHINTALAVALASA,VIZIANAGARAM

Upload: tansnvarma

Post on 12-Sep-2015

42 views

Category:

Documents


1 download

DESCRIPTION

Embedded systems PPT

TRANSCRIPT

  • WELCOME TO

    TRAINING PROGRAMME ON 8051 MICROCONTOLLER& APPLICATIONS

    September 1-2,2008DEPARTMENT OF ECEMVGR COLLEGE OF ENGINEERINGCHINTALAVALASA,VIZIANAGARAM

  • DAY 2SESSION 1

  • ContentsMore InstructionsPorts Timers & CountersSerial port communicationInterrupts

  • Logical OperationsANL - ORL - XRLEXAMPLE:ANL dest, source; dest=dest and sourceMOVR5,#89H; 10001001ANLR5,#08H; 00001000 therefore R5=00001000(08H)

  • Logical Operations.CPLA;1s complementExample:MOVA,#55H;A=01010101 BL01:CPLAMOVP1,AACALLDELAYSJMPL01

  • RR RL RRC RLC A

    EXAMPLE:RRA

    RR:

    RRC:

    RL:

    RLC:

    Logical Operations.

    C

    C

  • LOOP and JUMP InstructionsDJNZ:

    Write a program to clear ACC, thenadd 3 to the accumulator ten time

    Solution:MOVA,#0;MOVR2,#10AGAIN:ADDA,#03DJNZR2,AGAING ;repeat until R2=0 (10 times)MOVR5,A

  • Other conditional jumps :

  • SJMP and LJMP:

    LJMP(long jump)LJMP is an unconditional jump. It is a 3-byte instruction in which the first byte is the opcode, and the second and third bytes represent the 16-bit address of the target location. The 20byte target address allows a jump to any memory location from 0000 to FFFFH.SJMP(short jump)In this 2-byte instruction. The first byte is the opcode and the second byte is the relative address of the target location. The relative address range of 00-FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.

  • CJNE , JNCExercise:

    Write a program that compare R0,R1.If R0>R1 then send 1 to port 2,else if R0

  • CALL InstructionsAnother control transfer instruction is the CALL instruction, which is used to call a subroutine.

    LCALL(long call)In this 3-byte instruction, the first byte is the opcode an the second and third bytes are used for the address of target subroutine. Therefore, LCALL can be used to call subroutines located anywhere within the 64K byte address space of the 8051.

  • ACALL (absolute call)

    ACALL is 2-byte instruction in contrast to LCALL, which is 13 bytes. Since ACALL is a 2-byte instruction, the target address of the subroutine must be within 2K bytes address because only 11 bits of the 2 bytes are used for the address. There is no difference between ACALL and LCALL in terms of saving the program counter on the stack or the function of the RET instruction. The only difference is that the target address for LCALL can be anywhere within the 64K byte address space of the 8051 while the target address of ACALL must be within a 2K-byte range.

  • I/O PROGRAMMING

  • I/O Port Programming.Port 1 is denoted by P1.P1.0 ~ P1.7We use P1 as examples to show the operations on ports.P1 as an output port (i.e., write CPU data to the external pin)P1 as an input port (i.e., read pin data into CPU bus)Port 1pins 1-8

  • Other PinsP1, P2, and P3 have internal pull-up resisters.P1, P2, and P3 are not open drain.P0 has no internal pull-up resistors and does not connects to Vcc inside the 8051.P0 is open drain.Compare the figures of P1.X and P0.X. However, for a programmer, it is the same to program P0, P1, P2 and P3.All the ports upon RESET are configured as output.

  • A Pin of Port 0 8051 IC P1.x

  • Tri-state BufferOutputInputTri-state control (active high)LHLowHighimpedance (open-circuit)HHLH

  • Port 0pins 32-39P0 is an open drain.Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips. When P0 is used for simple data I/O we must connect it to external pull-up resistors.Each pin of P0 must be connected externally to a 10K ohm pull-up resistor.With external pull-up resistors connected upon reset, port 0 is configured as an output port.

  • Port 0 with Pull-Up Resistors

  • Dual Role of Port 0When connecting an 8051/8031 to an external memory, the 8051 uses ports to send addresses and read instructions.8031 is capable of accessing 64K bytes of external memory.16-bit addressP0 provides both address A0-A7, P2 provides address A8-A15.Also, P0 provides data lines D0-D7. When P0 is used for address/data multiplexing, it is connected to the 74LS373 to latch the address.There is no need for external pull-up resistors as shown in Chapter 14.

  • 74LS373

  • Reading ROM (1/2)1. Send address to ROM2. 74373 latches the address and send to ROMAddress

  • Reading ROM (2/2)2. 74373 latches the address and send to ROMAddress3. ROM send the instruction back

  • ALE PinThe ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch.When ALE=0, P0 provides data D0-D7.When ALE=1, P0 provides address A0-A7.The reason is to allow P0 to multiplex address and data.

  • Port 2pins 21-28Port 2 does not need any pull-up resistors since it already has pull-up resistors internally.In an 8031-based system, P2 are used to provide address A8-A15.

  • Port 3pins 10-17Port 3 does not need any pull-up resistors since it already has pull-up resistors internally.Although port 3 is configured as an output port upon reset, this is not the way it is most commonly used.Port 3 has the additional function of providing signals.Serial communications signalRxD, TxDChapter 10External interrupt/INT0, /INT1Chapter 11Timer/counterT0, T1Chapter 9External memory accesses in 8031-based system/WR, /RDChapter 14

  • Port 3 Alternate Functions

  • A Pin of Port 1 8051 ICP0.x

  • Hardware Structure of I/O Pin Each pin of I/O portsInternal CPU buscommunicate with CPUA D latch store the value of this pinD latch is controlled by Write to latchWrite to latch1write data into the D latch2 Tri-state bufferTB1: controlled by Read pinRead pin1really read the data present at the pinTB2: controlled by Read latchRead latch1read value from internal latchA transistor M1 gateGate=0: openGate=1: close

  • Writing 1 to Output Pin P1.X8051 IC2. output pin is Vcc1. write a 1 to the pin10output 1TB1TB2

  • Writing 0 to Output Pin P1.X8051 IC2. output pin is ground1. write a 0 to the pin01output 0TB1TB2

  • Port 1 as OutputWrite to a PortSend data to Port 1

    MOV A,#55H BACK: MOV P1,A ACALLDELAYCPL ASJMP BACK

    Let P1 toggle.You can write to P1 directly.

  • Reading High at Input Pin8051 IC2. MOV A,P1 external pin=Highwrite a 1 to the pin MOV P1,#0FFH103. Read pin=1 Read latch=0 Write to latch=11TB1TB2

  • Reading Low at Input Pin8051 IC2. MOV A,P1external pin=Lowwrite a 1 to the pinMOV P1,#0FFH103. Read pin=1 Read latch=0 Write to latch=10TB1TB2

  • Port 1 as InputRead from PortIn order to make P1 an input, the port must be programmed by writing 1 to all the bit.

    MOV A,#0FFH ;A=11111111BMOV P1,A ;make P1 an input port BACK:MOV A,P1 ;get data from P0MOV P2,A ;send data to P2SJMP BACK

    To be an input port, P0, P1, P2 and P3 have similar methods.

  • Instructions For Reading an Input PortFollowing are instructions for reading external pins of ports:

  • Read-Modify-Write InstructionsExampleMnemonics

  • Timer/Counter Logic

  • PROGRAMMING 8051 TIMERSTimer 0 registersTL0 ( timer 0 low byte ) TH0 ( timer 0 high byte )

  • Timer 1 registersTL1 ( timer 1 low byte ) TH1 ( timer 1 high byte )

  • TMOD (timer mode) register

  • Operation of Timer on Mode-0

  • Operation of Timer in Mode 1

  • Operation of Timer in Mode 2

  • Operation of Timer in Mode 3

  • 8051 SERIAL COMMUNICATION

  • Basics of serial communication

  • Start and stop bits

  • SCON (Serial control) register

  • SM0,SM1SM0 and SM1 are D7 and D6 of the SCONSM0 SM1 0 0 Serial Mode 0 0 1 Serial Mode 1,8 bit data, 1 stop bit, 1 start bit 1 0 Serial Mode 2 1 1 Serial Mode 3

  • Doubling the baud rate in the 8051To use a higher frequency crystalTo change a bit in the PCON registerD7D0MOV A,PCON ;place a copy of PCON in ACCSETB ACC.7 ;make D7=1 MOV PCON,A ;now SMOD=1 without ;changing any other bits

    SMOD------GF1GF0PDIDL

  • Baud rates for SMOD=0Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHzand921.6 kHz / 32 = 28,800 Hz since SMOD = 0

  • Baud rates for SMOD=1Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHzand921.6 kHz / 16 = 57,600 Hz since SMOD = 1

  • INTERRUPTS PROGRAMMING

  • Six interrupts in the 8051

  • Step in enabling an interrupt

  • Writing the ISRExample:Writing the ISR for Timer0 interruptORG 0000H;resetLJMP MAINORG 000BH;Timer0 entry pointT0ISR:.;Timer0 ISR begins.RETI;return to main programMAIN:.;main program..END

  • Interrupt priority upon reset

  • 8051 Instruction SetACALL: Absolute Call ADD, ADDC: Add Acc. (With Carry) AJMP: Absolute Jump ANL: Bitwise AND CJNE: Compare & Jump if Not Equal CLR: Clear Register CPL: Complement Register DA: Decimal Adjust DEC: Decrement Register DIV: Divide Accumulator by B DJNZ: Dec. Reg. & Jump if Not Zero INC: Increment Register JB: Jump if Bit Set JBC: Jump if Bit Set and Clear Bit JC: Jump if Carry Set JMP: Jump to Address JNB: Jump if Bit Not Set JNC: Jump if Carry Not Set JNZ: Jump if Acc. Not Zero JZ: Jump if Accumulator Zero LCALL: Long Call LJMP: Long Jump MOV: Move Memory MOVC: Move Code Memory MOVX: Move Extended Memory MUL: Multiply Accumulator by B NOP: No Operation ORL: Bitwise OR POP: Pop Value From Stack PUSH: Push Value Onto Stack RET: Return From Subroutine RETI: Return From Interrupt RL: Rotate Accumulator Left RLC: Rotate Acc. Left Through Carry RR: Rotate Accumulator Right RRC: Rotate Acc. Right Through Carry SETB: Set Bit SJMP: Short Jump SUBB: Sub. From Acc. With Borrow SWAP: Swap Accumulator Nibbles XCH: Exchange Bytes XCHD: Exchange Digits XRL: Bitwise Exclusive OR Undefined: Undefined Instruction

  • DAY 1End of session 1

    Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips.

    Program is to read data from P0 and then send data to P1

    ANL: Latch data AND with A , then save back to latch and write to the external pinORL: OR XRL: XORJBC: jump to TARGET if bit set and clear bitCPL: complementINC: increase DEC: decreaseDJNZ: decrease P1 and jump if P1 not zeroMOV the latch value to carryCLR: clear bit, SETB: set bit