📄 serint.c
字号:
/* CONSOLE.C -- serial I/O code */
/*---------------------------------------------------------------------------*/
/* Initialize serial port hardware and variables */
#include "serint.h"
#include "reg54.h"
#include "bootloader.h"
#include "stdio.h"
unsigned char nTimeOutCounter = 0;
extern bit nTimeCountFlag;
union add
{
unsigned int nCounter; //address is correct?
struct {unsigned char ah;unsigned char al;}half;
}ADDR;
void SerInitialize() {
/*--- set up Timer 1 to produce serial rate */
TCON &= 0x3F; /* clear run & interrupt flags */
TMOD &= 0x0F; /* flush existing Timer 1 setup */
TMOD |= 0x20; /* flush existing Timer 1 setup */
SCON = 0x50; /* flush existing Timer 1 setup */
PCON |= 0x80; /* set smod = 1; */
TH1 = TL1 = T1RELOAD & 0x00FF; /* flush existing Timer 1 setup */
TR1 = 1; /* start the timer */
ADDR.nCounter = 0;
// ES = 1; /* enable serial interrupts */
// TI = 1;
}
//默认串口传输速率为9600BPS
/*---------------------------------------------------------------------------*/
/* Serial console interrupt handler */
/* If transmitter output is disabled, we fake trans interrupts until empty */
void Timer0_Init()
{
TMOD &= 0xF0; /* flush existing Timer 1 setup */
TMOD |= 0x01; /* set timer0 to 1 mode 16bits timer */
nTimeOutCounter = 0;
}
void SerInt() interrupt 4 using 1
{
if(RI)
{ /* receiver interrupt active? */
RI = 0; /* indicate we have it */
TL0 = 0; //set time out timer
TH0 = 0;
nTimeOutCounter = 0;
SFRAH = ADDR.half.ah;
SFRAL = ADDR.half.al;
ADDR.nCounter++;
SFRCN = 0x21;
TR0 = 1;
SFRFD = SBUF;
// PCON |= 0x01; //enter idle mode
}
if(TI) { /* transmitter interrupt active? */
TI = 0; /* indicate done with it */
}
}
extern bit bEndProgFlag;
void Timer0() interrupt 1 using 1
{
// TR0 = 0; //disable Timer 0
if(bEndProgFlag)
{
TL0 = 0x5a;
TH0 = 0xff;
return;
}
if(nTimeCountFlag) //enter program mode and count time?
{ //timer out?
if(nTimeOutCounter==20) //about 0.7s
{
//display "Program End and Exit"
bEndProgFlag = 1;
SFRAH = 0xff;
SFRAL = 0xff;
SFRCN = 0x21;
TR0 = 1;
SFRFD = 0;
nTimeOutCounter++;
}
else if(nTimeOutCounter == 21)
{
ES = 0;
TI = 1;
putstr("Program End, You should manual reset CPU...");
RI = 0;
while(!RI);
CHPENR = 0x87; //Reset CPU
CHPENR = 0x59;
CHPCON = 0x80;
TR0 = 0;
}
else
nTimeOutCounter++;
}
//else return
}
void putstr(unsigned char *pStr)
{
unsigned char i=0;
while(*(pStr+i))
{
putchar(*(pStr+i));
i++;
/* TI = 0;
SBUF = *(pStr+i);
i++;
while(!TI);
*/ }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -