Serial port interruption experiment based on 51 single chip microcomputer

Publisher:脑电狂潮Latest update time:2014-12-26 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

**************************  Copyright(C) CaKe  ****************************************

==================================================
===============**Copyright information: CaKen Studio ==============
===============**File name: main.c ==============
===============**Author : CaKen ==============
===============**Version number: V1.0 ==============
===============**Time : 2014.08.27 ===============
===============**Function description: Serial port interruption experimental test ===============
================================================
*******************************************************
 
#include
#define uchar unsigned  char
#define uint unsigned int
uchar data trdata[]={'C','a','K','e','n','_', 'Z','H','C','K','-' ,'5','1',0xaa};
sbit KEY = P3^2;
*******************************************************
Function name: UART_Init();
transfer:();
 
Remark:
*******************************************************
void UART_Init(void)
{
   SCON = 0x50; //Serial port working mode is 1, serial reception is allowed
   TMOD = 0x20; //Timer 1 works in mode 2
   TH1 = 0xfd; //Baud rate 9600bps fosc="11".0592MHz
   TL1  = 0xfd;
   EA = 1; // Enable general interrupt
   ES = 1; //Open serial port interrupt
   TR1 = 1; // Allow timer 1 to work
}
********************************************************
Function name: Delay function
transfer:();
******************************************************
 
void delay1ms(uint i)
{
flying j;
  while(i--)
  {
   for(j=0;j<115;j++) //1ms benchmark delay program
   {;}
  }
}
 
******************************************************
Function name: Uart_SendChar();
transfer:();
Parameters:
return value:
result:
Note: Send characters
*******************************************************
 
void Uart_SendChar(unsigned char dat)
{
   SBUF = dat; //The data to be sent is written into the buffer
   while(!TI); //Wait for sending to complete
   TI = 0; // Clear the send flag
}
 
****************************************************
Function name: Uart_SendString();
transfer:();
Parameters:
return value:
result:
Note: Send string
*****************************************************
 
void Uart_SendString()
{ fly i=0;
    while(trdata[i]!=0xaa)
  {
   SBUF=trdata[i];
        while(!IF);
        IF = 0;
        i++;
     }
}
 
***********************************************
Function name:
transfer:();
Parameters:
return value:
result:
Remark:
*****************************************************
void main(void)
{
    UART_Init(); //Initialize the serial port
    while(1)
     {
    if(KEY==0)
     {
    delay1ms(20);
    if(KEY==0)
    {
      Uart_SendString();
   while(!KEY); //Wait for the key to be released
    }
  }
     }
}
 
******************************************************
Function name: Serial port interrupt function
********************************************************
 
void INT_UartRcv(void) interrupt 4
{
    unsigned char Rcv = 0;
    if(RI) //Query the receive flag (set to 1 when data is sent)
      {
         RI = 0; // clear the receiving flag
         Rcv = SBUF; //Storage buffer data
         P0 = SBUF;
         Uart_SendChar(Rcv); //Let the data uploaded from the computer to the MCU be displayed on the computer when it is returned
 
      }
}
 
=================================================
 ------------------- CaKen Studio Original Work --------------------
 ------------------- CaKen Studio All Rights Reserved --------------------
========================================================
Reference address:Serial port interruption experiment based on 51 single chip microcomputer

Previous article:16-key electronic keyboard experimental routine based on 51 single-chip microcomputer
Next article:PWM (LED gradient) experimental test based on 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号