#include "reg51.h"
#include "stdio.h"
#include "intrins.h"
#define FOSC 16000000L
#define BAUD 2400
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT NONE_PARITY
sfr T2CON = 0xC8; //timer2 control register
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
sbit PIN1 = P1^0;
sbit PIN2 = P1^1;
sbit PIN3 = P1^2;
unsigned int nCount7F = 0;
unsigned char lIsRx = 0;
unsigned char nRxCount = 0;
bit busy;
void Sleep(unsigned int nLoop)
{
unsigned int i;
for (; nLoop > 0; nLoop--) for (i = 0; i < 10000; i++);
}
void SetUSARTReceiverEnabled()
{
ES = 1;
}
void SetUSARTReceiverDisabled()
{
ES = 0;
}
void UARTInit()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TL2 = RCAP2L = -(FOSC/32/BAUD); //Set auto-reload vaule
TH2 = RCAP2H = (-(FOSC/32/BAUD)) >> 8;
T2CON = 0x34; //Timer2 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
}
void Uart_Isr() interrupt 4
{
char cFlag;
if (RI)
{
RI = 0;
cFlag = SBUF;
if (cFlag == 0x7F) {
nCount7F++;
if (nCount7F > 100) nCount7F = 100;
} else nCount7F = 0;
lIsRx = 1;
}
if (TI)
{
TI = 0;
busy = 0;
}
}
void main() {
char nLoop;
UARTInit();
nCount7F = 0;
PIN1 = 0;
PIN3 = 1;
for (nLoop = 0; nLoop < 4; nLoop++) {
PIN2 = 1;
Sleep(1);
PIN2 = 0;
Sleep(1);
}
for (;;) {
if (nCount7F > 30) {
PIN1 = 1;
PIN2 = 1;
PIN3 = 1;
SetUSARTReceiverDisabled();
Sleep(20);
SetUSARTReceiverEnabled();
PIN1 = 0;
PIN2 = 0;
nCount7F = 0;
}
nRxCount++;
if (nRxCount > 200) {
nRxCount = 0;
lIsRx = 0;
}
if (nRxCount > 100 && lIsRx) {
PIN3 = 0;
} else {
PIN3 = 1;
}
}
}
元件 | 单价 | 数量 | 金额 |
5*7CM洞洞板 | 0.25 | 1 | 0.25 |
STC89C52 直插 | 1.50 | 1 | 1.50 |
LED | 0.07 | 1 | 0.07 |
1K电阻 直插 | 0.02 | 1 | 0.02 |
1*40P排针 | 0.16 | 3 | 0.48 |
104电容 直插 | 0.03 | 1 | 0.03 |
10K电阻 直插 | 0.02 | 1 | 0.02 |
10uF电容 直插 | 0.09 | 2 | 0.18 |
47pF电容 直插 | 0.05 | 2 | 0.10 |
10K电阻 贴片0805封装 | 0.01 | 30 | 0.30 |
16M无源晶体 直插 | 0.24 | 1 | 0.24 |
合计 | 3.19 |
欢迎光临 5iMX.com 我爱模型 玩家论坛 ——专业遥控模型和无人机玩家论坛(玩模型就上我爱模型,创始于2003年) (http://5imx.com./) | Powered by Discuz! X3.3 |