Stc11f04 MCU serial port terminal program

Source: Internet
Author: User

Serial port terminal program made of stc11f04 single-chip microcomputer. Similar to the Telnet command of a vswitch.

Due to the rush of time, only a few simple commands are implemented.

Help
Ledbench on
Ledshortoff
Led2_on
Led2_off

For example, enter?

Enter the time command to query the current time in the microcontroller.

You can enter led1_on to light up led1 on the board.

Enter led1_off to extinguish led1 on the board.

If not, an error message is displayed.

In general, the above functions are now implemented. If you are interested, you can add some commands on your own.

The 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 performance_buf [32];
Char character _len = 0;

Void cmd (void );

Char putchar (unsigned char I)
{
Es = 0; // serial disconnection
Ti = 0; // clear the serial port sending complete interrupt request flag
Sbuf = I;
While (Ti = 0); // wait until the sending is completed
Ti = 0; // clear the serial port sending complete interrupt request flag
Es = 1; // allow serial port interruption
Return sbuf;
}

Void task_1s ()
{
// Printf ("s = [% d: % d] \ r \ n", HH, mm, SS );
_ NOP _();
}
Void task_1m ()
{
// Printf ("m = [% d: % d] \ r \ n", HH, mm, SS );
_ NOP _();
}
Void task_1h ()
{
// Printf ("H = [% d: % d] \ r \ n", HH, mm, SS );
_ NOP _();
}
// 0.1 Ms = 100us
Void timer0 (void) interrupt 1 using 1 // timer 0 interrupt Principle
{
Tr0 = 0;
Th0 = (65536-2000)/256;
Tl0 = (65536-2000) % 256;

Tick ++; // Count value + 1
If (tick> 999) // Add 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 (); // one-minute scheduled task
}
Task_1s (); // 1 s scheduled task
}
Tr0 = 1;
}

Void uart_one_interrupt_receive (void) interrupt 4
{
Unsigned char K = 0;
If (Ri = 1)
{
Ri = 0;
K = sbuf;
If (then _len> 32)
{
Required _len = 0;
}
Pai_buf [pai_len ++] = K;
Printf ("% C", k );

}
Else
{
Ti = 0;
}
}

Void cmd ()
{
If (strncmp (pai_buf, "time", 4) = 0)
{
Printf ("time = [% d: % d] \ r \ n", HH, mm, SS );
}
Else if (strncmp (performance_buf, "ledbench on", 7) = 0)
{
Led1 = 1;
}
Else if (strncmp (rj_buf, "led1_off", 8) = 0)
{
Led1 = 0;
}
Else if (strncmp (performance_buf, "led2_on", 7) = 0)
{
Led2 = 1;
}
Else if (strncmp (performance_buf, "led2_off", 8) = 0)
{
Led2 = 0;
}
Else if (strncmp (performance_buf ,"? ", 1) = 0 | strncmp (performance_buf," help ", 4) = 0)
{
Printf ("time \ r \ n ");
Printf ("Help \ r \ n ");
Printf ("ledbench on \ r \ n ");
Printf ("ledbench off \ r \ n ");
Printf ("led2_on \ r \ n ");
Printf ("led2_off \ r \ n ");
}
Else
{
Printf ("\ r \ n wrong command. If you do not know the command, enter 【?] Or [help ]. \ R \ n ");
}
}
Void main ()
{
Scon = 0x50; // 0101,0000 8-bit variable baud rate, no parity bit
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; // allow serial port interruption
Ea = 1; // total disconnection
Printf ("systemstart... \ r \ n ");
While (1)
{
_ NOP _();
If (then _len> 0)
If (performance_buf [cmd_len-1] = '\ R ')
{
CMD ();
Printf ("\ r \ nexos_v1 #:");
Required _len = 0;
}
}
}

Conclusion: The STC microcontroller is powerful enough. Haha

The key of 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

Xshell is recommended as a terminal debugging tool. For example

If you are interested, copy the code to keilc51 V2 and compile it directly to run it.

Good luck!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.