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

📄 lighting.c

📁 门禁系统 interated with LCD
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "Lighting.h"

int Light_Init(void)
{
	int n = 0;
	
	if (i2c_Write(DIMMER_W,PSC0,0x00)) return I2C_ERROR;	//Fastest Freq 
	if (i2c_Write(DIMMER_W,PSC1,0x00)) return I2C_ERROR;	//Fastest Freq 
	if (i2c_Write(DIMMER_W,PWM0,0xFF)) return I2C_ERROR;	// PWM0 100 % Duty Cycle
	if (i2c_Write(DIMMER_W,PWM1,DIM_LEVEL)) return I2C_ERROR;	// PWM1 30 % Duty Cycle
	if (i2c_Write(DIMMER_W,LS0,0x00)) return I2C_ERROR;	//Turn off LED 0-3
	if (i2c_Write(DIMMER_W,LS1,0x00)) return I2C_ERROR;	//Turn off LED 4-7	
	if (i2c_Write(DIMMER_W,LS2,0x00)) return I2C_ERROR;	//Turn off LED 8-11	
	if (i2c_Write(DIMMER_W,LS3,0x00)) return I2C_ERROR;	//Turn off LED 11-15	
	return FALSE;
}

int 
Light_RGB1_Mode(int mode)
{
	int temp;
	if (mode == 1) {Light_On(15);Light_On(14);Light_On(13);} //On All
	else if (mode == 2) {Light_Off(15);Light_Off(14);Light_Off(13);} //Off All
	else if (mode == 3) {Light_On(15);Light_Off(14);Light_Off(13);} //RED On
	else if (mode == 4) {Light_On(14);Light_Off(15);Light_Off(13);} //GREEN on
	else if (mode == 5) {Light_On(13);Light_Off(14);Light_Off(15);} //Blue On
	else if (mode == 6)
	{	
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return I2C_ERROR;
		temp = ((temp | 0xA8) & 0xAB);	//set bit 7 and clear bit 6 (PWM0)
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;		
		
	}
	else if (mode == 7) //Blink Red 
	{
		Light_Off(14);
		Light_Off(13);
		
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return I2C_ERROR;
		temp = ((temp | 0x80) & 0xBF);	//set bit 7 and clear bit 6 (PWM0)
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;			
	}
//	else if (mode == 8) //Dim all
//	{
//		//Set all 3 LEDs to take PWM 0
//		if ((temp = i2c_Read(DIMMER_R,LS3)) == I2C_ERROR)	return I2C_ERROR; 
//		temp = (temp | 0xA8) & 0x9B;
//		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;	
//		
//		temp = 0xFF;
//	
//		while (temp >= DIM_LEVEL) 
//		{
//			i2c_Write(DIMMER_W,PWM0,temp);
//			DelayMs(20);
//			temp--;
//		}
//		//Set all 3 LEDs to take PWM1 (Always Dimmed)
//		if ((temp = i2c_Read(DIMMER_R,LS3)) == I2C_ERROR)	return I2C_ERROR; 
//		temp |= 0xFC;
//		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;	
//		
//				
//	}		
	return FALSE;
}

int 
Light_RGB2_Mode(int mode)
{	
	int temp;
	if (mode == 1) {Light_On(12);Light_On(11);Light_On(10);} //On All
	else if (mode == 2) {Light_Off(12);Light_Off(11);Light_Off(10);} //Off All
	else if (mode == 3) {Light_On(12);Light_Off(11);Light_Off(10);} //RED On
	else if (mode == 4) {Light_On(11);Light_Off(12);Light_Off(10);} //GREEN on
	else if (mode == 5) {Light_On(10);Light_Off(12);Light_Off(11);} //Blue On
	else if (mode == 6)
	{
		i2c_Write(DIMMER_W,PWM0,0x80); //50% Duty Cycle
		i2c_Write(DIMMER_W,PSC0,0x79); //Blink at 2Hz
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return I2C_ERROR;
		temp = ((temp | 0x02) & 0xF7);	//set bit 7 and clear bit 6 (PWM0)
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;		

		if ((temp = i2c_Read(DIMMER_R,LS2)) < 0)	return I2C_ERROR;
		temp = ((temp | 0xA0) & 0xAF);	//set bit 7 and clear bit 6 (PWM0)
		if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return I2C_ERROR;		
		
		
	}	
	else if (mode == 7) //Blink Red 
	{
		Light_Off(11);
		Light_Off(10);
		
		//Set LED 12 to take PWM0 
		if ((temp = i2c_Read(DIMMER_R,LS3)) < 0)	return I2C_ERROR;
		temp = ((temp | 0x02) & 0xFE);	//set bit 7 and clear bit 6 (PWM0)
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;			
	}
//	else if (mode == 8) //Dim all
//	{
//		//Set all 3 LEDs to take PWM 0
//		if ((temp = i2c_Read(DIMMER_R,LS3)) == I2C_ERROR)	return I2C_ERROR; 
//		temp = (temp | 0x02) & 0xFE; //LED12
//		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;	
//		
//		if ((temp = i2c_Read(DIMMER_R,LS2)) == I2C_ERROR)	return I2C_ERROR; 
//		temp = (temp | 0xA0) & 0x50;
//		if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return I2C_ERROR;	
//		temp = 0xFF;
//	
//		while (temp >= DIM_LEVEL) 
//		{
//			i2c_Write(DIMMER_W,PWM0,temp);
//			DelayMs(20);
//			temp--;
//		}
//		//Set all 3 LEDs to take PWM1 (Always Dimmed)
//		if ((temp = i2c_Read(DIMMER_R,LS3)) == I2C_ERROR)	return I2C_ERROR; 
//		temp |= 0x03; //LED12
//		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return I2C_ERROR;	
//		
//		
//		if ((temp = i2c_Read(DIMMER_R,LS2)) == I2C_ERROR)	return I2C_ERROR; 
//		temp = (temp | 0xF0);
//		if ((i2c_Write(DIMMER_W,LS2,temp))== TRUE) return I2C_ERROR;	
//	}
		
	return FALSE;
}

