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

📄 jz100.c

📁 实现基本的89C51RC12路输入
💻 C
📖 第 1 页 / 共 2 页
字号:
		sendByte[2] = writeByte;
		sendFlag = 1;
	}
	
	if (sendFlag)	{	
		sendFlag = 0;
		SendChar('M');			// M
		SendChar(sendByte[0]);	// 发状态
		SendChar(sendByte[1]);	// 发状态
		SendChar(sendByte[2]); 	// 发报警
		SendChar('L');
	}
*/	
}


void PowerDetect(void)	{	// Power Suply Detect, when V < 5.0v, Battery will work.
	char i;
	if (GetCurrent(7) < 0xcc)	{

		for (i = 0; i < 6; i++)	{
			if (status[i] == 1 || status[i] == 2)	{
				status[i] = 4;
			}
			offTime[i] = 30000;
		}

		P1 = 0xc0;
		
		if (battCntl)	{
			if (++PDCount > 7)	{
				PDCount = 0;
				battCntl = 0;
			}
		}
	}
	else	{
		if (!battCntl)	{
			for (i = 0; i < 6; i++)	{
				offTime[i] = 0;
			}
		}
		battCntl = 1;
		PDCount = 0;		
	}
}

void  serial(void) interrupt 4 using 1	{
	
	if (RI)
	{
		unsigned char uartBuf;
		RI = 0;
		uartBuf = SBUF;
		if (uartBuf == 'R')
		{
			resetFlag = 1;
		}
	}

}

void set_fstsmpbit_data(unsigned char para, bit bVal)
 {
	 switch(para)
	 {
	 	case 0:
				firstSampleBit0 = bVal;
				break;
	 	case 1:
				firstSampleBit1 = bVal;
				break;
	 	case 2:
				firstSampleBit2 = bVal;
				break;
	 	case 3:
				firstSampleBit3 = bVal;
				break;
	 	case 4:
				firstSampleBit4 = bVal;
				break;
	 	case 5:
				firstSampleBit5 = bVal;
				break;
		default:
				break;
	 }

 }

void SetOnCntl(unsigned char para, bit bVal)	{
	switch (para)	{
		case 0:
			ON0 = bVal;
			break;
		case 1:
			ON1 = bVal;
			break;
		case 2:
			ON2 = bVal;
			break;
		case 3:
			ON3 = bVal;
			break;
		case 4:
			ON4 = bVal;
			break;
		case 5:
			ON5 = bVal;
			break;
		default:
	        break;
	}
}

bit GetOnCntl(unsigned char para)	{
	switch(para)	{
		case 0:
			return ON0;
			break;
		case 1:
			return ON1;
			break;
		case 2:
			return ON2;
			break;
		case 3:
			return ON3;
			break;
		case 4:
			return ON4;
			break;
		case 5:
			return ON5;
			break;
	}
}

void SetRedLedBit(unsigned char para, bit bVal)	{
	switch (para)	{
		case 0:
			redLedBit0 = bVal;
			break;
		case 1:
			redLedBit1 = bVal;
			break;
		case 2:
			redLedBit2 = bVal;
			break;
		case 3:
			redLedBit3 = bVal;
			break;
		case 4:
			redLedBit4 = bVal;
			break;
		case 5:
			redLedBit5 = bVal;
			break;
		default:
	        break;
	}
}

void SetGreenLedBit(unsigned char para, bit bVal)	{
	switch(para)	{
		case 0:
				greenLedBit0 = bVal;
				break;
		case 1:
				greenLedBit1 = bVal;
				break;
		case 2:
				greenLedBit2 = bVal;
				break;
		case 3:
				greenLedBit3 = bVal;
				break;
		case 4:
				greenLedBit4 = bVal;
				break;
		case 5:
				greenLedBit5 = bVal;
				break;
		default:
				break;
	}

}

void SetBlueLedBit(unsigned char para, bit bVal)	{
	switch(para)	{
		case 0:
				blueLedBit0 = bVal;
				break;
		case 1:
				blueLedBit1 = bVal;
				break;
		case 2:
				blueLedBit2 = bVal;
				break;
		case 3:
				blueLedBit3 = bVal;
				break;
		case 4:
				blueLedBit4 = bVal;
				break;
		case 5:
				blueLedBit5 = bVal;
				break;
		default:
				break;
	}

}

