fork download
  1. #include <avr/io.h>
  2. #include <avr/pgmspace.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5.  
  6. #include "lib/lcd/lcd_hd44780_avr.h"
  7. #include "lib/keypad/keypad.h"
  8.  
  9. #define MICRO_DDR DDRD
  10. #define MICRO_PORT PORTD
  11. #define MICRO_POS PD0
  12.  
  13. #define GRILL_DDR DDRD
  14. #define GRILL_PORT PORTD
  15. #define GRILL_POS PD1
  16.  
  17. #define BUZZER_DDR DDRB
  18. #define BUZZER_PORT PORTB
  19. #define BUZZER_POS PB1
  20.  
  21. #define ON 1
  22. #define OFF 0
  23.  
  24. #define TYPE_MICRO 1
  25. #define TYPE_GRILL 2
  26.  
  27. void Init();
  28. void SetMicro(uint8_t state);
  29. void SetGrill(uint8_t state);
  30.  
  31. void SetTime();
  32. void CountDown();
  33.  
  34. void SetBuzzer(uint8_t);
  35. void Beep();
  36.  
  37. volatile uint8_t m=0;
  38. volatile uint8_t s=0;
  39. volatile uint8_t timer_running=0;
  40.  
  41. uint8_t type=TYPE_MICRO;
  42.  
  43. int main(void)
  44. {
  45. Init();
  46.  
  47. while(1)
  48. {
  49. LCDClear();
  50.  
  51. LCDWriteFStringXY(3,0,PSTR("* Press *"));
  52. LCDWriteFStringXY(1,1,PSTR("Micro or Grill"));
  53.  
  54. uint8_t k=GetKeypadCmd(1);
  55.  
  56. switch(k)
  57. {
  58. case KEY_MICRO:
  59. {
  60. Beep();
  61. type=TYPE_MICRO;
  62. SetTime();
  63. timer_running=1;
  64.  
  65. if(type==TYPE_MICRO)
  66. SetMicro(ON);
  67. else
  68. SetGrill(ON);
  69.  
  70. CountDown();
  71.  
  72. break;
  73. }
  74. case KEY_GRILL:
  75. {
  76. Beep();
  77. type=TYPE_GRILL;
  78. SetTime();
  79. timer_running=1;
  80.  
  81. if(type==TYPE_MICRO)
  82. SetMicro(ON);
  83. else
  84. SetGrill(ON);
  85.  
  86. CountDown();
  87. break;
  88. }
  89. case KEY_START:
  90. {
  91. Beep();
  92. //QUICK START
  93. type=TYPE_MICRO;
  94. if(!timer_running)
  95. {
  96. s=30;
  97. timer_running=1;
  98. SetMicro(ON);
  99. CountDown();
  100. break;
  101. }
  102.  
  103. }
  104. }
  105.  
  106. }
  107. }
  108. void Beep()
  109. {
  110. SetBuzzer(ON);
  111. _delay_ms(25);
  112. SetBuzzer(OFF);
  113. _delay_ms(25);
  114. }
  115. void Init()
  116. {
  117. //Set relay io lines as out put
  118. MICRO_DDR|=(1<<MICRO_POS); //Microwave relay
  119. GRILL_DDR|=(1<<GRILL_POS); //Grill relay
  120.  
  121. //Set Buzzer line as output
  122. BUZZER_DDR|=(1<<BUZZER_POS);
  123.  
  124. //Initialize LCD Module
  125. LCDInit(LS_NONE);
  126.  
  127. //Initiallize keypad lib
  128. KeypadInit();
  129.  
  130. //Init TIMER1 for main timer operation
  131. TCCR1B=(1<<WGM12)|(1<<CS12)|(1<<CS10); //CTC Mode prescaller 1024
  132. OCR1A=976;
  133. TIMSK|=(1<<OCIE1A);
  134. }
  135. ISR(TIMER1_COMPA_vect)
  136. {
  137. if(!timer_running)
  138. return;
  139.  
  140. if(s==0)
  141. {
  142. if(m==0)
  143. {
  144. //off
  145. timer_running=0;
  146. }
  147. else
  148. {
  149. m--;
  150. s=59;
  151. }
  152. }
  153. else
  154. {
  155. s--;
  156. }
  157.  
  158. }
  159. void SetMicro(uint8_t state)
  160. {
  161. if(state==ON)
  162. {
  163. MICRO_PORT|=(1<<MICRO_POS);
  164. }
  165. else
  166. {
  167. MICRO_PORT&=~(1<<MICRO_POS);
  168. }
  169. }
  170. void SetBuzzer(uint8_t state)
  171. {
  172. if(state==ON)
  173. {
  174. BUZZER_PORT|=(1<<BUZZER_POS);
  175. }
  176. else
  177. {
  178. BUZZER_PORT&=~(1<<BUZZER_POS);
  179. }
  180. }
  181. void SetGrill(uint8_t state)
  182. {
  183. if(state==ON)
  184. {
  185. GRILL_PORT|=(1<<GRILL_POS);
  186. }
  187. else
  188. {
  189. GRILL_PORT&=~(1<<GRILL_POS);
  190. }
  191. }
  192.  
  193. void SetTime()
  194. {
  195. uint8_t min=0;
  196. uint8_t sec=0;
  197.  
  198. LCDClear();
  199.  
  200. while(1)
  201. {
  202. if(type==TYPE_MICRO)
  203. {
  204. LCDWriteFStringXY(0,0,PSTR("MICRO"));
  205. }
  206. else
  207. {
  208. LCDWriteFStringXY(0,0,PSTR("GRILL"));
  209. }
  210.  
  211. LCDWriteFStringXY(6,0,PSTR("MM:SS "));
  212.  
  213. LCDWriteIntXY(6,1,min,2);
  214. LCDWriteIntXY(9,1,sec,2);
  215.  
  216. LCDWriteFStringXY(8,1,PSTR(":"));
  217.  
  218. uint8_t key=GetKeypadCmd(1);
  219.  
  220. switch(key)
  221. {
  222. case KEY_TS:
  223. if(min<90)
  224. {
  225. Beep();
  226. sec+=10;
  227. if(sec==60)
  228. {
  229. sec=0;
  230. min++;
  231.  
  232. if(min==91)
  233. min=90;
  234. }
  235.  
  236. }
  237.  
  238. break;
  239. case KEY_M:
  240. {
  241.  
  242. if(min<90)
  243. {
  244. min++;
  245. Beep();
  246. }
  247. break;
  248. }
  249.  
  250. case KEY_TM:
  251. {
  252. if(min<90)
  253. {
  254. min+=10;
  255. if(min>=90)
  256. {
  257. min=90;
  258. sec=0;
  259. }
  260. else
  261. Beep();
  262. }
  263.  
  264. break;
  265. }
  266.  
  267. case KEY_STOP:
  268. Beep();
  269. min=0;
  270. sec=0;
  271. break;
  272.  
  273. case KEY_GRILL:
  274. Beep();
  275. type=TYPE_GRILL;
  276. break;
  277.  
  278. case KEY_MICRO:
  279. Beep();
  280. type=TYPE_MICRO;
  281. break;
  282.  
  283. case KEY_START:
  284. {
  285. if(min>0 || sec>0)
  286. {
  287. Beep();
  288. m=min;
  289. s=sec;
  290. return;
  291.  
  292. }
  293.  
  294. }
  295.  
  296. }
  297.  
  298.  
  299. }
  300. }
  301.  
  302. void CountDown()
  303. {
  304. LCDClear();
  305.  
  306. while(1)
  307. {
  308. uint8_t k=GetKeypadCmd(0);
  309.  
  310. if(k==KEY_STOP)
  311. {
  312. //Pause
  313. timer_running=0;
  314.  
  315. SetMicro(OFF);
  316. SetGrill(OFF);
  317.  
  318. LCDWriteFStringXY(7,0,PSTR("PAUSED!"));
  319. LCDWriteFStringXY(6,1,PSTR("Press Start"));
  320.  
  321. Beep();
  322.  
  323. while(1)
  324. {
  325. k=GetKeypadCmd(1);
  326.  
  327. if(k==KEY_START)
  328. {
  329. Beep();
  330. break;
  331. }
  332. }
  333.  
  334. timer_running=1;
  335.  
  336. if(type==TYPE_MICRO)
  337. SetMicro(ON);
  338. else
  339. SetGrill(ON);
  340.  
  341. LCDClear();
  342.  
  343. }
  344. else if(k==KEY_START)
  345. {
  346. if(m<=89)
  347. {
  348. if(s<30)
  349. {
  350. s+=30;
  351. if(s>=60)
  352. {
  353. m++;
  354. s=s-60;
  355.  
  356. }
  357. Beep();
  358.  
  359. }
  360.  
  361. }
  362. }
  363.  
  364. if(m==0 && s==0)
  365. {
  366. LCDClear();
  367. LCDWriteFStringXY(0,5,PSTR("Ready !"));
  368.  
  369.  
  370.  
  371. SetMicro(OFF);
  372. SetGrill(OFF);
  373.  
  374. //Alert
  375. for(uint8_t i=0;i<4;i++)
  376. {
  377. SetBuzzer(ON);
  378. _delay_ms(500);
  379. SetBuzzer(OFF);
  380. _delay_ms(500);
  381. }
  382.  
  383. GetKeypadCmd(1);
  384.  
  385. Beep();
  386.  
  387. return;
  388. }
  389. LCDWriteIntXY(0,1,m,2);
  390. if(s%2)
  391. {
  392. LCDWriteStringXY(2,1,":");
  393. }
  394. else
  395. {
  396. LCDWriteStringXY(2,1," ");
  397. }
  398.  
  399. LCDWriteIntXY(3,1,s,2);
  400.  
  401. if(type==TYPE_MICRO)
  402. {
  403. LCDWriteFStringXY(0,0,PSTR("MICRO"));
  404. }
  405. else
  406. {
  407. LCDWriteFStringXY(0,0,PSTR("GRILL"));
  408. }
  409. }
  410.  
  411. }
Success #stdin #stdout 0.03s 25864KB
stdin
Standard input is empty
stdout
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#include "lib/lcd/lcd_hd44780_avr.h"
#include "lib/keypad/keypad.h"

#define MICRO_DDR	DDRD
#define MICRO_PORT	PORTD
#define MICRO_POS	PD0

#define GRILL_DDR	DDRD
#define GRILL_PORT	PORTD
#define GRILL_POS	PD1

#define BUZZER_DDR	DDRB
#define BUZZER_PORT	PORTB
#define BUZZER_POS	PB1

#define ON	1
#define OFF 0

#define TYPE_MICRO	1
#define TYPE_GRILL	2

void Init();
void SetMicro(uint8_t state);
void SetGrill(uint8_t state);

void SetTime();
void CountDown();

void SetBuzzer(uint8_t);
void Beep();

volatile uint8_t m=0;
volatile uint8_t s=0;
volatile uint8_t timer_running=0;

uint8_t type=TYPE_MICRO;

int main(void)
{
	Init();
	
	while(1)
	{
		LCDClear();
		
		LCDWriteFStringXY(3,0,PSTR("* Press *"));
		LCDWriteFStringXY(1,1,PSTR("Micro or Grill"));
		
		uint8_t k=GetKeypadCmd(1);
		
		switch(k)
		{
			case KEY_MICRO:
			{
				Beep();
				type=TYPE_MICRO;
				SetTime();
				timer_running=1;
				
				if(type==TYPE_MICRO)
					SetMicro(ON);
				else
					SetGrill(ON);
				
				CountDown();
				
				break;
			}
			case KEY_GRILL:
			{
				Beep();
				type=TYPE_GRILL;
				SetTime();
				timer_running=1;
				
				if(type==TYPE_MICRO)
					SetMicro(ON);
				else
					SetGrill(ON);
				
				CountDown();
				break;
			}
			case KEY_START:
			{
				Beep();
				//QUICK START
				type=TYPE_MICRO;
				if(!timer_running)
				{
					s=30;
					timer_running=1;	
					SetMicro(ON);
					CountDown();
					break;
				}
				
			}
		}
		
	}
}
void Beep()
{
	SetBuzzer(ON);
	_delay_ms(25);
	SetBuzzer(OFF);
	_delay_ms(25);
}
void Init()
{
	//Set relay io lines as out put
	MICRO_DDR|=(1<<MICRO_POS);	//Microwave relay
	GRILL_DDR|=(1<<GRILL_POS);	//Grill relay
	
	//Set Buzzer line as output
	BUZZER_DDR|=(1<<BUZZER_POS);
	
	//Initialize LCD Module
	LCDInit(LS_NONE);
	
	//Initiallize keypad lib
	KeypadInit();
	
	//Init TIMER1 for main timer operation
	TCCR1B=(1<<WGM12)|(1<<CS12)|(1<<CS10); //CTC Mode prescaller 1024
	OCR1A=976;
	TIMSK|=(1<<OCIE1A);
}
ISR(TIMER1_COMPA_vect)
{
	if(!timer_running)
		return;
	
	if(s==0)
	{
		if(m==0)
		{
			//off
			timer_running=0;
		}
		else
		{
			m--;
			s=59;
		}
	}
	else
	{
		s--;
	}
	
}
void SetMicro(uint8_t state)
{
	if(state==ON)
	{
		MICRO_PORT|=(1<<MICRO_POS);
	}
	else
	{
		MICRO_PORT&=~(1<<MICRO_POS);
	}
}
void SetBuzzer(uint8_t state)
{
	if(state==ON)
	{
		BUZZER_PORT|=(1<<BUZZER_POS);
	}
	else
	{
		BUZZER_PORT&=~(1<<BUZZER_POS);
	}
}
void SetGrill(uint8_t state)
{
	if(state==ON)
	{
		GRILL_PORT|=(1<<GRILL_POS);
	}
	else
	{
		GRILL_PORT&=~(1<<GRILL_POS);
	}
}