//Toggles the On Off state of the selected Light.
int Light_Toggle(int Light)

{
	int temp;
	if ((temp = Light_Status(Light))==0) Light_On(Light);	
	else if (temp == 3 || temp == 1)  Light_Off(Light);
	return TRUE;				
}
//----------------------------------------------------
// To check the status of the Light
// returns 0,1,2,3 OFF/ON/PWM0/PWM1
int Light_Status(int Light)
{
	int temp;
	
	//Deals with LED 0 - 3
	if ((Light <=3) && (Light >= 0)) {
		if ((temp = i2c_Read(DIMMER_R,LS0)) < 0)	return -1;
		if 		(Light == 0) temp = temp & 0x03;	
		else if (Light == 1) temp = (temp & 0x0C)/4; 	
		else if (Light == 2) temp = (temp & 0x30)/16;
		else 				 temp = (temp & 0xC0)/64;
		
	}
	
	//Deals 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;	
		else if (Light == 5) temp = (temp & 0x0C)/4; 	
		else if (Light == 6) temp = (temp & 0x30)/16;	
		else 				 temp = (temp & 0xC0)/64;	
	}
	
	//Deals 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;	
		else if (Light == 9 ) temp = (temp & 0x0C)/4; 	
		else if (Light == 10) temp = (temp & 0x30)/16;	
		else 				 temp = (temp & 0xC0)/64;	
		}
		
	//Deals 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 & 0x03;	
		else if (Light == 13) temp = (temp & 0x0C)/4; 	
		else if (Light == 14) temp = (temp & 0x30)/16;	
		else 				  temp = (temp & 0xC0)/64;	
	}
	else return -1;
	return temp;	
}

//----------------------------------------------------
int Light_On(int Light)
{
	int temp;
	
	//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 | 0x01) & 0xFD;	//set bit 0 and clear bit 1
		else if (Light == 1) temp = (temp | 0x04) & 0xF7;	//set bit 2 and clear bit 3
		else if (Light == 2) temp = (temp | 0x10) & 0xDF;	//set bit 4 and clear bit 5
		else 				 temp = (temp | 0x40) & 0x7F;	//set bit 6 and clear bit 7
		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 | 0x01) & 0xFD;	//set bit 0 and clear bit 1
		else if (Light == 5) temp = (temp | 0x04) & 0xF7;	//set bit 2 and clear bit 3
		else if (Light == 6) temp = (temp | 0x10) & 0xDF;	//set bit 4 and clear bit 5
		else 				 temp = (temp | 0x40) & 0x7F;	//set bit 6 and clear bit 7
		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 | 0x01) & 0xFD;	//set bit 0 and clear bit 1
		else if (Light == 9 ) temp = (temp | 0x04) & 0xF7;	//set bit 2 and clear bit 3
		else if (Light == 10) temp = (temp | 0x10) & 0xDF;	//set bit 4 and clear bit 5
		else 				  temp = (temp | 0x40) & 0x7F;	//set bit 6 and clear bit 7	
		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 | 0x01) & 0xFD;	//set bit 0 and clear bit 1
		else if (Light == 13) temp = (temp | 0x04) & 0xF7;	//set bit 2 and clear bit 3
		else if (Light == 14) temp = (temp | 0x10) & 0xDF;	//set bit 4 and clear bit 5
		else 				  temp = (temp | 0x40) & 0x7F;	//set bit 6 and clear bit 7	
		if ((i2c_Write(DIMMER_W,LS3,temp))== TRUE) return -1;			
	}
	else return -1;
	return FALSE;
}

//----------------------------------------------------
int Light_Off(int Light)
{
	int temp;
	
	//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 & 0xFC;	//clear bit 0 & 1
		else if (Light == 1) temp = temp & 0xF3; 	//clear bit 2 & 3
		else if (Light == 2) temp = temp & 0xCF;	//clear bit 4 & 5
		else 				 temp = temp & 0x3F;	//clear bit 6 & 7
		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 & 0xFC;	//clear bit 0 & 1
		else if (Light == 5) temp = temp & 0xF3; 	//clear bit 2 & 3
		else if (Light == 6) temp = temp & 0xCF;	//clear bit 4 & 5
		else 				 temp = temp & 0x3F;	//clear bit 6 & 7
		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 & 0xFC;	//clear bit 0 & 1
		else if (Light == 9 ) temp = temp & 0xF3; 	//clear bit 2 & 3
		else if (Light == 10) temp = temp & 0xCF;	//clear bit 4 & 5
		else 				 temp = temp & 0x3F;	//clear bit 6 & 7
		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;

⌨️ 快捷键说明

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