+ All Categories
Home > Documents > Training Program Day2 RAKESH

Training Program Day2 RAKESH

Date post: 04-Nov-2015
Category:
Upload: tansnvarma
View: 219 times
Download: 0 times
Share this document with a friend
Description:
Embedded systems PPT
56
Training Program on “8051 Microcontroller and Applications” September 1 st - 2 nd, 2008 DA 2 S!SS"#$ 2
Transcript
  • Training Programon8051 Microcontroller and Applications

    September 1st- 2nd, 2008

    DAY 2 SESSION 2

  • INTERFACING 8051 WITH PHERIPHERALS

    LEDLCDKEYBOARD7 SEGMENTSERIAL PORTADC, STEPPER MOTOR,DC MOTOR etc..

  • Interfacing LED with 8051Light Emitting Diodes (LED) are the most commonly used components, usually for displaying Pins Digital State.

  • Interfacing 7 segment displayThey are composed of 8 LEDs, 7 segments are arranged as a rectangle for symbol displaying and there is additional segment for decimal point displaying.

    Problem - valuable I/O pins which they occupy. Ex: Two 6-digit numbers - 96 output pins are needed The solution on this problem is called MULTIPLEXING

  • A microcontrollers port is connected to display in a way that bit 0 activates segment a, bit 1 activates segment b, bit 2 segment c etc., the table below shows mask for each digit

  • LCD InterfacingDisplay Data on 2x16 character LCD

  • LCD INTERFACING ContdLCDs are typically used for displaying outputs from aMicro controller or for debugging purposes.

    LCDs come in varying sizes some of the typical ones are

    16 X 1= one line 16 characters per line16 X 2= Two lines 16 characters per line20 X 4= Four lines 20 characters per line

    To display on LCD characters should be sent in ASCII format

  • LCD INTERFACING Contd

    PinSYMDESCRIPTION1VssGround2VccSupply , +5v3VeeSupply to control contrast4RSRS=0 to select Command REGRS=1 to select DATA REG5R/WR/W = 1 to Read from LCDR/W = 0 to Write to LCD6ENEnable Pin7-14D0-D78 bit DATA Bus

  • LCD INTERFACING ContdLCD power supply

  • There are three important control pins for the LCD

    RS , Register Select : There are two registers inside LCD one is the command register and the second is Data register. RS is used to select between the two RS =0 Command Register RS =1 Data Register

    Typical Commands include Clear Display, Set cursor etc.These commands should be sent to command regs. The Data which is to displayed should be sent to the data reg LCD INTERFACING Contd

  • 2. R/W Pin: R/W =1 Read information from LCD R/W =0 Write information to LCD3. EN : Enable pin, used by LCD to latch information presented on data pins. When data/command is supplied to the LCD a HI to LO pulse must pulse must be applied to this pin in order for the LCD to latch the data/command present on the pins

    IMP: The pulse must be a minimum 450 nsec wide LCD INTERFACING Contd

  • In addition to the above pins there is a back light pin which if Connected to + 5v will provide back light Connection DiagramLCD INTERFACING Contd

  • Sending Commands to LCDThe typical commands sent to the LCD are as followsLCD INTERFACING Contd

    CodeCommand01hClear Display screen02hReturn Home06hIncrement Cursor after write0ChDisplay on Cursor Off0FhDisplay on Cursor blinkingC0hForce cursor to beginning of 2nd line38h2 lines 5X7 font (initialization code)

  • WRTCMD:MOV PDATA,A CLR RS CLR RW SETB EN CLR EN ;Hi to Lo pulse RET

    The command to be sent is in A regRS EQU P3.5RW EQU P3.6EN EQU P3.7PDATA EQU P2

    AlgorithmLCD INTERFACING Contd

  • Sending DATA to LCDWRTDAT: MOV PDATA,A SETB RS CLR RW SETB EN CLR EN ;Hi to Lo pulse RET

    The data to be sent is in A regRS EQU P3.5RW EQU P3.6EN EQU P3.7PDATA EQU P2LCD INTERFACING Contd

  • Busy Waiting

    The LCD takes some time to execute the commands and to display.The data sent to it and while it is doing execution we are not Suppose to send new commands or data to it To send data there are two Ways

    Giving some predefined delay say 30msec between two commands or data, not a good method as different LCD modules take different times to execute

    Busy Wait Flag : Bit 7 of the command reg is the busy wait flag. We can use it to check if the LCD is busy D7 =1 ; Busy D7 =0 ; Free to process next instructionLCD INTERFACING Contd

  • P3.5 EQU RSP3.6 EQU RW P3.7 EQU ENP2 EQU PDATA BSYWT: SETB PDATA.7 CLR RS SET RWLOOP: SETB EN CLR EN JB pdata.7, LOOP RET LCD INTERFACING Contd

  • So before issuing any command or data to the LCD, we have to check if LCD is free or not. This can be done using the BSYWTRoutine as shown in the previous slide, so for correct operationsOur WRTCMD and WRTDAT routines should be modified as Follows

    WRTCMD:LCALL BSYWT MOV PDATA,A CLR RS CLR RW SETB EN CLR EN RET

    WRTDAT: LCALL BSYWT MOV PDATA,A SETB RS CLR RW SETB EN CLR EN RET

    The command/data to be sent is in A regLCD INTERFACING Contd

  • startLCD On , Cursor Blinking code=0FhClear LCD code=01hstopAuto increment cursor code=06hLCDINIT: MOV A,#38H LCALL WRTCMD MOV A,#0FH LCALL WRTCMD MOV A,#01H LCALL WRTCMD MOV A,#06H LCALL WRTCMD RET LCD INITIALIZATION : To initialize the LCD the following commands have to Be issued,once this done you ready to send data

  • EX1: Write an ALP to Print HELLO on the LCDStart: LCALL LCDINIT MOV A,#H LCALL WRTDAT MOV A,#E LACLL WRTDAT MOV A,#L LCALL WRTDAT MOV A,#L LCALL WRTDAT MOV A,#O LCALL WRTDAT HERE: SJMP HERE LCD INTERFACING Contd

  • Putting data at any LocationScope is there to put data at any location of our choice, to Accomplish this we need to place the cursor at the desired Location Address locations in a 16X2 LCDSo to place the cursor at any location we need to issue a command with address as the parameterEx: To place the the cursor at line 2 at pos 4 MOV A,#0C3H LCALL WRTCMD

    LCD INTERFACING Contd

    80h81828E8FC0C1C2CECF

  • KEYPAD INTERFACINGStructure of 4X4 Matrix keyboardContains 4 Rows and 4 Columns : whenever a key is pressedThe corresponding rows and columns are shorted

  • KEYPAD INTERFACINGKeypad Connector Structure ( LAB Keypad)8 pin connector at the bottom of the Key Pad

  • Interfacing Keypad to 8051:We will use one port P1 for interfacing The lower nibleOf Port1 i.e. P1.0To P1.3 are Connected to the 4 columns

    The upper nible Is connected to the4 Rows note that Row 1 is connectedTo P1.7

  • KEYPAD INTERFACINGEquivalent Interfacing Diagram:

  • KEYPAD INTERFACINGImportant Steps in Reading Keypad Step1: See if any key is pressed , so keep scanning the key padtill a key is presses

    Step2: Once we have detected a key press we have to decode the key i.e we have to identify which key is pressed

    Step3: We have to wait till the pressed key is released before We go on to process the next key

  • KEYPAD INTERFACINGStep1: Detecting Key PressConfigure P1.0 P1.3(Columns) : Outputs and P1.4 P1.7(Rows) : Inputs

  • 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

  • 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

  • KEYPAD INTERFACINGKeypad when connected to the portVcc

  • KEYPAD INTERFACING

    Ground all the Columns and read the rows

    2. If no key is pressed you will read all rows as high(??) Remember the port structure P1 has internal pull up resistors

    3. If some key is pressed then one of the rows will indicate a zero

  • KEYPAD INTERFACINGNOC1 EQU P1.0 R1 EQU P1.7

    C2 EQU P1.1 R2 EQU P1.6 C3 EQU P1.2 R3 EQU P1.5 C4 EQU P1.3 R4 EQU P1.4 PORT EQU P1

    KEYCHK: MOV PORT,#0F0H ;make all column low MOV A,PORT ; read from the ports ANL A,#0F0H ; mask the lower nibble CJNE A,#0F0H,HERE ; check if there is key press SJMP KEYCHKIf there is no key press the higher nibble will be all ones (F)

  • KEYPAD INTERFACINGKEY DEBOUNCE

    Some times key press detected in the previous step could be due to noise or transients so in order to be sure that it is key press we need to do key debounce

    Key debounce involves giving a small delay of 20 ms and then checking again for a key press, If we find a key press the second time also, we can be assured that we have a valid key press, else we have to again go back to key scan mode

  • KEYPAD INTERFACING

    KEYCHK: MOV PORT,#0F0H ; make all the columns LOW MOV A,PORT ; read from the ports ANL A,#0F0H ; mask the lower nibble CJNE A,#0F0H,HERE ; check if there is key press SJMP KEYCHK

    HERE: ACALL DELAY ; debounce MOV A,PORT ; read port again to make sure it was not noise ANL A,#0F0H CJNE A,#0F0H,KEYFIND ; If valid key, jump to decode the key SJMP KEYCHK ; If invalid go back to key scan mode Key Check Routine with debounce

  • KEYPAD INTERFACINGStep2: Key Decode , to decode the key we will use a look up Table approach so let us look at generation of look up tableSuppose we send the code1111 1110 on port 1

    And suppose key 1 is pressed Then if you read back P1 and Mask the lower bits what will youGet

    P1= 0111 0000 = 70h16So if we send the code FE h on port 1 and on reading it back and if we get 70 h we can conclude that key 1 is pressed, you can assign any code to key 1 like A , decimal 1 , Carriage return (CR)

  • KEYPAD INTERFACINGPin5:Input : 1111 1110 Output:1011 0000

    So input FE h , output B0h , key= 5 Pin2,6,10:Input : 1111 1101 Output: 0111 0000 So input FD h , output 70h , key= 2

    Input : 1111 1101 Output:1011 000So input FD h , Output: B0h key= 6

    Input : 1111 1101 Output:1101 000So input FD h , Output: D0h key= 10

  • So in our look up table we will store the code , followed by Expected output ,followed by key code , suppose we want to Create a look up table with key codes being ASCII 0 to 9 with Key 1 being 1 and Key 10 being 0 , it would appear as below KEYCODE: DB 0FEH,70H,'1' DB 0FEH,0B0H,'5' DB 0FEH,0D0H,'9' DB 0FDH,70H,'2' DB 0FDH,0B0H,'6' DB 0FDH,0D0H,'0' DB 0FBH,70H,'3' DB 0FBH,0B0H,'7' DB 0F7H,70H,'4' DB 0F7H,0B0H,'8'After creating the look up tableStore it in On chip Rom .

  • KEYPAD INTERFACINGDecoding Algorithm :

    Step1: Initialize pointer to Look Up Table

    Step2: Output code ,in pointer and read input

    Step3: Input == Expected Input Key found exit loop

    Step4: Inc pointer to point to next code location and go to Step 2

  • KEYPAD INTERFACING KEYFND: MOV DPTR, #KEYCODE keyfnd1: MOV A,#00H ; initialize acc to 0 MOVC A,@A+DPTR ; get the first code into acc INC DPTR ; inc DPTR to point to expected output MOV PORT,A ; PUT KEYCODE INTO PORT1 NOP NOP MOV A,PORT ; READ FROM THE PORT ANL A,#0F0H ; MASK THE LOWER NIBBLE MOV R1,A ; STORE ACC. IN REG. R1 MOV A,#00H MOVC A,@A+DPTR ; LOAD THE EXPECTED NO. IN AThe code for the above is as follows

  • CJNE A,01,NEXT INC DPTR ;IF KEY FOUND POINT TO THE CODE CLR A MOVC A,@A+DPTR MOV R3,A ;STORE RESULT IN REG R3 SJMP KEYREL NEXT: INC DPTR INC DPTR SJMP KEYFND 1

  • KEYREL: MOV PORT,#0F0H ; make all the columns LOW MOV A,PORT ; read from the ports ANL A,#0F0H ; mask the lower nibble CJNE A,#0F0H,KEYREL ; check if there is key press MOV A,R3 LCALL WRTDAT SJMP KEYCHK KEY RELEASE

  • SERIAL COMMUNICATIONDB 9 ConnectorFor most of our applicationsonly PIN 2,3 and 5 arerequired

    PINDescription1Data carrier detect2Receive Data (RxD)3Transmit Data (TxD)4Data terminal ready5Signal Ground (GND)6Data set ready7Request to send8Clear to send9Ring Indicator

  • CLASSIFICATIONDTE: Data terminal equipment ,like computers that send and receive dataDCE: Data communication equipment like modems that transferdataTo connect two DTE equipment we use what is called as the nullmodem connection , we will be using this scheme to connect ourcontroller to PC NULL MODEM CONNECTION

  • 8051 and RS 232 8051 is a TTL based chip where as the RS 232 standardIs not TTL compatible so we Require line drivers like the MAX 232 chips which convertTTL logic to RS 232 compatible signals and also vice versa. Each MAX 232 has two sets of line drivers as shown in the Fig

    SERIAL COMMUNICATION

  • SERIAL COMMUNICATION8051 uses two pins of port 3 for serial communication

    P3.0 = Rxd P3.1 = TxD As they are TTL compatible they need to be connected to a linedriver

  • SERIAL COMMUNICATIONProgramming the Serial Interface:

    Programming involves three Registers

    TMOD Register : Timer 1 in mode 2 i.e. auto reload mode is used to generate the baud rate for serial data transfer

    SBUF Register : This the register where data ,to be sent , received is stored

    3. SCON Register : Serial Control register used to configure the various modes also has flags to test whether transfer is compete etc.

  • SERIAL COMMUNICATIONBAUD RATE : The BAUD rate for serial communication is programmable . Basic clock for BAUD rate is derived as follows Typical BAUD Rates can be derived from the above as shown

    28 800/24 = 1200 28 800/12 = 240028 800/6 = 480028 800/3 = 9600

  • SERIAL COMMUNICATIONTimer Mode 2:8 bit timer so can count from 00 to FF h only Count is loaded in TH and when timer is started count value Is loaded into TL automatically.3. When TL rolls over from FF to 00 the TF flag is set and also the count stored in TH is reloaded into TL, so just by clearing The TF flag the whole process can be repeated again, no need To reload as in mode 1.

  • SERIAL COMMUNICATIONCount Values : If we compute the count values to be loaded intoTH to generate the standard baud rates we would get the followingSo to set a 9600 Baud rate the following statement would be reqd

    MOV TMOD ,# 20h ; Timer 1 mode 2MOV TH1,#FDh or MOV TH1,# -3

    BAUD RATETH1(HEX)TH1(DEC)9600FD-34800FA-62400F4-12

  • SCON register : 8 bit Used for programming the start and Stop bit and data bits of data framing

    SM0 SM1 SM2 REN TB8 RB8 TI RISBUF register : 8 bit Used for serial comm of 8051 You place a byte in sbuff prior to sending Byte is placed in sbuff when received from out side

    MOVSBUFF , #D; Load sbuff=44h , ascii for DMOVsbuff,A;copy acc into sbufMOVA,SBUFF;copy SBUF into accum

  • SM0SCON.7 Serial mode specifierSM1SCON.6 Serial port mode specifierSM2SCON.5 Used for multiprocessor communicationRENSCON.4 Set/Cleared by soft ware to enable/disable receptionTB8 SCON.3 not widely usedRB8SCON.2 not widely usedTiSCON.1 Transmit interrupt Flag. Set by hardware at the beginning of the stop bit in mode 1 Must be cleared by softwareRiSCON.0 Receive interrupt flag set by hardware half way thru the stop bit time in mode 1

  • START: MOV TMOD,#20h MOV TH1, #-3 MOV SCON,#50h SETB TR1AGAIN: MOV SBUF,#AHERE: JNB TI,HERE CLR TI SJMP AGAIN MOV TMOD ,#20h MOV TH1,#-3 MOV SCON ,#50h SETB TR1

    HERE: JNB Ri, HERE MOV A, SBUF MOV P2,A CLR RI SJMP HERE

  • THANK YOU

    END OF SESSION 2


Recommended