void SetTime()
{
	uint8_t min=0;
	uint8_t sec=0;
	
	LCDClear();
	
	while(1)
	{
		if(type==TYPE_MICRO)
		{
			LCDWriteFStringXY(0,0,PSTR("MICRO"));
		}			
		else
		{
			LCDWriteFStringXY(0,0,PSTR("GRILL"));
		}			
			
		LCDWriteFStringXY(6,0,PSTR("MM:SS      "));
		
		LCDWriteIntXY(6,1,min,2);
		LCDWriteIntXY(9,1,sec,2);
		
		LCDWriteFStringXY(8,1,PSTR(":"));
		
		uint8_t key=GetKeypadCmd(1);
		
		switch(key)
		{
			case KEY_TS:
				if(min<90)
				{
					Beep();
					sec+=10;
					if(sec==60)
					{
						sec=0;
						min++;
						
						if(min==91)
						min=90;
					}
					
				}
				
				break;
			case KEY_M:
			{
				
				if(min<90)
				{
					min++;
					Beep();
				}					
				break;
			}
			
			case KEY_TM:
			{
				if(min<90)
				{
					min+=10;
					if(min>=90)
					{
						min=90;
						sec=0;
					}	
					else
						Beep();
				}
				
				break;
			}
			
			case KEY_STOP:
				Beep();
				min=0;
				sec=0;
			break;
			
			case KEY_GRILL:
				Beep();
				type=TYPE_GRILL;
				break;
			
			case KEY_MICRO:
				Beep();
				type=TYPE_MICRO;
				break;
			
			case KEY_START:
			{
				if(min>0 || sec>0)
				{
					Beep();
					m=min;
					s=sec;
					return;
					
				}
				
			}				
				
		}

		
	}
}

