C51 program for simulating RS232 serial communication with microcontroller IO port

Publisher:星辰耀眼Latest update time:2015-07-02 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This program has been used in one of my projects and is very stable. During the writing process, I referred to    the article http://www.51hei.com/mcu/1541.html on the 51hei website. Some subroutines are excerpted here.

#include "reg52.h"
#include "intrins.h"  
#include "math.h"     
#include "stdio.h"
sbit BT_SND =P1^5;
sbit BT_REC =P1^6;
sbit LED =P1^7;
bit LED_flage=1;
//MCU IO port simulates 232 serial port communication program
//C program using two methods to occupy timer 0
#define MODE_QUICK
#define F_TM F0
#define TIMER0_ENABLE TL0=TH0; TR0=1;
#define TIMER0_DISABLE TR0=0;
sbit ACC0= ACC^0;
sbit ACC1= ACC^1;
sbit ACC2= ACC^2;
sbit ACC3= ACC^3;
sbit ACC4= ACC^4;
sbit ACC5= ACC^5;
sbit ACC6= ACC^6;
sbit ACC7= ACC^7;

void IntTimer0() interrupt 1
{
  F_TM=1;
}
//Send a character
void PSendChar(unsigned char inch)
{
 #ifdef MODE_QUICK
    ACC=inch;
    F_TM=0;
    BT_SND=0; //start bit
    TIMER0_ENABLE; //Start
    while (!F_TM);
    BT_SND=ACC0; //Send the low bit first
    F_TM=0;
    while(!F_TM);
    BT_SND=ACC1;
    F_TM=0;
    while(!F_TM);
    BT_SND=ACC2;
    F_TM=0;
    while(!F_TM) );
    BT_SND=ACC3;
    F_TM=0;
    while(!F_TM);
    BT_SND=ACC4;
    F_TM=0;
    while(!F_TM);
    BT_SND=ACC5;
    F_TM=0;
    while(!F_TM);
    BT_SND=ACC6;
    F_TM= 0;
    while(!F_TM);
    BT_SND=ACC7;
    F_TM=0;
    while(!F_TM);
    BT_SND=1;
    F_TM=0;
    while(!F_TM);
    TIMER0_DISABLE; //Stop timer
    #else
    unsigned char ii;
     ii=0;
     F_TM=0;
     BT_SND= 0; //start bit
     TIMER0_ENABLE; //Start
     while(!F_TM);
     while(ii<8)
 {
   if(inch&1)
  {
   BT_SND=1;
  }
  else
  {
   BT_SND=0;
  }
   F_TM=0;
 while(!F_TM) ;
   ii++;
  inch>>=1;
    }
  BT_SND=1;
  F_TM=0;
  while(!F_TM);
    #endif
    TIMER0_DISABLE; //Stop timer
 }
//Receive a character
unsigned char PGetChar()
{
 #ifdef MODE_QUICK
 TIMER0_ENABLE;
 F_TM =0;
 while(!F_TM); //Wait until the start bit
 ACC0=BT_REC;
 TL0=TH0;
 F_TM=0;
 while(!F_TM);
 ACC1=BT_REC;
 F_TM=0;
 while(!F_TM);
 ACC2=BT_REC;
 F_TM =0;
 while(!F_TM);
 ACC3=BT_REC;
 F_TM=0;
 while(!F_TM);
 ACC4=BT_REC;
 F_TM=0;
 while(!F_TM);
 ACC5=BT_REC; 
 F_TM=0;
 while(!F_TM) ;
 ACC6=BT_REC; 
 F_TM=0;
 while(!F_TM);
 ACC7=BT_REC;
 F_TM=0;
 while(!F_TM)
    {
  if(BT_REC)
  {
    break;
  }
    }
 TIMER0_DISABLE; //Stop timer
 return ACC;
 #else
 unsigned char rch,ii;
 TIMER0_ENABLE;
 F_TM=0;
 ii=0;
 rch=0;
 while(!F_TM); //Wait for the start
 bitwhile(ii<8)
    {
     rch>>=1;
  if(BT_REC)
  {
  rch|=0x80;
  }     
  ii++;
  F_TM=0;
  while(!F_TM); 
    }
 F_TM=0;
  while(!F_TM)
    {
  if(BT_REC)
  {
     break;
  }
    }
 TIMER0_DISABLE; //Stop timer
 return rch;
 #endif
}
//Check if there is a start
bit StartBitOn()
{
  return (BT_REC==0);
}
//Timer 1 initialization
void Time1_Init(void)
{
   TMOD=0x22; //Timer 1 is working mode 2 (8-bit automatic reload), 0 is mode 2 (8-bit automatic reload) 
   PCON=00;
   TR0=0; //Start using it when sending or receiving
   TF0=0;
   TH0=(256-96); //9600bps means the timer is executed in 1000000/9600=104.167 microseconds//104.167*11.0592/12= 96
   TL0=TH0;
   ET0=1;
   EA=1;
}
//Send string
void Send_Char(char *byte)
{
   int i=0;
   for(i=0;*(byte+i)!='';i++)
   {
      PSendChar(*(byte+i));
   }
}
//void delay(int x)
//{
// int a,b;
// for(a=x;a>0;a--)
// for(b=10;b>0;b--); 
//}
//void main()
//{
// unsigned char gch;
// Time1_Init();
// LED=0;
// // Send_Char("S00.0C00.0%E00.0C00.0%L00000lx");
// while(1)
// {   
//      
// PSendChar('1');
// delay(1000);
//// if(StartBitOn())
//// {
//// gch=PGetChar();
//// if(gch=='1')
//// {
//// LED=LED_flage;  
//// delay(1000);
//// LED_flage=~LED_flage;
//// } 
////  
//// } 
//    
// }    
//
//} 

