1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
| sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
// LEDs and Relays and Sound Buzzers
sbit Relay_1 at RA7_bit;
sbit led_RED at RA2_bit; //led red for 30 mins
sbit led_BLUE at RA3_bit; //led red for 30 mins
sbit led_GREEN at RA4_bit; //led green for 3 hrs
//sbit led_YELLOW at RA4_bit; //led yellow for 8 hrs
sbit SND at RB0_bit; //Buzzer
//Tact Switches
sbit StartStop at RB1_bit; // Start Stop Timer Select
sbit Min30_Button at RB2_bit; // Set unit min
sbit Hour3_Button at RB3_bit; // Set ten min
//sbit Hour8_Button at RB3_bit; // Set ten min
#define seconds *1000
#define minutes *60000
#define hours *3600000
//const long int 30min, 3hours, 8hours;
const int min30 = 30 minutes;
const int hr3 = 3 hours;
const int hr8 = 8 hours;
// Messages
char Message1[]="30min-8hr: Timer";
char Message2[]="Device ON";
char Message3[]="Device OFF";
char Message4[]="Set Time: ";
char Message5[]="Time Left: min";
char Message6[] = "HH:MM";
unsigned short i, j, unit=30, ten=0, ON_OFF=0, index=0, clear, time;
char *digit = "000";
// 300ms Delay
void Delay_300(){
Delay_ms(300);
}
//Display digits functions
void Display_Digits(){
digit[1]=unit+48; //unit
digit[0]=ten+48;
Lcd_Out(2,11,digit);
}
// Buzzer sound
void play_sound(){
Sound_Play(2500, 500);
}
void debounce(){
Delay_ms(300);
}
//Timer function
void start_timer(unsigned short MinVal){
unsigned short temp1, temp2;
play_sound();
Relay_1 = 1 ;
led_BLUE = 1;
led_RED = 1;
led_GREEN = 1;
ON_OFF = 1;
Lcd_Cmd(_LCD_CLEAR); //Clear LCD
Lcd_Out(1,1,Message2); //Show message 1 on LCD
Lcd_Out(2,1,Message5); //Show message 2 on LCD
OPTION_REG = 0x80 ; //Enable PORTB Pull UP
INTCON = 0x90; //Enable Global Interupt
for (i=0; i<MinVal; i++){
temp1 = (MinVal-i)%10 ; //10
temp2 = (MinVal-i)/10 ;
Lcd_Chr(2, 12, temp2+48);
Lcd_Chr(2, 13, temp1+48);
j=1;
do {
Delay_ms(1000);
j++;
} while(((j<=60) && (Clear ==0)));
if (Clear) {
led_RED = 0;
led_BLUE = 0;
led_GREEN = 0;
Delay_ms(500);
Lcd_Out(1,1,Message3);
INTCON = 0x00;
goto stop;
}
}
stop:
Relay_1 = 0;
led_BLUE = 0;
led_RED = 1;
led_GREEN = 0;
//led_YELLOW = 0;
ON_OFF = 0;
unit = 0; //unit
ten = 0;
clear = 1;
play_sound();
}
//Interupt
void interrupt(void){
if (INTCON.INTF == 1) // Check if INTF flag is set
{
Clear = 1;
INTCON.INTF = 0; // Clear interrupt flag before exiting ISR
}
}
void main() {
CMCON |= 7;
TRISA = 0b00110000; //TRISA = 0b11110000; // Disable Comparators
TRISB = 0b00001111; //TRISB = 0b00001111;
Relay_1 = 0;
led_BLUE = 0;
led_RED = 0;
led_GREEN = 0;
//led_YELLOW = 0;
Sound_Init(&PORTB,0); // Initialize Buzzer o/p pin
Lcd_Init(); // Initialize LCD
start:
clear = 0;
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,Message1);
Lcd_Out(2,1,Message4);
Display_Digits() ;
do {
if(!Min30_Button){ //Unit
Delay_300();
unit=30; //unit
//unit ++;
//if(unit=30) unit=0;
Display_Digits();
} // If !Unit_Button
if(!Hour3_Button){
Delay_300();
unit=199;
//ten ++;
//if(ten==10) ten=0;
Display_Digits();
} // If !Ten_Button
if(!StartStop){
Delay_300();
time = ten*500+unit ; //unit
if(time > 0) start_timer(time);
} // If !SS_Select
if(clear){
goto start;
}
} while(1);
} |