int main(void)
{
	//Library initialization
	initBjDevLib();
	
	//third-party LCD library initialization
	//third-party LCD library initialization
	LCDInit(LS_ULINE);
	LcdHideCursor();
	
	//register pedal change position callback
	//you can use callback, which will invoke only then pedals was turn or check pedal position manually,
	//compare it with previous value and process changes
	expRegisterPedalChangePositionCallback(expPedalsCallback);
	
	LCDWriteStringXY(0, 0,"Pedal 1 : ");
	LCDWriteStringXY(0, 1,"Pedal 2 : ");
	
	//manual read current pedal position
	pedalPositions[0] = expGetPedalPosition(EXP_PEDAL1);
	pedalPositions[1] = expGetPedalPosition(EXP_PEDAL2);
	
	//print current pedal positions
	updateScreen();

	while(1)
    {
		//continuously reading all pedals and run callback, if any changes detected
		expProcess();
    }
}
Esempio n. 2
0
int main(int argc, const char *argv[])
{
	unsigned char i;
	LCDInit(LS_NONE);
	LCDClear();

	// Initialize usart
	init_usart(BAUDRATE, TRANSMIT_RATE, DATA_BITS, STOP_BITS, PARITY_BITS);

	char recipe_name[RECIPE_NAME_LENGTH];

    eeprom_read_block((void*)&recipe_name, (const void*)0x00, RECIPE_NAME_LENGTH);
    
	LCDWriteString("Recipe Name:");
	LCDWriteStringXY(0,1,recipe_name);

	for(i=0;i<25;i++) _delay_loop_2(0);
	
	// Clear second row
	LCDWriteStringXY(0,1,"                ");
    eeprom_read_block((void*)&recipe_name, (const void*)0x76, RECIPE_NAME_LENGTH);

	LCDWriteString("Recipe Name:");
	LCDWriteStringXY(0,1,recipe_name);

	return 0;
}
Esempio n. 3
0
void toggleTimeColon(){
	if(bTimeCommaState){
		LCDWriteStringXY(CLOCK_CURSOR_POSITION+2, 0, ":");
		bTimeCommaState=0;
	}else{
		LCDWriteStringXY(CLOCK_CURSOR_POSITION+2, 0, " ");
		bTimeCommaState=1;
	}
}
Esempio n. 4
0
void main()
{
     unsigned char i;

   //Initialize LCD module
   LCDInit(LS_BLINK|LS_ULINE);

   //Clear the screen
   LCDClear();

   //Simple string printing
   LCDWriteString("Congrats ");

   //A string on line 2
   LCDWriteStringXY(0,1,"Loading ");

   //Print some numbers
   for (i=0;i<99;i+=1)
   {
      LCDWriteIntXY(9,1,i,3);
      LCDWriteStringXY(12,1,"% ");
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);

   }

   //Clear the screen
   LCDClear();

   //Some more text

   LCDWriteString("Hello world");
   LCDWriteStringXY(0,1,"By YourName Here");    // <--- Write ur NAME HERE !!!!!!!!!!!

   //Wait
   for(i=0;i<100;i++) _delay_loop_2(0);

   //Some More ......
   LCDClear();
   LCDWriteString("    eXtreme");
   LCDWriteStringXY(0,1,"  Electronics");

	//Wait
   for(i=0;i<100;i++) _delay_loop_2(0);

   //Custom Chars ......
   LCDClear();
   LCDWriteString("Custom Char !!!");
   LCDWriteStringXY(0,1,"%0%1%2%3%4%5%6%7");

}
void main()
{
    unsigned char i;
   for(i=0;i<10;i++) _delay_loop_2(0);
   
    while(1)
   {

   //Initialize LCD module
   LCDInit(LS_BLINK|LS_ULINE);
  
   for(i=0;i<10;i++) _delay_loop_2(0);
   //Clear the screen
   LCDClear();

   //Simple string printing
   LCDWriteStringXY(0,0,"Welcome to CELAB");
   //LCDWriteString("Congrats ");

   //A string on line 2
   LCDWriteStringXY(0,1,"Loading ");

   //Print some numbers
   for (i=0;i<99;i+=1)
   {
      LCDWriteIntXY(9,1,i,3);
      LCDWriteStringXY(12,1,"%");
      _delay_loop_2(0);

   }

   //Clear the screen
   LCDClear();

   //Some more text

   LCDWriteString("Hello world");
   LCDWriteStringXY(0,1,"ComputronicsLab");    // <--- Write ur NAME HERE !!!!!!!!!!!

   //Wait
  for(i=0;i<50;i++) _delay_loop_2(0);

   //Some More ......
   LCDClear();
   LCDWriteString("Computronics");
   LCDWriteStringXY(0,1,"Lab");
   for(i=0;i<50;i++) _delay_loop_2(0);
   }

}
Esempio n. 6
0
void init_lcd(void)
{
	LCDInit(LS_NONE);
	LCDClear();

	LCDWriteString("    Mr. Pour    ");
	LCDWriteStringXY(0,1," Senior Project");
}
Esempio n. 7
0
void _init(){
	/******************* Pin Configuration ***********************/
	DDRA = 0xff;		// PORTA = output
	DDRB = 0x1;			// PORTB.0 = output (backlight)
	
	BUTTON_PORT = 0x13;		// pins 0,1,4 of BUTTON_PORT (portE) are pulled high
	
	
	/******************* ADC Setup / Temperature first *********************/
	ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);		// ADC Prescaler = Fck/128
	ADCSRA |= (1<<ADFR)|(1<<ADEN)|(1<<ADIE);		// Free-running mode, enabling ADC and ADC Interrupt
	
	
	/******************* PWM Setup *********************/
	//DDRB |= (1<<PB7);
	//OCR2 = 64;
	//TCCR2 = (1<<WGM21)|(1<<WGM20)|(1<<COM21)|(1<<CS20)|(1<<CS21);
	
	
	/******************* LCD Setup *********************/
	InitLCD(0);
	LCDClear();
	LCDWriteStringXY(CLOCK_CURSOR_POSITION,0,"00:00");
	LCDWriteStringXY(0,1,"T=00.0");
	LCDByte(0b11011111, 1);		// Scrive il carattere °: dalla tabella 4 del datasheet HD44780.pdf vediamo che il carattere è 11011111;
								// lo mandiamo come byte (LCDByte()) sapendo che dobbiamo mettere RS a 1 (dato!)
	LCDWriteStringXY(HUM_CURSOR_POSITION-4, 1, "C_H=88%");
	LCDWriteStringXY(ZONE_CURSOR_POSITION-1,1,"_1");
	
	
	/******************* RTC Setup *********************/
	TCCR0 |= (1<<CS02)|(1<<CS01)|(1<<CS00);			// clock: F_CPU / 1024
	TCCR0 |= (1<<WGM01)|(0<<WGM00);					// Clear Timer on Compare
	TIMSK |= (1<<OCIE0);							// Output compare match interrupt enable
	OCR0 = 156;										// Interrupt every 10ms
	
	/******************* Timer2 setup: backlight *********************/
	//TCCR2 |= (1<<CS22)|(0<<CS21)|(1<<CS20);		// clock: F_CPU / 1024
	TCCR2 |= (1<<WGM21)|(0<<WGM20);					// Clear Timer on Compare
	TIMSK |= (1<<OCIE2);							// Output compare match interrupt enable
	OCR2 = 156;										// Interrupt every 10ms
	
		
	sei();
	ADCSRA |= 1<<ADSC;		// ADC Start Conversion
}
Esempio n. 8
0
void refreshQuote(){
	if(bDateChanged){
		bDateChanged=0;
		sprintf(str, "%02d/%02d/%02d,", tTime.bDay, tTime.bMonth, tTime.bYear);
		LCDWriteStringXY(0,0,str);
	}
	if(bTimeChanged){
		bTimeChanged=0;
		//sprintf(str, "%02d:%02d:%02d", tTime.bHour, tTime.bMin, tTime.bSec);
		//LCDWriteStringXY(8,0,str);
		sprintf(str, "%02d", tTime.bHour);
		LCDWriteStringXY(CLOCK_CURSOR_POSITION, 0, str);
		sprintf(str, "%02d", tTime.bMin);
		LCDWriteStringXY(CLOCK_CURSOR_POSITION+3, 0, str);
	}
	if(bTempChanged){
		bTempChanged=0;
		sprintf(str, "%04.1f", dTemperature);		// float printed with 4 digits (dot included), 1 of which are decimal, zero padded
		LCDWriteStringXY(0,1, "T=");
		LCDWriteStringXY(TEMP_CURSOR_POSITION,1, str);
	}
	if(bHumChanged){
		bHumChanged=0;
		sprintf(str, "%2d", bHumidity);
		LCDWriteStringXY(HUM_CURSOR_POSITION, 1, str);
		if(bHumidity>99){ bSfo=1; }		// se l'umidità arriva al 100% cancella il carattere '%':
		if(bHumidity<100 && bSfo){		// lo ripristiniamo in questo modo.
			bSfo=0;
			LCDWriteStringXY(HUM_CURSOR_POSITION+2,1,"%");
		}
	}
}
Esempio n. 9
0
void displayStatus()
{
   	LCDClear();
	LCDWriteStringXY(0, 0, bm_cfg.ProfileName);
	uint8_t sel_antenna_Id = bm_cfg.AntSelBandCfg[BandSelected];
	const char * msg = bm_cfg.AntLongNames[sel_antenna_Id];
	if (sel_antenna_Id == NO_ANTENNA)
	{
		msg = err_no_ant;
	}
	else if (isPeerAntConflict(sel_antenna_Id))
	{
		msg = err_ant_conflict;
	}
	uint8_t len = strLen(msg);
	uint8_t x = 7 + (10 - len) / 2;		
	LCDWriteStringXY(x, 0, msg);

	LCDWriteStringXY(18, 0, band_str[BandSelected]);

	// display antennas to select:
	for (uint8_t i = 0; i < MAX_ANT_PER_BAND; i++)
	{
		uint8_t antenna_Id = bm_cfg.AntAvailBandCfg[BandSelected][i];
		if (antenna_Id == NO_ANTENNA)
		{
			displayAntShort(i, "", "-");
		}
		else
		{
			const char* decor = " ";
			if (antenna_Id == sel_antenna_Id)
			{
				decor = "*";
			}
			if (isPeerAntConflict(antenna_Id))
			{
				decor = "X";
			}

			displayAntShort(i, bm_cfg.AntShortNames[antenna_Id], decor);		
		}
	}
}
Esempio n. 10
0
void displayMsg(const char* msg1, const char* msg2, uint8_t time_s)
{
	uint8_t len1 = strLen(msg1);
	if (len1 > 24)
	{
		return;
	}
	uint8_t len2 = strLen(msg2);
	if (len2 > 24)
	{
		return;
	}
   	LCDClear();
	LCDWriteStringXY(12 - (len1 / 2), 0, msg1);
	LCDWriteStringXY(12 - (len2 / 2), 1, msg2);
	if (time_s > 0)
	{
		scheduleDispRefresh(time_s);
	}
}
Esempio n. 11
0
void update_LCD_screen(int view)
{
	/* Switch view on LCD */
	if (view == LCD_CURRENT_TEMP) {
		/* Create string to display on LCD */
		sprintf(lcd_string, "     %1.1f F", (double)g_current_temp);

		/* Print update to LCD */
		LCDClear();
		LCDWriteString(" Internal Temp  ");
		LCDWriteStringXY(0, 1, lcd_string);
	} 
	if (view == LCD_DEFINED_TEMP) {
		/* Create string to display on LCD */
		sprintf(lcd_string, "     %1.1f F", (double)g_defined_temp);

		/* Print update to LCD */
		LCDClear();
		LCDWriteString("  Desired Temp");
		LCDWriteStringXY(0, 1, lcd_string);
	}
}
Esempio n. 12
0
void main()
{
	uint8_t cmd=0;


	RemoteInit();

	LCDInit(LS_BLINK);

	LCDClear();

	LCDWriteString("IR RemoteDecoder");
	LCDWriteStringXY(0,1,"Key Code:");

	while(1)
	{
		cmd=GetRemoteCmd(1);
		LCDWriteIntXY(10,1,cmd,3);
	}
}
Esempio n. 13
0
int main(void)
{
	stdin = stdout = &usart0_str;

	perform_inits();

	/* Local variables */
	char c;
	char *ing1;
	char *ing2;
	char *ing3;
	char *ing4;
	char *sensor_temp;
	char *defined_temp;
	TempData *temps;
	Recipe *recipe;

	/* Allocate space for temp struct */
	temps = malloc(sizeof(TempData));

	/* Allocate space for recipe struct */
	recipe = malloc(sizeof(Recipe));

	/* Allocate space for holding each ingredient */
	ing1 = malloc(3 * sizeof(char));
	ing2 = malloc(3 * sizeof(char));
	ing3 = malloc(3 * sizeof(char));
	ing4 = malloc(3 * sizeof(char));

	/* Allocate space for temp data */
	sensor_temp = malloc(4 * sizeof(char));
	defined_temp = malloc(2 * sizeof(char));

	/* Check for memory allocation errors */
	if ((temps == NULL) || (recipe == NULL) || (ing1 == NULL) || (ing2 == NULL) || (ing3 == NULL) || (ing4 == NULL) || (sensor_temp == NULL) || (defined_temp == NULL)) {
		LCDClear();
		LCDWriteString("Error while alloc");
		LCDWriteStringXY(0,1,"ating memory :(");
		while(1);
	}

	/* Initialize structure values */
	init_temps_struct(temps);
	init_recipe_struct(recipe);

	/* Enable Timer3 overflow interrupt - used for changing display of LCD */
	enable_temp_sensor_timer_interrupt();

	while(1) {
		// Wait for signal from website
		scanf(" %1c", &c);

		switch(c) {
			case 'c':
			// get value from temp sensor
			// compare with set temp
			// decide whether cooler is on/off
				disable_temp_sensor_timer_interrupt();
				scanf(" %4s", sensor_temp);
				convert_temp_to_float(temps, sensor_temp, 0);
				g_current_temp = temps->sensor_temp;
				if (temps->sensor_temp >= 20 || temps->sensor_temp <= 90) {
					update_LCD_screen(LCD_CURRENT_TEMP);
					compare_temps(temps);
				}
				enable_temp_sensor_timer_interrupt();
				break;
			case 'p':
			// read the four ingredient amounts
			// turn each motor on to pour the ingredients
				disable_temp_sensor_timer_interrupt();
				isPouring = true;
				scanf(" %s", ing1);
				scanf(" %s", ing2);
				scanf(" %s", ing3);
				scanf(" %s", ing4);
				convert_recipe_to_float(recipe, ing1, ing2, ing3, ing4);

				if ((recipe->ing1 >= 0. || recipe->ing1 <= 8.) && (recipe->ing2 >= 0. || recipe->ing2 <= 8.)  && (recipe->ing3 >= 0. || recipe->ing3 <= 8.)  && (recipe->ing4 >= 0. || recipe->ing4 <= 8.)) {
					LCDClear();
					LCDWriteString("Pouring recipe..");
					
					/* Go pour the recipe */
					pour_recipe(recipe);

					update_LCD_screen(LCD_CURRENT_TEMP);
				}
				enable_temp_sensor_timer_interrupt();
				break;
			case 't':
				/* Grab updated internal temp from site */
				disable_temp_sensor_timer_interrupt();
				scanf(" %2s", defined_temp);
				convert_temp_to_float(temps, defined_temp, 1);

				/* Create string to display on LCD */
				g_defined_temp = temps->defined_temp;
				sprintf(lcd_string, "     %1.1f F", (double)g_defined_temp);
				LCDClear();
				LCDWriteString("  Temp updated");
				LCDWriteStringXY(0, 1, lcd_string);

				_delay_ms(1000);
				update_LCD_screen(LCD_CURRENT_TEMP);
				enable_temp_sensor_timer_interrupt();
				break;
		}
	}
	if (ing1) free(ing1);
	if (ing2) free(ing2);
	if (ing3) free(ing3);
	if (ing4) free(ing4);
	if (sensor_temp) free(sensor_temp);
	if (defined_temp) free(defined_temp);
	if (temps) free(temps);
	if (recipe) free(recipe);
	return 0;
}
Esempio n. 14
0
void sMCIfaceLCD_writeStringXY(const sc_string chr, const sc_integer x, const sc_integer y) {
	LCDWriteStringXY(x,y,chr);
}
Esempio n. 15
0
void main()
{

InitTIMER();

InitADC();


InitLCD(LS_BLINK|LS_ULINE);	//Initialize LCD module


LCDClear();	//Clear the screen

sei();
//************


/*	while(0)
	{


		a=Beolvas8bitADC(5);
		a*=2;
		a-=273;
		i=a; //mert a fv csak int értéket vár és az a egfy long



		LCDWriteIntXY(0,0,hour,2);
		LCDWriteStringXY(2,0,":");
		LCDWriteIntXY(3,0,min,2);
		LCDWriteStringXY(5,0,":");
		LCDWriteIntXY(6,0,sec,2);

		LCDWriteIntXY(10,0,i,3);
		LCDData(0b11011111);
		LCDWriteStringXY(14,0,"C");

		_delay_ms(100);
	
		if(sec==60){sec=0; min++;}
		if(min==60){min=0; hour++;}
		if(hour==24){hour=0;}

		

	}
	*/



unsigned char alma;
	

while(1)
{
alma=Beolvas8bitADC(5);
LCDWriteIntXY(0,0,alma,3);
LCDWriteStringXY(15,0,"C");
_delay_ms(1000);

}







}
Esempio n. 16
0
int main(void){
	
	bPriLev=PRI_MAIN;
	
	tTime.bDay=7;
	tTime.bMonth=8;
	tTime.bYear=11;
	
	
	
	_init();
	
	while(1) { /* Infinite Loop */
		switch(bState){

/*--------------------------------------------------------------------------------------------------*/
			case STATE_IDLE:
				switch(bBtn){
					case NO_BTN:
						if(bTimeColonToToggle){ toggleTimeColon(); bTimeColonToToggle=0; }
						refreshQuote();
						bFirst=1;
						
						break;
					case BTN_A:
					case BTN_B:
					case BTN_C:
					case BTN_A_LONG:
					case BTN_B_LONG:
						START_BACKLIGHT();
						bBtn=NO_BTN;
						break;
					case BTN_C_LONG:
						bState = STATE_MENU;
						BACKLIGHT_ON();
						bBtn=NO_BTN;
						break;
					default:
						break;
				}
				break;
				
/*--------------------------------------------------------------------------------------------------*/				
			case STATE_MENU:
				switch(bBtn){
					case NO_BTN:
						if(bSelectionMenuChanged || bFirst){
							bFirst=0;
							bSelectionMenuChanged=0;
							LCDWriteStringXY(0,0,"-");
							LCDWriteStringXY(1,0, options[bSelectionMenu]);
							LCDWriteStringXY(0,1," ");
							LCDWriteStringXY(1,1, options[bSelectionMenu+1]);
						}
						break;
					
					case BTN_A:
						bSelectionMenu++;
						bSelectionMenu %= NUMBER_OF_OPTIONS;
						bSelectionMenuChanged=1;
						bBtn=NO_BTN;
						break;
					
					case BTN_B:
						if(bSelectionMenu>0) bSelectionMenu--;
						else bSelectionMenu=(NUMBER_OF_OPTIONS-1);
						bSelectionMenu %= NUMBER_OF_OPTIONS;
						bSelectionMenuChanged=1;
						bBtn=NO_BTN;
						break;
					
					case BTN_C:
						switch(bSelectionMenu){
							case SEL_DATE:
								bState = STATE_EDIT_DATE;
								bBtn = NO_BTN;
								break;
							case SEL_TIME:
								bState = STATE_EDIT_TIME;
								bBtn = NO_BTN;
								break;
							case SEL_TIMEZONE:
								bState = STATE_EDIT_TIMEZONE;
								bBtn = NO_BTN;
								break;
							default:
								break;
						}
						bFirst=1;
						break;
					
					case BTN_C_LONG:
						bState = STATE_IDLE;
						bBtn=NO_BTN;
						BACKLIGHT_OFF();
						START_BACKLIGHT();
						
						LCD_RESET();
						
						bSelectionMenu=0;
						bDateChanged=1;		// Appena rientro in idle stampo le quote
						bTimeChanged=1;
						bTempChanged=1;
						break;
					
					default:
						break;
				}				
				break;
			
/*--------------------------------------------------------------------------------------------------*/			
			case STATE_EDIT_DATE:
				switch(bBtn){
					case NO_BTN:
						if(bFirst){
							bFirst=0;
							LCDClear();
							tTimeEditing = tTime;
							sprintf(str, "%02d/%02d/%02d", tTimeEditing.bDay, tTimeEditing.bMonth, tTimeEditing.bYear);
							LCDWriteStringXY(0,0, "Editing date:");
							LCDWriteStringXY(3,1, str);
							LCDCmd(0x0f);	// set blinking cursor
							LCD_CURSOR_LEFT_N(8);
						}
						break;
					case BTN_A:
						bBtn = NO_BTN;
						changeEditDate(bSelectionDate, BTN_A);
						sprintf(str, "%02d/%02d/%02d", tTimeEditing.bDay, tTimeEditing.bMonth, tTimeEditing.bYear);
						LCDWriteStringXY(3,1,str);
						LCD_CURSOR_LEFT_N(8-bSelectionDate);
						break;
					case BTN_B:
						bBtn = NO_BTN;
						changeEditDate(bSelectionDate, BTN_B);
						sprintf(str, "%02d/%02d/%02d", tTimeEditing.bDay, tTimeEditing.bMonth, tTimeEditing.bYear);
						LCDWriteStringXY(3,1,str);
						LCD_CURSOR_LEFT_N(8-bSelectionDate);
						break;
					case BTN_C:
						if(bSelectionDate<7){ LCD_CURSOR_RIGHT_N(1); bSelectionDate++; }
						else{ bSelectionDate=0; LCD_CURSOR_LEFT_N(7); }
						// eseguo il controllo della data solo se passo da giorno a mese e da mese a anno
						if(bSelectionDate==2 || bSelectionDate==5){
							LCD_CURSOR_RIGHT_N(1);
							bSelectionDate++;
							if(checkDate(&tTimeEditing, aDays)){
								sprintf(str, "%02d/%02d/%02d", tTimeEditing.bDay, tTimeEditing.bMonth, tTimeEditing.bYear);
								LCDWriteStringXY(3,1,str);
								LCD_CURSOR_LEFT_N(8-bSelectionDate);
							}
						}
						bBtn = NO_BTN;
						break;
					case BTN_C_LONG:
						bState=STATE_EDIT_DATE_CONFIRM;
						bBtn=NO_BTN;
						bFirst=1;
						break;
					default:
						break;
				}
				break;

/*--------------------------------------------------------------------------------------------------*/
				case STATE_EDIT_DATE_CONFIRM:
					switch(bBtn){
						case NO_BTN:
							if(bFirst){ 
								LCDWriteStringXY(0,0, "Confermi? Si/No");
								LCD_CURSOR_LEFT_N(5);
								bFirst=0;
								bSelectionDate=0;
							}
							if(bSelectionDateChanged){
								if(bSelectionDate==0){ LCD_SET_CURSOR_POSITION(10); }
								else{ LCD_SET_CURSOR_POSITION(13); }
								bSelectionDateChanged=0;
							}
							break;
						
						case BTN_C:
							bSelectionDate = (bSelectionDate == 0)?1:0;
							bSelectionDateChanged=1;
							bBtn=NO_BTN;
							break;
						
						case BTN_C_LONG:
							if(bSelectionDate){
								bState = STATE_MENU;
								bSelectionDate=0;
							}else{
								tTime.bDay = tTimeEditing.bDay;
								tTime.bMonth = tTimeEditing.bMonth;
								tTime.bYear = tTimeEditing.bYear;
								bState = STATE_MENU;
								LCD_RESET();
							}
							bBtn = NO_BTN;
							bFirst=1;
							break;
						default: break;
					}
				break;

/*--------------------------------------------------------------------------------------------------*/
			case STATE_EDIT_TIME:
					switch(bBtn){
						case NO_BTN:
							if(bFirst){
								bFirst=0;
								LCDClear();
								tTimeEditing = tTime;
								sprintf(str, "%02d:%02d:%02d", tTimeEditing.bHour, tTimeEditing.bMin, tTimeEditing.bSec);
								LCDWriteStringXY(0,0, "Editing time:");
								LCDWriteStringXY(3,1, str);
								LCDCmd(0x0f);	// set blinking cursor
								LCD_CURSOR_LEFT_N(8);
							}
							break;
						case BTN_A:
							bBtn = NO_BTN;
							changeEditTime(bSelectionTime, BTN_A);
							sprintf(str, "%02d:%02d:%02d", tTimeEditing.bHour, tTimeEditing.bMin, tTimeEditing.bSec);
							LCDWriteStringXY(3,1,str);
							LCD_CURSOR_LEFT_N(8-bSelectionTime);
							break;
						case BTN_B:
							bBtn = NO_BTN;
							changeEditTime(bSelectionTime, BTN_B);
							sprintf(str, "%02d:%02d:%02d", tTimeEditing.bHour, tTimeEditing.bMin, tTimeEditing.bSec);
							LCDWriteStringXY(3,1,str);
							LCD_CURSOR_LEFT_N(8-bSelectionTime);
							break;
						case BTN_C:
							if(bSelectionTime<7){ LCDCmd(0x14); bSelectionTime++; }
							else{ bSelectionTime=0; LCD_CURSOR_LEFT_N(7); }
							if(bSelectionTime==2 || bSelectionTime==5){ LCDCmd(0x14); bSelectionTime++; }
							bBtn = NO_BTN;
							break;
						case BTN_C_LONG:
							bState=STATE_EDIT_TIME_CONFIRM;
							bBtn=NO_BTN;
							bFirst=1;
							break;
						default:
							break;
					}
				break;

/*--------------------------------------------------------------------------------------------------*/
			case STATE_EDIT_TIME_CONFIRM:
					switch(bBtn){
						case NO_BTN:
							if(bFirst){ 
								LCDWriteStringXY(0,0, "Confermi? Si/No");
								LCD_CURSOR_LEFT_N(5);
								bFirst=0;
								bSelectionTime=0;
							}
							if(bSelectionTimeChanged){
								if(bSelectionTime==0){ LCD_SET_CURSOR_POSITION(10); }
								else{ LCD_SET_CURSOR_POSITION(13); }
								bSelectionTimeChanged=0;
							}
							break;
						
						case BTN_C:
							bSelectionTime = (bSelectionTime == 0)?1:0;
							bSelectionTimeChanged=1;
							bBtn=NO_BTN;
							break;
						
						case BTN_C_LONG:
							if(bSelectionTime){
								bState = STATE_MENU;
								bSelectionTime=0;
							}else{
								tTime.bSec = tTimeEditing.bSec;
								tTime.bMin = tTimeEditing.bMin;
								tTime.bHour = tTimeEditing.bHour;
								bState = STATE_MENU;
								LCD_RESET();
							}
							bBtn = NO_BTN;
							bFirst=1;
							break;
						default: break;
					}
				break;
				
/*--------------------------------------------------------------------------------------------------*/
			case STATE_EDIT_TIMEZONE:
				switch(bBtn){
					case NO_BTN:
						if(bFirst){
							bFirst=0;
							LCDClear();
							LCDWriteStringXY(0,0, "Edit timezone:");
							LCDWriteStringXY(0,1,"1=");
							
						}
						break;
					default:
						break;
				}
				break;
			default:
				break;
		}
	}	
}
Esempio n. 17
0
void ShowMainMenu()
{
	//The Main Menu
	char *menu_items[]={ 	"Set Time",
							"Set On Time",
							"Set Off Time",
							"Quit"
						};
	uint8_t menu_count=4;
	uint8_t selected=0;

	_delay_loop_2(0);
	_delay_loop_2(0);

	while(1)
	{
		LCDClear();
		LCDWriteString("    Main Menu  ");

		LCDWriteStringXY(2,1,menu_items[selected]);

		LCDWriteStringXY(0,1,"<");
		LCDWriteStringXY(15,1,">");

		if(GetKeyStatus(1))
		{
			//Left Key(No 1) is pressed
			//Check that it was not pressed previously
			if(!GetPrevKeyStatus(1))
			{
				if(selected !=0)
					selected--;
			}
		}

		if(GetKeyStatus(0))
		{
			//Right Key(No 0) is pressed
			//Check that it was not pressed previously
			if(!GetPrevKeyStatus(0))
			{
				if(selected !=(menu_count-1))
					selected++;
			}
		}

		if(GetKeyStatus(2))
		{
			//Enter Key Pressed
			//Check that it was not pressed previously
			if(!GetPrevKeyStatus(2))
			{
				//Call Appropriate Function
				switch (selected)
				{
					case 0:
						SetTime();
						break;
					case 1:
						//SetOnTime();
						break;

					case 2:
						//SetOffTime();
						break;

					case 3:
						return;//Quit
				}
				
			}
		}

		PREV_PINB=PINB;

		_delay_loop_2(5000);
	}
}
Esempio n. 18
0
void main()
{
	//Wait Util Other device startup
	_delay_loop_2(0);
	_delay_loop_2(0);

	//Initialize the LCD Module
	LCDInit(LS_BLINK);

	//Initialize I2C Bus
	I2CInit();

	//Enable Pull ups on keys
	PORTB|=((1<<PB2)|(1<<PB1)|(1<<PB0));

	//Clear CH bit of RTC
	#define CH 7

	uint8_t temp;
	DS1307Read(0x00,&temp);

	//Clear CH Bit
	temp&=(~(1<<CH));

	DS1307Write(0x00,temp);

	//Set 12 Hour Mode
	DS1307Read(0x02,&temp);

	//Set 12Hour BIT
	temp|=(0b01000000);

	//Write Back to DS1307
	DS1307Write(0x02,temp);

	
	LCDClear();
	
	LCDWriteString("DS1307 RTC Exmple");
	
	char Time[12];	//hh:mm:ss AM/PM
	
	//Now Read and format time
	uint8_t data;
	
	while(1)
	{
		
		DS1307Read(0x00,&data);
		Time[8]='\0';
		Time[7]=48+(data & 0b00001111);
		Time[6]=48+((data & 0b01110000)>>4);
		Time[5]=':';
	
		DS1307Read(0x01,&data);
	
		Time[4]=48+(data & 0b00001111);
		Time[3]=48+((data & 0b01110000)>>4);
		Time[2]=':';
	
		DS1307Read(0x02,&data);
	
		Time[1]=48+(data & 0b00001111);
		Time[0]=48+((data & 0b00010000)>>4);

		LCDClear();
	
		LCDWriteString("DS1307 RTC Exmple");
	
		LCDWriteStringXY(2,1,Time);

		//AM/PM
		if(data & 0b00100000)
		{
			LCDWriteStringXY(11,1,"PM");
		}
		else
		{
			LCDWriteStringXY(11,1,"AM");
		}

		//Wait Some time and keep testing key input
		uint8_t i;
		for(i=0;i<20;i++)
		{
		
			if(GetKeyStatus(2))
			{
				//Go To Main Menu
				ShowMainMenu();

				_delay_loop_2(0);
				_delay_loop_2(0);
				_delay_loop_2(0);

			}
			_delay_loop_2(5000);
		}
	}
	

	
}
int main(void)
{
	
	Port_Init();
	DDRE |= (1<<3);
	DDRA |= (1<<6);
	LCDInit(LS_NONE);
	
	DDRC |= (1<<3);
	PORTC |= (1<<3);
	cal_point_1 = eeprom_read_dword(0);
	cal_point_2 = eeprom_read_dword((uint32_t*)5);
	target_feedback = (cal_point_1 + cal_point_2)/2;
	cli();
	TIMSK |= ((1 << TICIE1)| (1 << TOIE1));   //Set capture interrupt
	TCCR1B |= ((1 << ICES1) | (1<<CS10));
	TCCR3A = ((1<<COM3A1));
	TCCR3B = ((1<<CS30) | (1<<WGM33));
	ICR3 = 1000;
	OCR3A = 0;
	sei(); 
	LCDClear();
	//LCDWriteString("Powering On...");
	red_light();
	WDT_Init();
	OCR3A = (int)pwm_duty_cycle;
	_delay_ms(100);
	/*int warm_up_timer = 10;
	while(warm_up_timer > 0)
	{
		if (feedback_speed < (target_feedback - 7))
		{
			if (feedback_speed > (target_feedback - 100))
			{
				pwm_duty_cycle += 1;
			}
		}
		if (feedback_speed > (target_feedback + 7))
		{
			if(feedback_speed < (target_feedback + 100))
			{
				pwm_duty_cycle -= 1;
			}
			
		}
		if (feedback_speed < (target_feedback - 100) )
		{
			pwm_duty_cycle += 3;
		}
		if (feedback_speed > (target_feedback + 100))
		{
			pwm_duty_cycle -= 3;
		}
		if(pwm_duty_cycle > 499){pwm_duty_cycle = 499;}
		if(pwm_duty_cycle < 0 ){pwm_duty_cycle = 0;}
		
		wdt_reset();
		_delay_ms(100);
		LCDClear();
		char feedback_speed_string[4];
		snprintf(feedback_speed_string,4,"%d",feedback_speed);
		LCDWriteString(feedback_speed_string);
		warm_up_timer -= 1;
	}
	*/
	
	
	
	
    while(1)
    {
		feedback_speed = (new_feedback_speed);
        if (page == 0)
		{
			wdt_reset();
			real_speed = ((double) feedback_speed / (double) target_feedback)*4.5f;
			average_speed = (real_speed+average_speed*4.0f)/5.0f;
			int avg_speed_int = (int)average_speed;
			float avg_speed_float = average_speed - avg_speed_int;
			int avg_speed_dec = avg_speed_float*10;
			snprintf(avg_speed_string,4,"%01d.%01d",avg_speed_int,avg_speed_dec);
			LCDClear();
			LCDWriteString("Airflow: ");
			LCDWriteString(avg_speed_string);
			LCDWriteString(" m/s");
			LCDWriteStringXY(0,1,"Target: 4.5 m/s");
			wdt_reset();
			_delay_ms(100);
			wdt_reset();
			
			// speed control section
			if (feedback_speed < (target_feedback - 10))
			{
				if (feedback_speed > (target_feedback - 200))
				{
					pwm_duty_cycle += 0.25;
				}
			}
			if (feedback_speed > (target_feedback + 10))
			{
				if(feedback_speed < (target_feedback + 200))
				{
					pwm_duty_cycle -= 0.25;
				}
				
			}
			if (feedback_speed < (target_feedback - 200) )
			{
				pwm_duty_cycle += 4;
			}
			if (feedback_speed > (target_feedback + 200))
			{
				pwm_duty_cycle -= 4;
			}
			if (feedback_speed < (target_feedback - 60))
			{
				errors += 20;
			}
			if (feedback_speed > (target_feedback + 60))
			{
				errors += 20;
			}
			if (feedback_speed < (target_feedback + 40))
			{
				if (feedback_speed > (target_feedback - 40))
				{
					errors = 0;
				}
			}
			if (errors > 250) 
			{
				errors = 250;
				
			}
			if (pwm_duty_cycle > 999) {pwm_duty_cycle = 999;}
			if (pwm_duty_cycle < 0){pwm_duty_cycle = 0;}
				
		
			
			OCR3A = (int)pwm_duty_cycle;
			wdt_reset();
		}
		
		
		
		
		
		if (page == 1)
		{
			LCDClear();
			LCDWriteString("Calibration Mode");
			if(cal_number == 0)
			{
				LCDWriteStringXY(0,1,"Cal. Point 1");
			}
			if(cal_number == 1)
			{
				LCDWriteStringXY(0,1,"Cal. Point 2");
			}
			if(cal_number == 2)
			{
				LCDWriteStringXY(0,1,"Press %1 To Save");
			    _delay_ms(50);
				LCDWriteStringXY(0,1,"Press %2 To Save");
				_delay_ms(50);
				wdt_reset();
			}
			wdt_reset();
			_delay_ms(150);
			wdt_reset();
			
		}
		
		
		
		
		if (!(PINA & (1<<3)))
		{
			page++;
			while(!(PINA & (1<<3)))
			{
				wdt_reset();
			} //right
			_delay_ms(25);
			if (page > 1)
			{
				page = 0;
			}
		}
		
		
		
		if (!(PINA & (1<<0))) //up
		{
			if (page == 1)
			{
				cal_speed += 5;
				if (cal_speed > 1000)
				{
					cal_speed = 1000;
				}
				OCR3A = cal_speed;
				wdt_reset();
			}	
			_delay_ms(25);	
		}
		
		
		
		
		if (!(PINA & (1<<4))) //down
		{
			if (page == 1)
			{
				cal_speed -= 5 ;
				if (cal_speed < 0)
				{
					cal_speed = 0;
				}
				OCR3A = cal_speed;
				wdt_reset();
			}
			_delay_ms(25);
		}
		
		
		
		
		if (!(PINA & (1<<2))) //center
		{
			wdt_reset();
			if (page == 1)
			{			
				switch (cal_number)
				{
					case 0:
					cal_point_1 = feedback_speed;
					cal_number = 1;
					break;
					
					case 1:
					cal_point_2 = feedback_speed;
					cal_number = 2;
					break;
					
					case 2:
					wdt_reset();
					eeprom_write_dword(0,cal_point_1);
					eeprom_write_dword((uint32_t*)5,cal_point_2);
					wdt_reset();
					page = 0;
					cal_number = 0;
					break;
					
				}
			}
			
			
			while(!(PINA & (1<<2)))
			{
				wdt_reset();
			}
			_delay_ms(25);
		}
		if (!(PINA & (1<<1))) //left
		{
			page--;
			while(!(PINA & (1<<3)))
			{
				wdt_reset();
			}
			_delay_ms(25);
			if (page < 0)
			{
				page = 1;
			}
		}
		if (errors == 0)
		{
			green_light();
		}
		if(errors >= 250)
		{
			red_light();
		}
		
		
    }
}
int main(void)
{
	// Ждем стабилизации внешнего резонатора
	_delay_ms(500);
	_delay_ms(500);
	
	//Конфигурируем порты
	DDRA=0b11111110;
	DDRB=0b00001111;
	DDRC=0b00010000;
	DDRD=0b10000011;


	_delay_ms(250);

/*	
	//Выбор языка согласно установленным свичам
	if((PINB&(0<<PINB0))&&(PINB&(0<<PINB1))&&(PINB&(0<<PINB2))&&(PINB&(0<<PINB3)))
	{
		langsw=0; //English - all off
	}	
	if((PINB&(1<<PINB0))&&(PINB&(0<<PINB1))&&(PINB&(0<<PINB2))&&(PINB&(0<<PINB3)))
	{
		langsw=1; //Deutch
	}
	if((PINB&(0<<PINB0))&&(PINB&(1<<PINB1))&&(PINB&(0<<PINB2))&&(PINB&(0<<PINB3)))
	{
		langsw=2; //French
	}
	if((PINB&(0<<PINB0))&&(PINB&(0<<PINB1))&&(PINB&(1<<PINB2))&&(PINB&(0<<PINB3)))
	{
		langsw=3; //Espaniol
	}
	if((PINB&(0<<PINB0))&&(PINB&(0<<PINB1))&&(PINB&(0<<PINB2))&&(PINB&(1<<PINB3)))
	{
		langsw=4; //Russian
	}
*/	
	
	//Инициализация экрана
	LCDInit(LS_BLINK|LS_ULINE);
	LCDClear();
	LCDWriteStringXY(5,0,"CRYOMED");

	_delay_ms(200);
	
	// Отключаем прерывания Таймера 2.
	TIMSK &= ~(_BV(TOIE2) | _BV(OCIE2));
	
	// Переводим Таймер 2 в асинхронный режим (тактирование от
	// часового кварцевого резонатора).
	ASSR |= _BV(AS2);

	TCNT2 = 0x00;
	TCCR2 = 0x05; //Устанавливаем коэффициент деления равным 128.
	OCR2  = 0x00;

	// Ждем готовности таймера.
	while (ASSR & (_BV(TCN2UB) | _BV(OCR2UB) | _BV(TCR2UB)));

	// Разрешаем прерывание от Таймера 2.
	TIMSK |= _BV(TOIE2);
	
	//Разрешаем прерывания по INT0
	GICR |= (1<<INT0);
	MCUCR=(0<<ISC00)|(0<<ISC01);

	// Запрещаем прерывания глобально.
	cli();
	
menuloop:

	//Инициализируем переменные
	work=false;
	alarm=false;
	workmode=10;
	taskwork=0;
	t.minute=0;
	t.second=0;
	tmp_t.minute=99;
	tmp_t.second=99;

	//Главное меню
	//Включаем клавиатуру
	PORTD &= ~(1<<7);
	_delay_ms(250);
//	strcpy_P(buffer, (PGM_P)pgm_read_word(&(lang_1[langsw])));
	LCDClear();
	LCDWriteStringXY(0,0,"  Choose mode  ");
	LCDWriteStringXY(0,1,"  Program  ");
//	strcpy_P(buffer, (PGM_P)pgm_read_word(&(lang_2[langsw])));
	while(1) //Опрос кнопок
	{
		if(PINC&(1<<PINC3))
		{
			workmode=0;
			LCDWriteStringXY(10,1," #1");
		}
		if(PINC&(1<<PINC2))
		{
			workmode=3;
			LCDWriteStringXY(10,1," #2");
		}
		if(PINC&(1<<PINC1))
		{
			workmode=6;
			LCDWriteStringXY(10,1," #3");
		}
		
		if((PINC&(1<<PINC0))&&(workmode<7))
		{
			LCDClear();
			LCDWriteStringXY(0,0,"  Program     ");
			LCDWriteIntXY(12,0,workmode,1);
			LCDWriteStringXY(0,1,"  will started  ");
			_delay_ms(200);
			_delay_ms(200);
			work=true;
			LCDClear();
			goto workloop;
		}
	}

workloop:
	//Блокируем клавиатуру
	PORTD |= 1<<7;
	//Светодиод индикации работы
	PORTD &= ~(1<<1);	//GND
	PORTD |= 1<<0;		//VCC
	
	//Включаем кулер
	PORTB |= 1<<0;

	GIFR |= (1<<INTF0);
	
	// Разрешаем прерывания глобально
	sei();
	
	LCDWriteStringXY(0,0,"Remaining:   :");
	step=0;
	taskminute=pgm_read_byte(&(mode[step][workmode]));
	tasksecond=pgm_read_byte(&(mode[step][workmode+1]));
	taskwork=pgm_read_byte(&(mode[step][workmode+2]));
	
	while(1)
	{
		TCCR2=0x05;           // Записываем "отфанарное значение" в регистр

		// Ждем готовности таймера 2.
		while (ASSR & (_BV(TCN2UB) | _BV(OCR2UB) | _BV(TCR2UB)));
		
		if(alarm==true)
		{
			goto alarm_cont;
		}
		
					if((taskminute==t.minute)&&(tasksecond==t.second))
					{
						if(taskwork==1)
						{
							PORTB |= 1<<1;
							PORTB |= 1<<2;
							PORTD |= 1<<0;
							goto cont;
						}
						if(taskwork==2)
						{
							PORTB &= ~(1<<1);
							PORTB &= ~(1<<2);
							PORTD &= ~(1<<0);
							goto cont;
						}
						if(taskwork==3)
						{
							PORTB |= 1<<3;
							PORTD |= 1<<0;
							goto cont;
						}
						if(taskwork==4)
						{
							PORTB &= ~(1<<1);
							PORTB &= ~(1<<2);
							PORTB |= 1<<3;
							PORTD |= 1<<0;
							goto cont;
						}						
						if(taskwork==5)
						{
							work=false;
							PORTB &= ~(1<<0);
							PORTB &= ~(1<<1);
							PORTB &= ~(1<<2);
							PORTB &= ~(1<<3);
							PORTD &= ~(1<<1);
							goto menuloop;
						}
cont:
						step++;						
						
						taskminute=pgm_read_byte(&(mode[step][workmode]));
						tasksecond=pgm_read_byte(&(mode[step][workmode+1]));
						taskwork=pgm_read_byte(&(mode[step][workmode+2]));
						//goto cont2;
					}

cont2:				
				if(tmp_t.minute!=t.minute)
				{
					LCDWriteIntXY(11,0,29-t.minute,2);
					tmp_t.minute=t.minute;
				}					
//				if(t.second==00)
//				{
//					LCDWriteStringXY(14,0,"00");
//				}
//				else
//				{
					LCDWriteIntXY(14,0,60-t.second,2);
//				}				
	}
alarm_cont:
	PORTB &= ~(1<<0);
	PORTB &= ~(1<<1);
	PORTB &= ~(1<<2);
	PORTB &= ~(1<<3);
	PORTC &= ~(1<<4);
	PORTD &= ~(1<<0);
	PORTD |= 1<<1;
	LCDClear();
	LCDWriteStringXY(0,0,"   EXECUTION");
	LCDWriteStringXY(0,1,"   TERMINATE");
	return 0;
}
Esempio n. 21
0
void SetTime()
{
	

	uint8_t hr,min,sec,am_pm,temp;

	//Read the Second Register
	DS1307Read(0x00,&temp);
	sec=(((temp & 0b01110000)>>4)*10)+(temp & 0b00001111);

	//Read the Minute Register
	DS1307Read(0x01,&temp);
	min=(((temp & 0b01110000)>>4)*10)+(temp & 0b00001111);

	//Read the Hour Register
	DS1307Read(0x02,&temp);
	hr=(((temp & 0b00010000)>>4)*10)+(temp & 0b00001111);

	am_pm=(temp & 0b00100000)>>4;

	//If Hour Register is 0 make it 12, as 00:00:00 invalid time
	if(hr==0) hr=12;

	uint8_t sel=0;

	while(1)
	{
		LCDClear();

		LCDWriteString("00:00:00    <OK>");
		
		LCDWriteIntXY(0,0,hr,2);
		LCDWriteIntXY(3,0,min,2);
		LCDWriteIntXY(6,0,sec,2);

		if(am_pm)
		{
			LCDWriteStringXY(9,0,"PM");
		}
		else
		{
			LCDWriteStringXY(9,0,"AM");
		}

		//Draw Pointer
		LCDWriteStringXY(sel*3,1,"^^");

		//Input Up key
		if(GetKeyStatus(1))
		{
			if(!GetPrevKeyStatus(1))
			{
				if(sel==0)
				{	
					//Hour
					if(hr==12)
					{
						hr=1;
					}
					else
					{
						hr++;
					}
				}

				if(sel==1)
				{	
					//Min
					if(min==59)
					{
						min=0;
					}
					else
					{
						min++;
					}
				}

				if(sel==2)
				{	
					//Sec
					if(sec==59)
					{
						sec=0;
					}
					else
					{
						sec++;
					}
				}

				if(sel==3)
				{	
					//AM-PM
					if(am_pm==0)
					{
						am_pm=1;
					}
					else
					{
						am_pm=0;
					}
				}
				if(sel == 4)
				{
					//OK
					break;
				}
			}
		}

		//Input Down
		if(GetKeyStatus(0))
		{
			if(!GetPrevKeyStatus(0))
			{
				if(sel==0)
				{	
					//Hour
					if(hr==1)
					{
						hr=12;
					}
					else
					{
						hr--;
					}
				}

				if(sel==1)
				{	
					//Min
					if(min==0)
					{
						min=59;
					}
					else
					{
						min--;
					}
				}

				if(sel==2)
				{	
					//Sec
					if(sec==0)
					{
						sec=59;
					}
					else
					{
						sec--;
					}
				}

				if(sel==3)
				{	
					//AM-PM
					if(am_pm==0)
					{
						am_pm=1;
					}
					else
					{
						am_pm=0;
					}
				}
				if(sel == 4)
				{
					//OK
					break;
				}
			}
		}

		if(GetKeyStatus(2))
		{
			if(!GetPrevKeyStatus(2))
			{
				//Change Selection
				if(sel==4)
					sel=0;
				else
					sel++;
			}
		}

		PREV_PINB=PINB;

		_delay_loop_2(30000);

	}

	//Now write time back to RTC Module
	temp=((sec/10)<<4)|(sec%10);
	DS1307Write(0x00,temp);

	temp=((min/10)<<4)|(min%10);
	DS1307Write(0x01,temp);

	temp=((hr/10)<<4)|(hr%10);
	temp|=0b01000000; //12 Hr Mode
	if(am_pm)
	{
		temp|=0b00100000;
	}
	DS1307Write(0x02,temp);

	LCDClear();
	LCDWriteString("Message !");
	LCDWriteStringXY(0,1,"Main Time Set");

	uint8_t i;
	for(i=0;i<10;i++)
		_delay_loop_2(0);




}
Esempio n. 22
0
int main(void)
{
    ConfigureOscillator();

    InitApp();
    
    ADCInit();

    LCD_Init(NONE);
    
    __delay_ms(100);
    
    LCD_Clear();
    
    RTCC_Initialize();
    
    InitCustomChars();
    
// ******************************************************************************
    char * WeekDay[7] = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
    
    int8_t loopCounter = 12, previousLoopCounter = 12;
    
    int OutAirTemp;                                                     

    int Temp[11];

    int OldTemp[11] = {300};

    _Bool Out[11] = {0};                                                         // Startup values for all outputs, 0 = OFF (Output 0 - 11))
    
    _Bool OutSum = 0, outSumOldState = 0;                                        // Sum of all Outputs, Previous scan OutSum state
    
    unsigned char i = 0;
    
    char reset = 0;

    char TestKey;                                                               // Variable used for Storing Which Menu Key is Pressed

    int internalBGV;
    
    unsigned int backLightTimer = 0;

// ******************************************************************************

    while(1)
    {
        time = getRTCTime();                                                    // get the time
        
        unsigned int timer = 0;                                                 // Used to count up time in a loop, to auto exit if user in a menu too long

// ******************************************************************************
//        OutAirTemp = ((ADCRead(9) - 785)/3.2 - 500);                            // Setup for protoBoard 
//        OutAirTemp = ((ADCRead(9) - 804)/3.178 - 500);                          // Setup for first Board built, Green LED's 
        OutAirTemp = ((ADCRead(9) - 807)/3.196 - 500);                          // Setup for second Board built, Blue LED's  
// ******************************************************************************
        Temp[0] = ADCRead(0);   //Read Deck air temperature Pin 19
// ******************************************************************************
        Temp[1] = ADCRead(4);   //Read Deck floor temperature Pin 23
// ******************************************************************************
        Temp[2] = ADCRead(5);   //Read Utility room floor temperature Pin 24
// ******************************************************************************
        Temp[3] = ADCRead(6);   //Read Entrance floor temperature Pin 25
// ******************************************************************************
        Temp[4] = ADCRead(10);   //Read Master bathroom floor temperature Pin 14
// ******************************************************************************
        Temp[5] = ADCRead(11);   //Read Office floor temperature Pin 11
// ******************************************************************************
        Temp[6] = ADCRead(12);   //Read Craft room floor temperature Pin 10
// ******************************************************************************
        Temp[7] = ADCRead(17);   //Read SE basement bedroom floor temperature Pin 41
// ******************************************************************************
        Temp[8] = ADCRead(18);   //Read Media room floor temperature Pin 42
// ******************************************************************************
        Temp[9] = ADCRead(19);   //Read Garage floor temperature Pin 43
// ******************************************************************************
        Temp[10] = ADCRead(20);   //Read Garage room air temperature Pin 44
// ******************************************************************************
        internalBGV = ADCRead(0x1A);
        
         for(i = 0;i<11;++i)
        {
        Temp[i] = TempCalc(Temp[i]);
        
        if(Temp[i] > OldTemp[i] + 1 || Temp[i] < OldTemp[i] - 1)
        {
            Temp[i] = ((OldTemp[i] + Temp[i])/2);
            OldTemp[i] = Temp[i];
        }
        else Temp[i] = OldTemp[i];
        }
// ******************************************************************************
        for(i=0;i<11;i++)
        {
            if(OutAirTemp <= -250)
            {
                Bias[i] = eepromGetData(biasNeg25[i]);
            }
            
            else if (OutAirTemp > -250 && OutAirTemp <= -240)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > -240 && OutAirTemp <= -150)
            {
                Bias[i] = eepromGetData(biasNeg15[i]);
            }
            
            else if (OutAirTemp > -150 && OutAirTemp <= -140)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > -140 && OutAirTemp <= -50)
            {
                Bias[i] = BiasNeg5[i];
            }

            else if (OutAirTemp > -50 && OutAirTemp <= -40)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > -40 && OutAirTemp <= 0)
            {
                Bias[i] = Bias0[i];
            }
    
            else if (OutAirTemp > 0 && OutAirTemp <= 10)
            {
                Bias[i] = Bias[i];
            }
    
            else if (OutAirTemp > 10 && OutAirTemp <= 50)
            {
                Bias[i] = Bias5[i];
            }

            else if (OutAirTemp > 50 && OutAirTemp <= 60)
            {
                Bias[i] = Bias[i];
            }
    
            else 
            {
                Bias[i] = BiasWarm[i];
            }
        }
