STC11F04 MCU serial terminal program

Publisher:AningmengLatest update time:2017-01-17 Source: eefocusKeywords:STC11F04 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A serial terminal program made with STC11F04 microcontroller. Similar to the telnet command of the switch.

Due to time constraints, only a few simple commands were implemented.

 help
LED1_ON
LED1_OFF
LED2_ON
LED2_OFF


For example, enter in the serial terminal? 



Enter the Time command to query the current time inside the microcontroller.


Input LED1_ON to light up LED1 on the board


Input LED1_OFF to turn off LED1 on the board


If the command you enter is not the one above, an error message will be displayed.



In general, the above functions have been realized so far. Interested friends can add some more commands by themselves.


The entire source code is as follows:

#include "reg51.h"
#include "stdio.h"
#include "intrins.h" //_nop_()
#include "string.h"

unsigned int tick;

int hh,mm,ss; //Hour: minute: second
sbit LED1=P1^0;
sbit LED2=P3^7;

unsigned char cmd_buf[32];
char cmd_len=0;

void cmd(void);

char putchar (unsigned char i)
{
    ES = 0; //Turn off serial port interrupt
    TI = 0; //Clear serial port send completion interrupt request flag
    SBUF = i;
    while(TI ==0); //Wait for sending to complete
    TI = 0; //Clear serial port send completion interrupt request flag
    ES = 1; //Allow serial port interrupt
return SBUF;
}

void Task_1s()
{
  //printf("S=[%d:%d:%d]\r\n",hh,mm,ss);     _nop_
  ();
}
void Task_1m()
{  
  // printf("M=[%d:%d:%d]\r\n",hh,mm,ss);  _nop_(); } void Task_1h() {    //printf("H=[%d:%d:%d]\r\n",hh,mm,ss);      _nop_(); } //0.1ms = 100us void Timer0(void) interrupt 1 using 1 //Timer 0 interrupt handler {    TR0=0;    TH0=(65536-2000)/256;     TL0=(65536-2000)%256;  tick++; //Count value + 1     if(tick>999) //Add to 10 times, that is, 1 second     {        tick=0;  ss++;  if(ss>59){         mm++; ss=0; if(mm>59){  hh++;  mm=0;  if(hh>11){  hh=0;  }  Task_1h();//1 hour scheduled task }         Task_1m(); //1 minute scheduled task   }   Task_1s();//1 second scheduled task     } TR0=1; }













    



          










          









void UART_one_Interrupt_Receive(void) interrupt 4
{
    unsigned char   k   =   0;
    if(RI==1)
    {
        RI  =   0;
        k   =   SBUF;
        if(cmd_len>32)
{
 cmd_len=0;
}
cmd_buf[cmd_len++]=k;
printf("%c",k);


    }
    else
    {
        TI  =  0;
    }
}

void cmd()
{
   if(strncmp(cmd_buf,"time",4)==0)
   {
      printf("Time=[%d:%d:%d]\r\n",hh,mm,ss);
   }
   else if(strncmp(cmd_buf,"LED1_ON",7)==0)
   {
     LED1=1;
   }
   else if(strncmp(cmd_buf,"LED1_OFF",8)==0)
   {
     LED1=0;
   }
    else if(strncmp(cmd_buf,"LED2_ON",7)==0)
   {
     LED2=1;
   }
    else if(strncmp(cmd_buf,"LED2_OFF",8)==0)
   {
     LED2=0;
   }
   else if(strncmp(cmd_buf,"?",1)==0||strncmp(cmd_buf,"help",4)==0)
   {
      printf("time\r\n");
      printf("help\r\n");
 printf("LED1_ON\r\n");
 printf("LED1_OFF\r\n");
 printf("LED2_ON\r\n");
 printf("LED2_OFF\r\n");
   }
   else
   {
     printf("\r\n错误的命令,如果不知道命令请输入【?】或【help】。\r\n"); 
   }
}
void main()
{
  SCON=0x50; //0101,0000 8位可变波特率,无奇偶校验位
  TMOD=0X21;
  TH1=TL1=-(11059200L/12/32/9600);
  TR1=1;
  tick=0;
  hh=0;
  mm=0;
  ss=0;
  TH0=(65536-2000)/256; 
  TL0=(65536-2000)%256; 
  TR0=1;
  ET0=1;
  ES=1;    //允许串口中断
  EA=1;    //开总中断 
  printf("SystemStart...\r\n");
    while(1)
  {
     _nop_();
if(cmd_len>0)
    if(cmd_buf[cmd_len-1]=='\r')
    {
      cmd();
      printf("\r\nEXOS_V1#:");
      cmd_len=0;
    }
  }
}


Summary: STC microcontroller is still powerful enough.

The key to this program is to use the built-in functions of C language to complete the above functions.

stdio.h ------ printf standard output function

string.h ----- strncmp string comparison function


It is recommended to use Xshell software as the terminal debugging tool. As shown below



If you are interested, just copy the code into KeilC51 V2, compile and run it.


Keywords:STC11F04 Reference address:STC11F04 MCU serial terminal program

Previous article:Several important block diagrams of Cortex-M3
Next article:MCU Suffix Description

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号