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

📄 lighting.c

📁 门禁系统 interated with LCD
💻 C
📖 第 1 页 / 共 2 页
字号:
		if 		(Light == 12) temp = temp & 0xFC;	//clear bit 0 & 1
		else if (Light == 13) temp = temp & 0xF3; 	//clear bit 2 & 3
		else if (Light == 14) temp = temp & 0xCF;	//clear bit 4 & 5
		else 				 temp = temp & 0x3F;	//clear bit 6 & 7
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return -1;			
	}
	else return -1;
	return FALSE;
}
//----------------------------------------------------
int Light_Dim_Down(int Light,int Status)
{
	int temp;
	if ((i2c_Write(DIMMER_W,PWM0,0xFF))==TRUE) return -1;	//Full duty Cycle (LED always on)
	if ((i2c_Write(DIMMER_W,PSC0,0x00))==TRUE) return -1;	//Fastest freq
	
	//Switch the selected LED to use PWM0
	//Deal with LED 0 - 3
	if ((Light <=3) && (Light >= 0)) {
		if ((temp = i2c_Read(DIMMER_R,LS0)) < 0)	return -1;
		if 		(Light == 0) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 1) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 2) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS0,temp))== TRUE) return -1;	
	}
	
	//Deal with LED 4 - 7
	else if (Light >=4 && Light <=7) {
		if ((temp = i2c_Read(DIMMER_R,LS1)) < 0)	return -1;
		if 		(Light == 4) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 5) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 6) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS1,temp))== TRUE) return -1;	
	}
	
	//Deal with LED 8 - 11
	else if (Light >=8 && Light <=11) {
		if ((temp = i2c_Read(DIMMER_R,LS2)) < 0)	return -1;
		if 		(Light == 8) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 9 ) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 10) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return -1;	
		}
		
	//Deal with LED 12 - 15
	else if	(Light >=12 && Light <=15) {
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return -1;
		if 		(Light == 12) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 13) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 14) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return -1;			
	}
	else return -1;
	
	temp = 0xFF;
	
	while (temp >= DIM_LEVEL) {
		i2c_Write(DIMMER_W,PWM0,temp);
		DelayMs(10);
		temp--;
	}
	//Finished Dim Down, latch to PWM1
	if ((Light <=3) && (Light >= 0)) {
		if ((temp = i2c_Read(DIMMER_R,LS0)) < 0)	return -1;
		if 		(Light == 0) temp = (temp | 0x03);	//set bit 1 and clear bit 0
		else if (Light == 1) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
		else if (Light == 2) temp = (temp | 0x30);	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS0,temp))== TRUE) return -1;	
	}
	
	//Deal with LED 4 - 7
	else if (Light >=4 && Light <=7) {
		if ((temp = i2c_Read(DIMMER_R,LS1)) < 0)	return -1;
		if 		(Light == 4) temp = (temp | 0x03);	//set bit 1 and clear bit 0
		else if (Light == 5) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
		else if (Light == 6) temp = (temp | 0x30);	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS1,temp))== TRUE) return -1;	
	}
	
	//Deal with LED 8 - 11
	else if (Light >=8 && Light <=11) {
		if ((temp = i2c_Read(DIMMER_R,LS2)) < 0)	return -1;
		if 		(Light == 8) temp = (temp | 0x03);	//set bit 1 and clear bit 0
		else if (Light == 9 ) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
		else if (Light == 10) temp = (temp | 0x30);	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return -1;	
		}
		
	//Deal with LED 12 - 15
	else {
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return -1;
		if 		(Light == 12) temp = (temp | 0x03);	//set bit 1 and clear bit 0
		else if (Light == 13) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
		else if (Light == 14) temp = (temp | 0x30);	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return -1;			
	}
	
	return FALSE;	
}

//----------------------------------------------------
//----------------------------------------------------
//If LED currently on, Dim it down to 30% (status 1)
//If LED currently off,Dim it up to 30%	(status 0)
//If LED currently at 30%, dim it up	(status 3) 
//LED should never be in status 2
//Use PWM0 to generate the Dimming Effect before Latching to PWM1 for Permanent Dim.

