Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Button functions with reset

Status
Not open for further replies.

Jay23

Newbie level 5
Joined
Oct 14, 2013
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,469
Hi All,
I have coded a timer which counts down from 30 min, 2 hours and 5 hours and resets the counter when the time is 0.
Now I want to add some features in this counter.
1) When the counter gets the in signal EN_RelayStop the counter should reset if the counter is counting down for all 3 counters.
2) When if the counter if for example counting down 30 min the Green LED should blink every 1 sec till the counter goes to 0.
3) The START/STOP button should go to START when pressed first time and go to STOP when pressed the second time.
Appreciate all help.
//Cheers ,Gonzo


Code C++ - [expand]
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
////////////////////start code//////////////////////////////////////////////////////
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_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 TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_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;
 
sbit Relay_1 at RA0_bit;
sbit Relay_2 at RA1_bit;
sbit Relay_3 at RA2_bit;
 
//Outputs:LEDs
sbit led_RED at RC5_bit;         // led Red for Start/Stop of timers
sbit led_GREEN at RA4_bit;       // led Green for 30 mins
sbit led_YELLOW at RA5_bit;      // led Yellow for 3 hrs
sbit led_BLUE at RB0_bit;        // led Blue for 8 hrs
 
//Outputs: Buzzer
sbit Buzzer at RB1_bit; //Buzzer
 
//Inputs: Tact Switches and external signal
sbit Start_Stop_Button at RC0_bit;     // Starts or Stops Timer Select
sbit Min30_Button at RC1_bit;          // Set 30 min timer
sbit Hour2_Button at RC2_bit;          // Set 3 hours timer
sbit Hour5_Button at RA3_bit;          // Set 8 hours timer
sbit Enable_RelayStop at RC6_bit;      //  a in signal to Close all Relays
 
//sbit Off_3Relays at RA4_bit; // Shutdown all 3 relays
// Messages
char Message1[]="30min-5hr:Timer OFF";
char Message2[]="Timer OFF";
char Message3[]="Timer ON: HH:MM";
char Message4[]="Set Time: ";
char Message5[]="Time Left: ";
unsigned int i, j, v, unit= 0, ten=0, cent=0, Start_Stop=0, index=0, clear, time;
char *digit[5] = "0:00";
unsigned int HHMM_Pos[] = {6, 7, 8, 9, 10};
unsigned short Blink;
 
//300ms Delay
void Delay_300(){
    Delay_ms(300);
}
 
void debounce(){
    Delay_ms(250);
}
 
// convert minutes to ASCII hours and minutes
Display_Hours(){
unsigned int dminutes;
 
    dminutes = (unit + (ten * 10) + (100 * cent));    // put minutes into one varible
    digit[0] = (dminutes / 60) + 48;                  // hours as ascii
    dminutes %= 60;
    digit[1] = ':';
    digit[2] = (dminutes / 10) + 48;
    digit[3] = (dminutes %10) +48;
    digit[4] = 0;                                     // null terminator
    Lcd_Out(2,11,digit);
}
 
//Display digits functions
void Display_Digits(){
    digit[2]=unit+48;
    digit[1]=ten+48;
    digit[0]=cent+48;
    Lcd_Out(2,11,digit);
}
 
// Buzzer sound
void Play_Sound(){
    //Sound_Play(1000, 1000);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
    Delay_ms(1000);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
}
 
//Timer function
void start_timer(int MinVal){
unsigned  temp1, temp2, temp3;
unsigned int dminutes;
 
    Play_Sound();
    Relay_1 = 1;
    Relay_2 = 1;
    Relay_3 = 1;
    Start_Stop = 0;
    led_RED = 1;
    Lcd_Cmd(_LCD_CLEAR);           // Clear LCD
    Lcd_Out(1,1,Message2);         // "Timer OFF"
    Lcd_Out(1,1,Message3);         // "Timer ON: HH:MM"
    Lcd_Out(2,1,Message5);         // "Time Left: "
    //Lcd_Out(2,9,Message5);       // "Time Left: "
    OPTION_REG = 0x80 ;            // bit7 Enable PORTB Pull UP
    T1CON = 0x90;                  // Enable Global Interupt
    INTCON = 0x90;                 // C8 Enable Global Interupt
    T1CON = 0b00000001;
    TMR1L = 0xF0;
    TMR1H = 0xFF;
    ADCON1 = 0x07;
    for (i=0; i<MinVal; i++){
       temp1 = (MinVal-i) % 10;
       temp2 = ((MinVal-i)/10)%10;
       temp3 = ((MinVal-i)/100)%10;
       dminutes = minval-i;                       // put minutes into one varible
       Lcd_Chr(2, 12,  (dminutes / 60) + 48);     // hours as ascii
       dminutes %= 60;
       Lcd_Chr(2, 13,  ':');
    Lcd_Chr(2, 14, (dminutes / 10) + 48);
    Lcd_Chr(2, 15, (dminutes %10) +48);
       j = 1;
       do {
           Delay_ms(1000);
           j++;
       } while(((j<=60) && (Clear ==0)));
       if (Clear) {
           Delay_ms(500);
           Lcd_Out(1,1,Message2);
           INTCON = 0x00;
           goto stop;
       }
    }
stop:
    Relay_1 = 0;
    Relay_2 = 0;
    Relay_3 = 0;
    led_BLUE = 0;
    led_RED = 0;
    led_GREEN = 0;
    led_YELLOW = 0;
    unit = 0;
    ten = 0;
    cent = 0;
    clear = 1;
    Start_Stop = 1;
    Play_Sound();
    Lcd_Out(1,1,Message2);
}
 
//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
        Blink = ~Blink;
    }
}
 
void main() {
    ANSEL = 0x00;
    ANSELH = 0x00;
    CM1CON0 = 7;                     // Disable Comparators
    TRISA = 0b11001000;              // TRISA = 0b11110000;
    TRISB = 0b00000000;              // TRISB = 0b00001111;
    TRISC = 0b11001111;              // TRISC = 0b00001111;
    Relay_1 = 0;                     // RA0
    Relay_2 = 0;                     // RA1
    Relay_3 = 0;                     // RA2
    led_BLUE = 0;                    // RB0
    led_RED = 0;                     // RC5
    led_GREEN = 0;                   // RA4
    led_YELLOW = 0;                  // RA5
    Sound_Init(&PORTB,1);            // Initialize Buzzer pin #1
    Lcd_Init();                      // Initialize LCD
start:
    clear = 0;
    Lcd_Cmd(_LCD_CLEAR);                 // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);            // Cursor off
    Lcd_Out(1,1, "");
    Lcd_Out(2,1, "");
    i=0;
    while(i<3){
         debounce();
         i ++;
    }
    Lcd_Out(1,1,Message1);              // "30min-5hr:Timer OFF"
    Lcd_Out(2,1,Message4);              // "Set Time: "
    Display_Hours();                    //Display_Digits
 
    do {
        if(Min30_Button == 0){         // RC1 - 30 minute button is pressed
            Delay_300();
            cent = 0;
            ten = 0;                   //
            unit = 30;                  // 10 minutes time for test purpose
            led_GREEN = 1;
            Display_Hours();
            //if(Enable_RelayStop = 1){
            //goto stop;
            //}
        } // If !Min30_Button
       
        if(Hour2_Button == 0){          // RC2 - 2 hours(120 min) button is pressed
            Delay_300();
            cent = 1;
            ten = 2;
            unit = 0;
            led_YELLOW = 1;             // RA5
            Display_Hours();
        } // If !Hour2_Button
       
        if(Hour5_Button == 0){          // RA3 - 5 hour(300 min) button is pressed
            Delay_300();
            cent = 3;
            ten = 0;
            unit = 0;
            led_BLUE = 1;               // RB0
            Display_Hours();
        } // If !Hour5_Button
       
        if(Start_Stop_Button == 0){     // Start_Stop button is pressed
            Delay_300();
            time = cent*100 + ten*10 + unit ;  //unit
            if(time > 0) start_timer(time);
        } // If !Start_Stop_Button
        if(clear){
            goto start;
        }
    } while(1);
}
 