void SetWarnBit(unsigned char para, bit bVal)	{
	switch(para)	{
		case 0:
			warnBit0 = bVal;
			break;
		case 1:
			warnBit1 = bVal;
			break;
		case 2:
			warnBit2 = bVal;
			break;
		case 3:
			warnBit3 = bVal;
			break;
		case 4:
			warnBit4 = bVal;
			break;
		case 5:
			warnBit5 = bVal;
			break;
		default:
			break;
	}
}
/*
SetOnFlag(unsigned char para, bit bVal)	{
	switch(para)	{
		case 0:
			onFlag0 = bVal;
			break;
		case 1:
			onFlag1 = bVal;
			break;
		case 2:
			onFlag2 = bVal;
			break;
		case 3:
			onFlag3 = bVal;
			break;
		case 4:
			onFlag4 = bVal;
			break;
		case 5:
			onFlag5 = bVal;
			break;
	}
}

bit GetOnFlag(unsigned char para)	{
	switch(para)	{
		case 0:
			return onFlag0;
			break;
		case 1:
			return onFlag1;
			break;
		case 2:
			return onFlag2;
			break;
		case 3:
			return onFlag3;
			break;
		case 4:
			return onFlag4;
			break;
		case 5:
			return onFlag5;
			break;
	}
}
*/
bit get_fstsmpbit_data(unsigned char para)	{
	switch(para)	{
		case 0:
			return firstSampleBit0;
			break;
		case 1:
			return firstSampleBit1;
			break;
		case 2:
			return firstSampleBit2;
			break;
		case 3:
			return firstSampleBit3;
			break;
		case 4:
			return firstSampleBit4;
			break;
		case 5:
			return firstSampleBit5;
			break;
	}
}


