51 MCU serial communication test assembly program

Publisher:EtherealBeautyLatest update time:2015-05-26 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
(I just figured it out today): PC transmits commands and data to the MCU system through the serial port assistant: it starts with A5 and ends with 5A; the data in the middle has different lengths, and the data part is required to be displayed with LED lights; and it is required to be displayed in a loop;

//This is my x_Ed program code
//we use it as the pc communicated with the mcu
//At the same time,we want to see the result by LCD; 
     STFLAG BIT 00H //Received start code flag, 1 for received start code
     EDFLAG BIT 01H //Arrived end code flag, 1 for received end code
     TMFLAG BIT 02H //Timing time arrived flag, 1 for timing time arrived
  ORG 0000H
  SJMP Initialize//Main program entry (initialization program)
  ORG 000BH //Timer 0 entry
     LJMP TIMER0 //Timer 0 interrupt
  ORG 0023H //Entry address of serial port interrupt program
  LJMP Transfer //Jump to accept interrupt entry
////////////////////////////////////////////////////////////////////
     ORG 0050H
Initialize:
     MOV SP,#70H //Set stack
  MOV TMOD,#21H //T1 working mode 2 T0 working
  MOV TH1,#0FDH //Baud rate 9600
  MOV TL1,#0FDH //Baud rate 9600 automatic reload
  MOV TH0,#3CH //Timing 50ms
     MOV TL0,#0BH //Timing 50ms
  MOV SCON,#50H //Serial port working mode 1
     MOV R6,#00H //Timing times counter 20 one second
     MOV R5,#00H //Received data length counter
     MOV R4,#00H //Control output control register
     MOV R0,#30H //Data storage address
     MOV R1,#30H //Control output data cache
     CLR STFLAG //Clear start flag
     CLR EDFLAG //Clear end flag
     CLR TMFLAG //Clear clock flag
     SETB PS //Increase the priority of serial port interrupt
     SETB TR1 //Turn on timer 1;
     SETB ES //Turn on serial port interrupt enable bit
     SETB ET0 //Timer 0 interrupt enable bit
     SETB EA //Turn on global interrupt enable bit
//////////////////Wait for receiving command ////////////////////////
Main: JB STFLAG,NODE3 //Start bit has been received
        SJMP Main //Not started, continue waiting
NODE3: JB EDFLAG,NODE4 //End bit has been received
        SJMP Main //Not ended, continue waiting
NODE4: SETB TR0 //Turn on timer 0;
NODE5: JB TMFLAG,OUTPUT
  SJMP NODE5         
 ////////////////////Wait for host computer to transmit data and record it ////////
Transfer: CLR ES
          MOV A,SBUF
    CJNE A,#0A5H,NODE0 //Start bit detected
    SETB STFLAG
    SJMP JIEDIAN
NODE0: CJNE A,#05AH,NODE1 //Detect the end bit
          SETB EDFLAG
    MOV DPH,R5
    MOV R4,DPH
    clr ES //Open the serial port interrupt enable bit
    SJMP ret00
NODE1: MOV @R0,A //Neither the start code nor the end code, it is data
          INC R0
          INC R5
    MOV SBUF,#055H  
JIEDIAN: CLR TI
    CLR RI
          SETB ES
ret00: RETI
TIMER0: CLR TR0
        MOV TH0,#3CH
        MOV TL0,#0B0H
        INC R6
        CJNE R6,#20,RTN
        SETB TMFLAG
        MOV R6,#00H
RTN: SETB TR0
     RETI

OUTPUT: CLR TR0
        clr TMFLAG
        MOV A,@R1
        MOV P1,A
        INC R1
        DJNZ R4,NODE4
        MOV R1,#30H
  CLR TMFLAG
  MOV DPH,R5
  MOV R4,DPH
  SJMP NODE4
  RETI
END

Reference address:51 MCU serial communication test assembly program

Previous article:MCU Assembly Experiment 7: Scanning Keyboard and LCD Display Experiment
Next article:51 MCU clock assembly program

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号