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

📄 main.c

📁 门禁系统 interated with LCD
💻 C
字号:
#include <htc.h>//#include <math.h>#include <stdlib.h>#include "main.h"#include "always.h"#include "delay.h"#include "RTC.h"#include "i2c.h"#include "Lighting.h"#include "Display.h"#include "ADC.h"// Debug = 0  to turn off all displaysint debug = 0;bit INT_Alarm ,IR_Changed,Door_Toggle;unsigned int MODE = 4,IR_Status = 0;short unsigned int INT_Mode[2]={0,0};short unsigned int Time_Flag[3]={0,0,0};//short unsigned int INT_Button[2] = {0,0};short unsigned int Alarm_Mode[4]={0,0,0};struct ROOM {	unsigned PEOPLE:4;	unsigned IR_Change:1;	unsigned IR_STATUS:3;	}RM_Living = {0,0,0},RM_Master = {0,0,0},RM_Bath = {0,0,0},RM_Kitchen = {0,0,0};// =========================================================// MAIN// =========================================================voidmain(void){			int hour,min,sec,temp;	Module_Init();	while (1)	{	//		hour = RTC_Read(HOURS);//		min = RTC_Read(MINUTES);//		sec = RTC_Read(SECONDS);//		Display_Time(min,sec);			if (MODE == 0) 			ALL_OFF();//if (debug) Display(4);					if (MODE == 3) 			PARTY_MODE();		if (MODE == 4)		 	EMERGENCY_MODE();		if (MODE == 5) 			NORMAL_MODE();		if (MODE == 6)		 	VACATION_MODE();		if (MODE == 7)			ALL_ON();		}}//---------------------------------//Mode 0 Emergency//On Lights to Exit// void ALL_ON(void){	int n = 0;	for (n = 0;n<16;n++)	{		Light_On(n);	}	Light_Off(UltraV);		while(1) 	{		if (MODE_Change_Check()) return;	}	}void ALL_OFF(void){	int n = 0;	for (n = 0;n<16;n++)	{		Light_Off(n);	}	while(1) 	{		if (MODE_Change_Check()) return;	}	}void EMERGENCY_MODE(void){	int n = 0;	char Lights_On;	MODE0_Init();		for (n = 0;n<10;n++)		{			Light_On(n);		}			Light_RGB1_Mode(BLINK); //Blink Red Lights	Light_RGB2_Mode(BLINK); //Blink Red Lights			while(1) 	{		if (MODE_Change_Check()) return;	}	}//---------------------------------//Mode 1 Normal// Off lights at 7am// Switches to on lights// Off lights at 12pm// On lights using LDR for Porch lights// IR Detector to on lights// On lights for room if someone in room and darkvoid NORMAL_MODE(void){		int n,temp;	//if (debug) Display(0001);	MODE1_Init();	for (n = 0;n<10;n++)	{		Light_Off(n);	}	while(1) 	{				if (INT_Alarm) //Alarm Interrupt Occured		{			int temp;			temp = RTC_Read(HOURS);			if (Alarm_Mode[0] == 0) 			{				RTC_Write(HOURS_ALARM,0x92);				Alarm_Mode[0] = 1;			}			else if (Alarm_Mode[1] == 0)			{				RTC_Write(HOURS_ALARM,0x19);				Alarm_Mode[1] = 1;			}			else if (Alarm_Mode[2] == 0)				{				RTC_Write(HOURS_ALARM,0x00);				Alarm_Mode[2] = 1;			}			else 			{				Alarm_Mode[0] = 0;				Alarm_Mode[0] = 1;				Alarm_Mode[0] = 2;				RTC_Write(HOURS_ALARM,0x87);			}			INT_Alarm = 0;		}					Switches_Check();		IR_Check(); //IR_Array changed		LDR_Check();		Door_Check();		if (MODE_Change_Check()) return;	}}	//---------------------------------// Mode 2 Vacation// On lights randomly during 7pm - 11pmvoid VACATION_MODE(void){	char num_lights,light_num,n;	//if (debug) Display(0002);	MODE2_Init();	while(1) 	{		//Generate the number of lights to randomly On Off		num_lights = (rand() % 8) + 1;		//Display(num_lights);		for (n = 0;n<num_lights;n++)		{			light_num = rand() % 9 ;			Light_Toggle(light_num);		}		Light_Off(UltraV);		DelayS(2);		if (MODE_Change_Check()) return;	}}//---------------------------------//	Mode 11 Party// 	Multi colour LED to cyclevoid PARTY_MODE(void){	//if (debug) Display(0003);	MODE3_Init();	Light_Dim_Down_All();	Light_RGB1_Mode(BLINK_ALL);	Light_RGB2_Mode(BLINK_ALL);	Light_Off(UltraV);	while(1) 	{		Door_Check();		if (MODE_Change_Check()) return;	}}voidModule_Init(void) {//	ADCON0 = 0x80;	//AD module OFF	ADCON1 = 0x87;	//PORT A,E as Digital I/Os	TRISA = 0xFF;	//PORT A as Inputs		TRISB = 0xFF;	//Set PORTB as INPUTS	TRISC &= 0xE7;	//PORT C Bits 3/4 as OUTPUTS	TRISC |= 0xC0;  //PORT C Bits 7-5 as INPUTS	TRISD |= 0xF0;  //PORT D Bits 7-4 as INPUTS	TRISD &= 0xFE;  // PORTD Bits 0 as output	TRISE |= 0x07;	//PORT E Bits 0-2 as INPUTS		RTC_Init();		//Initialise RTC Clock	Light_Init();	//Ensure all lights are off, preset PWM to 50% and 100%	//initialize_ADC();	INT_Alarm = IR_Changed = Door_Toggle = 0;}// =========================================================// Initialisations// =========================================================voidMODE0_Init(void){	//To set PWM0 to blink for Emergency Lighting	i2c_Write(DIMMER_W,PWM1,0xFF); //Full Brightness	i2c_Write(DIMMER_W,PSC1,0x00); //No blink	i2c_Write(DIMMER_W,PWM0,0x80); //50% Duty Cycle	i2c_Write(DIMMER_W,PSC0,0x36); //Blink at 2Hz	GIE = 1;	RBIE = 0;	//Disable Interrupts on change		RTC_Write(HOURS_ALARM,0x87); //Set hour to go off at 0700		}voidMODE1_Init(void){	//Set Blink Modes	i2c_Write(DIMMER_W,PSC0,0x00);	//Fastest Freq  	i2c_Write(DIMMER_W,PWM0,0xFF);	// PWM0 100 % Duty Cycle	i2c_Write(DIMMER_W,PSC1,0x00);	//Fastest Freq 	i2c_Write(DIMMER_W,PWM1,DIM_LEVEL);	// PWM1 30 % Duty Cycle	//Set up alarm	Light_RGB1_Mode(BLUE);	Light_RGB2_Mode(BLUE);		RBIE = 0;		//Port on Change Interrupts	GIE = 1;		//Enable global interrupts}voidMODE2_Init(void){	i2c_Write(DIMMER_W,PSC0,0x00);	//Fastest Freq  	i2c_Write(DIMMER_W,PWM0,0xFF);	// PWM0 100 % Duty Cycle	i2c_Write(DIMMER_W,PSC1,0x00);	//Fastest Freq 	i2c_Write(DIMMER_W,PWM1,DIM_LEVEL);	// PWM1 30 % Duty Cycle	RBIE = 0;	GIE = 1;}voidMODE3_Init(void){		i2c_Write(DIMMER_W,PWM0,0x80); //50% Duty Cycle		i2c_Write(DIMMER_W,PSC0,0x36); //Blink at 2Hz		i2c_Write(DIMMER_W,PWM1,DIM_LEVEL);	// PWM1 30 % Duty Cycle		i2c_Write(DIMMER_W,PSC1,0x00);	//Fastest Freq 		INTEDG = 0; 		RBIE = 0;		GIE = 1;		}// =========================================================// Interrupts// =========================================================voidinterrupt ISR(void){	int temp;	if(RBIF) 	//Port on Change interrupt occured		{		//INT_Button[0] = 1;//		temp = ((PORTB & 0xF0)>>4); //get the Port values//		if (temp == 6) INT_Alarm = 1;			//Timer event//		else if (temp == 7) { INT_Button[0] = 1; INT_Button[1] = 8;} //Switch 1 Depressed//		else if (temp == 8) { INT_Button[0] = 1; INT_Button[1] = 7;} //Switch 1 Depressed//		else if (temp == 9) { INT_Button[0] = 1; INT_Button[1] = 6;} //Switch 1 Depressed//		else if (temp == 10) { INT_Button[0] = 1; INT_Button[1] = 5;} //Switch 1 Depressed//		else if (temp == 11) { INT_Button[0] = 1; INT_Button[1] = 4;} //Switch 1 Depressed//		else if (temp == 12) { INT_Button[0] = 1; INT_Button[1] = 3;} //Switch 1 Depressed//		else if (temp == 13) { INT_Button[0] = 1; INT_Button[1] = 2;} //Switch 1 Depressed//		else if (temp == 14) { INT_Button[0] = 1; INT_Button[1] = 1;} //Switch 1 Depressed//		Mode Change happened//		else if (temp == 13 || temp == 14) { INT_Mode[0] = 1;INT_Mode[1] = ((PORTB & 0xF0)>>6); }			RBIF = 0; //Reset interrupt flag	}}// =========================================================// Other functions// =========================================================//Check if main controller activated a mode change/ENable Disableint MODE_Change_Check(void) {	int newmode = (RE2<<2) + (RE1<<1) + RE0;//(PORTE & 0x07) ;	if (newmode!=MODE) //Mode has changed	{		MODE = newmode;		return TRUE;	}	else return FALSE;}//Checks the status of the IR sensor array and return true if change occured.voidIR_Check(void){	char newstatus = IR_PORT;   //Load newstatus with current IR values		if (newstatus != IR_Status) //IR array was triggered	{			//Check which room's IR was changed		//if ( ((newstatus & 0x0C) ^ (IR_Status & 0x0C)) != 0) RM_Living.IR_Change = 1;		if ( ((newstatus & 0x03) ^ (IR_Status & 0x03)) != 0) RM_Master.IR_Change = 1;		if ( ((newstatus & 0xF0) != 0)) RM_Bath.IR_Change = 1;		//if ( (((newstatus & 0x03) ^ (IR_Status & 0x03))) !=0 )		  RM_Room.IR_Change = 1;				IR_Status = newstatus;		//		if (RM_Living.IR_Change == 1) //		{//			int sensor = ((IR_Status & 0x0C)>>2);//			Light_RGB2_Mode(GREEN);//			if (RM_Living.IR_STATUS == 0)  		// 00//			{//				if (sensor == 1) RM_Living.IR_STATUS = 1;//				else if (sensor == 2) RM_Living.IR_STATUS = 3;//			}//			else if (RM_Living.IR_STATUS == 1) //01//			{//				if (sensor == 3) RM_Living.IR_STATUS = 2;//				else if (sensor == 0) //Person Exited the room//				{//				 	RM_Living.IR_STATUS = 0;//				 	if (RM_Living.PEOPLE != 0 ) RM_Living.PEOPLE--;//				}//			}//			else if (RM_Living.IR_STATUS == 2) //11//			{//				if (sensor == 2) RM_Living.IR_STATUS = 3;//				else if (sensor == 1) RM_Living.IR_STATUS = 1;				 //			}//			else if (RM_Living.IR_STATUS == 3) //10//			{//				if (sensor == 0) // Person Went into room//				{//				 	RM_Living.IR_STATUS = 0; //				 	RM_Living.PEOPLE++;//				}//				else if (sensor == 3) RM_Living.IR_STATUS = 2;				 //			}//			if (RM_Living.PEOPLE != 0) //			{//				Light_On(Living1);//				Light_On(Living2);//				Light_On(Living3);//			}//			else//			{//				Light_Off(Living1);//				Light_Off(Living2);//				Light_Off(Living3);//			}//			//			RM_Living.IR_Change = 0;//		}				if (RM_Master.IR_Change == 1) 			{			int sensor = (IR_Status & 0x03);						if (RM_Master.IR_STATUS == 0)  		// 00			{				if (sensor == 1) RM_Master.IR_STATUS = 1;				else if (sensor == 2) RM_Master.IR_STATUS = 3;			}			else if (RM_Master.IR_STATUS == 1) //01			{				if (sensor == 3) RM_Master.IR_STATUS = 2;				else if (sensor == 0) //Person Exited the room				{				 	RM_Master.IR_STATUS = 0;				 	if (RM_Master.PEOPLE != 0 ) RM_Master.PEOPLE--;				}			}			else if (RM_Master.IR_STATUS == 2) //11			{				if (sensor == 2) RM_Master.IR_STATUS = 3;				else if (sensor == 1) RM_Master.IR_STATUS = 1;				 			}			else if (RM_Master.IR_STATUS == 3) //10			{				if (sensor == 0) // Person Went into room				 {				 	RM_Master.IR_STATUS = 0; 				 	RM_Master.PEOPLE++;				 }				 else if (sensor == 3) RM_Master.IR_STATUS = 2;				 			}			if (RM_Master.PEOPLE != 0) 			{				Light_On(Master1);				Light_On(Master2);							}			else			{				Light_Off(Master1);				Light_Off(Master2);			}			RM_Master.IR_Change = 0;		}				if (RM_Bath.IR_Change == 1) 			{			int sensor = ((IR_Status & 0xF0)>>4);						if (sensor == 0x0F) //1011			{				FALL_ALARM = 1;				Light_RGB1_Mode(BLINK);				Light_RGB2_Mode(BLINK);			}			else 			{				FALL_ALARM = 0;				Light_RGB1_Mode(BLUE);				Light_RGB2_Mode(BLUE);				}					RM_Master.IR_Change = 0;		}		}//End of newstatus check}//End IR_Check()voidLDR_Check(void){//	if (!LDR1) //MasterBed Room//	{//		Light_On(Master1);//		Light_On(Master2);//		//	} //	else //	{//		Light_Off(Master1);//		Light_Off(Master2);//	}//	if (!LDR2)//	//if (!LDR2 && (RM_Master.PEOPLE != 0)) //Master BedRoom//	{//		Light_On(Bathroom);		//	}//	//	else //	{//		Light_Off(Bathroom);			//	} //	if (!LDR3)//	//	//if (!LDR3 && (RM_Room.PEOPLE != 0)) //Room//	{//		Light_On(Living1);//		Light_On(Living2);	//		Light_On(Living3);	//	} //	else //		Light_Off(Living1);//		Light_Off(Living2);	//		Light_Off(Living3);	//		if (!LDR4)		//if (!LDR4 && (RM_Living.PEOPLE != 0)) //Living Room	{		Light_On(Porch);	}	else 	{		Light_Off(Porch);	}}//End LDR_Check()voidDoor_Check(void) {	if (!DOOR_STATUS) Door_Toggle = 1;	else if (Door_Toggle) 	{		Light_On(UltraV);		DelayS(5);		Light_Off(UltraV);		Door_Toggle = 0;			}}void Switches_Check(void){	char temp;	temp = ((PORTB & 0xF0)>>4); 	if  (temp != 0x0F)	{		DelayMs(40);		temp = ((PORTB & 0xF0)>>4); 		switch (temp)		{			case 6:				INT_Alarm = 1;				break;			case 7: // master bedroom on/off				Light_Dim(Master1);				Light_Dim(Master2);				break;			case 8: // master bedroom dim				Light_Toggle(Master1);				Light_Toggle(Master2);				break;			case 9:				Light_Dim(Bathroom);				break;			case 10:				Light_Toggle(Bathroom);				break;			case 11:				Light_Dim(Kitchen);				break;			case 12:				Light_Toggle(Kitchen);				break;			case 13:				Light_Dim(Living1);				Light_Dim(Living2);				Light_Dim(Living3);				break;			case 14:				Light_Toggle(Living1);				Light_Toggle(Living2);				Light_Toggle(Living3);								break;			default:				break;		}		DelayMs(50); //debounce for upward press	}}//void //interrupt isr0(void)//{//	if (RBIF == 1)//	{//		if ((PORTB & 0xF0)>>4 == 0) {}	//Alarm//		else if ((PORTB & 0xF0)>>4 == 1) {} //SW1//		else if ((PORTB & 0xF0)>>4 == 2) {} //SW2//		else if ((PORTB & 0xF0)>>4 == 3) {} //SW3//		else if ((PORTB & 0xF0)>>4 == 4) {}//		else if ((PORTB & 0xF0)>>4 == 5) {}//		else if ((PORTB & 0xF0)>>4 == 6) {}//		else if ((PORTB & 0xF0)>>4 == 7) {}//		else if ((PORTB & 0xF0)>>4 == 8) {}//		else if ((PORTB & 0xF0)>>4 == 9) {}//	}	//}

⌨️ 快捷键说明

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