serial.c

来自「terawin的t103 LCD驱动程序」· C语言 代码 · 共 131 行

C
131
字号


#define __serProc_C__
#include "reg51.h"
#include "serial.h"
#include "twowire.h"

void serInterrupt(void) interrupt 4 using 1
{
	if(RI){
		RI=0;
		serInBuf[(pserIn+pserInCnt)&7]=SBUF;
	     
        pserInCnt++;
	}else if(TI){
		TI=0;
		pserOutCnt--;
		pserOut++;
		pserOut=pserOut&7;
		if(pserOutCnt){
			SBUF=serOutBuf[pserOut];
		}
	}
}

void serialIni()
{
	pserIn=pserOut=pserInCnt=pserOutCnt=0;
	PCON|=0x80;
	AUXR=0x40;
	SCON=0x50;//40;
//	SADDR=0;
//	SADEN=0;
	TMOD=(TMOD&0xf)|0x20;
    TMOD=0x21; 
	TH1=243;//3 19200bps at 12M/1/16
	TR1=1;
	RI=TI=0;
	ES=1;
    EA=1;
}
void serSendByte(U8 d)
{
	while(pserOutCnt>8);
	ES=0;
	serOutBuf[(pserOut+pserOutCnt)&7]=d;
	if(!pserOutCnt)SBUF=d;
	pserOutCnt++;
	ES=1;
}
bit serReadByte(U8 *d)
{
	if(pserInCnt==0) return 0;
	*d=serInBuf[pserIn];
	ES=0;
	pserIn++,pserInCnt--;
	pserIn=pserIn&7;
	ES=1;
	return 1;
}
bit waitData(U8 *d)
{
	uWORD i;
	for(i=65535;i!=0;i--){
		if(pserInCnt) return(serReadByte(d));
		while(!TF1)if(pserInCnt) return(serReadByte(d));
		TF1=0;
		if(pserInCnt) return(serReadByte(d));
		while(!TF1)if(pserInCnt) return(serReadByte(d));
		TF1=0;
		if(pserInCnt) return(serReadByte(d));
		while(!TF1)if(pserInCnt) return(serReadByte(d));
		TF1=0;
		if(pserInCnt) return(serReadByte(d));
		while(!TF1)if(pserInCnt) return(serReadByte(d));
		TF1=0;
	}
	return 0;
}
void serCommand()
{
	U8 i,d,addr,len,page;
	if(!serReadByte(& d)) return;
	if(d!=0x8f) return;
	if(!waitData(&d)) return;
	if(d!='T') return;
	if(!waitData(&d)) return;
	if(d!='S') return;
	if(!waitData(&d)) return;
	if(d!='T') return;
	serSendByte(0x61);
	
	while(1){
		if(serReadByte(&d)&&(d==0x83)){
			if(!waitData(&page)) continue;
			if(page==0xff){
				//3 Stop
				serSendByte(0x6f);
				break;
			}
			if(page&0x80){
				//2 Read data p a len
				page&=0x7f;
				if(!waitData(&addr)) continue;
				if(!waitData(&len)) continue;
				do{
					serSendByte(I2CReadByte(page, addr));
					addr++;
					len--;
				}while(len);
			}else{
				//2 Write data p a len
				if(!waitData(&addr)) continue;
				if(!waitData(&len)) continue;
				i=0;
				do{
					if(!waitData(&d)) break;
					I2CWriteByte(page, addr, d);
					addr++;
					len--;
					i++;
					if((i&7)==0){
						serSendByte(0x60);
					}
				}while(len);
				serSendByte(0x61);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?