// ******************************************************************************
        for (i=0;i<11;i++)
        {
            Out[i] = SetOutput(Out[i], eepromGetData(setpoint[i]), Bias[i], Temp[i], eepromGetData(deadband[i]));
        }
// ******************************************************************************
        for(i=0;i<11;i++)
        {
            if (Temp[i] <= eepromGetData(setpoint[i]) + Bias[i])                //If Out is not Off
            {
                outState[i] = 1;
            }
          
            else if (Temp[i] >= eepromGetData(setpoint[i]) + Bias[i] + eepromGetData(deadband[i]))
            {
                outState[i] = 0;
            }
            
            else
            {
                outState[i] = outState[i];
            }
            
            if(outState[i] != 0)
            {
                if (outState[i] != lastOutState[i])                             //If Out changed since last read
                {
                    outStateCounter[i]+=1;                                      //Increment the OutState Counter
                }
            }
            lastOutState[i] = outState[i];                                      //And set them equal to each other, so, it doesn't count again next time through
        }
// ******************************************************************************
        OutSum = Out[1] + Out[2] + Out[3] + Out[4] + Out[5] + Out[6] + Out[7] + Out[8] + Out[9];
        
        if(outSumOldState != OutSum)                                            // OutSum has changed,
        {
            if(OutSum != 0)                                                     // because an Out is turned on
            {
                for(i=0;i<11;i++)
                {
                    if (Temp[i] < eepromGetData(setpoint[i]) + eepromGetData(deadband[i]) + Bias[i])// Check for other PV's below SP + DB + Bias,
                    {
                        Out[i] = 1;                                             // and turn them on.
                    }

                    else
                    {
                        Out[i] = 0;                                             // Turn them off if they are already too hot!!
                    }
                }
            }
            outSumOldState = OutSum;
        }
