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.
Previous article:Several important block diagrams of Cortex-M3
Next article:MCU Suffix Description
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How can LM3447 achieve constant power control?
- 【Nucleo G071 Review】 Final Conclusion
- Differences between CPU and CLA and Error Handling Techniques
- Automobile crankshaft dynamic balance problem-N600
- zstack protocol stack serial port problem
- MY-8188EUS Linux-3.14.52 Test
- MSP430 MCU Development Record (12)
- EEWORLD University Hall----Choosing the latest boost converter and Class D amplifier from Texas Instruments to significantly increase the battery life of the trolley speakers
- Award-winning live broadcast | TI Embedded Live Broadcast Month is waiting for you [Low-power Wi-Fi MCU, Sitara AM57X platform, machine learning]
- SPI settings for msp430f149