///////////////////////////end code/////////////////////////////////////////////////////

 
Last edited by a moderator:

HI All,
I ran into have some problems in coding in mikroC.
I have a timer if when the a certain time button is pressed in my timer the (GREEN) button for 30 min is activated.
In order to start the timer the Start/Stop button(RED) must be pressed but if I want to abort the timer like change my mind then if I pressed the Start/Stop button again it will reset the timer. i cannot get this to work.
thanks
gonzo

C:
//Inputs: Tact Switches and external signal
sbit Start_Stop_Button at RC0_bit;     // Starts or Stops Timer Select
sbit Min30_Button at RC1_bit;          // Set 30 min timer
sbit Hour2_Button at RC2_bit;          // Set 3 hours timer
sbit Hour5_Button at RA3_bit;          // Set 8 hours timer
sbit Oven_OFF at RC7_bit;              //  Close oven and all 3Relays

//sbit Off_3Relays at RA4_bit; // Shutdown all 3 relays
// Messages
char Message1[]="30min-5hr:Timer OFF";
char Message2[]="Timer OFF";
char Message3[]="Timer ON: HH:MM";
char Message4[]="Set Time: ";
char Message5[]="Time Left: ";
unsigned int i, j, v, unit= 0, ten=0, cent=0, Start_Stop=0, index=0, clear, time;
char *digit[5] = "0:00";
unsigned int HHMM_Pos[] = {6, 7, 8, 9, 10};
unsigned short Blink;

//300ms Delay
void Delay_300(){
    Delay_ms(300);
}

void debounce(){
    Delay_ms(250);
}

// convert minutes to ASCII hours and minutes
Display_Hours(){
unsigned int dminutes;

    dminutes = (unit + (ten * 10) + (100 * cent));    // put minutes into one varible
    digit[0] = (dminutes / 60) + 48;                  // hours as ascii
    dminutes %= 60;
    digit[1] = ':';
    digit[2] = (dminutes / 10) + 48;
    digit[3] = (dminutes %10) +48;
    digit[4] = 0;                                     // null terminator
    Lcd_Out(2,11,digit);
}

//Display digits functions
void Display_Digits(){
    digit[2]=unit+48;
    digit[1]=ten+48;
    digit[0]=cent+48;
    Lcd_Out(2,11,digit);
}

// Buzzer sound
void Play_Sound(){
    //Sound_Play(1000, 1000);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
    /*Delay_ms(1000);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;*/
}

//Timer function
void start_timer(int MinVal){
unsigned  temp1, temp2, temp3;
unsigned int dminutes;

    Play_Sound();
    Relay_1 = 1;
    Relay_2 = 1;
    Relay_3 = 1;
    Start_Stop = 0;
    led_RED = 1;
    Lcd_Cmd(_LCD_CLEAR);           // Clear LCD
    Lcd_Out(1,1,Message2);         // "Timer OFF"
    Lcd_Out(1,1,Message3);         // "Timer ON: HH:MM"
    Lcd_Out(2,1,Message5);         // "Time Left: "

    OPTION_REG = 0x80 ;            // bit7 Enable PORTB Pull UP
    T1CON = 0x90;                  // Enable Global Interupt
    INTCON = 0x90;                 // C8 Enable Global Interupt
    T1CON = 0b00000001;
    TMR1L = 0xF0;
    TMR1H = 0xFF;
    ADCON1 = 0x07;
    for (i=0; i<MinVal; i++){
       temp1 = (MinVal-i) % 10;
       temp2 = ((MinVal-i)/10)%10;
       temp3 = ((MinVal-i)/100)%10;
       dminutes = minval-i;                       // put minutes into one varible
       Lcd_Chr(2, 12,  (dminutes / 60) + 48);     // hours as ascii
       dminutes %= 60;
       Lcd_Chr(2, 13,  ':');
    Lcd_Chr(2, 14, (dminutes / 10) + 48);
    Lcd_Chr(2, 15, (dminutes %10) +48);
       j = 1;
       do {
           Delay_ms(1000);
           j++;
       } while(((j<=60) && (Clear ==0)));
       if (Clear) {
           Delay_ms(500);
           Lcd_Out(1,1,Message2);
           INTCON = 0x00;
           goto stop;
       }
    }
stop:
    Relay_1 = 0;
    Relay_2 = 0;
    Relay_3 = 0;
    led_BLUE = 0;
    led_RED = 0;
    led_GREEN = 0;
    led_YELLOW = 0;
    unit = 0;
    ten = 0;
    cent = 0;
    clear = 1;
    Start_Stop = 1;
    Play_Sound();
    Lcd_Out(1,1,Message2);
}