// ******************************************************************************
        if(Out[0] == 0)                                                         // If Deck Air Temp is NOT calling,
        {
            Out[1] = 0;                                                         // turn OFF Deck Floor Out           
        }
        
        if(Out[10] == 0)                                                        // If Garage Air Temp is NOT calling,
        {
            Out[9] = 0;                                                         // turn OFF Garage Floor Out
        }
        
        DeckFloorOut =          Out[1];
        UtilityRoomFloorOut =   Out[2];
        EntranceFloorOut =      Out[3];
        MasterBathFloorOut =    Out[4];
        OfficeFloorOut =        Out[5];
        CraftRoomFloorOut =     Out[6];
        SEBasementFloorOut =    Out[7]; 
        MediaRoomFloorOut =     Out[8];
        GarageFloorOut =        Out[9];

// ******************************************************************************
        if(previousLoopCounter != loopCounter)
        {
            LCD_Clear();
            mainTimer = 0;
            previousLoopCounter = loopCounter;
        }

        
        if(loopCounter < 11)
        {
            LCD_Set_Cursor(0,0);                                                //LCD Line 0 Display
            LCD_Write_String(desc[loopCounter]);
            LCDWriteStringXY(0,13,"Loop ");
            LCD_Write_Int(loopCounter,2);
            
            LCDWriteStringXY(1,0,"Bs:");                                        //LCD Line 1 SetPoint Display
            LCDWriteDecIntXY(1,3,eepromGetData(setpoint[loopCounter]) + Bias[loopCounter],3);
            LCD_Write_Char(0);
            LCD_Write_Char('C');
            LCDWriteStringXY(1,10,"Set:");
            LCDWriteDecIntXY(1,14,eepromGetData(setpoint[loopCounter]),3);
            LCD_Write_Char(0);
            LCD_Write_Char(67);

            LCDWriteStringXY(2,0,"Temp:");                                      //LCD Line 2 Temperature Display
            LCDWriteDecIntXY(2,5,Temp[loopCounter],3);
            LCD_Write_Char(0);
            LCD_Write_Char(67);

            LCDWriteStringXY(2,12,"Db:");                                       //LCD Line 2 Deadband Display
            LCDWriteDecIntXY(2,15,eepromGetData(deadband[loopCounter]),2);
            LCD_Write_Char(0);
            LCD_Write_Char(67);
            
            LCDWriteIntXY(3,0,outStateCounter[loopCounter],5);
            
            LCDWriteStringXY(3,6,"On: ");                                       //LCD Line 3 Out Display
            
            if(outState[loopCounter] == 1)
            {
                LCD_Write_Char('Y');
            }
            
            else
            {
                LCD_Write_Char('N');
            }

            LCDWriteStringXY(3,12,"FOn: ");
            
            if(outState[loopCounter] == 1 || Out[loopCounter] == 0)
            {
                LCD_Write_Char('N');
            }
            
            else
            {
                LCD_Write_Char('Y');
            }
        }

        if(loopCounter == 11)
        {
            if(mainTimer < 10)
            {
            LCDWriteStringXY(0,0,"Output loop counters");
            }
            if(mainTimer >= 10)
            {
            LCDWriteStringXY(0,0,"Hold Cancel to Reset");
            }
            
            LCDWriteIntXY(1,0,outStateCounter[0],4);
            LCDWriteIntXY(1,5,outStateCounter[1],4);
            LCDWriteIntXY(1,10,outStateCounter[2],4);
            LCDWriteIntXY(1,15,outStateCounter[3],4);
            LCDWriteIntXY(2,0,outStateCounter[4],4);
            LCDWriteIntXY(2,5,outStateCounter[5],4);
            LCDWriteIntXY(2,10,outStateCounter[6],4);
            LCDWriteIntXY(2,15,outStateCounter[7],4);
            LCDWriteIntXY(3,0,outStateCounter[8],4);
            LCDWriteIntXY(3,5,outStateCounter[9],4);
            LCDWriteIntXY(3,10,outStateCounter[10],4);
            LCD_Set_Cursor(3,15);
//            LCD_Write_2VDec_Int(421872/internalBGV,3);                          // Board with Green LCD's
            LCD_Write_2VDec_Int(405070/internalBGV,3);                          // Board with Blue LCD's

            if(TestKey == KEY_CANCEL)
            {
                reset += 1;
                
                if(reset > 10)
                {
                    for(i=0;i<11;i++)
                    {
                        outStateCounter[i]= 0;
                        reset = 0;
                    }
                }
            }
            else
            {
                reset -= 1;
                if(reset < 0)
                {
                    reset = 0;
                }
            }
        }
        
        if(loopCounter == 12)
        {
            if(powerFail == 1)
            {
                if(toggle == 1)
                {
                    LCDWriteIntXY(0,0,time.year,2);
                    LCDWriteStringXY(0,2,"/");
                    LCDWriteIntXY(0,3,time.month,2);
                    LCDWriteStringXY(0,5,"/");
                    LCDWriteIntXY(0,6,time.day,2);
                    LCDWriteStringXY(0,9,WeekDay[time.weekday]);
                    LCDWriteIntXY(0,12,time.hour,2);
                    LCDWriteStringXY(0,14,":");
                    LCDWriteIntXY(0,15,time.minute,2);
                    LCDWriteStringXY(0,17,":");
                    LCDWriteIntXY(0,18,time.second,2);
                }
                
                else 
                {
                    LCDWriteStringXY(0,0,"                    ");
                }
            }
            
            else
            {
                LCDWriteIntXY(0,0,time.year,2);
                LCDWriteStringXY(0,2,"/");
                LCDWriteIntXY(0,3,time.month,2);
                LCDWriteStringXY(0,5,"/");
                LCDWriteIntXY(0,6,time.day,2);
                LCDWriteStringXY(0,9,WeekDay[time.weekday]);
                LCDWriteIntXY(0,12,time.hour,2);
                LCDWriteStringXY(0,14,":");
                LCDWriteIntXY(0,15,time.minute,2);
                LCDWriteStringXY(0,17,":");
                LCDWriteIntXY(0,18,time.second,2);
            }
               
/*            LCDWriteStringXY(1,0,"X Pos:");
            LCDWriteIntXY(1,7,x,5);
            LCDWriteStringXY(1,13,"Col:");
            LCDWriteIntXY(1,18,col,1);

            LCDWriteStringXY(2,0,"Y Pos:");
            LCDWriteIntXY(2,7,y,5);
            LCDWriteStringXY(2,13,"Row:");
            LCDWriteIntXY(2,18,row,1);
*/
            if(mainTimer <= 8)
            {
                LCDWriteStringXY(1,0,"OutSide Temp:");
                LCDWriteSignedDecIntXY(1,13,OutAirTemp,3);
                LCD_Write_Char(0);
                LCD_Write_Char(67);
//                LCDWriteSignedDecIntXY(2,0,ADCRead(6),6);
                LCDWriteStringXY(2,0,"Enter Key Sets Time ");
                LCDWriteStringXY(3,0,"Menu Key Sets Temp ");
            }
            
            if(mainTimer > 8 && mainTimer <= 16)
            {
                LCDWriteStringXY(1,0,"OutSide Temp:");
                LCDWriteSignedDecIntXY(1,13,OutAirTemp,3);
                LCD_Write_Char(0);
                LCD_Write_Char(67);
                LCDWriteStringXY(2,0," <- / -> Keys page ");
                LCDWriteStringXY(3,0," through Loop Info ");
            }
            
            if(mainTimer > 16 && mainTimer <= 20)
            {
                LCDWriteStringXY(1,0,"Left Key to display ");
                LCDWriteStringXY(2,0," all Loop Run-time  ");
                LCDWriteStringXY(3,0,"  Info on One Page ");
            }
        }
        

        if(mainTimer > 19)
        {
            mainTimer = 0;
//            loopCounter = 12;
        }
