Help with A Timer on PIC with mikroC coding

Thread Starter

jaygonzo

Joined Jan 29, 2018
24
Hi All,
I have coded a timer using mikroC compiler for a PIC microcontroller. The code is working but I want to make some improvements but I ran into some problems.
The code is quite simple: It is a timer where the user presses a button (3 buttons for 3 different time)to set off the countdown.
It counts down to 0 and sets off a buzzer. There is a 16x2 display to show the time left. Now the display display the time in minutes but I want the minutes and hours to be in this format: I just cannot get the code to show in this format. Hope there is someone who know how to fix this.
HH:MM
00:00

//regards,Jay
 

Attachments

Last edited:

hexreader

Joined Apr 16, 2011
581
Note that I am only a hobby coder. Others would write better code.

Here is my attempt:

C:
// random bit of code found on forum
// amateur code - author accepts no responsibility for anything

// PIC16F886 with 4MHz clock

//###########################################################################
//Needs to Fix:
// 1)START_STOP_Button when pressed stops the timer
// 2)Time on display in this format HH:MM
// 3)Blink on LCD display needs to work when timer is ON
// 4)led_RED is ON when Start_Stop button is pressed --Fixed
// 5) Led_RED need to Blink with 0.5 sec when ON (Start_Stop) is pressed
//###########################################################################

// 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 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(){
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
    Delay_ms(500);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
}

void led_RED_blink(){
    Start_Stop = 0;
    led_RED = 1;
    Delay_ms(2000);
    led_RED = 0;
    Delay_ms(2000);
}

//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_blink();
    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 = 0b00001111;                                                      // 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
    //Off_3Relays = 0;
    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, "Timer");
    Lcd_Out(2,1, "Timer AB");
    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;                                                         // ten = 3; =3x10 minutes time
            unit = 3;                                                        // 3 minutes time for test purpose
            led_GREEN = 1;
            Display_Hours();
            //Display_Digits();
        } // 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();
            //Display_Digits();
        } // 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();
            //Display_Digits();
        } // 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);
}
 

Thread Starter

jaygonzo

Joined Jan 29, 2018
24
Note that I am only a hobby coder. Others would write better code.

Here is my attempt:

C:
// random bit of code found on forum
// amateur code - author accepts no responsibility for anything

// PIC16F886 with 4MHz clock

//###########################################################################
//Needs to Fix:
// 1)START_STOP_Button when pressed stops the timer
// 2)Time on display in this format HH:MM
// 3)Blink on LCD display needs to work when timer is ON
// 4)led_RED is ON when Start_Stop button is pressed --Fixed
// 5) Led_RED need to Blink with 0.5 sec when ON (Start_Stop) is pressed
//###########################################################################

// 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 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(){
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
    Delay_ms(500);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
}

void led_RED_blink(){
    Start_Stop = 0;
    led_RED = 1;
    Delay_ms(2000);
    led_RED = 0;
    Delay_ms(2000);
}

//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_blink();
    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 = 0b00001111;                                                      // 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
    //Off_3Relays = 0;
    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, "Timer");
    Lcd_Out(2,1, "Timer AB");
    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;                                                         // ten = 3; =3x10 minutes time
            unit = 3;                                                        // 3 minutes time for test purpose
            led_GREEN = 1;
            Display_Hours();
            //Display_Digits();
        } // 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();
            //Display_Digits();
        } // 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();
            //Display_Digits();
        } // 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);
}
Note that I am only a hobby coder. Others would write better code.

Here is my attempt:

C:
// random bit of code found on forum
// amateur code - author accepts no responsibility for anything

// PIC16F886 with 4MHz clock

//###########################################################################
//Needs to Fix:
// 1)START_STOP_Button when pressed stops the timer
// 2)Time on display in this format HH:MM
// 3)Blink on LCD display needs to work when timer is ON
// 4)led_RED is ON when Start_Stop button is pressed --Fixed
// 5) Led_RED need to Blink with 0.5 sec when ON (Start_Stop) is pressed
//###########################################################################

// 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 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(){
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
    Delay_ms(500);
    Buzzer=1;
    Delay_ms(1000);
    Buzzer=0;
}

void led_RED_blink(){
    Start_Stop = 0;
    led_RED = 1;
    Delay_ms(2000);
    led_RED = 0;
    Delay_ms(2000);
}

//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_blink();
    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 = 0b00001111;                                                      // 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
    //Off_3Relays = 0;
    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, "Timer");
    Lcd_Out(2,1, "Timer AB");
    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;                                                         // ten = 3; =3x10 minutes time
            unit = 3;                                                        // 3 minutes time for test purpose
            led_GREEN = 1;
            Display_Hours();
            //Display_Digits();
        } // 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();
            //Display_Digits();
        } // 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();
            //Display_Digits();
        } // 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);
}
Hi HEXREADER,
Thanks a lot for your help.It is working fine.
Just another thing:
My timer works like this:
If I want to chose a timer I press a for ex GREEN Button and then press START_STOP Button.
But I I want to stop the timer I want to be able to press the START_STOP again to stop the timer.
I do not want to press the RESET Button.
Do you think I can implement on my timer?
//regards,Jay
 

hexreader

Joined Apr 16, 2011
581
I reckon this can be done.

If I get very bored, I may do this for you, but no promises. It will take a lot of time to work out how your code works.

1) Which PIC pin is the GREEN button connected to?
 
Last edited:

Thread Starter

jaygonzo

Joined Jan 29, 2018
24
I reckon this can be done.

If I get very bored, I may do this for you, but no promises. It will take a lot of time to work out how your code works.

1) Which PIC pin is the GREEN button connected to?
Hi Hexreader,
I wrote wrongly.
Correction:
The Start_Stop button is connected to the RC2 on the PIC
The 30 min button is connected to the RC1 on the PIC
The 2 hours button is connected to the RC3 on the PIC
The 5 hours button to the RC4 on the PIC.

One of the timers are activated by pressing one the these buttons and to start the timer the Start_Stop must be pressed.
If I want to stop the timer while it is in process then I press the Start_Stop button again and this should stop the timer
instead of pressing the RESET button.
Hope I have explained it clearly otherwise feel free to ask me.
//regards,Jay
 

hexreader

Joined Apr 16, 2011
581
In case you are waiting for me, I don't think I will get around to doing anything with this any time soon. Other stuff came up.

You may have to work this out for yourself.
 

Thread Starter

jaygonzo

Joined Jan 29, 2018
24
Hi All,
I have this timer I have coded partly and I need help to finish it .
I want to add some features like this:
There are 3 timers . 30 min, 2 hours and 5 hours and I want to add this to the timers.
Five minutes before the timer counts down the buzzer should should the alarm.
The buzzer(the alarm) function is already in the code.
Thanks in advance.
//Jay
 
Top