//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
        Blink = ~Blink;
    }
}

void main() {
    ANSEL = 0x00;
    ANSELH = 0x00;
    CM1CON0 = 7;                     // Disable Comparators
    TRISA = 0b11001000;              // TRISA = 0b11110000;
    TRISB = 0b00000000;              // TRISB = 0b00001111;
    TRISC = 0b11001111;              // TRISC = 0b00001111;
    Relay_1 = 0;                     // RA0
    Relay_2 = 0;                     // RA1
    Relay_3 = 0;                     // RA2
    led_BLUE = 0;                    // RB0
    led_RED = 0;                     // RC5
    led_GREEN = 0;                   // RA4
    led_YELLOW = 0;                  // RA5
    Sound_Init(&PORTB,1);            // Initialize Buzzer pin #1
    Lcd_Init();                      // Initialize LCD
start:
    clear = 0;
    Lcd_Cmd(_LCD_CLEAR);                 // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);            // Cursor off
    Lcd_Out(1,1, "");
    Lcd_Out(2,1, "");
    i=0;
    while(i<4){
         debounce();
         i ++;
    }
    Lcd_Out(1,1,Message1);              // "30min-5hr:Timer OFF"
    Lcd_Out(2,1,Message4);              // "Set Time: "
    Display_Hours();                    //Display_Digits

    do {
        if(Min30_Button == 0){         // RC1 - 30 minute button is pressed
            Delay_300();
            cent = 0;
            ten = 0;                   //
            unit = 30;                  // 10 minutes time for test purpose
            led_GREEN = 1;
            Display_Hours();
        } // If !Min30_Button
        
        if(Hour2_Button == 0){          // RC2 - 2 hours(120 min) button is pressed
            Delay_300();
            cent = 1;
            ten = 2;
            unit = 0;
            led_YELLOW = 1;             // RA5
            Display_Hours();
        } // If !Hour2_Button
        
        if(Hour5_Button == 0){          // RA3 - 5 hour(300 min) button is pressed
            Delay_300();
            cent = 3;
            ten = 0;
            unit = 0;
            led_BLUE = 1;               // RB0
            Display_Hours();
        } // If !Hour5_Button
        
        if(Start_Stop_Button == 0){     // Start_Stop button is pressed
            Delay_300();
            time = cent*100 + ten*10 + unit ;  //unit
            if(time > 0) start_timer(time);
        } // If !Start_Stop_Button
        if(clear){
            goto start;
        }
    } while(1);
}
 

Attachments

  • button_30min.txt
    5.9 KB · Views: 44
Last edited by a moderator:

Did you do a code flow chart, one of the easiest ways of finding
logic problems in coding.



Some utilities to flow chart.


Regards, Dana.
 

Personally I would have tackled the problem in a different way.

1. Use TMR1 and an ISR to establish a stable counter that doesn't block when other code runs.
2. Use the buttons to load the counter with the desired values (or add on the value of the button to the existing value)
3. Loop in the display routine. You could also move the digit multiplexing to the ISR to reduce display flicker.

It should make simpler code and more accurate timing.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top