|
本帖最后由 skypup 于 2013-9-23 17:03 编辑
今天研究了一下STC在ISP烧程序时,自动冷启动的逻辑。
用USB转TTL,加上一个电阻和一个8050三极管做的电子开关。成功实现了自动冷启动功能。
原理:
jian听上位机串口发来的数据,波特率 2400,如果连续收到若干个(20个) 0x7F 字符,则电子开关给低电平,延时几百ms,再拉高。
STC89C52 源代码:
#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;
}
}
}
|
|