int Light_Dim_Up(int Light,int Status)
{
	int temp;
	if ((i2c_Write(DIMMER_W,PWM0,0x00))==TRUE) return -1;	//Off 
	if ((i2c_Write(DIMMER_W,PSC0,0x00))==TRUE) return -1;	//Fastest freq
	
	//Switch the selected LED to use PWM0
	//Deal with LED 0 - 3
	if ((Light <=3) && (Light >= 0)) {
		if ((temp = i2c_Read(DIMMER_R,LS0)) < 0)	return -1;
		if 		(Light == 0) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 1) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 2) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS0,temp))== TRUE) return -1;	
	}
	
	//Deal with LED 4 - 7
	else if (Light >=4 && Light <=7) {
		if ((temp = i2c_Read(DIMMER_R,LS1)) < 0)	return -1;
		if 		(Light == 4) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 5) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 6) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS1,temp))== TRUE) return -1;	
	}
	
	//Deal with LED 8 - 11
	else if (Light >=8 && Light <=11) {
		if ((temp = i2c_Read(DIMMER_R,LS2)) < 0)	return -1;
		if 		(Light == 8) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 9 ) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 10) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return -1;	
		}
		
	//Deal with LED 12 - 15
	else if	(Light >=12 && Light <=15) {
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return -1;
		if 		(Light == 12) temp = (temp | 0x02) & 0xFE;	//set bit 1 and clear bit 0
		else if (Light == 13) temp = (temp | 0x08) & 0xFB;	//set bit 3 and clear bit 2
		else if (Light == 14) temp = (temp | 0x20) & 0xEF;	//set bit 5 and clear bit 4
		else 				 temp = (temp | 0x80) & 0xBF;	//set bit 7 and clear bit 6
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return -1;			
	}
	else return -1;
	
	//If status 00 (Light Off), Dim up all the way
	if (Status == 0) {
		temp = 0x00;
		
		while (temp <= DIM_LEVEL) {
			i2c_Write(DIMMER_W,PWM0,temp);
			DelayMs(10);
			temp++;
		}
	}
	//Status is Dimmed, Turn on the Light instead
	else {
		temp = DIM_LEVEL;
		
		while (temp <= 0xFF) {
			i2c_Write(DIMMER_W,PWM0,temp);
			DelayMs(10);
			temp++;
		}
	}
	if (Status == 0) {
		//Finished Dim Up, Turn_On
		if ((Light <=3) && (Light >= 0)) {
			if ((temp = i2c_Read(DIMMER_R,LS0)) < 0)	return -1;
			if 		(Light == 0) temp = (temp | 0x03);	//set bit 1 and clear bit 0
			else if (Light == 1) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
			else if (Light == 2) temp = (temp | 0x30);	//set bit 5 and clear bit 4
			else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
			if ((i2c_Write(DIMMER_W,LS0,temp))== TRUE) return -1;	
		}
		
		//Deal with LED 4 - 7
		else if (Light >=4 && Light <=7) {
			if ((temp = i2c_Read(DIMMER_R,LS1)) < 0)	return -1;
			if 		(Light == 4) temp = (temp | 0x03);	//set bit 1 and clear bit 0
			else if (Light == 5) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
			else if (Light == 6) temp = (temp | 0x30);	//set bit 5 and clear bit 4
			else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
			if ((i2c_Write(DIMMER_W,LS1,temp))== TRUE) return -1;	
		}
		
		//Deal with LED 8 - 11
		else if (Light >=8 && Light <=11) {
			if ((temp = i2c_Read(DIMMER_R,LS2)) < 0)	return -1;
			if 		(Light == 8) temp = (temp | 0x03);	//set bit 1 and clear bit 0
			else if (Light == 9 ) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
			else if (Light == 10) temp = (temp | 0x30);	//set bit 5 and clear bit 4
			else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
			if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return -1;	
			}
			
		//Deal with LED 12 - 15
		else  {
			if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return -1;
			if 		(Light == 12) temp = (temp | 0x03);	//set bit 1 and clear bit 0
			else if (Light == 13) temp = (temp | 0x0C);	//set bit 3 and clear bit 2
			else if (Light == 14) temp = (temp | 0x30);	//set bit 5 and clear bit 4
			else 				 temp = (temp | 0xC0);	//set bit 7 and clear bit 6
			if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return -1;			
		}
	}
	
	else {
	//Finished Dim Up, latch to On
	Light_On(Light);
	}
	
	return FALSE;	
}


int Light_Dim(int Light) 
{

	
	int temp;
	if ((temp = Light_Status(Light))==0) Light_Dim_Up(Light,temp);	
	else if (temp == 1)					 Light_Dim_Down(Light,temp);
	else if (temp== 3) Light_Dim_Up(Light,temp);	//PWM1, Dim up
	else if (temp<0) return -1;
	else return FALSE;				//Either status 2 or 3
	return TRUE;				
}

int Light_Dim_Down_All(void)
{
	char temp;
	if ((i2c_Write(DIMMER_W,PSC0,0x00))==TRUE) return -1;	//Fastest freq
	//Set to PWM0 for all LEDs
	if (i2c_Write(DIMMER_W,LS0,0xAA)) return I2C_ERROR;
	if (i2c_Write(DIMMER_W,LS1,0xAA)) return I2C_ERROR;	
	if (i2c_Write(DIMMER_W,LS2,0x5A)) return I2C_ERROR;
	//if (i2c_Write(DIMMER_W,LS3,0xAA)) return I2C_ERROR;	
	
	temp = 0xFF;
	
	while (temp >= DIM_LEVEL) {
		i2c_Write(DIMMER_W,PWM0,temp);
		DelayMs(10);
		temp--;
	}
	
	if (i2c_Write(DIMMER_W,LS0,0xFF)) return I2C_ERROR;
	if (i2c_Write(DIMMER_W,LS1,0xFF)) return I2C_ERROR;	
	if (i2c_Write(DIMMER_W,LS2,0x0F)) return I2C_ERROR;
	//if (i2c_Write(DIMMER_W,LS3,0xFF)) return I2C_ERROR;	
	return FALSE;
}

⌨️ 快捷键说明

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