void CountDown()
{
	LCDClear();
	
	while(1)
	{
		uint8_t k=GetKeypadCmd(0);
		
		if(k==KEY_STOP)
		{
			//Pause
			timer_running=0;
			
			SetMicro(OFF);
			SetGrill(OFF);
			
			LCDWriteFStringXY(7,0,PSTR("PAUSED!"));
			LCDWriteFStringXY(6,1,PSTR("Press Start"));
			
			Beep();
			
			while(1)
			{
				k=GetKeypadCmd(1);
				
				if(k==KEY_START)
				{
					Beep();
					break;	
				}					
			}
			
			timer_running=1;
			
			if(type==TYPE_MICRO)
				SetMicro(ON);
			else
				SetGrill(ON);
				
			LCDClear();
		
		}
		else if(k==KEY_START)
		{
				if(m<=89)
				{
					if(s<30)
					{
						s+=30;
						if(s>=60)
						{
							m++;
							s=s-60;
							
						}
						Beep();
						
					}	
					
				}
		}
		
		if(m==0 && s==0)
		{
			LCDClear();
			LCDWriteFStringXY(0,5,PSTR("Ready !"));
			
			
			
			SetMicro(OFF);
			SetGrill(OFF);
			
			//Alert
			for(uint8_t i=0;i<4;i++)
			{
				SetBuzzer(ON);
				_delay_ms(500);
				SetBuzzer(OFF);
				_delay_ms(500);
			}
			
			GetKeypadCmd(1);
			
			Beep();
			
			return;
		}
		LCDWriteIntXY(0,1,m,2);
		if(s%2)
		{
			LCDWriteStringXY(2,1,":");
		}
		else
		{
			LCDWriteStringXY(2,1," ");
		}
		
		LCDWriteIntXY(3,1,s,2);
		
		if(type==TYPE_MICRO)
		{
			LCDWriteFStringXY(0,0,PSTR("MICRO"));
		}
		else
		{
			LCDWriteFStringXY(0,0,PSTR("GRILL"));
		}
	}
	
}