Communication example between MCU and PC

Publisher:csZhouLatest update time:2016-09-14 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "STC89C51RC_RD_PLUS.h"

#define SENDBUFSIZE 0x0f
#define BR_19K2 0xfd
#define RMAX 0x10 //Must be an integer power of 2. Seen on 21ic, seems to be the idea of ​​"Senior Liu"
unsigned char xdata *cpSendBufPtr;
unsigned char cFront;
unsigned char cBack;
unsigned char sendData;
bit cTranFlag;
unsigned char cTranData;
bit cRecFlag;
unsigned char cRecData;
bit cJudge;
unsigned char temp;

void HardWareInit();
void Uart_Init(void);
void Uart_Init(void); 
void COMM_Init(unsigned char cBaudRate, bit bSMOD);   //初始化UART

//sfr AUXR = 0x8E;
//MODE COM4 19200,0,8,1;ASSIGN COM4 SOUT
//The baud rate is 19200Hz. Will the data received by the serial port be forwarded again?
//The data receiving speed may be greater than the sending speed. Increase the buffer and save the received data first.
main()
{
 sendData=0;
 cFront=0; //
 cBack=0;//
 cRecFlag=0; //No data received yet
 cTranFlag=1;//After the system is reset, as long as the queue is not empty, data can be sent at any time
 RS0 = RS1 = 0;// Use register group 0  
    AUXR = 0x01; //Disable ALE signal output
 EA = 0;
 P1 = 0xFF;
    P2 = 0xFF;
    P3 = 0xFF;
    P4 = 0xFF;
 
 ES = 0;
 TR1 = 0;
 TMOD = TMOD & 0x0f | 0x20;
 TH1 = BR_19K2;
 TL1 = BR_19K2; ////------------------------------------------------------------------------------------------
 PCON = 0x80; // If SMOD=1, baud rate multiplication
 
 SCON = 0x50; // 8-bit
 IP = 0x10; // Serial port priority interrupt
 ES = 1;

 // wifi module: baud rate 19200/38400/57600/115200bps data bit: 8bits check bit: none stop bit: 1bit flow control: none
 EA = 1;
 TR1 = 1;
// SBUF=0x12;
 while(1)
 {
/*   
*/// 14 15 16 17 18 19 20 21
   if(cRecFlag)
   {
    temp=((cFront+1)^cBack)&0x0f;
  
    if(temp!=0) //The circular linked list is not full yet, you can continue to add data
    {
    cFront=cFront+1;
    cFront=cFront&(RMAX-1);
  cpSendBufPtr[cFront]=cRecData;
  cRecFlag=0;
  }
   }
   if(cTranFlag) 
   {
  temp=(cBack^cFront)&0x0f;//A new number is sent, this place is cBack instead of cBack+1
  
    if(temp!=0) //Data can be sent when the circular linked list is not empty
    {

    cBack=cBack+1;
    SBUF=cpSendBufPtr[cBack];
    cpSendBufPtr[cBack]=0x00;
    cBack=cBack&(RMAX-1);
    cTranFlag=0;
    }
   }   
 }
}

/*******************************************************************************
// Function: UART completes data transmission and reception and generates an interrupt program
// Input parameter: None
// Output parameter: None
// Return value: None
******************************************************************************/
void COMM_INT() interrupt 4 using 3

  if(RI)//If the received data is being processed, the data in SBUF is discarded ^cRecFlag
 {
  cRecData=SBUF;
  cRecFlag=1;
  RI=0;
 }
 if(TI) 
 {
  cTranFlag=1;
  TI=0;
 }//Sending is completed, and cTranData is assigned a new value*/
}

Keywords:MCU Reference address:Communication example between MCU and PC

Previous article:DAC0832 generates triangle wave and sine wave
Next article:Analysis and Research on the Baud Rate of Simulated Serial Port

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号