TRAINING REPORT ON 8051 MICROCONTR

Embed Size (px)

Citation preview

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    1/27

    TRAINING

    REPORT ON

    8051

    MICROCONTR-OLLER

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    2/27

    Table of ContentsDAY 1: .............................................................................................................................................................................. 3

    a) DEFINITION: ........................................................................................................................................................ 3

    b) HISTORY: ............................................................................................................................................................. 3

    c) MICROCONTROLLER VS MICROPROCESSOR ....................................................................................................... 3

    d) IMPORTANT FEATURES ....................................................................................................................................... 3e) PIN DIAGRAM ...................................................................................................................................................... 4

    f) BLOCK DIAGRAM ................................................................................................................................................. 5

    g) 8051 programming model .................................................................................................................................. 5

    h) 8051 Instruction Set Summary ........................................................................................................................... 6

    Softwares to be used: ................................................................................................................................................... 10

    KEIL:........................................................................................................................................................................... 10

    KEIL TUTORIAL .......................................................................................................................................................... 11

    FLASH MAGIC: ........................................................................................................................................................... 12

    FLASH MAGIC TUTORIAL: .......................................................................................................................................... 12

    LCD PROGRAMMING .................................................................................................................................................... 15

    Pin Description .......................................................................................................................................................... 15

    Commands and Instruction set ................................................................................................................................. 15

    SERIAL PROGRAMMING ................................................................................................................................................ 17

    Introduction .............................................................................................................................................................. 17

    MAX232 Interfacing with Microcontrollers .............................................................................................................. 188051 Programming for UART .................................................................................................................................... 19

    Uses of Timers & Counters ........................................................................................................................................... 21

    Martix Keypad Interfacing with Microcontrollers: Introduction .................................................................................. 25

    Introduction .............................................................................................................................................................. 25

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    3/27

    DAY 1:

    a) DEFINITION:

    A microcontroller (also microcomputer, MCU or C) is a small computer on a singleintegrated

    circuit consisting internally of a relatively simple CPU,clock,timers,I/O ports, and memory.

    b)HISTORY:

    a) First general purpose microprocessor was invented by INTEL: Intel 4004 in early 1970s. it is a 4

    bit microprocessor.

    b) In 1974 the more powerful second generation microprocessor (the 8008) was announced fabricated

    as a single chip. Followed by the Intel 8080. These are 8 bit microprocessors which can be operated

    by +5V.

    c) At about the same time Motorola released its first microprocessor, the 6800, which was also an 8 bit

    processor with about the same processing power as that of the intel 8080.

    d) Then the third generation 16 bit 8086 microprocessor which, in its 8088 pseudo 16 bit form, was

    used in the first IBM PCs. The 8088 was an 8086 but with only an 8 bit data bus.e) In time this was followed by the 80186, the 80286, the 800386 ( a 32 bit processor), and 80486,

    leading to the Pentium range of microprocessors (64 bit processors) available today.

    f) The first computer system on a chip optimized for control applications - microcontrollerwas

    theIntel 8048 released in 1975, with bothRAM andROM on the same chip.

    c) MICROCONTROLLER VS MICROPROCESSOR

    Microprocessor

    CPU is stand-alone, RAM, ROM, I/O, timer are separate

    Designer can decide on the amount of ROM, RAM and I/O ports.

    expansive

    versatility

    general-purpose

    Microcontroller

    CPU, RAM, ROM, I/O and timer are all on a single chip

    fix amount of on-chip ROM, RAM, I/O ports

    for applications in which cost, power and space are critical

    single-purpose

    d) IMPORTANT FEATURES

    It provides many functions (CPU,RAM,ROM,I/O,interrupt logic,timer,etc.) in a singlepackage

    8-bit ALU, Accumulator and Registers; hence it is an8-bitmicrocontroller

    8-bitdata bus - It can access 8 bits of data in one operation

    16-bitaddress bus - It can access 216

    memory locations - 64kB (65536 locations) each of RAM and

    ROM On-chip RAM - 128bytes (data memory)

    http://en.wikipedia.org/wiki/Integrated_circuithttp://en.wikipedia.org/wiki/Integrated_circuithttp://en.wikipedia.org/wiki/Crystal_oscillatorhttp://en.wikipedia.org/wiki/Watchdog_timerhttp://en.wikipedia.org/wiki/Intel_8048http://en.wikipedia.org/wiki/Random_access_memoryhttp://en.wikipedia.org/wiki/Read_only_memoryhttp://en.wikipedia.org/wiki/Central_processing_unithttp://en.wikipedia.org/wiki/Random_access_memoryhttp://en.wikipedia.org/wiki/Read-only_memoryhttp://en.wikipedia.org/wiki/Input/outputhttp://en.wikipedia.org/wiki/Interrupthttp://en.wikipedia.org/wiki/Timerhttp://en.wikipedia.org/wiki/Integrated_circuit_packaginghttp://en.wikipedia.org/wiki/8-bithttp://en.wikipedia.org/wiki/Microcontrollerhttp://en.wikipedia.org/wiki/Data_bushttp://en.wikipedia.org/wiki/Address_bushttp://en.wikipedia.org/wiki/Kibibytehttp://en.wikipedia.org/wiki/Byteshttp://en.wikipedia.org/wiki/Byteshttp://en.wikipedia.org/wiki/Kibibytehttp://en.wikipedia.org/wiki/Address_bushttp://en.wikipedia.org/wiki/Data_bushttp://en.wikipedia.org/wiki/Microcontrollerhttp://en.wikipedia.org/wiki/8-bithttp://en.wikipedia.org/wiki/Integrated_circuit_packaginghttp://en.wikipedia.org/wiki/Timerhttp://en.wikipedia.org/wiki/Interrupthttp://en.wikipedia.org/wiki/Input/outputhttp://en.wikipedia.org/wiki/Read-only_memoryhttp://en.wikipedia.org/wiki/Random_access_memoryhttp://en.wikipedia.org/wiki/Central_processing_unithttp://en.wikipedia.org/wiki/Read_only_memoryhttp://en.wikipedia.org/wiki/Random_access_memoryhttp://en.wikipedia.org/wiki/Intel_8048http://en.wikipedia.org/wiki/Watchdog_timerhttp://en.wikipedia.org/wiki/Crystal_oscillatorhttp://en.wikipedia.org/wiki/Integrated_circuithttp://en.wikipedia.org/wiki/Integrated_circuit
  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    4/27

    On-chip ROM - 4 kB (program memory) Fourbytebi-directionalinput/outputport

    UART (serial port)

    Two 16-bit Counter/timers

    Two-levelinterruptpriority

    Power saving mode

    e) PIN DIAGRAM

    http://en.wikipedia.org/wiki/Bytehttp://en.wikipedia.org/wiki/Input/outputhttp://en.wikipedia.org/wiki/Serial_porthttp://en.wikipedia.org/wiki/Timerhttp://en.wikipedia.org/wiki/Interrupthttp://en.wikipedia.org/wiki/Power_managementhttp://en.wikipedia.org/wiki/Power_managementhttp://en.wikipedia.org/wiki/Interrupthttp://en.wikipedia.org/wiki/Timerhttp://en.wikipedia.org/wiki/Serial_porthttp://en.wikipedia.org/wiki/Input/outputhttp://en.wikipedia.org/wiki/Byte
  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    5/27

    f) BLOCK DIAGRAM

    g) 8051 programming model

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    6/27

    The figure also shows the usual CPU components: program counter, ALU , working register and clock

    circuit.

    The 8051 architecture consists of these specific feature:

    eight- bit CPU with registers A (the accumulator) and B

    Sixteen bit program counter (PC) and data pointer(DPTR)

    Eight bit program status word (PSW)

    Eight bit stack pointer (SP)

    Internal ROM of 4k

    Internal RAM of 128 bytes

    --- four register banks each containing eight registers

    --- Sixteen bytes which may be addressed at bit level

    --- Eight bytes of general purpose data memory

    Thirty two i/p-o/p pins arranged as 4 eight bit ports- P0-P3

    Two sixteen bit timer/counter (T0-T1)

    Full duplex serial data receiver/transmitter: SBUF

    Control registers: TMOD, SCON ,PCON, TCON, IP AND IE

    h)8051 Instruction Set Summary

    Rn Register R7-R0 of the currently selected Register Bank.

    Data 8-bit internal data locations address. This could be an internal DataRAM location (0-127)or a SFR [i.e. I/O port, control register, status register, etc. (128-255)].

    @Ri 8-bit Internal Data RAM location (0-255) addressed indirectly through register R1 or R0.

    #data 8-bit constant included in instruction.

    #data16 16-bit constant included in instruction.

    addr16 16-bit destination address. Used by LCALL and LJMP. A branch can be anywhere withinthe 64k byte Program Memory address space.

    addr11 11-bit destination address. Used by ACALL and AJMP. The branch will be within the same2k byte page of Program Memory as the first byte of the following instruction.

    rel Signed (twos component) 8-bit offset byte. Used by SJMP and all conditional jumps.

    Range is128 to +127 bytes relative to first byte of the following instruction.

    bit Direct Addressed bit in Internal Data RAM or Special Function Register.

    Instruction Flag Instruction Flag

    C OV AC C OV AC

    ADD X X X CLR C OADDC X X X CPL C XSUBB X X X ANL C,bit X

    MUL O X ANL C,/bit X

    DIV O X ORL C,bit X

    DA X ORL C,/bit XRRC X MOV C,bit X

    RLC X CJNE X

    SETB C 1

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    7/27

    Arithmetic Operations

    Mnemonic Description Bytes Cycles

    ADD A,Rn Add register to A 1 1

    ADD A,direct Add direct byte to A 2 1

    ADD A,@Ri Add indirect RAM to A 1 1

    ADD A,#data Add immediate data to A 2 1

    ADDC A,Rn Add register to A with Carry 1 1

    ADDC A,direct Add direct byte to A with Carry 2 1

    ADDC A,@Ri Add indirect RAM to A with Carry 1 1

    ADDC A,#data Add immediate data to A with Carry 2 1

    SUBB A,Rn Subtract register from A with Borrow 1 1

    SUBB A,direct Subtract direct byte from A with Borrow 2 1

    SUBB A,@Ri Subtract indirect RAM from A with Borrow 1 1

    SUBB A,#data Subtract immediate data from A with Borrow 2 1

    INC A Increment A 1 1

    INC Rn Increment register 1 1

    INC direct Increment direct byte 2 1

    INC @Ri Increment indirect RAM 1 1

    DEC A Decrement A 1 1

    DEC Rn Decrement register 1 1

    DEC direct Decrement direct byte 2 1

    DEC @Ri Decrement indirect RAM 1 1

    INC DPTR Increment Data Pointer 1 2

    MUL AB Multiply A and B (A x B => BA) 1 4

    DIV AB Divide A by B (A/B => A + B) 1 4

    DA A Decimal Adjust A 1 1

    Logical Operations

    Mnemonic Description Bytes Cycles

    ANL A,Rn AND register to A 1 1

    ANL A,direct AND direct byte to A 2 1

    ANL A,@Ri AND indirect RAM to A 1 1

    ANL A,#data AND immediate data to A 2 1

    ANL direct,A AND A to direct byte 2 1

    ANL direct,#data AND immediate data to direct byte 3 2

    ORL A,Rn OR register to A 1 1

    ORL A,direct OR direct byte to A 2 1

    ORL A,@Ri OR indirect RAM to A 1 1

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    8/27

    ORL A,#data OR immediate data to A 2 1

    ORL direct,A OR A to direct byte 2 1

    ORL direct,#data OR immediate data to direct byte 3 2

    XRL A,Rn Exclusive-OR register to A 1 1

    XRL A,direct Exclusive-OR direct byte to A 2 1

    XRL A,@Ri Exclusive-OR indirect RAM to A 1 1

    XRL A,#data Exclusive-OR immediate data to A 2 1

    XRL direct A Exclusive-OR A to direct byte 2 1

    XRL direct,#data Exclusive-OR immediate data to direct byte 3 2

    CLR A Clear A 1 1

    CPL A Complement A 1 1

    RL A Rotate A Left 1 1

    RLC A Rotate A Left through Carry 1 1

    RR A Rotate A Right 1 1

    RRC A Rotate A Right through Carry 1 1

    SWAP A Swap nibbles within A 1 1

    Data Transfer Operations

    Mnemonic Description Bytes Cycles

    MOV A,Rn Move register to A 1 1

    MOV A,direct Move direct byte to A 2 1

    MOV A,@Ri Move indirect RAM to A 1 1

    MOV A,#data Move immediate data to A 2 1

    MOV Rn,A Move A to register 1 1

    MOV Rn,direct Move direct byte to register 2 2

    MOV Rn,#data Move immediate data to register 2 1

    MOV direct,A Move A to direct byte 2 1

    MOV direct,Rn Move register to direct byte 2 2

    MOV direct,direct Move direct byte to direct byte 3 2

    MOV direct,@Ri Move indirect RAM to direct byte 2 2

    MOV direct,#data Move immediate data to direct byte 3 2

    MOV @Ri,A Move A to indirect RAM 1 1MOV @Ri,direct Move direct byte to indirect RAM 2 2

    MOV @Ri,#data Move immediate data to indirect RAM 2 1

    MOV DPTR,#data16 Load Data Pointer with 16-bit constant 2 1

    MOVC A,@A+DPTR Move Code byte relative to DPTR to A 1 2

    MOVC A,@A+PC Move Code byte relative to PC to A 1 2

    MOVX A,@Ri Move External RAM (8-bit addr) to A 1 2

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    9/27

    MOVX A,@DPTR Move External RAM (16-bit addr) to A 1 2

    MOVX @Ri,A Move A to External RAM (8-bit addr) 1 2

    MOVX @DPTR A Move A to External RAM (16-bit addr) 1 2

    PUSH direct Push direct byte onto stack 2 2

    POP direct Pop direct byte from stack 2 2

    XCH A,Rn Exchange register with A 1 1

    XCH A,direct Exchange direct byte with A 2 1

    XCH A,@Ri Exchange indirect RAM with A 1 1

    XCHD A,@Ri Exchange low-order Digit indirect RAM with A 1 1

    Single Bit (Boolean Variable) Operations

    Mnemonic Description Bytes Cycles

    CLR C Clear Carry flag 1 1

    CLR bit Clear direct bit 2 1

    SETB C Set Carry flag 1 1

    SETB bit Set direct bit 2 1

    CPL C Complement Carry flag 1 1

    CPL bit Complement direct bit 2 1

    ANL C,bit AND direct bit to Carry flag 2 2

    ANL C,/bit AND complement of direct bit to Carry flag 2 2

    ORL C,bit OR direct bit to Carry flag 2 2

    ORL C,/bit OR complement of direct bit to Carry flag 2 2

    MOV C,bit Move direct bit to Carry flag 2 1

    MOV bit,C Move Carry flag to direct bit 2 2

    Program Flow Control

    Mnemonic Description Bytes Cycles

    ACALL addr11 Absolute subroutine call 2 2

    LCALL addr16 Long subroutine call 3 2

    RET Return from subroutine 1 2

    RETI Return from interrupt 1 2

    SJMP addr11 Absolute Jump 2 2

    LJMP addr16 Long Jump 3 2

    SJMP rel Short Jump at relative address 2 2

    JMP @A+DPTR Jump indirect relative to DPTR 1 2

    JZ rel Jump if A is Zero 2 2

    JNZ rel Jump if A is Not Zero 2 2

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    10/27

    JC rel Jump if Carry flag is set 2 2

    JNC rel Jump if No Carry flag 2 2

    JB bit,rel Jump if direct Bit is set 3 2

    JNB bit,rel Jump if direct Bit is Not set 3 2

    JBC bit,rel Jump if direct Bit is set and Clear bit 3 2

    CJNE A,direct,rel Compare direct to A and Jump if Not Equal 3 2

    CJNE A,#data,rel Compare immediate to A and Jump if Not Equal 3 2

    CJNE Rn,#data,rel Compare immediate to register and Jump if Not Equal 3 2

    CJNE @Ri,#data,rel Compare immediate to indirect and Jump if Not Equal 3 2

    DJNZ Rn,rel Decrement register and Jump if Not Zero 2 2

    DJNZ direct,rel Decrement direct byte and Jump if Not Zero 3 2

    NOP No operation 1 1

    Softwares to be used:

    KEIL:

    This tutorial will assist you in writing your first 8051 Assembly language program using the popular KeilCompiler. Keil offers an evaluation package that will allow the assembly and debugging of files 2K or less.

    This package is freely available at their web site. Keils website address iswww.keil.com.

    http://www.keil.com/http://www.keil.com/http://www.keil.com/http://www.keil.com/
  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    11/27

    KEIL TUTORIAL

    1. Open Keil from the Start menu2. The Figure below shows the basic names of the windows referred in this document

    3. Select New Project from the Project Menu.

    4. Name the project new5. Click on the Save Button.

    6. The device window will be displayed.

    7. Select the part you will be using to test with. Here we select NXP(P89v51rd2)8. Double Click on NXP(founded by philips)

    9. Scroll down and select the P89v51rd2 Part10.Click OK

    Creating Source File

    1. Click File Menu and select New.

    2. A new window will open up in the Keil IDE.

    3. Write your program in the file window.

    4. Click on File menu and select Save As

    5. Name the file new.asm6. Click the Save Button.

    Adding File to the Project

    1. Expand Target 1 in the Tree Menu . Right click on source group 1.

    2. Click on Add files to group Source group 13. Select the file and click on add.4. Expand the Source Group 1 in the Tree menu to ensure that the file was added to the project5. Select Target Tab6. Change Xtal (Mhz) from 50.0 to 11.05927. Select Output Tab8. Click on Create Hex File check box9. Click OK Button

    10.Click on Project Menu and select11.Rebuild all Target Files12.In the Build Window it should report

    13.0 Errors (s), 0 Warnings14.You are now ready to Program your Part

    Testing Program in Debugger

    1. Comment out line ACALL DELAY by placing a Semicolon at the beginning. This will allow youto see the port Change immediately.

    2. Click on the File Menu and select Save3. Click on Project Menu and select Rebuild all Target Files

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    12/27

    4. In the Build Window it should report 0 Errors (s), 0 Warnings5. Click on Debug Menu and Select Start/Stop Debug Session.

    Running the Keil Debugger

    1. The Keil Debugger should be now be Running2. Click on Peripherals.Select I/O Ports, SelectPort 1.3. A new window should port will pop up. This represents the Port and Pins.4. Step through the code by Pressing F11 on the Keyboard. The Parallel Port 1 Box should change as

    you completely step through the code.

    To exit out, Click on Debug Menu and Select Start/Stop Debug Session

    FLASH MAGIC:

    NXP Semiconductors produce a range of Microcontrollers that feature both on-chip Flash memory and the

    ability to be reprogrammed using In-System Programming technology.Flash Magic is Windows software

    from the Embedded Systems Academy that allows easy access to all the ISP features provided by thedevices. These features include:

    Erasing the Flash memory (individual blocks or the whole device) Programming the Flash memory Modifying the Boot Vector and Status Byte Reading Flash memory Performing a blank check on a section of Flash memory Reading the signature bytes Reading and writing the security bits Direct load of a new baud rate (high speed communications) Sending commands to place device in Bootloader mode

    FLASH MAGIC TUTORIAL:

    The following is a screenshot of the main Flash Magic window. The appearance may differ

    Slightly depending on the device selected.

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    13/27

    STEP 1: select the device as 89V51RD2, com port -COM 1, baud rate- 9600, InterfaceNone (ISP)

    STEP 2: Goto OPTIONS-advanced options

    STEP 3: Untick all the options.

    STEP 4: Browse the HEX file

    you want to burn

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    14/27

    STEP 5: Click on start button. Keep the reset button in off state

    STEP 6: when this window opens , press the reset button.

    STEP 7: To view the output or make controller work for you. Switch off the reset button and switch it on

    again.

    WRITING AND DEBUGGING A BASIC PROGRAM

    KIT DESCRIPTION

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    15/27

    LCD PROGRAMMING

    Pin Description

    The most commonly used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have

    only 1 controller and support at most of 80 charachers, whereas LCDs supporting more than 80 characters

    make use of 2 HD44780 controllers.

    Most LCDs with 1 controller has 14 Pins and LCDs with 2 controller has 16 Pins (two pins are extra in

    both for back-light LED connections). Pin description is shown in the table below.

    g)Figure 1: Character LCD type HD44780 Pin diagram

    Pin No. Name Description

    Pin no. 1 VSS Power supply (GND)

    Pin no. 2 VCC Power supply (+5V)

    Pin no. 3 VEE Contrast adjust

    Pin no. 4 RS0 = Instruction input

    1 = Data input

    Pin no. 5 R/W0 = Write to LCD module1 = Read from LCD module

    Pin no. 6 EN Enable signal

    Pin no. 7 D0 Data bus line 0 (LSB)

    Pin no. 8 D1 Data bus line 1

    Pin no. 9 D2 Data bus line 2

    Pin no. 10 D3 Data bus line 3

    Pin no. 11 D4 Data bus line 4

    Pin no. 12 D5 Data bus line 5

    Pin no. 13 D6 Data bus line 6

    Pin no. 14 D7 Data bus line 7 (MSB)h) Table 1: Character LCD pins with 1 Controller

    Commands and Instruction set

    Only the instruction register (IR) and the data register (DR) of the LCD can be controlled by the MCU.

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    16/27

    Before starting the internal operation of the LCD, control information is temporarily stored into theseregisters to allow interfacing with various MCUs, which operate at different speeds, or various peripheral

    control devices. The internal operation of the LCD is determined by signals sent from the MCU. These

    signals, which include register selection signal (RS), read/write signal (R/W), and the data bus (DB0 to

    DB7), make up the LCD instructions (Table 3). There are four categories of instructions that:

    Designate LCD functions, such as display format, data length, etc. Set internal RAM addresses

    Perform data transfer with internal RAM

    Perform miscellaneous functions

    No. Instruction Hex Decimal

    1 Function Set: 8-bit, 1 Line, 5x7 Dots 0x30 48

    2 Function Set: 8-bit, 2 Line, 5x7 Dots 0x38 56

    3 Function Set: 4-bit, 1 Line, 5x7 Dots 0x20 32

    4 Function Set: 4-bit, 2 Line, 5x7 Dots 0x28 40

    5 Entry Mode 0x06 6

    6 Display off Cursor off(clearing display without clearing DDRAM

    content)

    0x08 8

    7 Display on Cursor on 0x0E 14

    8 Display on Cursor off 0x0C 12

    9 Display on Cursor blinking 0x0F 15

    10 Shift entire display left 0x18 24

    12 Shift entire display right 0x1C 30

    13 Move cursor left by one character 0x10 16

    14 Move cursor right by one character 0x14 20

    15 Clear Display (also clear DDRAM content) 0x01 1

    16Set DDRAM address or coursor position on

    display0x80+add* 128+add*

    17Set CGRAM address or set pointer to CGRAMlocation

    0x40+add**64+add**

    Table 4: Frequently used commands and instructions for LCD

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    17/27

    Table 3: Command and Instruction set for LCD type HD44780

    SERIAL PROGRAMMING

    Introduction

    UART (Universal Asynchronous Receiver Transmitter) or USART (Universal Synchronous AsynchronousReceiver Transmitter) are one of the basic interface which you will find in almost all the controllers

    available in the market till date. This interface provide a cost effective simple and reliable communication

    between one controller to another controller or between a controller and PC

    RS-232 Basics

    RS-232 (Recommended Standard 232) is a standard for serial binary data signals connecting between aDTE (Data terminal equipment) and a DCE (Data Circuit-terminating Equipment)

    VoltageLevels:

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    18/27

    The RS-232 standard defines the voltage levels that correspond to logical one and logical zero levels. Validsignals are plus or minus 3 to 25 volts. The range near zero volts is not a valid RS-232 level; logic one is

    defined as a negative voltage, the signal condition is called marking, and has the functional significance of

    OFF. Logic zero is positive, the signal condition is spacing, and has the function ON. So a Logic Zero

    represented as +3V to +25V and Logic One represented as -3V to -25V.

    RS-232 Level Converters

    Usually all the digital ICs works on TTL or CMOS voltage levels which cannot be used to communicateover RS-232 protocol. So a voltage or level converter is needed which can convert TTL to RS232 andRS232 to TTL voltage levels.

    The most commonly used RS-232 level converter is MAX232. This IC includes charge pump which can

    generate RS232 voltage levels (-10V and +10V) from 5V power supply. It also includes two receiver andtwo transmitters and is capable of full-duplex UART/USART communication.

    MAX232 Interfacing with Microcontrollers

    To communicate over UART or USART, we just need

    three basic signals which are namely, RXD (receive),

    TXD (transmit), GND (common ground). So to interfaceMAX232 with any microcontroller (AVR, ARM, 8051, PIC etc..) we just need the basic signals. A simple

    schematic diagram of connections between a microcontroller and MAX232 is shown below

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    19/27

    -

    8051 Programming for UART

    In 8051, we make use of Timer 1 to generate the required baud rate. Following are the registers that are

    need to be configured to communicate over UART. TMOD

    SCON TH1 TL1 TCON

    TMOD:This register is used to set the mode of Timer0 and Timer1. It is also used to select whether thetimers are used as Timer or Counter.

    SCON:Serial Control register has various functions like.. it has flags for Framing error, Transmit interrupand receive interrupt. Its used to select the serial port mode, to enable or disable the reception etc.

    TCON:This register has various flag and control bits e.g. Timer overflow flags, interrupt edge flags, timercontrol bits to start/stop the timer.

    TH1 & TL1:Timer registers for Timer 1 determines the baud rate of UART.

    Initializing USART in 8051

    CODE:Serial_Init:

    ;Set timer 1 mode to 8-bit Auto-ReloadmovTMOD,#20H

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    20/27

    ;Enable reception;Set Serial port mode to 8-bit UARTmovSCON,#50H;Set baudrate to 9600 at 11.0592MHzmovTH1,#0FDHmovTL1,#0FDH;Start TimersetbTR1ret

    in C we can do this as..

    CODE:#include .voidserial_init(){

    TMOD =0x20;SCON =0x50;TH1 =0xFD;TL1 =0xFD;TR1 =1;

    }

    To Send data to the serial port we just have to move the data in SBUF (serial buffer register) and wait for

    the Transmit Interrupt flag to be set. While receiving we wait for the Receive interrupt flag to be set and

    read the data from SBUF register. This can be done as shown below...

    CODE:Serial_Send:

    ;wait for last data to be;sent completelyjnbTI,Serial_Send;clear the transmit interrupt flagclrTI;Then move the data to send in SBUFmovSBUF,Aret

    Serial_Read:;Wait for Receive interrupt flagjnbRI,Serial_Read;If falg is set then clear itclrRI;Then read data from SBUFmovA,SBUFret

    in C we can do this as..

    CODE:voidserial_send(unsignedchardat){

    while(!TI);TI =0;SBUF =dat;

    }unsignedcharserial_read(){

    while(!RI);

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    21/27

    RI =0;returnSBUF;

    }

    Uses of Timers & Counters

    - Interval Timing Periodic event timing Time base for measurements

    - Event Counting

    - Baud Rate Generation

    8051 Timers- 2 timers (Timer 0 and Timer 1)

    - 16-bit timers (65,535) max

    - Flag is set when the timer overflows- Timers can be based on internal clock (OSC/6) or from external

    source (counter mode).

    Internal operation of the 8051 timer (same for both timers)

    Timer Registers- TCON Timer Control

    - TMOD Timer Mode- TH0/TL0 Timer 0 16 bit register (byte addressable only)

    - TH1/TL1 Timer 1 16 bit register (byte addressable only)

    TCONTCON SFR and its individual bits- IT0/IT1: Used for timer Interrupts

    - IE0/IE1: Used for external Interrupts

    - TR0/TR1: Timer 0/1 run control flag

    1 = Run- TF0/TF1: Timer 0/1 overflow flag1 = Overflow

    TMODTMOD SFR and its individual bits

    - M0/M1: sets the Mode of the respective timer

    - C/T: External Counter/Internal Timer select

    1 = Counter, 0 = Timer

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    22/27

    - Gate: When set (1), timer runs only when respective INT input is

    \

    Timer Modes- 0: 13 bit timer- 1: 16-bit timer

    - 2: 8-Bit auto reload

    - 3: Split timer mode

    Mode 0: 13-Bit Timer- Lower byte (TL0/TL1) + 5 bits of upper bytes (TH0/TH1).

    - Backward compatible to the 8048

    - Not generally used

    Timer operation in Mode 0

    Mode 1: 16-bit- All 16 bits of the timer (TH0/TL0, TH1,TL1) are used.- Maximum count is 65,536

    - At 12Mhz, maximum interval is 65536 microseconds or 65.536

    milliseconds- TF0 must be reset after each overflow

    - THx/TLx must be manually reloaded after each overflow

    Timer operation in Mode 1

    Mode 2: 8-bit Auto Reload- Only the lower byte (TLx) is used for counting.- Upper byte (THx) holds the value to reload into TLx after an

    overflow.

    - TFx must be manually cleared.- Maximum count is 256

    - Maximum interval is 256 Microseconds or .256 milliseconds

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    23/27

    Timer operation in Mode 2

    Mode 3- Split Timer

    - Splits Timer 0 into two 8-bit timers- TL0 sets TF0

    - TH0 sets TF1

    - Timer 1 is available for other 3 modes, but the TF1 is not available.

    Timer operation in Mode 3

    Timer Delay and Timer Reload ValueTimer Delay = Delay Value Timer Clock Cycle Duration

    Delay Value = how many counts before register(s) roll overTimer Clock Cycle Duration = 6/oscillator frequency

    Delay Value = Maximum Register CountTimer Reload Value

    Maximum Register Count = 65535

    Timer Reload Value = ?

    Example 1

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    24/27

    Calculation of Timer 0 reload value needed to achieve timer delay of 20 ms. Oscillator frequency is11.0592 MHz.

    Delay Value = Timer Delay / Timer Clock Cycle Duration

    = 20 X 10

    611.0592 x 106

    = 36864 (must be rounded to the nearest integer)

    Timer Reload Value = Maximum Register Count - Delay Value

    = 6553536864

    = 28671= 0x6FFF

    so Timer 0 is loaded with:

    TH0 = 0x6F;

    TL0 = 0xFF;

    Example 2Function to generate 100 s delay using timer 0.

    Procedure is:Initialise TMOD register

    Initialise TL0 and TH0

    Start the Timer

    Monitor TF0 until it is set

    Delay: MOV TMOD,#01H ; Initialise TMOD

    MOV TL0,#47H ; initialise TL0

    MOV TL0,#FFH ; initialise TH0

    SETB TR0 ; start timer

    Wait: JNB TF0,Wait ; wait for TF0

    CLR TR0 ; stop timerCLR TF0 ; clear TF0

    RET

    Delay Value = Timer Delay / Timer Clock Cycle Duration

    = 20 X 10

    6

    11.0592 x 106

    Timer Reload Value = 65535184 = 65351 = 0xFF47

    so Timer 0 is loaded with:

    TH0 = 0x6F; TL0 = 0xFF;

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    25/27

    Martix Keypad Interfacing with Microcontrollers: Introduction

    Introduction

    Keypads are a part of HMI or Human Machine Interface and play really important role in a small

    embedded system where human interaction or human input is needed. Martix keypads are well known for

    their simple architecture and ease of interfacing with any microcontroller. In this part of tutorial we will

    learn how to interface a 4x4 matrix keypad with AVR and 8051 microcontroller. Also we will see how toprogram then in Assembly and C.

    Constructing a Matrix Keypad

    Constuction of a keypad is really simple. As per the outline shown in the figure below we have four rowsand four columns. In between each overlapping row and column line there is a key.

    So keeping this outline we can constuct a keypad using simple SPST Switches as shown below:

    Now our keypad is ready, all we have to do is connect the rows and columns to a port of microcontroller

    and program the controller to read the input.

    Scanning a Matrix Keypad

    There are many methods depending on how you connect your keypad with your controller, but the basic

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    26/27

    logic is same. We make the coloums as i/p and we drive the rows making them o/p, this whole procedure of

    reading the keyboard is called scanning.

    In order to detect which key is pressed from the matrix, we make row lines low one by one and read the

    coloums. Lets say we first make Row1 low, then read the columns. If any of the key in row1 is pressed will

    make the corrosponding column as low i.e if second key is pressed in Row1, then column2 will give low.

    So we come to know that key 2 of Row1 is pressed. This is how scanning is done.

    So to scan the keypad completely, we need to make rows low one by one and read the columns. If any of

    the button is pressed in a row, it will take the corrosponding column to a low state which tells us that a key

    is pressed in that row. If button 1 of a row is pressed then Column 1 will become low, if button 2 then

    column2 and so on...

  • 8/13/2019 TRAINING REPORT ON 8051 MICROCONTR

    27/27