Вы находитесь на странице: 1из 5

Embedded Systems IE403

Lab Work

Lab #12

Objective
To perform serial communication between two microcontrollers

Theory
Serial communication is often used either to control or to receive data from an embedded microprocessor. Serial communication is a form of I/O in which the bits of a byte begin transferred appear one after the other in a timed sequence on a single wire. Serial communication has become the standard for intercomputer communication. As shown in figure 12.1, each byte is preceded by a start bit and followed by one stop bit. The start and stop bits are used to synchronize the serial recivers. The data byte is always transmitted least-significantbit first. For error checking it is possible to include a parity bit as well, just prior to the stop bit. The bits are transmitted at specific time intervals determined by the baud rate of the serial signal. The baud rate is the reciprocal of the time to send 1 bit. Error-free serial communication requires that the baud rate, number of data bits, number of stop bits, and presence or absence of a parity bit be the same at the transmitter and at the receiver.

Figure 12.1: Data being transmitted serially

8051 provides a transmit channel and a receive channel of serial communication. The transmit data pin (TXD) is specified at P3.1, and the receive data pin (RXD) is at P3.0. The serial signals provided on these pins are TTL signal levels and must be boosted and inverted through a suitable converter(MAX232) to comply with RS232 standard if we want to communicate with the PC. All modes are controlled through SCON, the Serial CONtrol register. The SCON bits are defined as SM0, SM1, SM2, REN, TB8, RB8, TI, RI from MSB to LSB. The timers are controlled using TMOD, the Timer MODe register, and TCON, the Timer CONtrol register. The hardware connections are simple as shown in the figure 12.2

Figure 12.2: Hardware Connections for serially interfacing two microcontrollers

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #12

Flow Chart
Serial Receive ISR Serial Transmit ISR

START

Initialize Serial Communication

Display this data on 7-seg

Clear TI flag

Wait for user input by keypad

Clear RI flag

Return

Prepare to transmit this data

Return

END
Figure 12.3: Flow Chart for serial communication between two controllers

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #12

Source Code
LOC OBJ LINE 1 2 communication 3 microcontroller 4 5 ORG 0000H 6 LJMP MAIN 7 0023 8 ORG 0023H 0023 020188 9 LJMP SERIAL 10 0160 11 ORG 0160H ;7-Seg. Common Anode code for digits 0 to 9 and letters A to F 0160 C0F9A4B0 12 CA: DB 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E 0164 999282F8 0168 80908883 016C C6A1868E 13 0170 14 MAIN: 0170 900160 15 MOV DPTR,#CA ;Common-Anode/Cathode Choice 0173 758920 16 MOV TMOD,#20H ;timer 1, mode-2(autoreload) 0176 758DFD 17 MOV TH1,#0FDH ;9600 baud-rate 0179 759850 18 MOV SCON,#50H ;8-bit, 1 stop, REN enabled 017C 75A890 19 MOV IE,#90H ;enable serial interrupt only 017F D28E 20 SETB TR1 ;ready serial communication 0181 21 FOREVER: 0181 3199 22 CALL IN_HEX ;get hex code from keypad 0183 93 23 MOVC A,@A+DPTR ;Encode data to 7-segment 0184 F599 24 MOV SBUF,A ;transmit this data serially 0186 80F9 25 SJMP FOREVER ;repeat this forever 26 0188 27 SERIAL: 0188 20990B 28 JB TI,TRANS ;jump if TI is high 018B C0E0 29 PUSH ACC ;save A 018D E599 30 MOV A,SBUF ;otherwise due to receive 31 ;MOVC A,@A+DPTR ;encode data to 7-segment 018F F5A0 32 MOV P2,A ;send this 7-segment at port2 0191 D0E0 33 POP ACC ;restore A 0193 C298 34 CLR RI ;clear RI since CPU does not 0195 32 35 RETI ;return from ISR 0196 36 TRANS: 0196 C299 37 CLR TI 0198 32 38 RETI 39 40 41 ;IN_HEX - input hex code from keypad with debouncing 0000 0000 020170 ;it also displays data on 7-seg received from other SOURCE ;This program reads data from a hex keypad ;and sends it to another microcontroller using serial

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403


42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

Lab Work

Lab #12

0199 0199 019B 019B 019D 019F 01A1 01A3 01A3 01A5 01A5 01A7 01A9 01AB 01AD

7B01 31AE 50FA DBFA C0E0 7B01 31AE 40FA DBFA D0E0 22

;for key press and key release (50 repeats for each) IN_HEX: MOV R3,#1 ;debounce count BACK: CALL GET_KEY ;key pressed? JNC IN_HEX ;no: check again DJNZ R3,BACK ;yes: repeat 50 times PUSH ACC ;save hex code BACK2: MOV R3,#1 ;wait for key release BACK3: CALL GET_KEY ;key pressed? JC BACK2 ;yes: keep checking DJNZ R3,BACK3 ;no: repeat 50 POP ACC ;recover hex code RET ;return ;GET_KEY - get keypad status ; - returns C = 0 if no key is pressed ; - returns C = 1 and hex code in GET_KEY: MOV A,#0FEH ;start with column 0 by grounding it MOV R6,#4 ;use R6 as column counter TEST: MOV P1,A ;activate (ground) current column MOV R7,A ;save ACC (accumulator) MOV A,P1 ;read back port ANL A,#0F0H ;isolate rows by masking columns CJNE A,#0F0H,KEY_HIT ;any row active? MOV A,R7 ;no: move to RL A ;ground next column DJNZ R6,TEST CLR C ;no key pressed SJMP EXIT ;return with C = 0 KEY_HIT: MOV R7,A ;save in R7 MOV A,#4 ;prepare to calculate column CLR C ;clear carry flag SUBB A,R6 ;column number = 4 - R6 MOV R6,A ;save result in R6 MOV A,R7 ;restore rows scan result SWAP A ;put this in low nibble MOV R5,#4 ;use R5 as counter AGAIN: RRC A ;rotate-right in carry JNC DONE ;done when C = 0 INC R6 ;add 4 INC R6 ;until the row is detected INC R6 ;R6 finally INC R6 ;holds the hexcode of pressed key DJNZ R5,AGAIN ;repeat until all rows checked

Accumulator if pressed 01AE 62 01AE 74FE 63 01B0 7E04 64 01B2 65 01B2 F590 66 01B4 FF 67 01B5 E590 68 01B7 54F0 69 01B9 B4F007 70 01BC EF 71 01BD 23 72 01BE DEF2 73 01C0 C3 74 01C1 8015 75 01C3 76 01C3 FF 77 01C4 7404 78 01C6 C3 79 01C7 9E 80 01C8 FE 81 01C9 EF 82 01CA C4 83 01CB 7D04 84 01CD 85 01CD 13 86 01CE 5006 87 01D0 0E 88 01D1 0E 89 01D2 0E 90 01D3 0E 91 01D4 DDF7 92

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403


01D6 01D6 D3 01D7 EE 01D8 01D8 22 93 94 95 96 97 98

Lab Work
DONE: SETB C MOV A,R6 EXIT: RET END

Lab #12

;C = 1 (key press confirmed!) ;save key hex code in A

Code 12.1: ASM listing for serial communication between two controllers

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Вам также может понравиться