C language source code for simulating serial port

Publisher:TurquoiseLatest update time:2011-04-11 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This program is written to simulate the serial port hardware mechanism. When used, a timed interrupt can be set with a time interval of 1/4 baud rate. The receiving function is called once for each interrupt, and the sending function is called once for each 4 interrupts. However, the clock does not need to be fast for the microcontroller. You should know that the time interval of each BIT of the 9600 baud rate is 104us. The time for the microcontroller to push and pop the stack once is about 20us (standard 51-core 12M crystal). So the processing time must be considered clearly. Haha. The following program is placed in the timer interrupt program function

//Receiving part
sbit JieShou_D= ;//Define receiving port
uint8 DingShiJiShu, JieShou_h;//Timing count, receiving buffer
uint16 JieShou_T;//Receiving temporary register
bit KaiShi, JieShou_b;//Start receiving standard, receiving completion flag


void JieShou(void) //Receive function, receive one bit every 4 calls
{
if((KaiShi==0) && (JieShou_D==0)) //Serial start bit arrives
{
DingShiJiShu=0; //Start timing
countKaiShi=1;
JieShou_T=0xffff; //Receive temporary register set to all 1s
}
else if((KaiShi==1) && (DingShiJiShu==1)) //2nd call, serial data sampling time arrives
{
JieShou_T >>= 1;
if(JieShou_D) JieShou_T |=0x8000;
}
else if(JieShou_T & 0x807f ==0x803f) //Receive completedJieShou_T=1xxx_xxxx_x011_1111
{
KaiShi=0;
DingShiJiShu=0;
JieShou_h = JieShou_T >> 7; //Shift right 7 bits to get serial data
JieShou_b=1;
}

if(KaiShi)
{
DingShiJiShu++; //Count +1 only when receiving
DingShiJiShu &=0x03; //Receive one bit every 4 calls
}
}


//Sending part
sbit FaSong_D= ;//define sending port
uint16 FaSong_h; //sending buffer
bit FaSong_b; //sending completion flag

void KaiShiFaSong(uint8 fs) //Set the data to be sent and start sending
{
FaSong_h = fs;
FaSong_h <<= 1;
FaSong_h |= 0x0200; //FaSong_h=0000_001x_xxxx_xxx_0
FaSong_b=0; //Send flag = 0 means sending is in progress
}

void FaSong(void) //Sending function, one bit is sent each time it is called
{
if(FaSong_h) //Sending is not completed
{
if(FaSong_h & 0x0001)
FaSong_D=1;
else FaSong_D=0;

FaSong_h >>= 1;
}
else
FaSong_b =1; //Sending completed flag = 1 means sending completed
}

Reference address:C language source code for simulating serial port

Previous article:Simulation and production of music player based on single chip microcomputer
Next article:Detailed explanation of the role of volatile definition in 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号