/**
* FUNCTION_PURPOSE: timer0 interrupt
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: 
v */
void timer0(void) interrupt 1 /* interrupt address is 0x000b */
{
	unsigned char cSendData;
//	char cTmpRet;
	
	//TF0 = 0; 		/* reset interrupt flag (already done by hardware)*/
	TH0 = 0x50; 	/* init values */
	TL0 = 0x38;
	WDT_CONTR = 0x3e;

	P0_3 =0;
	
	SignalCycle();

	switch (status[statusCount])	{
		case 0:
			SetOnCntl(statusCount, 0);
			SetRedLedBit(statusCount, 1);
			SetGreenLedBit(statusCount, 1);
			//SendChar(statusCount);
			//SendChar(GetStatus(IDtoAdrr(statusCount)));
			
			GetStatus(IDtoAdrr(statusCount));
			
			if (counter[statusCount])	{
				if ((readByte > 5))	{
					if (++counter[statusCount] > 8)
						intBuff[statusCount] = intBuff[statusCount] + readByte;
				}
				else	{
					intBuff[statusCount] = 0;
					counter[statusCount] = 0;
				}
			}							
			else	{
				if ((readByte > 5))
					counter[statusCount] = 1;
			}
			
			if (counter[statusCount] > 23)	{
				initRes[statusCount] = intBuff[statusCount] / 16;
				counter[statusCount] = 0;
				status[statusCount] = 1;
			}

			break;

		case 1:
			if (GetOnCntl(statusCount))	{
				ChargeTest(statusCount);
			}
			else	{
				if (CurrentSum() < 200)	{
					SetOnCntl(statusCount, 1);
					SetGreenLedBit(statusCount, 0);
				}
				else {
					status[statusCount] = 3;
				}
			}
			
			//if (!GetOnFlag(statusCount))	{

			//}
			break;

		case 2:
			SetOnCntl(statusCount, 1);
			SetGreenLedBit(statusCount, 0);
			//SendChar(statusCount);
			//set_onflag_data(statusCount, 1);
			temp = CurrentSum();
			//SendChar(temp);

			if (temp > 200)	{
				//SendChar(statusCount);
				//SendChar(temp);
				//j = 1;
				SetOnCntl(statusCount, 0);
				offTime[statusCount] = 30000;
				status[statusCount] = 5;
			}

			if (GetCurrent(IDtoAdrr(statusCount)) > 250 )	{	// <900MA
				SetOnCntl(statusCount, 0);
				//state[statusCount] = 4;
				//	onFlag0 = 0;
				//set_onflag_data(statusCount, 0);
				offTime[statusCount] = 30000;
				status[statusCount] = 5;
				//SetOnFlag(statusCount, 0);
			}			
		
			if (++counter[statusCount] < 5)	{
				intBuff[statusCount] = intBuff[statusCount] + GetCurrent(IDtoAdrr(statusCount));
			}
			else	{
				current[statusCount] = intBuff[statusCount] / 4;
				
				intBuff[statusCount] = 0;
				counter[statusCount] = 0;
				//SendChar(current[statusCount]);
/*				
				if (CurrentSum() > 100)	{
					SetOnCntl(statusCount, 0);
					//state[statusCount] = 4;
					//	onFlag0 = 0;
					//set_onflag_data(statusCount, 0);
					offTime[statusCount] = 30000;
					status[statusCount] = 5;
					SetOnFlag(statusCount, 0);
				}					
*/				
				if (get_fstsmpbit_data(statusCount))	{
					if (current[statusCount] < 8)
					{
						status[statusCount] = 4;
						//onFlag0 = 0;
						//set_onflag_data(statusCount, 0);
						offTime[statusCount] = 30000;
						//SetOnFlag(statusCount, 0);
					}
				}
				else	{
					if (current[statusCount] < 3)
					{
						status[statusCount] = 4;
						//onFlag0 = 0;
						//set_onflag_data(statusCount, 0);
						offTime[statusCount] = 30000;
						//SetOnFlag(statusCount, 0);
					}
					else
					{
						//firstSampleBit0 = 1;
						set_fstsmpbit_data(statusCount,1);
					}					
				}

				//SendChar(current[statusCount]);
			
				if (blueLedFlash[statusCount])	{
					if (++currentCount[statusCount] < 2)	{
						SetBlueLedBit(statusCount, 0);
					}
					else	{
						SetBlueLedBit(statusCount, 1);
						currentCount[statusCount] = 0;
						blueLedFlash[statusCount]--;
					}
				}
				else	{
					if (++currentOffCount[statusCount] > 6)	{
					currentOffCount[statusCount] = 0;
					blueLedFlash[statusCount] = current[statusCount] / 13 + 1;
					//SendChar(blueLedFlash[statusCount]);
					}
				}
				
			}
		break;
			
	case 3:
		SetOnCntl(statusCount, 0);
				
		GetStatus(IDtoAdrr(statusCount));
		
		if (StatusRatio(readByte, initRes[statusCount]))	{
			if (++counter[statusCount] > 15)	{					
				status[statusCount] = 8;
				alarmCnt = 5;
				counter[statusCount] = 0;
			}			
		}
		else	{	
			counter[statusCount] = 0;
			if (offTime[statusCount] == 0)	{	// && !currLimitFlag)
				status[statusCount] = 1;
				//firstSampleBit0 = 0;	
				set_fstsmpbit_data(statusCount, 0);
			}
		}

		break;

	case 4:
		SetOnCntl(statusCount, 0);
		SetGreenLedBit(statusCount, 1);
		SetRedLedBit(statusCount, longSignal);
		if (offTime[statusCount])
			offTime[statusCount]--;
		
		GetStatus(IDtoAdrr(statusCount));
		
		if (StatusRatio(readByte, initRes[statusCount]))	{
			if (++counter[statusCount] > 15)	{					
				status[statusCount] = 8;
				alarmCnt = 5;
				counter[statusCount] = 0;
			}			
		}
		else	{	
			counter[statusCount] = 0;
			if (offTime[statusCount] == 0)	{	// && !currLimitFlag)
				status[statusCount] = 1;
				//firstSampleBit0 = 0;	
				set_fstsmpbit_data(statusCount, 0);
			}
		}

		break;
	
	case 5:
		SetOnCntl(statusCount, 0);
		SetGreenLedBit(statusCount, 1);
		SetRedLedBit(statusCount, shortSignal);
		if (offTime[statusCount])
			offTime[statusCount]--;
			
		GetStatus(IDtoAdrr(statusCount));
		
		if (StatusRatio(readByte, initRes[statusCount]))	{
			if (++counter[statusCount] > 15)	{					
				status[statusCount] = 8;
				
				alarmCnt = 5;	// alarm for 5 secon
				//alarmCnt = 120;
				
				counter[statusCount] = 0;
				if ((secRes[statusCount] != 0) && (readByte > 5) && (readByte < 250))
					secRes[statusCount] = readByte;
			}			
		}
		else	{	
			counter[statusCount] = 0;
			if (offTime[statusCount] == 0)	{	// && !currLimitFlag)
				status[statusCount] = 1;
				//firstSampleBit0 = 0;	
				set_fstsmpbit_data(statusCount,0);
			}
		}		
		
		//warnBit0 = 0;
		//set_warnBit_data(statusCount, 0);
		break;
		
	/*	
	case 5:
		SetOnCntl(statusCount, 0);
		break;
	*/
		
	case 8:
		SetOnCntl(statusCount, 0);
		SetRedLedBit(statusCount, 0);
		SetWarnBit(statusCount, 1);
		
		GetStatus(IDtoAdrr(statusCount));
		
		if (readByte < 5)	{
			alarmCnt = 5;
		}
		
/*
		if (status[statusCount] < 3)	{
			alarmCnt = 5;				
			status[statusCount] = 3;
		}
		
		if (status[statusCount] == 3)	{
			if (++counter[statusCount] < 9)
			{
				intBuff[statusCount] = intBuff[statusCount] + GetStatus(IDtoAdrr(statusCount));
			}
			else
			{
				if (intBuff[statusCount] / 8 > 
				{
					status[statusCount] = 4;
				}
				intBuff[statusCount] = 0;
				counter[statusCount] = 0;
			}
		}
		
		if (status[statusCount] == 4)	{
			if (GetStatus(IDtoAdrr(statusCount)) < 15 || GetStatus(IDtoAdrr(statusCount)) > 254)//(StatusRatio(GetStatus(IDtoAdrr(statusCount)), initStatus[statusCount]))	// >or< 50%	 1.5*16 or 0.5*16
			{
				if (++counter[statusCount] > 15)
				{					
					counter[statusCount] = 0;
					alarmCnt = 5;
					status[statusCount] = 3;
				}			
			}
		}							
*/
		break;		
	}
	//SendChar(CurrentSum());
	//SendChar(status[0]);
	//SendChar(status[1]);
	//SendChar(status[2]);
	//SendChar(status[3]);
	//SendChar(status[4]);
	//SendChar(status[5]);

	temp = status[0] | status[1] | status[2] | status[3] | status[4] | status[5];
	
	if (temp > 7)	{	// 报警
		redLED = shortSignal;
		greenLED = 1;			
	}
	else	{
		if (temp < 4)	{	// nomoral
			redLED = 1;
			greenLED = longSignal;
		}
		else	{	// lower or over
			redLED = 1;
			greenLED = shortSignal;
		}
	}			

	if (status[0] != 2)	{
		blueLedBit0 = ConnectTest(0);
	}
	if (status[1] != 2)	{
		blueLedBit1 = ConnectTest(1);
	}	
	if (status[2] != 2)	{
		blueLedBit2 = ConnectTest(2);
	}	
	if (status[3] != 2)	{
		blueLedBit3 = ConnectTest(3);
	}
	if (status[4] != 2)	{
		blueLedBit4 = ConnectTest(4);
	}
	if (status[5] != 2)	{
		blueLedBit5 = ConnectTest(5);
	}

	writeByte0 = redLedBit5;
	writeByte1 = redLedBit4;
	writeByte2 = redLedBit3;
	writeByte3 = redLedBit2;	
	writeByte4 = redLedBit1;
	writeByte5 = redLedBit0;
	writeByte6 = greenLedBit5;
	writeByte7 = greenLedBit4;
	DisplaySignal(writeByte, 1);
	
	writeByte0 = greenLedBit3;
	writeByte1 = greenLedBit2;
	writeByte2 = greenLedBit1;
	writeByte3 = greenLedBit0;	
	writeByte4 = Bt0;
	writeByte5 = Bt1;
	writeByte6 = Bt2;
	writeByte7 = Bt3;
	DisplaySignal(writeByte, 2);

	writeByte0 = Bt4;
	writeByte1 = Bt5;
	writeByte2 = Bt7;
	writeByte3 = Bt6;	
	writeByte4 = Bt8;
	writeByte5 = Bt9;
	writeByte6 = Bt10;
	writeByte7 = Bt11;
	DisplaySignal(writeByte, 3);

	DisplaySignal(blueLedByte, 4);
	
	Alarm();

	if (++sendTime > 49)	{	// 	等待半秒,格式:"M 0x00 (XXXXXXXX XXXX0000 TTTTTT00) L"
		SendChar('M');			// M
		
		writeByte7 = !Bt0;
		writeByte6 = !Bt1;
		writeByte5 = !Bt2;
		writeByte4 = !Bt3;
		writeByte3 = !Bt4;
		writeByte2 = !Bt5;
		writeByte1 = !Bt6;
		writeByte0 = !Bt7;
		SendChar(writeByte);	// 发状态
		
		writeByte7 = !Bt8;
		writeByte6 = !Bt9;
		writeByte5 = !Bt10;
		writeByte4 = !Bt11;
		writeByte3 = warnBit0;
		writeByte2 = warnBit1;
		writeByte1 = warnBit2;
		writeByte0 = warnBit3;
		SendChar(writeByte);	// 发状态
		
		writeByte = 0;	
		writeByte7 = warnBit4;
		writeByte6 = warnBit5;
		SendChar(writeByte); 	// 发报警
		
		SendChar('L');
		
		sendTime = 0;
	}

/*	
	if (ReadSelfStatus())	
	{
		//cSendData = cRet +(cDeviceAddr - 2) * 12;
		
		SendStatus();
	}		
*/
	PowerDetect();
	
	if (resetFlag)	{	
		Reset();
	}
	
	if (++statusCount > 5)
		statusCount = 0;
}

void main(void)	{
 	InitCPU();

	CLK = 0;
	CS0 = 1;
	CS1 = 1;

	SDA = 1;
	SCL = 1;

	Reset();

	Cntl485 =0; //Set 485 low

	TI = 1;

	cDeviceAddr = ReadDevice() & 0x0F;		
			
 	while (1);
}

⌨️ 快捷键说明

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