📄 main.c
字号:
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
#include <C8051F020.H>
#include <stdio.h> /* prototype declarations for I/O functions */
bit sio_port = 1; /* SIO port to use (0 = SIO0) */
/*-----------------------------------------------------------------------------
The following putchar function replaces the one in the library.
-----------------------------------------------------------------------------*/
char putchar (char c)
{
if (sio_port == 0)
{
while (!TI0);
TI0 = 0;
SBUF0 = c;
}
else
{
while (!(SCON1 & (1 << 1))); /* While TI1 = 0 */
SCON1 &= ~(1 << 1); /* TI1 = 0 */
SBUF1 = c;
}
return (c);
}
/*-----------------------------------------------------------------------------
The following _getkey function replaces the one in the library.
-----------------------------------------------------------------------------*/
char _getkey (void)
{
char c;
if (sio_port == 0)
{
while (!RI0);
c = SBUF0;
RI0 = 0;
}
else
{
while (!(SCON1 & (1 << 0))); /* While RI1 = 0 */
c = SBUF1;
SCON1 &= ~(1 << 0); /* RI1 = 0 */
}
return (c);
}
/*-----------------------------------------------------------------------------
The main C function.
-----------------------------------------------------------------------------*/
void main (void) {
/*------------------------------------------------
NOTE: You must properly configure the crossbar
for your application.
------------------------------------------------*/
/*------------------------------------------------
Disable the watchdog timer. This should probably
be done in the STARTUP code.
------------------------------------------------*/
WDTCN = 0xDE;
WDTCN = 0xAD;
/*------------------------------------------------
Setup the serial port 0 for 1200 baud at 2MHz
using timer 2.
See http://www.keil.com/c51/baudrate.asp for
baudrate calculations and reload values.
------------------------------------------------*/
SCON0 = 0x50; /* SCON: Mode 1, 8-bit UART, enable rcvr */
T2CON = 0x30; /* T2CON: Use T2 for Baud Rate on SIO 0 */
RCAP2L = 0xCC; /* RCAP2: Reload value for 1200 baud */
TL2 = 0xCC;
RCAP2H = 0xFF;
TH2 = 0xFF;
TR2 = 1; /* TR2: T2 Run */
TI0 = 1; /* TI0: Set TI0 to send first char of SIO 0 */
/*------------------------------------------------
Setup the serial port 1 for 2400 baud at 2MHz
using timer 4.
See http://www.keil.com/c51/baudrate.asp for
baudrate calculations and reload values.
------------------------------------------------*/
SCON1 = 0x50; /* SCON1: Mode 1, 8-bit UART, enable rcvr */
T4CON = 0x30; /* T4CON: Use T4 for Baud Rate on SIO 1 */
RCAP4L = 0xE6; /* RCAP4: Reload value for 2400 baud */
TL4 = 0xE6;
RCAP4H = 0xFF;
TH4 = 0xFF;
T4CON |= 1 << 2; /* TR4: T4 Run */
SCON1 |= 1 << 1; /* TI1: Set TI1 to send first char of SIO 1 */
/*------------------------------------------------
------------------------------------------------*/
while (1) {
char c;
sio_port = !sio_port; /* switch ports */
printf ("Press a key: ");
c = _getkey ();
printf ("\r\nYou pressed %c\r\n", c);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -