⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jz100.c

📁 实现基本的89C51RC12路输入
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "JZ100.h"

bit resetFlag;
//bit currLimitFlag;	// total current limit within 1Amps

//add for multi connect
char xdata cRet =0;
char xdata cDeviceAddr =2;

char xdata wRet =0;

//bit signalTemp;
bit shortSignal;
bit longSignal;

char statusCount; 
char PDCount;

//unsigned char testCnt;
//unsigned int tempValue;

bit intervalFlag;
unsigned char alarmCnt;
//unsigned char warning;

bit sendFlag;

unsigned char temp;

unsigned char shortSignalCnt;
unsigned char longSignalCnt;


unsigned char bdata readByte;
sbit readByte0 = readByte^0;	
sbit readByte1 = readByte^1;
sbit readByte2 = readByte^2;
sbit readByte3 = readByte^3;
sbit readByte4 = readByte^4;
sbit readByte5 = readByte^5;
sbit readByte6 = readByte^6;
sbit readByte7 = readByte^7;

unsigned char bdata writeByte;
sbit writeByte0 = writeByte^0;	
sbit writeByte1 = writeByte^1;
sbit writeByte2 = writeByte^2;
sbit writeByte3 = writeByte^3;
sbit writeByte4 = writeByte^4;
sbit writeByte5 = writeByte^5;
sbit writeByte6 = writeByte^6;
sbit writeByte7 = writeByte^7;
// Byte send to PC, 0--no warn; 1--warn.
unsigned char bdata warnByte;	
sbit warnBit0 = warnByte^0;
sbit warnBit1 = warnByte^1;
sbit warnBit2 = warnByte^2;
sbit warnBit3 = warnByte^3;
sbit warnBit4 = warnByte^4;
sbit warnBit5 = warnByte^5;


//unsigned char abnormalCnt[6];

unsigned char sendTime;


unsigned char bdata sendByte[3];

sbit sendByte00 = sendByte[0]^0;
sbit sendByte01 = sendByte[0]^1;
sbit sendByte02 = sendByte[0]^2;
sbit sendByte03 = sendByte[0]^3;
sbit sendByte04 = sendByte[0]^4;
sbit sendByte05 = sendByte[0]^5;

sbit sendByte10 = sendByte[1]^0;
sbit sendByte11 = sendByte[1]^1;
sbit sendByte12 = sendByte[1]^2;
sbit sendByte13 = sendByte[1]^3;
sbit sendByte14 = sendByte[1]^4;
sbit sendByte15 = sendByte[1]^5;

sbit sendByte20 = sendByte[2]^0;
sbit sendByte21 = sendByte[2]^1;
sbit sendByte22 = sendByte[2]^2;
sbit sendByte23 = sendByte[2]^3;
sbit sendByte24 = sendByte[2]^4;
sbit sendByte25 = sendByte[2]^5;

unsigned char counter[6];

unsigned char currentCount[6];
unsigned char currentOffCount[6];

 		//
//unsigned int statusTemp;

// green LED: 1--no phone connect; 0--phone connect; longSignal--charge.
unsigned char bdata greenLedByte;
sbit greenLedBit0 = greenLedByte^0;
sbit greenLedBit1 = greenLedByte^1;
sbit greenLedBit2 = greenLedByte^2;
sbit greenLedBit3 = greenLedByte^3;
sbit greenLedBit4 = greenLedByte^4;
sbit greenLedBit5 = greenLedByte^5;
// red LED: 1--no alarm; longSignal--smor current; shortSignal--large current; 0--alarm.
unsigned char bdata redLedByte;
sbit redLedBit0 = redLedByte^0;
sbit redLedBit1 = redLedByte^1;
sbit redLedBit2 = redLedByte^2;
sbit redLedBit3 = redLedByte^3;
sbit redLedBit4 = redLedByte^4;
sbit redLedBit5 = redLedByte^5;

unsigned char bdata blueLedByte;
sbit blueLedBit0 = blueLedByte^0;
sbit blueLedBit1 = blueLedByte^1;
sbit blueLedBit2 = blueLedByte^2;
sbit blueLedBit3 = blueLedByte^3;
sbit blueLedBit4 = blueLedByte^4;
sbit blueLedBit5 = blueLedByte^5;

unsigned char bdata onFlagByte;
sbit onFlag0 = onFlagByte^0;
sbit onFlag1 = onFlagByte^1;
sbit onFlag2 = onFlagByte^2;
sbit onFlag3 = onFlagByte^3;
sbit onFlag4 = onFlagByte^4;
sbit onFlag5 = onFlagByte^5;

bit firstSampleBit0;
bit firstSampleBit1;
bit firstSampleBit2;
bit firstSampleBit3;
bit firstSampleBit4;
bit firstSampleBit5;

// [0] red5--red0, green5--green4; [1] green3--green0, yellow0--yellow3; [2] yellow4--yellow11
unsigned char displayChar[3];
	 
//char state[6];
char status[6];


unsigned char initRes[6];
unsigned char secRes[6];
unsigned char current[6];

unsigned char blueLedFlash[6];
//unsigned char chBuff[6];

unsigned int intBuff[6];
unsigned int offTime[6];

void InitCPU(void)
{
	SCON = 0x50;	// uart in mode 1 (8bit), REN=1
	T2CON &= 0xf0;	// EXEN2=0, TR2=0, C/T=0, CP/RL=0
	T2CON |= 0x30;	// RCLK=1, TCLK=1
	TH2 = 0xff;		// init value 
	TL2 = 0x50;		// init value
	RCAP2H = 0xff;	// reload value, 9600 Bds at 54.0MHz
	RCAP2L = 0x50;  // reload value, 9600 Bds at 54.0MHz
	ES = 1;			// Enable serial interrupt
	//PS = 1;			// Setting serial interrupt high-priority
	EA = 1;			// Enable global interrupt
	TR2 = 1;		// Timer 2 run

	TMOD = 0x01; 	// GATE0=0; C/T0#=0; M10=0; M00=1;
	TH0 = 0x50; 	// init values
	TL0 = 0x38;
	ET0 = 1; 		// enable timer0 interrupt
	TR0 = 1; 		// timer0 run
	
	WDT_CONTR = 0x3e;
}

void DelayUs(void)
{
	//char i = 5;
	//while (i-- > 0);
	char i;
	for (i = 0; i < 6; i++);
}

void Reset(void)
{
	char i;
	P1 = 0xc0;
	
	greenLedByte = 0xff;
	redLedByte = 0xff;
	
	warnByte = 0;
	//warnBit0 = 1;
	//warnBit1 = 1;
	//warnBit2 = 1;
	//warnBit3 = 1;
	//warnBit4 = 1;
	//warnBit5 = 1;


	//onFlag0 = 0;
	//onFlag1 = 0;
	//onFlag2 = 0;
	//onFlag3 = 0;
	//onFlag4 = 0;
	//onFlag5 = 0;

	firstSampleBit0 = 0;
	firstSampleBit1 = 0;
	firstSampleBit2 = 0;
	firstSampleBit3 = 0;
	firstSampleBit4 = 0;
	firstSampleBit5 = 0;


	for (i = 0; i < 6; i++)
	{
		initRes[i] = 0;
		intBuff[i] = 0;
		counter[i] = 0;
		offTime[i] = 0;
		counter[i] = 0;
		//state[i] = 0;
		status[i] = 0;	
	}

//	abnormalBit = 0;
	
	alarmCnt = 1;

	resetFlag = 0;			
}

bit StatusRatio(unsigned char x, unsigned char x0)
{
	unsigned int temp;
	temp = x * (256 - x0);
	if (temp < 4096)
	{
		temp = temp * 16 / (x0 * (256 - x));
	}
	else
	{
		temp = temp / ((x0 * (256 - x)) / 16);
	}

	if (temp > 40 || x < 10)	// >or< 50%	 1.5*16 or 0.5*16
	{
		return 1;
	}
	else
	{
		return 0;
	}

}
/*
void Counter(void)
{
	unsigned char i;

	for(i=0; i<6; i++)
	{
		if(offTime[i])
		{
			offTime[i]++;
			if (offTime[i] > 36000)		// 5 min
				offTime[i] = 0;			
		}
	}
}
*/
void SendChar(unsigned char ch)	{
	//Cntl485 =1;
	//DelayUs();
	SBUF = ch;
	while (!TI);
	TI = 0;
	//Cntl485 =0;	
	//DelayUs();
}

void CLKPulse(void)
{
	DelayUs();
	CLK = 1;
	DelayUs();
	CLK = 0;
	DelayUs();	
}

void ADCover(unsigned char adrr)
{
	char i;
	writeByte = adrr;
	
	DelayUs();
	DI = 1;
	CLKPulse();
	CLKPulse();
	DI = writeByte2;
	CLKPulse();
	DI = writeByte1;
	CLKPulse();
	DI = writeByte0;
	CLKPulse();

	for (i = 1; i < 9; i++)
	{
		CLKPulse();
		readByte = readByte << 1;
		readByte = readByte | DO;
	}
}

char IDtoAdrr(char ID)
{
	switch (ID)
	{
		case 0:
			return 6;
			break;
		case 1:
			return 2;
			break;
		case 2:
			return 5;
			break;
		case 3:
			return 1;
			break;
		case 4:
			return 4;
			break;
		case 5:
			return 0;
			break;
	}	
}

unsigned char GetCurrent(unsigned char CHAdrr)
{
	CS0 = 0;
	ADCover(CHAdrr);
	CS0 = 1;
	return readByte;	
}

unsigned char CurrentSum(void)
{
	unsigned char i;
	unsigned int intTemp = 0;
	
	for (i = 0; i < 6; i++)
	{
		intTemp = intTemp + GetCurrent(IDtoAdrr(i));
	}
	
	return intTemp / 6;	
}

unsigned char GetStatus(unsigned char CHAdrr)
{
	CS1 = 0;
	ADCover(CHAdrr);
	CS1 = 1;
	return readByte;
}


bit ConnectTest(char ID)
{
	if (GetStatus(IDtoAdrr(ID)) < 10)
	{
		return 1;
	}
	else	{
		return 0;
	}	
}

void StatusSample(unsigned char ID)
{
	unsigned char CHID;
	CHID = IDtoAdrr(ID);

	if (++counter[ID] < 2)
	{
		initRes[ID] = GetStatus(CHID);
	}
	else
	{
		if (StatusRatio(GetStatus(CHID), initRes[ID]))
		{
			//testFlag = 0;
			counter[ID] = 0;
			status[ID] = 0;		
		}
		else
		{
			if (counter[ID] > 10)
			{					
				status[ID] = 1;
				counter[ID] = 0;
			}								
		}	
	}
}

void Alarm(void)	{
	static char i, j;
	
	if (alarmCnt)	{
		if (intervalFlag)	{
			if (j < 3)	{
				if (++i < 26)	{
					if (i < 16)
						beep = 1;
					else
						beep = 0;
				}
				else	{
					i = 0;
					j++;
				}
			}
			else	{
				if (++i < 26)	{
					beep = 0;
				}
				else	{
					i = 0;
					j = 0;
					alarmCnt--;
				}
			}
		}
		else	{
			if (++j < 101)
				beep = 1;
			else	{
				j = 0;
				if (--alarmCnt == 0)	{
					beep = 0;
					intervalFlag = 1;
				}
			}
		}		
	}
}

void ChargeTest(unsigned char ID)	{
	unsigned char adrr;
	adrr = IDtoAdrr(ID);
	//SendChar(GetCurrent(adrr));
	//if (GetCurrent(adrr) > 200)
	//	SetOnCntl(ID, 0);
	
	//intBuff[ID] = GetCurrent(adrr);
	//intBuff[ID] = intBuff[ID] + GetCurrent(adrr);
	current[ID] = GetCurrent(adrr);;
	//SendChar(current[ID]);
	if (++counter[ID] < 84)	{
		
		if (current[ID] > 250)	{	// <900MA
			SetOnCntl(ID, 0);
			status[ID] = 5;
			offTime[ID] = 30000;
			counter[ID] = 0;
			current[ID] = 0;
		}					
		
		if (counter[ID] > 68)	{
			if (current[ID] < 2)	{	// <7MA
				status[ID] = 4;				
				offTime[ID] = 30000;
				counter[ID] = 0;
				current[ID] = 0;
			}							
		}
	}
	else	{
		status[ID] = 2;
		current[ID] = 0;
		counter[ID] = 0;
		intBuff[ID] = 0;
	}
}

void SignalCycle(void)
{
	shortSignalCnt++;
	longSignalCnt++;

	if (shortSignalCnt > 24)
	{
		shortSignal = !shortSignal;
		shortSignalCnt = 0;
	}

	if (longSignalCnt > 99)
	{
		longSignal = !longSignal;
		longSignalCnt = 0;
	}
}	

void IICStart(void)
{
	SDA = 0;
	DelayUs();
	SCL = 0;
}

void IICWaitAck(void)
{
	SDA = 1;
	DelayUs();
	SCL = 1;		// acknowledge clock pulse
	DelayUs();
	//while (SDA);
	SCL = 0;		// acknowledge clock pulse
}

void IICAck(bit ackBit)
{
	SDA = ackBit;

	SCL = 1;
	DelayUs();
	SCL = 0;
}

void IICStop(void)
{
	DelayUs();
	SCL = 1;
	DelayUs();
	SDA = 1;
}

void IICSendByte(char sendByte)
{
	char i;
	for (i = 1; i < 9; i++)
	{
		SDA = sendByte & 0x80;
		sendByte = sendByte << 1;
		DelayUs();
		SCL = 1;
		DelayUs();
		SCL = 0;		
	}
}

unsigned char IICReadByte(void)	{
	unsigned char i;
	unsigned char readBuff;
    for (i = 1; i < 9; i++)	{
		DelayUs();
		SCL = 1;
		DelayUs();
		readBuff = readBuff << 1;
		readBuff = readBuff | SDA;
		SCL = 0;
	}
	return readBuff;
}