Keywords:MCU Reference address:C51 program for simulating RS232 serial communication with microcontroller IO port

Previous article:51 MCU + 315M wireless RF module receiving program
Next article:Experience in designing and debugging an "electronic calendar" based on the DS1302 digital chip

Recommended ReadingLatest update time:2024-11-16 13:40

Renesas unveils next-generation automotive SoC and MCU processor product roadmap
Future product lineup includes R-Car SoC using advanced chiplet integration technology and automotive MCUs based on Arm® cores November 8 , 2023 , Beijing, China - Renesas Electronics, a global semiconductor solutions provider , today unveiled its next-generation system-on-chip (SoC) and microcontroller
[Embedded]
Renesas unveils next-generation automotive SoC and MCU processor product roadmap
PCB design principles for single-chip microcomputer control boards
  According to incomplete statistics, electric shock accidents and fires caused by leakage in my country cause thousands of deaths and billions of economic losses every year, so higher requirements are put forward for the performance of leakage protectors that can prevent leakage fires and personal electric shock prote
[Microcontroller]
PCB design principles for single-chip microcomputer control boards
Design of Air Quality Monitoring System Based on Single Chip Microcomputer
This design uses the GP2Y1010AU0F sensor produced by Sharp to collect and detect the PM2.5 concentration value in the nearby air, and then uses the ADC0832 analog-to-digital conversion chip to convert the analog voltage signal collected and output by the sensor into a digital signal quantity that is easy to process, a
[Microcontroller]
Design of Air Quality Monitoring System Based on Single Chip Microcomputer
Analysis of various assembly language instructions of PIC microcontroller
PIC microcontroller is one of the most commonly used microcontrollers, and many programs are developed based on PIC microcontrollers. Therefore, it is particularly important to be proficient in PIC microcontroller programming. In order to ensure that everyone can accurately and proficiently master the use of PIC micro
[Microcontroller]
Analysis of various assembly language instructions of PIC microcontroller
51 MCU knowledge points sorting out - interrupt
The interrupt system of 89C51 has 5 interrupt sources: external interrupt 0, timer 0, external interrupt 1, timer 1, serial port interrupt (in descending order of priority); 2 interrupt priorities: high priority and low priority. Interrupt related registers Interrupt priority control register IP PS: Serial port i
[Microcontroller]
51 MCU knowledge points sorting out - interrupt
Introduction to software reset method of MCS51 series microcontroller
    Some single-chip microcomputers (such as 8098) have special reset instructions. Although some enhanced MCS-51 system single-chip microcomputers do not have reset instructions, they have integrated WATCHDOG circuits in the chip, so anti-interference is not a problem. Since the popular MCS-51 series single-chip micr
[Microcontroller]
Using STM8 microcontroller + NTC thermistor to make a simple temperature inspection instrument
  Recently, I need to monitor the temperature of the equipment when testing it. Usually, I use an infrared thermal imager to test it and then record the data manually. This kind of test is very labor-intensive and requires a few minutes to record the data. So I wondered if I could use a microcontroller to make a tempe
[Microcontroller]
Using STM8 microcontroller + NTC thermistor to make a simple temperature inspection instrument
Detecting Low-g Acceleration Using the ADuC7024 Precision Analog Microcontroller and the ADXL345 Digital Accelerometer
Circuit Functionality and Benefits The ADXL345 is a small, thin, low-power, 3-axis accelerometer that can measure accelerations up to ±16 g with high resolution (13 bits). The digital output data is in 16-bit two's complement format and can be accessed via an SPI (3-wire or 4-wire) or I2C digital interface.
[Analog Electronics]
Detecting Low-g Acceleration Using the ADuC7024 Precision Analog Microcontroller and the ADXL345 Digital Accelerometer
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号