Programming of Serial Communication between Two Single-Chip Microcomputers

Publisher:码农创想家Latest update time:2012-12-05 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Method 1 is used to realize the communication between two single-chip microcomputers. The main frequency is 6M and the baud rate is 2400bps. The circuit is shown in Figure 5-10. When the two single-chip microcomputers are close to each other, the sending end and the receiving end of machine A and machine B are directly connected, and the two machines share the same ground. When the program is executed, machine A sends a light-on signal to machine B. If the communication is normal, machine B will light up 20 light-emitting diodes after receiving the signal. Machine B uses two working modes: query and interrupt. Of course, the 20 LEDs of machine B can be controlled independently, or they can accept the control of machine A and execute the instructions of machine A. The program needs to be further improved.
Machine A sends the program:
org 0000h
sta: mov tmod,#20h ; set the baud rate
mov tl1,#0FAh
mov th1,#0FAh
setb tr1
mov scon,#40h ; set working mode 1
clr ti
mov a,#00h
mov sbuf,a ; send the light signal
wait: jbc ti,cont ; clear the flag after sending successfully
ajmp wait ; wait for the sending to be completed
cont: sjmp sta ; repeat the sending
end

Machine B queries the working mode and receives:

org 0000h
mov tmod,#20h ; set the communication baud rate
mov tl1,#0FAh
mov th1,#0FAh
setb tr1
mov scon,#40h
clr ri
setb ren ;Allow receiving
wait: jbc ri,read ;Receiving successfully clear flag
ajmp wait ;Receiving is not finished waiting
read: mov a ,sbuf
mov p1,a ;Receive the light signal and send it to P1 port
sjmp $
end

Machine B receives in interrupt working mode
org 0000h
ajmp main
org 0023h
ajmp zd ;Switch to serial port interrupt program
START: MOV TMOD,#20h
mov tl1,#0FAh
mov th1,#0FAh
setb tr1
mov scon,#50h
clr ri
mov ie,#90h ;Open interrupt

MAIN:sjmp $ main program
zd: clr ri ;Clear receiving flag
; ===============The interrupt program needs to be improved ==============
mov a ,sbuf ;Read receiving signal
mov p1,a
MOV R1,A ; Send the received signal to the cache
reti ; Interrupt return
end Use mode 2 communication, the data frame format is 11 bits, TB8 is the parity bit, the receiving process requires judging RB8, if there is an error, set the F0 flag to 1, if it is correct, set the F0 flag to 0, and then return. The sending baud rate is 375kbps, the crystal oscillator is 12MHz, so SMOD=1. Since the baud rate of the transmitted data has nothing to do with the timer, there is no need to program the timer in the program. The following program is from a microcontroller hero at http://www.51hei.com . I borrowed it for use. If there are any errors or areas that need improvement, please point them out, but I have basically passed the test here. send: MOV SCON, #80H ; Set the serial port to mode 2 MOV PCON, #80H ; SMOD=1 MOV R0, #50H ; Set the data block pointer MOV R7, #20 ; Set the data block length STA: MOV A, @R0 ; Get data to A MOV C, P MOV TB8, C ; Send the parity bit P to TB8, MOV SBUF, A ; Start sending ; ======================================================= WAIT: JBC TI, CONT ; If one frame of data is sent, clear the flag and send the next frame of data AJMP WAIT ; Unfinished waiting ;==================================================== CONT: INC R0 ; Modify data pointer DJNZ R7, STA ; Loop and send to end RET ;================= ; The receiving program of machine B is as follows: ; When performing two-machine communication, the two machines should use the same working mode and baud rate. ;================================================ MOV SCON, #90H ; Set the serial port to mode 2, REN to 1, and allow reception
Click to browse the next page
























MOV PCON, #80H; SMOD=1
MOV R0, #50H; Set the first address of the data block
MOV R7, #20; Set the length of the data block
; =============================================================
WAIT: JBC RI, READ; After receiving a frame of data, RI is cleared and data is read in
AJMP WAIT; Not finished waiting
; ==============================================
READ: MOV A, SBUF; Read in data
JNB PSW.0, PZ; If the received number is an even number, transfer to
JNB RB8, ERR; If the received number is an odd number and the sending end is an even number, a communication error is made
SJMP RIGHT ; If they match, it is correct
PZ: JB RB8, ERR ; If the received number is an even number and the sending end is an odd number, it is an error
; ===========================================
RlGHT: MOV @R0, A ; Communication is correct, store data
INC R0 ; Change address pointer
DJNZ R7, WAIT ; Whether the data block is received, if not, continue
CLR PSW.5 ; Communication is correct, set F0 to 0
RET ; Return
ERROR: SETB PSW.5 ; Communication error, set F0 to 1
RET ; Return
Reference address:Programming of Serial Communication between Two Single-Chip Microcomputers

Previous article:Listen to the technical experts talk about the 51 single chip microcomputer series
Next article:A Brief Discussion on the RETI Instruction of 51 Single Chip Microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号