// ******************************************************************************
        TestKey = menuRead();
// ******************************************************************************
        heartBeat();                                                            // HeartBeat displays the HeartBeat on the LCD,
// ******************************************************************************  but, also increments mainTimer every second 
     
        if(TestKey == KEY_NONE)             // If no key is pressed for 60 seconds
        {                                   // Turn OFF the LCD Backlight
            backLightTimer += 1;
        }
        
        else
        {
            backLightTimer = 0;
        }
        
        if (backLightTimer < 4450)
        {
            backLightOn = 1;
            
        }
        else
        {
            backLightTimer = 4450;
            backLightOn = 0;
        }
            
        
        if (TestKey == KEY_ENTER)
        {
//            TestKey = 9;
            SetTime();
        }

        if (TestKey == KEY_RESET_LCD)
        {
            LCD_Cmd(0x08);
            LCD_Cmd(0x00);
            LCD_Cmd(0x0C|0);      //Enable Display ON with style selected (BLINK, ULINE, BLUL, or NONE))
            LCD_Cmd(0x00);
        }

        if(TestKey == KEY_LEFT)
        {
            loopCounter -=1;
            
            if(loopCounter < 0)
            {
                loopCounter = 12;
            }
        }

        if(TestKey == KEY_RIGHT)
        {
            loopCounter +=1;
            
            if(loopCounter >12)
            {
                loopCounter = 0;
            }
        }

        if (TestKey == KEY_MENU)
        {
            signed char choice = 5;

            while(TestKey != KEY_ENTER)
            {
                TestKey = menuRead();
                
                if(timer > 1000)
                {
                    timer = 0;
                    goto Exit;                                                  //This uses less memory than TestKey = KEY_ENTER
                    
//                    TestKey = KEY_ENTER;                                      // This functions fine, but forces a write to EEProm
                }

                switch(TestKey)
                {
                    case KEY_DOWN:
                    {
                        choice -=1;
                            
                        if (choice < 0)
                        {
                            choice = 0;
                        }
                    }
                    break;

                    case KEY_LEFT:
                    {
                        choice -=1;
                            
                        if (choice < 0)
                        {
                            choice = 0;
                        }
                    }
                    break;
                        
                    case KEY_UP:
                    {
                        choice += 1;
                            
                        if(choice > 10)
                        {
                            choice = 10;
                        }
                    }
                    break;
                        
                    case KEY_RIGHT:
                    {
                        choice += 1;
                            
                        if(choice > 10)
                        {
                            choice = 10;
                        }
                    }
                    break;
                    
                    case KEY_CANCEL:
                    {
                        goto Exit;
                    }
                    break;
                        
                }

                if(timer < 2)
                {
                    LCD_Clear();
                }

                LCD_Set_Cursor(0,0);
                LCD_Write_String("Set Temperature for");
                LCD_Set_Cursor(1,0);
                LCD_Write_String(desc[choice]);
                LCDWriteStringXY(2,0,"Up/Dn Keys to change");
                LCDWriteStringXY(3,0,"Enter Key for Yes   ");
                
                heartBeat();                                                    // HeartBeat displays the HeartBeat on the LCD,
                                                                                // but, also increments mainTimer every second
                timer += 1;
            }
            
//            TestKey = 9;
            
            LCD_Clear();


            LCD_Set_Cursor(0,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(0,strlen(desc[choice]),"Set Tmp");
            eepromPutData(setpoint[choice], TempSetpoint(eepromGetData(setpoint[choice])));
            
            LCD_Set_Cursor(2,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(2,strlen(desc[choice]),"Set DB");
            eepromPutData(deadband[choice], SetDeadband(eepromGetData(deadband[choice])));            

            LCD_Clear();

            LCD_Set_Cursor(0,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(0,strlen(desc[choice]),"15 Bias");
            eepromPutData(biasNeg15[choice], SetBiasNeg15(eepromGetData(biasNeg15[choice])));

            LCD_Set_Cursor(2,0);
            LCD_Write_String(desc[choice]);
            LCDWriteStringXY(2,strlen(desc[choice]),"25 Bias");
            eepromPutData(biasNeg25[choice], SetBiasNeg25(eepromGetData(biasNeg25[choice])));
            
            Exit:
                        
            LCD_Clear();
        }
// ******************************************************************************
        ClrWdt();                                                               //Clr (Re-Set) the WatchDog Timer
    }
    return(0);
}
Esempio n. 23
0
int main()
{
	DDRC &= ~(1<<PC0);				//input for DPDT switch
	increase_day_count_eeprom();	//some more initialization of EEPROM
	
	/*** Students list ***/
	/** These are the bytes that are read from the RFID tags of the students**/
	uint8_t person_byte[MAX_PEOPLE][5] = {
		{0x23, 0x6D, 0xD6, 0x00, 0x98} , 
		{0xF9, 0x46, 0x1D, 0x00, 0xA2}, 
		{0xA3, 0x7E, 0x30, 0x02, 0xEF}
		
	};
	
	//Some tags that we used to experiment
	//{0xF9, 0x46, 0x1D, 0x00, 0xA2}
	//{0x23, 0x6D, 0xD6, 0x00, 0x98} - recognized1
	//{0xF9, 0x46, 0x1D, 0x00, 0xA2} - recognized2 (change any bit in any of these 2 to see effect of unrecognized person entering)
	//{0xA3, 0x7E, 0x30, 0x02, 0xEF} - Nimi - white
	//{0x5B, 0xA8, 0x2C, 0x00, 0xDF} - Adnan - blue
		
	// person name list .. these used to control people names etc
	int detected_person;
	int person_entry_list[MAX_PEOPLE] = {0, 0, 0};
	char *person_name[MAX_LEN] = { (char *)"Sibat", (char *)"Ripon" , (char *)"Nimi" };
	char *entered_msg = (char *)" entered";
	char *left_msg = (char *)" left";
	char msg_to_show[100];
	int person_count = 0;
	
	// iterator and byte array to use later
	uint8_t byte, i;
	uint8_t str[MAX_LEN];
	_delay_ms(50);
	
	// initialize the LCD
	LCDInit(LS_BLINK);
	LCDWriteStringXY(2,0,"RFID Reader");
	
	// spi initialization
	spi_init();
	_delay_ms(1000);
	LCDClear();
	
	//init reader
	mfrc522_init();
	
	// poll until reader is found
	while(1)
	{
		byte = mfrc522_read(VersionReg);
		if(byte == 0x92)
		{
			LCDClear();
			LCDWriteStringXY(2,0,"Detected");
			_delay_ms(1000);
			break;
		}
		else
		{
			LCDClear();
			LCDWriteStringXY(0,0,"No reader found");
			_delay_ms(800);
			LCDClear();
			LCDWriteStringXY(0, 0, "Connect reader");
			_delay_ms(1000);
		}
	}
	
	// ready to run
	_delay_ms(1500);
	LCDClear();
	
	// initializing the RGB LEDs
	DDRA = 0xFF;
	PORTA = 0xFE;
	
	
	//Interrupt INT2
	DDRB |= (1<<PB2);		// Set PB2 as input (Using for interrupt INT2)
	PORTB |= (1<<PB2);		// Enable PB2 pull-up resistor
	
	// setting up the timer codes
	// setting up the LED animation initials
	// TODO timer code ... START HERE ...
	TCCR1A = 0x00;
	TCCR1B = 0x01;
	TIMSK = 0x04;
	min = 0;
	sec = 0;
	hour = 0;
	sec_counter = 0;
	LED_animation_status = 0;
	LED_animation_on = 1;
	program_status_2_first_time = 1;
	program_status_3_first_time = 1;
	program_status_4_first_time = 1;
	
	// TODO timer code ... ENDS  HERE ...
	
	// The loop starts here
	// program status : 1 means entrance period
	//				  : 2 means session period
	//				  : 3 means session ended
	program_status = 1;
	
	//Using interrupt 2 for reading history mode
	GICR &= ~(1<<INT2);		// Disable INT2
	MCUCSR |= (1<<ISC2);	// Trigger INT2 on 1 = rising edge , 0 falling edge
	GIFR |= (1<<INTF2);	// clear INTF2
	GICR |= (1<<INT2);		// Enable INT2
	
	sei();
	while(1)
	{
		spi_init();
		mfrc522_init();
		byte = mfrc522_read(ComIEnReg);
		mfrc522_write(ComIEnReg,byte|0x20);
		byte = mfrc522_read(DivIEnReg);
		mfrc522_write(DivIEnReg,byte|0x80);
		
		if(program_status == 1)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "Show your card.");
			LCDWriteStringXY(0, 1, "#students:");
			LCDWriteIntXY(12,1, person_count ,2 );
			
			byte = mfrc522_request(PICC_REQALL,str);
			if(byte == CARD_FOUND)
			{
				byte = mfrc522_get_card_serial(str);
				if(byte == CARD_FOUND)
				{
					LED_animation_on = 0;
					
					// check for person 1
					detected_person = -1;
					for(i = 0; i < MAX_PEOPLE; i++)
					{
						for(byte = 0; byte < 5; byte++)
						{
							/***The following piece of code was written to test and read the bytes off the tags and print them***/
							//LCDClear();
							//LCDWriteIntXY(0,0,byte,2);
							//LCDWriteIntXY(0,1,str[byte],14);
							//_delay_ms(3000);
							
							if(str[byte] != person_byte[i][byte])
							{
								break;
							}
						}
						if(byte == 5)
						{
							detected_person = i;
							break;
						}
					}
					
					if(i == MAX_PEOPLE)
					{
						detected_person = -1;
					}
					
					// showing message on LCD upon decision of the person
					LCDClear();
					if(detected_person == -1)
					{
						PORTA = 0x7E;
						LCDClear();
						LCDWriteStringXY(0, 0, "Access denied!");
						_delay_ms(2000);
						PORTA = 0xFE;
						LCDClear();
						LCDWriteStringXY(0, 0, "Unrecognized!");
					}
					else
					{
						PORTA = 0xFD;
						LCDClear();
						LCDWriteStringXY(0, 0, "Access granted!");
						_delay_ms(2000);
						if(person_entry_list[i] == 0)
						{
							person_entry_list[i] = 1;
						}
						else
						{
							person_entry_list[i] = 0;
						}
						strcpy(msg_to_show, person_name[detected_person]);
						if(person_entry_list[i] == 0)
						{
							/** Code to sound buzzer when a student is leaving **/
							PORTA = 0x7D;
							_delay_ms(200);
							PORTA = 0xFD;
							// end of buzzer code
							
							//EEPROM WRITE
							if(write_enable_eeprom == 1){
								eeprom_update_byte ((uint8_t*) (&NonVolatileIsPresent[curr_day][i]), 0);
							}
							person_count--;
							strcat(msg_to_show, left_msg);
						}
						else
						{
							/** Code to sound buzzer when a student is entering **/
							PORTA = 0x7D;
							_delay_ms(200);
							PORTA = 0xFD;
							_delay_ms(100);
							PORTA = 0x7D;
							_delay_ms(200);
							PORTA = 0xFD;
							// end of buzzer code
							
							//EEPROM WRITE
							if(write_enable_eeprom == 1) {
								eeprom_write_byte ((uint8_t*) (&NonVolatileIsPresent[curr_day][i]), 1);
								eeprom_update_byte ((uint8_t*) (&NonVolatileHour[curr_day][i]), hour);
								eeprom_update_byte ((uint8_t*) (&NonVolatileMinute[curr_day][i]), min);
								eeprom_update_byte ((uint8_t*) (&NonVolatileSecond[curr_day][i]), sec);
							}
							person_count++;
							strcat(msg_to_show, entered_msg);
						}
						LCDClear();
						LCDWriteStringXY(0, 0, msg_to_show);
					}
					
					_delay_ms(3000);
					
					PORTA = 0xFE;
					LCDClear();
					LED_animation_on = 1;
				}
				else
				{
					LCDClear();
					LCDWriteStringXY(0,1,"Error");
				}
			}
			
			_delay_ms(200);
		}
		else if(program_status == 2)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "In session now!");
			LCDWriteStringXY(0, 1, "#students:");
			LCDWriteIntXY(12,1, person_count ,2 );
			
			byte = mfrc522_request(PICC_REQALL,str);
			if(byte == CARD_FOUND)
			{
				LED_animation_on = 0;
				PORTA = 0x7E;
				LCDClear();
				LCDWriteStringXY(0, 0, "Warning!!");
				_delay_ms(2000);
				LCDClear();
				LCDWriteStringXY(0, 0, "Session running.");
				_delay_ms(2000);
				PORTA = 0xFE;
				LCDClear();
				LCDWriteStringXY(0, 0, "Can't go out/in.");
				_delay_ms(2000);
				LCDClear();
				PORTA = 0xFB;
				LED_animation_on = 1;
			}
			_delay_ms(200);
		}
		else if(program_status == 3)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "Session ended!");
			LCDWriteStringXY(0, 1, "#students:");
			LCDWriteIntXY(12,1, person_count ,2 );
			
			byte = mfrc522_request(PICC_REQALL,str);
			if(byte == CARD_FOUND)
			{
				byte = mfrc522_get_card_serial(str);
				if(byte == CARD_FOUND)
				{
					LED_animation_on = 0;
					
					// check for person 1
					detected_person = -1;
					for(i = 0; i < MAX_PEOPLE; i++)
					{
						for(byte = 0; byte < 5; byte++)
						{
							if(str[byte] != person_byte[i][byte])
							{
								break;
							}
						}
						if(byte == 5)
						{
							detected_person = i;
							break;
						}
					}
					
					// showing message on LCD upon decision of the person
					if(detected_person == -1)
					{
						PORTA = 0x7E;
						LCDClear();
						LCDWriteStringXY(0, 0, "Unrecognized!");
						_delay_ms(2000);
						PORTA = 0xFE;
					}
					else
					{
						
						PORTA = 0xFB;
						if(person_entry_list[detected_person] != 0){
							LCDClear();
							strcpy(msg_to_show, "Take care ");
							strcat(msg_to_show, person_name[detected_person]);
							LCDWriteStringXY(0, 0, msg_to_show);
							_delay_ms(1000);
							LCDClear();
							strcpy(msg_to_show, person_name[detected_person]);
							strcat(msg_to_show, left_msg);
							LCDWriteStringXY(0, 0, msg_to_show);
							person_entry_list[detected_person] = 0;
							person_count--;
							//unnecessary sanity check
							if(person_count == 0)
							{
								break;
							}
						}
						
					}
					
					_delay_ms(3000);
					
					PORTA = 0xFD;
					LCDClear();
					LED_animation_on = 1;
				}
				else
				{
					PORTA = 0x7E;
					LCDClear();
					LCDWriteStringXY(0,1,"Error");
					_delay_ms(2000);
					PORTA = 0xFE;
					LCDClear();
					PORTA = 0xFD;
				}
			}
			
			_delay_ms(200);
		}
		else
		{
			/***This is the warning phase.
			When leaving period has ended, but some students were still stuck in the classroom,
			the buzzer buzzes off continuously suspecting that some students might be sick or in trouble.
			***/
			if(person_count != 0)
			{
				LED_animation_on = 0;
				PORTA = 0x7E;
				
				LCDClear();
				LCDWriteIntXY(0,0, person_count ,2 );
				LCDWriteStringXY(3, 0, "student could");
				LCDWriteStringXY(0, 1, "not get out");
				_delay_ms(1500);
				LCDClear();
				LCDWriteStringXY(0, 0, "Take caution!");
				_delay_ms(1500);
				PORTA = 0xFE;
				_delay_ms(400);
			}
			else
			{
				/**When all students have left, nothing to do. **/
				LCDClear();
				break;
			}
		}
	}
	
	LED_animation_on = 1;
	PORTA = 0xFB;
	temp_PORTA = PORTA;
	LCDClear();
	LCDWriteStringXY(0, 0, "Everyone left.");
	while(1)
	{
		;
	}
	
	cli();
	
}