unsigned char ReadDevice(void)	{
	unsigned char deviceCode;
	IICStart();
	IICSendByte(0x41);
	IICWaitAck();
	deviceCode = IICReadByte();
	IICAck(1);
	IICStop();
	return deviceCode;
}

void DisplaySignal(unsigned char signal, unsigned char addr)	{
	addr = addr << 1; 
	addr = 0x40 + addr;
	IICStart();
	IICSendByte(addr);
	IICWaitAck();
	IICSendByte(signal);
	IICWaitAck();
	IICStop();						
}

bit ReadSelfStatus(void)
{
	cRet =0;
	
	if (!Bt0 != sendByte00)	{
		sendByte00 = !Bt0;
		if (sendByte00 ==1)	{
			cRet =1;			
		}
	}

	else if (!Bt1 != sendByte01)	{
		sendByte01 = !Bt1;
		if (sendByte01 ==1)	{
			cRet =2;
		}	
	}
	
	else if(!Bt2 != sendByte02)	{
		sendByte02 = !Bt2;
		if (sendByte02 ==1)	{
			cRet =3;
		}		
	}
	
	else if(!Bt3 != sendByte03)	{
		sendByte03 = !Bt3;
		if (sendByte03 ==1)
		{
			cRet =4;
		}	
	}
	else if(!Bt4 != sendByte04)	{
		sendByte04 = !Bt4;		
			if (sendByte04 ==1)
		{		
			cRet =5;
		}		
	}	
	else if(!Bt5 != sendByte05)	{
		sendByte05 = !Bt5;		
			if (sendByte05 ==1)
		{
			cRet =6;
		}		
	}	
	else if(!Bt6 != sendByte10)	{
		sendByte10 = !Bt6;		
			if (sendByte10 ==1)
		{
			cRet =7;
		}	
	}
	else if(!Bt7 != sendByte11)	{
		sendByte11 = !Bt7;		
			if (sendByte11 ==1)
		{
			cRet =8;
		}	
	}	
	else if(!Bt8 != sendByte12)	{
		sendByte12 = !Bt8;		
			if (sendByte12 ==1)
		{
			cRet =9;
		}	
	}	
	else if(!Bt9 != sendByte13)	{
		sendByte13 = !Bt9;
		if (sendByte13 ==1)
		{
			cRet =10;
		}		
	}		
	else if(!Bt10 != sendByte14)	{
		sendByte14 = !Bt10;
					if (sendByte14 ==1)
		{
			cRet =11;
		}	
	}	
	else if(!Bt11 != sendByte15)	{
		sendByte15 = !Bt11;
					if (sendByte15 ==1)
		{
			cRet =12;
		}	
	}
	else if(warnBit0 != sendByte20)	{
		sendByte20 = warnBit0;
					if (sendByte20 == 1)
		{
			wRet =1;
		}	
	}	
	else if(warnBit1 != sendByte21)	{
		sendByte21 = warnBit1;
					if (sendByte21 == 1)
		{
			wRet =2;
		}	
	}	
	else if(warnBit2 != sendByte22)	{
		sendByte22 = warnBit2;
					if (sendByte22 == 1)
		{
			wRet =3;
		}	
	}	
	else if(warnBit3 != sendByte23)	{
		sendByte23 = warnBit3;
					if (sendByte23 == 1)
		{
			wRet =4;
		}	
	}	
	else if(warnBit4 != sendByte24)	{
		sendByte24 = warnBit4;
					if (sendByte24 == 1)
		{
			wRet =5;
		}	
	}	
	else if(warnBit5 != sendByte25)	{
		sendByte25 = warnBit5;
					if (sendByte25 == 1)
		{
			wRet = 6;
		}	
	}	

	if (cRet > 0 || wRet > 0)	{
		return 1;
	}
	else	{
		return 0;
	}
}
	
void SendStatus(void)	{		
	// 	等待半秒,格式:"M (XXXXXXXX XXXXTTTT TT000000) L"

	SendChar('M');	
	SendChar(cRet);
	SendChar(wRet);
	SendChar('L');
	
	/*	
	writeByte5 = !Bt6;
	writeByte4 = !Bt7;
	writeByte3 = !Bt8;
	writeByte2 = !Bt9;
	writeByte1 = !Bt10;
	writeByte0 = !Bt11;
		
	if (writeByte != sendByte[1])	{
		if (writeByte > sendByte[1])	{
			sendFlag = 1;
		}			
		sendByte[1] = writeByte;
	}
	
	writeByte5 = warnBit0;
	writeByte4 = warnBit1;				
	writeByte3 = warnBit2;
	writeByte2 = warnBit3;
	writeByte1 = warnBit4;
	writeByte0 = warnBit5;
	
	if (writeByte != sendByte[2])	{

⌨️ 快捷键说明

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