PIC controller resets when connected to 12V solenoid valve.

Thread Starter

pr123

Joined Mar 13, 2015
30
the solenoid valve is connected to a blower ,the rate of which is to be controlled.
The PORTD pins and few PORTC pins are connected to a rotary switch with 12 positions whichever position is on, that particular cycle is set and should keep running until interrupted with a pushbutton then if i want to select some other cycle i can do so by turning rotary switch knob or if i dont select any other cycle and press start button same cycle should continue.
 

Thread Starter

pr123

Joined Mar 13, 2015
30
Hello, i zipped it..kindly check and let me know where am i going wrong in code? Also in proteus simulation i am switching just a relay where as in actual circuit i have connected that relay to a solenoid valve which switches on/off according to relay
 

Attachments

jayanthd

Joined Jul 4, 2015
945
Blower power cannot be controlled using a TRIAC ?

RC0 is connected to a main button ? Main button is a tact switch or Rocker/Slide switch ?

All PORTD pins and RC4 to RC7 connect to 12 position rotart switch ?

Rotary switch should be enabled only when RC0 is low ?

When a rotary switch position is selected after pressing main switch then same cycle has to continue ?

dont select any other cycle and press start button same cycle should continue.
main button RC0 is the start button ? same cycle means previous cycle based on rotary switch position has to run until button connected to RB0 is pressed ?



Edit:

Sorry, main / start button is RC2 ?
 

Thread Starter

pr123

Joined Mar 13, 2015
30
Blower power cannot be controlled using a TRIAC ?

RC0 is connected to a main button ? Main button is a tact switch or Rocker/Slide switch ?

All PORTD pins and RC4 to RC7 connect to 12 position rotart switch ?

Rotary switch should be enabled only when RC0 is low ?

When a rotary switch position is selected after pressing main switch then same cycle has to continue ?



main button RC0 is the start button ? same cycle means previous cycle based on rotary switch position has to run until button connected to RB0 is pressed ?



Edit:

Sorry, main / start button is RC2 ?

Start button is RC2, RB0 interrupt pin is my stop button..
 

Thread Starter

pr123

Joined Mar 13, 2015
30
Does PORTC.RC3 = 1 work in mikroC PRO PIC Compiler ? I have never tried it like that. I always use PORTC.F3 or PORTC.B3.

why PORTC.RC3 should be used as PORTC.F3? yes my code works fine in mikroc without any errors it had compiled, also in proteus it worked fine.maybe im missing something else in the code. But i am not able to debug..
 

JohnInTX

Joined Jun 26, 2012
4,787
Do you have a diode across the 12v Solenoid?
The ULN2803 has internal diodes so that's OK.
If its true (as I read the original post) that the program works OK when things are quiet then fail when the solenoid is attached I would highly suspect r-m-w issues on PORTC. Seen that, been there.
Any reason to use the '877? Its kind of dog these days. Several 18F's will drop right in and eliminate r-m-w when you use the LATx registers. Take a look at the 4520.
The enhanced midrange parts (16F1xxx) are pretty good, too - at least in C. They have LATx registers for the IO so no r-m-w. They ALSO have the -ME2 debuggers which are fantastic things. 16F1787 et al drops right in.

Tying MCLR to Vdd temporarily will see if MCLR is a problem. The 10K series resistor could be an issue there.
The decoupling on the PIC is a little light.

I've seen this very IO issue many times. I won't use midrange at all any more and that's a big reason.
Good luck.
 

Thread Starter

pr123

Joined Mar 13, 2015
30
The ULN2803 has internal diodes so that's OK.
If its true (as I read the original post) that the program works OK when things are quiet then fail when the solenoid is attached I would highly suspect r-m-w issues on PORTC. Seen that, been there.
Any reason to use the '877? Its kind of dog these days. Several 18F's will drop right in and eliminate r-m-w when you use the LATx registers. Take a look at the 4520.
The enhanced midrange parts (16F1xxx) are pretty good, too - at least in C. They have LATx registers for the IO so no r-m-w. They ALSO have the -ME2 debuggers which are fantastic things. 16F1787 et al drops right in.

Tying MCLR to Vdd temporarily will see if MCLR is a problem. The 10K series resistor could be an issue there.
The decoupling on the PIC is a little light.

I've seen this very IO issue many times. I won't use midrange at all any more and that's a big reason.
Good luck.
Thank you for the input, i will certainly keep that in mind next time before selecting a pic. Since i have already made the PCB i would try to rectify this as for now.
 

jayanthd

Joined Jul 4, 2015
945
RC3 pin is connected to SPST locking switch or tact switch ?

Try this code. Use 4 MHz XT Oscillator. If you use any other Oscillator then tell me and Timer1Init() code has to be changed accordingly.

Test this first in Proteus and then in hardware but I am not sure whether this will solve the PIC resetting problem. What kind of blower is it ? Why do you use solenoid to turn on the blower and why you toggle the blower ON/OFF so rapidly ?

C:
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB5_bit;
sbit LCD_D7 at RB6_bit;

sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB6_bit;

sbit SOLENOID at RC3_bit;

#define ON 1
#define OFF 0

#define TRUE 1
#define FALSE 0

#define INPUT 1
#define OUTPUT 0

#define SET 1
#define CLEAR 0

char myFlags = 0;
char msg[20];
unsigned int onTimeInMilliSec = 0, offTimeInMilliSec = 0, milliSecCounter = 0;

const char msg1[] = "Select BPM rate";
const char msg2[] = "8 BPM";
const char msg3[] = "10 BPM";
const char msg4[] = "12 BPM";
const char msg5[] = "14 BPM";
const char msg6[] = "16 BPM";
const char msg7[] = "18 BPM";
const char msg8[] = "20 BPM";
const char msg9[] = "24 BPM";
const char msg10[] = "28 BPM";
const char msg11[] = "32 BPM";
const char msg12[] = "36 BPM";
const char msg13[] = "40 BPM";
const char msg14[] = "Stopped";

sbit runFlag at myFlags.B0;
sbit resetFlag at myFlags.B1;
sbit solenoidControlFlag at myFlags.B2;
sbit toggleOnTimeOffTime at myFlags.B3;
sbit runOnceFlag at myFlags.B4;

//Timer1
//Prescaler 1:1; TMR1 Preload = 64536; Actual Interrupt Time : 1 ms
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0xFC;
    TMR1L = 0x18;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void interrupt() {
    if(INTF_bit) {
        PORTC.F3 = 0;
        runFlag = CLEAR;
        resetFlag = TRUE;
        TMR1ON_bit = 0;
        INTF_bit = CLEAR;
    }
  
    if(TMR1IF_bit) {
        TMR1H = 0xFC;
        TMR1L = 0x18;
      
        if(runFlag) {
            ++milliSecCounter;
            if(toggleOnTimeOffTime) {
                if(milliSecCounter == onTimeInMilliSec) {
                        SOLENOID = OFF;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 0;
                }
            }
            else if(!toggleOnTimeOffTime) {
                if(milliSecCounter == offTimeInMilliSec) {
                        SOLENOID = ON;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 1;
                }
            }
        }
  
        TMR1IF_bit = 0;
    }
}

//copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;
  
    d = dest;
    for(;*dest++ = *src++;);

    return d;
}

void main() {

    CMCON = 0x07;
    CVRCON = 0x00;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x01;
    TRISC = 0xF4;
    TRISD = 0xFF;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    LCD_Init();
    Delay_ms(200);
    LCD_cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));

    INTEDG_bit = 1;
  
    runOnceFlag = 1;
  
    while(1) {
  
          if(!PORTC.F2) {
              Delay_ms(50);
              while(!PORTC.F2);
            
                  runOnceFlag = TRUE;
            
                  if(!PORTD.F0) {
                      Delay_ms(50);
                      if(!PORTD.F0) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg2));
                                runFlag = TRUE;
                                onTimeInMilliSec = 2500;
                                offTimeInMilliSec = 5000;
                            }
                      }
                  }
                  else if(!PORTD.F1) {
                      Delay_ms(50);
                      if(!PORTD.F1) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg3));
                                runFlag = TRUE;
                                onTimeInMilliSec = 2000;
                                offTimeInMilliSec = 4000;
                            }
                      }
                  }
                  else if(!PORTD.F2) {
                      Delay_ms(50);
                      if(!PORTD.F2) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg4));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1670;
                                offTimeInMilliSec = 3330;
                            }
                      }
                  }
                  else if(!PORTD.F3) {
                      Delay_ms(50);
                      if(!PORTD.F3) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg5));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1430;
                                offTimeInMilliSec = 2850;
                            }
                      }
                  }
                  else if(!PORTC.F4) {
                      Delay_ms(50);
                      if(!PORTC.F4) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg6));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1250;
                                offTimeInMilliSec = 2500;
                            }
                      }
                  }
                  else if(!PORTC.F5) {
                      Delay_ms(50);
                      if(!PORTC.F5) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg7));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1110;
                                offTimeInMilliSec = 2220;
                            }
                      }
                  }
                  else if(!PORTC.F6) {
                      Delay_ms(50);
                      if(!PORTC.F6) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg8));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1000;
                                offTimeInMilliSec = 2000;
                            }
                      }
                  }
                  else if(!PORTC.F7) {
                      Delay_ms(50);
                      if(!PORTC.F7) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg9));
                                runFlag = TRUE;
                                onTimeInMilliSec = 830;
                                offTimeInMilliSec = 1660;
                            }
                      }
                  }
                  else if(!PORTD.F4) {
                      Delay_ms(50);
                      if(!PORTD.F4) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg10));
                                runFlag = TRUE;
                                onTimeInMilliSec = 720;
                                offTimeInMilliSec = 1440;
                            }
                      }
                  }
                  else if(!PORTD.F5) {
                      Delay_ms(50);
                      if(!PORTD.F5) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg11));
                                runFlag = TRUE;
                                onTimeInMilliSec = 630;
                                offTimeInMilliSec = 1260;
                            }
                      }
                  }
                  else if(!PORTD.F6) {
                      Delay_ms(50);
                      if(!PORTD.F6) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg12));
                                runFlag = TRUE;
                                onTimeInMilliSec = 560;
                                offTimeInMilliSec = 1120;                                                                                           ;
                            }
                      }
                  }
                  else if(!PORTD.F7) {
                      Delay_ms(50);
                      if(!PORTD.F7) {
                            if(runOnceFlag) {
                                LCD_Cmd(_LCD_CLEAR);
                                LCD_Out(1,1,CopyConst2Ram(msg, msg13));
                                runFlag = TRUE;
                                onTimeInMilliSec = 500;
                                offTimeInMilliSec = 1000;
                            }
                      }
                  }
                
                  if(runOnceFlag) {
                      toggleOnTimeOffTime = 1;
                      SOLENOID = ON;
                      InitTimer1();
                      runOnceFlag = CLEAR;
                  }
          }
        
          if(resetFlag == TRUE) {
                  LCD_Cmd(_LCD_CLEAR);
                  LCD_Out(1,1,CopyConst2Ram(msg, msg14));
                  resetFlag = CLEAR;
          }
    }
}
 

JohnInTX

Joined Jun 26, 2012
4,787
Thank you for the input, i will certainly keep that in mind next time before selecting a pic. Since i have already made the PCB i would try to rectify this as for now.
OK.. do note that the chips I called out drop into the same socket. MikroC has pretty good support for both families.

If you think the problem fits r-m-w i.e. single bit changes on a port cause OTHER bits on the port to change, especially in noisy environments, you can try shadowing the port i.e. keep a local image of the port value, modify that then write the image to the port as a byte. Avoid writes to a shadowed port in an interrupt routine unless you disable interrupts when you are doing IO. Its really the only way to ensure that your midrange IO will be stable.

Good luck.
EDIT: @jayanthd shows something more along how I would do it also - a single routine that handles the relay with the on/off times, start and stop incorporated.
One thing to consider is that using the ext INT for a stop button can be problematic since it is fast and potentially noise sensitive..
 

Thread Starter

pr123

Joined Mar 13, 2015
30
Thanks i will check this, here are the answers:

The button is push button tact, for start and interrupt
The PORTD and few PORTC pins are connnected to a rotary switch which once connected pulls corresponding portd pin to zero.
Blower is a fan which gives a constant flow the project i am doing requires this to be controlled by switch valve(normally closed) on/off(open) this will control flow of air and will help in resuscitation.

I hope i am able to explain the concept now
 

jayanthd

Joined Jul 4, 2015
945
University project ? How is solenoid powered ? solenoid gets regulated 12V or unregulated 12V ? If regulated what regulator are you using and what is its max current rating ?

A slightly modified code. Just removed the multiple LCD_Cmd(_LCD_CLEAR); calls.

I prefer PIC18F46K22 for my projects.

C:
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB5_bit;
sbit LCD_D7 at RB6_bit;

sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB6_bit;

sbit SOLENOID at RC3_bit;

#define ON 1
#define OFF 0

#define TRUE 1
#define FALSE 0

#define INPUT 1
#define OUTPUT 0

#define SET 1
#define CLEAR 0

char myFlags = 0;
char msg[20];
unsigned int onTimeInMilliSec = 0, offTimeInMilliSec = 0, milliSecCounter = 0;

const char msg1[] = "Select BPM rate";
const char msg2[] = "8 BPM";
const char msg3[] = "10 BPM";
const char msg4[] = "12 BPM";
const char msg5[] = "14 BPM";
const char msg6[] = "16 BPM";
const char msg7[] = "18 BPM";
const char msg8[] = "20 BPM";
const char msg9[] = "24 BPM";
const char msg10[] = "28 BPM";
const char msg11[] = "32 BPM";
const char msg12[] = "36 BPM";
const char msg13[] = "40 BPM";
const char msg14[] = "Stopped";

sbit runFlag at myFlags.B0;
sbit resetFlag at myFlags.B1;
sbit solenoidControlFlag at myFlags.B2;
sbit toggleOnTimeOffTime at myFlags.B3;
sbit runOnceFlag at myFlags.B4;

//Timer1
//Prescaler 1:1; TMR1 Preload = 64536; Actual Interrupt Time : 1 ms
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0xFC;
    TMR1L = 0x18;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void interrupt() {
    if(INTF_bit) {
        PORTC.F3 = 0;
        runFlag = CLEAR;
        resetFlag = TRUE;
        TMR1ON_bit = 0;
        INTF_bit = CLEAR;
    }

    if(TMR1IF_bit) {
        TMR1H = 0xFC;
        TMR1L = 0x18;
    
        if(runFlag) {
            ++milliSecCounter;
            if(toggleOnTimeOffTime) {
                if(milliSecCounter == onTimeInMilliSec) {
                        SOLENOID = OFF;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 0;
                }
            }
            else if(!toggleOnTimeOffTime) {
                if(milliSecCounter == offTimeInMilliSec) {
                        SOLENOID = ON;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 1;
                }
            }
        }

        TMR1IF_bit = 0;
    }
}

//copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;
    for(;*dest++ = *src++;);

    return d;
}

void main() {

    CMCON = 0x07;
    CVRCON = 0x00;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x01;
    TRISC = 0xF4;
    TRISD = 0xFF;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    LCD_Init();
    Delay_ms(200);
    LCD_cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));

    INTEDG_bit = 1;

    runOnceFlag = 1;

    while(1) {

          if(!PORTC.F2) {
              Delay_ms(50);
              while(!PORTC.F2);
          
                  runOnceFlag = TRUE;
                  LCD_Cmd(_LCD_CLEAR);
              
                  if(!PORTD.F0) {
                      Delay_ms(50);
                      if(!PORTD.F0) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg2));
                                runFlag = TRUE;
                                onTimeInMilliSec = 2500;
                                offTimeInMilliSec = 5000;
                            }
                      }
                  }
                  else if(!PORTD.F1) {
                      Delay_ms(50);
                      if(!PORTD.F1) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg3));
                                runFlag = TRUE;
                                onTimeInMilliSec = 2000;
                                offTimeInMilliSec = 4000;
                            }
                      }
                  }
                  else if(!PORTD.F2) {
                      Delay_ms(50);
                      if(!PORTD.F2) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg4));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1670;
                                offTimeInMilliSec = 3330;
                            }
                      }
                  }
                  else if(!PORTD.F3) {
                      Delay_ms(50);
                      if(!PORTD.F3) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg5));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1430;
                                offTimeInMilliSec = 2850;
                            }
                      }
                  }
                  else if(!PORTC.F4) {
                      Delay_ms(50);
                      if(!PORTC.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg6));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1250;
                                offTimeInMilliSec = 2500;
                            }
                      }
                  }
                  else if(!PORTC.F5) {
                      Delay_ms(50);
                      if(!PORTC.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg7));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1110;
                                offTimeInMilliSec = 2220;
                            }
                      }
                  }
                  else if(!PORTC.F6) {
                      Delay_ms(50);
                      if(!PORTC.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg8));
                                runFlag = TRUE;
                                onTimeInMilliSec = 1000;
                                offTimeInMilliSec = 2000;
                            }
                      }
                  }
                  else if(!PORTC.F7) {
                      Delay_ms(50);
                      if(!PORTC.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg9));
                                runFlag = TRUE;
                                onTimeInMilliSec = 830;
                                offTimeInMilliSec = 1660;
                            }
                      }
                  }
                  else if(!PORTD.F4) {
                      Delay_ms(50);
                      if(!PORTD.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg10));
                                runFlag = TRUE;
                                onTimeInMilliSec = 720;
                                offTimeInMilliSec = 1440;
                            }
                      }
                  }
                  else if(!PORTD.F5) {
                      Delay_ms(50);
                      if(!PORTD.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg11));
                                runFlag = TRUE;
                                onTimeInMilliSec = 630;
                                offTimeInMilliSec = 1260;
                            }
                      }
                  }
                  else if(!PORTD.F6) {
                      Delay_ms(50);
                      if(!PORTD.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg12));
                                runFlag = TRUE;
                                onTimeInMilliSec = 560;
                                offTimeInMilliSec = 1120;                                                                                           ;
                            }
                      }
                  }
                  else if(!PORTD.F7) {
                      Delay_ms(50);
                      if(!PORTD.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg13));
                                runFlag = TRUE;
                                onTimeInMilliSec = 500;
                                offTimeInMilliSec = 1000;
                            }
                      }
                  }
              
                  if(runOnceFlag) {
                      toggleOnTimeOffTime = 1;
                      SOLENOID = ON;
                      InitTimer1();
                      runOnceFlag = CLEAR;
                  }
          }
      
          if(resetFlag == TRUE) {
                  LCD_Cmd(_LCD_CLEAR);
                  LCD_Out(1,1,CopyConst2Ram(msg, msg14));
                  resetFlag = CLEAR;
          }
    }
}
 
Last edited:

Thread Starter

pr123

Joined Mar 13, 2015
30
Thank you jayanthd, the code works fine here on proteus. I will check the same on the circuit tomorrow, before that i will understand the changes you made in the code. Is 4 MHz crystal necessary? i am using 20MHz is that ok?
 

jayanthd

Joined Jul 4, 2015
945
Wait a minute. I will make changes for 20 MHz crystal and post the code.

Edit:

Here is the code for 20 MHz HS crystal. In Project>Edit Project... dialog box set for HS and 20 MHz and compile. mikroC PRO PIC complete project is attached.

C:
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB5_bit;
sbit LCD_D7 at RB6_bit;

sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB6_bit;

sbit SOLENOID at RC3_bit;

#define ON 1
#define OFF 0

#define TRUE 1
#define FALSE 0

#define INPUT 1
#define OUTPUT 0

#define SET 1
#define CLEAR 0

char myFlags = 0;
char msg[20];
unsigned int onTimeInMilliSec = 0, offTimeInMilliSec = 0, milliSecCounter = 0;

const char msg1[] = "Select BPM rate";
const char msg2[] = "8 BPM";
const char msg3[] = "10 BPM";
const char msg4[] = "12 BPM";
const char msg5[] = "14 BPM";
const char msg6[] = "16 BPM";
const char msg7[] = "18 BPM";
const char msg8[] = "20 BPM";
const char msg9[] = "24 BPM";
const char msg10[] = "28 BPM";
const char msg11[] = "32 BPM";
const char msg12[] = "36 BPM";
const char msg13[] = "40 BPM";
const char msg14[] = "Stopped";

sbit runFlag at myFlags.B0;
sbit resetFlag at myFlags.B1;
sbit solenoidControlFlag at myFlags.B2;
sbit toggleOnTimeOffTime at myFlags.B3;
sbit runOnceFlag at myFlags.B4;

//Timer1
//Prescaler 1:1; TMR1 Preload = 15536; Actual Interrupt Time : 10 ms

//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0x3C;
    TMR1L = 0xB0;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void interrupt() {
    if(INTF_bit) {
        PORTC.F3 = 0;
        runFlag = CLEAR;
        resetFlag = TRUE;
        TMR1ON_bit = 0;
        INTF_bit = CLEAR;
    }

    if(TMR1IF_bit) {
        TMR1H = 0xFC;
        TMR1L = 0x18;
    
        if(runFlag) {
            ++milliSecCounter;
            if(toggleOnTimeOffTime) {
                if(milliSecCounter == onTimeInMilliSec) {
                        SOLENOID = OFF;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 0;
                }
            }
            else if(!toggleOnTimeOffTime) {
                if(milliSecCounter == offTimeInMilliSec) {
                        SOLENOID = ON;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 1;
                }
            }
        }

        TMR1IF_bit = 0;
    }
}

//copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;
    for(;*dest++ = *src++;);

    return d;
}

void main() {

    CMCON = 0x07;
    CVRCON = 0x00;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x01;
    TRISC = 0xF4;
    TRISD = 0xFF;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    LCD_Init();
    Delay_ms(200);
    LCD_cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));

    INTEDG_bit = 1;

    runOnceFlag = 1;

    while(1) {

          if(!PORTC.F2) {
              Delay_ms(50);
              while(!PORTC.F2);
          
                  runOnceFlag = TRUE;
                  LCD_Cmd(_LCD_CLEAR);
              
                  if(!PORTD.F0) {
                      Delay_ms(50);
                      if(!PORTD.F0) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg2));
                                runFlag = TRUE;
                                onTimeInMilliSec = 250;
                                offTimeInMilliSec = 500;
                            }
                      }
                  }
                  else if(!PORTD.F1) {
                      Delay_ms(50);
                      if(!PORTD.F1) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg3));
                                runFlag = TRUE;
                                onTimeInMilliSec = 200;
                                offTimeInMilliSec = 400;
                            }
                      }
                  }
                  else if(!PORTD.F2) {
                      Delay_ms(50);
                      if(!PORTD.F2) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg4));
                                runFlag = TRUE;
                                onTimeInMilliSec = 167;
                                offTimeInMilliSec = 333;
                            }
                      }
                  }
                  else if(!PORTD.F3) {
                      Delay_ms(50);
                      if(!PORTD.F3) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg5));
                                runFlag = TRUE;
                                onTimeInMilliSec = 143;
                                offTimeInMilliSec = 285;
                            }
                      }
                  }
                  else if(!PORTC.F4) {
                      Delay_ms(50);
                      if(!PORTC.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg6));
                                runFlag = TRUE;
                                onTimeInMilliSec = 125;
                                offTimeInMilliSec = 250;
                            }
                      }
                  }
                  else if(!PORTC.F5) {
                      Delay_ms(50);
                      if(!PORTC.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg7));
                                runFlag = TRUE;
                                onTimeInMilliSec = 111;
                                offTimeInMilliSec = 222;
                            }
                      }
                  }
                  else if(!PORTC.F6) {
                      Delay_ms(50);
                      if(!PORTC.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg8));
                                runFlag = TRUE;
                                onTimeInMilliSec = 100;
                                offTimeInMilliSec = 200;
                            }
                      }
                  }
                  else if(!PORTC.F7) {
                      Delay_ms(50);
                      if(!PORTC.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg9));
                                runFlag = TRUE;
                                onTimeInMilliSec = 83;
                                offTimeInMilliSec = 166;
                            }
                      }
                  }
                  else if(!PORTD.F4) {
                      Delay_ms(50);
                      if(!PORTD.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg10));
                                runFlag = TRUE;
                                onTimeInMilliSec = 72;
                                offTimeInMilliSec = 144;
                            }
                      }
                  }
                  else if(!PORTD.F5) {
                      Delay_ms(50);
                      if(!PORTD.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg11));
                                runFlag = TRUE;
                                onTimeInMilliSec = 63;
                                offTimeInMilliSec = 126;
                            }
                      }
                  }
                  else if(!PORTD.F6) {
                      Delay_ms(50);
                      if(!PORTD.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg12));
                                runFlag = TRUE;
                                onTimeInMilliSec = 56;
                                offTimeInMilliSec = 112;                                                                                           ;
                            }
                      }
                  }
                  else if(!PORTD.F7) {
                      Delay_ms(50);
                      if(!PORTD.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg13));
                                runFlag = TRUE;
                                onTimeInMilliSec = 50;
                                offTimeInMilliSec = 100;
                            }
                      }
                  }
              
                  if(runOnceFlag) {
                      toggleOnTimeOffTime = 1;
                      SOLENOID = ON;
                      InitTimer1();
                      runOnceFlag = CLEAR;
                  }
          }
      
          if(resetFlag == TRUE) {
                  LCD_Cmd(_LCD_CLEAR);
                  LCD_Out(1,1,CopyConst2Ram(msg, msg14));
                  resetFlag = CLEAR;
          }
    }
}
 

Attachments

MikeML

Joined Oct 2, 2009
5,444
Likely a ground loop. Temporarily, try running the solenoid on its own power supply (isolated from the supply that runs the timer) such that there are only two wires going from the solenoid to the relay's NO contacts. Goal is not to have the solenoid current flowing in any of the the PCB traces. If that fixes it, tell me and I have some ideas about how to wire the solenoid to the existing 12V supply.

Do you have a diode on the solenoid as well as the relay that drives it?
 

jayanthd

Joined Jul 4, 2015
945
Sorry, there was a serious mistake. Had forgotten to enable INTE_bit when any function is selected. I assume that you don't change rotary position and change function when the blower is operating. If you want to change the rotary position then you have to set the rotary switch to new position and then stop the current function and then press the start button.

You put a switch by removing one LED out of 12 LEDs used to show which function is selected. You need 12 LEDs for that. Shall I make changes to the code to use one button (RB0/INT0) to start and stop the system ? If that change is made then you can have 12 LEDs to show the status of selected function.

New code.

C:
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB5_bit;
sbit LCD_D7 at RB6_bit;

sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB6_bit;

sbit SOLENOID at RC3_bit;

#define ON 1
#define OFF 0

#define TRUE 1
#define FALSE 0

#define INPUT 1
#define OUTPUT 0

#define SET 1
#define CLEAR 0

char myFlags = 0;
char msg[20];
unsigned int onTimeInMilliSec = 0, offTimeInMilliSec = 0, milliSecCounter = 0;

const char msg1[] = "Select BPM rate";
const char msg2[] = "8 BPM";
const char msg3[] = "10 BPM";
const char msg4[] = "12 BPM";
const char msg5[] = "14 BPM";
const char msg6[] = "16 BPM";
const char msg7[] = "18 BPM";
const char msg8[] = "20 BPM";
const char msg9[] = "24 BPM";
const char msg10[] = "28 BPM";
const char msg11[] = "32 BPM";
const char msg12[] = "36 BPM";
const char msg13[] = "40 BPM";
const char msg14[] = "Stopped";

sbit runFlag at myFlags.B0;
sbit resetFlag at myFlags.B1;
sbit solenoidControlFlag at myFlags.B2;
sbit toggleOnTimeOffTime at myFlags.B3;
sbit runOnceFlag at myFlags.B4;

//Timer1
//Prescaler 1:1; TMR1 Preload = 15536; Actual Interrupt Time : 10 ms

//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0x3C;
    TMR1L = 0xB0;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void interrupt() {
    if(INTF_bit) {
        PORTC.F3 = 0;
        runFlag = CLEAR;
        resetFlag = TRUE;
        TMR1ON_bit = 0;
        INTE_bit = 0;
        INTF_bit = CLEAR;
    }
  
    if(TMR1IF_bit) {
        TMR1H = 0xFC;
        TMR1L = 0x18;
      
        if(runFlag) {
            ++milliSecCounter;
            if(toggleOnTimeOffTime) {
                if(milliSecCounter == onTimeInMilliSec) {
                        SOLENOID = OFF;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 0;
                }
            }
            else if(!toggleOnTimeOffTime) {
                if(milliSecCounter == offTimeInMilliSec) {
                        SOLENOID = ON;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 1;
                }
            }
        }
  
        TMR1IF_bit = 0;
    }
}

//copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;
  
    d = dest;
    for(;*dest++ = *src++;);

    return d;
}

void main() {

    CMCON = 0x07;
    CVRCON = 0x00;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x01;
    TRISC = 0xF4;
    TRISD = 0xFF;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    LCD_Init();
    Delay_ms(200);
    LCD_cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));

    INTEDG_bit = 1;
  
    runOnceFlag = 1;
  
    while(1) {
  
          if(!PORTC.F2) {
              Delay_ms(50);
              while(!PORTC.F2);
            
                  runOnceFlag = TRUE;
                  LCD_Cmd(_LCD_CLEAR);
                
                  if(!PORTD.F0) {
                      Delay_ms(50);
                      if(!PORTD.F0) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg2));
                                runFlag = TRUE;
                                onTimeInMilliSec = 250;
                                offTimeInMilliSec = 500;
                            }
                      }
                  }
                  else if(!PORTD.F1) {
                      Delay_ms(50);
                      if(!PORTD.F1) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg3));
                                runFlag = TRUE;
                                onTimeInMilliSec = 200;
                                offTimeInMilliSec = 400;
                            }
                      }
                  }
                  else if(!PORTD.F2) {
                      Delay_ms(50);
                      if(!PORTD.F2) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg4));
                                runFlag = TRUE;
                                onTimeInMilliSec = 167;
                                offTimeInMilliSec = 333;
                            }
                      }
                  }
                  else if(!PORTD.F3) {
                      Delay_ms(50);
                      if(!PORTD.F3) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg5));
                                runFlag = TRUE;
                                onTimeInMilliSec = 143;
                                offTimeInMilliSec = 285;
                            }
                      }
                  }
                  else if(!PORTC.F4) {
                      Delay_ms(50);
                      if(!PORTC.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg6));
                                runFlag = TRUE;
                                onTimeInMilliSec = 125;
                                offTimeInMilliSec = 250;
                            }
                      }
                  }
                  else if(!PORTC.F5) {
                      Delay_ms(50);
                      if(!PORTC.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg7));
                                runFlag = TRUE;
                                onTimeInMilliSec = 111;
                                offTimeInMilliSec = 222;
                            }
                      }
                  }
                  else if(!PORTC.F6) {
                      Delay_ms(50);
                      if(!PORTC.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg8));
                                runFlag = TRUE;
                                onTimeInMilliSec = 100;
                                offTimeInMilliSec = 200;
                            }
                      }
                  }
                  else if(!PORTC.F7) {
                      Delay_ms(50);
                      if(!PORTC.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg9));
                                runFlag = TRUE;
                                onTimeInMilliSec = 83;
                                offTimeInMilliSec = 166;
                            }
                      }
                  }
                  else if(!PORTD.F4) {
                      Delay_ms(50);
                      if(!PORTD.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg10));
                                runFlag = TRUE;
                                onTimeInMilliSec = 72;
                                offTimeInMilliSec = 144;
                            }
                      }
                  }
                  else if(!PORTD.F5) {
                      Delay_ms(50);
                      if(!PORTD.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg11));
                                runFlag = TRUE;
                                onTimeInMilliSec = 63;
                                offTimeInMilliSec = 126;
                            }
                      }
                  }
                  else if(!PORTD.F6) {
                      Delay_ms(50);
                      if(!PORTD.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg12));
                                runFlag = TRUE;
                                onTimeInMilliSec = 56;
                                offTimeInMilliSec = 112;                                                                                           ;
                            }
                      }
                  }
                  else if(!PORTD.F7) {
                      Delay_ms(50);
                      if(!PORTD.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg13));
                                runFlag = TRUE;
                                onTimeInMilliSec = 50;
                                offTimeInMilliSec = 100;
                            }
                      }
                  }
                
                  if(runOnceFlag) {
                      toggleOnTimeOffTime = 1;
                      SOLENOID = ON;
                      INTF_bit = 0;
                      INTE_bit = 1;
                      InitTimer1();
                      runOnceFlag = CLEAR;
                  }
          }
        
          if(resetFlag == TRUE) {
                  LCD_Cmd(_LCD_CLEAR);
                  LCD_Out(1,1,CopyConst2Ram(msg, msg14));
                  resetFlag = CLEAR;
          }
    }
}
 

jayanthd

Joined Jul 4, 2015
945
A slightly different version of the code. I removed RC2 button and made RB0 button as start/stop button. Now there are 12 LEDs. I have included code to turn ON LEDs based on rotary position. When stop button is pressed LEDs turn OFF.

C:
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB5_bit;
sbit LCD_D7 at RB6_bit;

sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB6_bit;

sbit SOLENOID at RC3_bit;

sbit LED1 at RA0_bit;
sbit LED2 at RA1_bit;
sbit LED3 at RA2_bit;
sbit LED4 at RA3_bit;
sbit LED5 at RB7_bit;
sbit LED6 at RA5_bit;
sbit LED7 at RE0_bit;
sbit LED8 at RE1_bit;
sbit LED9 at RE2_bit;
sbit LED10 at RC0_bit;
sbit LED11 at RC1_bit;
sbit LED12 at RC2_bit;

#define ON 1
#define OFF 0

#define TRUE 1
#define FALSE 0

#define INPUT 1
#define OUTPUT 0

#define SET 1
#define CLEAR 0

#define ENABLE 1
#define DISABLE 0

char myFlags = 0, startStopCounter = 0;
char msg[20];
unsigned int onTimeInMilliSec = 0, offTimeInMilliSec = 0, milliSecCounter = 0;

const char msg1[] = "Select BPM rate";
const char msg2[] = "8 BPM";
const char msg3[] = "10 BPM";
const char msg4[] = "12 BPM";
const char msg5[] = "14 BPM";
const char msg6[] = "16 BPM";
const char msg7[] = "18 BPM";
const char msg8[] = "20 BPM";
const char msg9[] = "24 BPM";
const char msg10[] = "28 BPM";
const char msg11[] = "32 BPM";
const char msg12[] = "36 BPM";
const char msg13[] = "40 BPM";
const char msg14[] = "Stopped";

sbit runFlag at myFlags.B0;
sbit resetFlag at myFlags.B1;
sbit solenoidControlFlag at myFlags.B2;
sbit toggleOnTimeOffTime at myFlags.B3;
sbit runOnceFlag at myFlags.B4;

//Timer1
//Prescaler 1:1; TMR1 Preload = 15536; Actual Interrupt Time : 10 ms

//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0x3C;
    TMR1L = 0xB0;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void interrupt() {
    if(INTF_bit) {
        startStopCounter++;
        INTE_bit = ENABLE;
        INTF_bit = CLEAR;
        if(startStopCounter == 3) {
              resetFlag = TRUE;
        }
    }
   
    if(TMR1IF_bit) {
        TMR1H = 0xFC;
        TMR1L = 0x18;
       
        if(runFlag) {
            ++milliSecCounter;
            if(toggleOnTimeOffTime) {
                if(milliSecCounter == onTimeInMilliSec) {
                        SOLENOID = OFF;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 0;
                }
            }
            else if(!toggleOnTimeOffTime) {
                if(milliSecCounter == offTimeInMilliSec) {
                        SOLENOID = ON;
                        milliSecCounter = 0;
                        toggleOnTimeOffTime = 1;
                }
            }
        }
   
        TMR1IF_bit = 0;
    }
}

//copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;
   
    d = dest;
    for(;*dest++ = *src++;);

    return d;
}

void TurnOffLeds() {
    LED1 = OFF;
    LED2 = OFF;
    LED3 = OFF;
    LED4 = OFF;
    LED5 = OFF;
    LED6 = OFF;
    LED7 = OFF;
    LED8 = OFF;
    LED9 = OFF;
    LED10 = OFF;
    LED11 = OFF;
    LED12 = OFF;
}

void main() {

    CMCON = 0x07;
    CVRCON = 0x00;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x01;
    TRISC = 0xF0;
    TRISD = 0xFF;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    LCD_Init();
    Delay_ms(200);
    LCD_cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    LCD_Out(1,1,CopyConst2Ram(msg, msg1));

    INTEDG_bit = 1;
   
    runOnceFlag = 1;
   
    INTEDG_bit = 1;
    INTE_bit = 1;
    INTF_bit = 0;
   
    while(1) {
   
          if(startStopCounter == 1) {

                  runOnceFlag = TRUE;
                  LCD_Cmd(_LCD_CLEAR);
                 
                  if(!PORTD.F0) {
                      Delay_ms(50);
                      if(!PORTD.F0) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg2));
                                runFlag = TRUE;
                                onTimeInMilliSec = 250;
                                offTimeInMilliSec = 500;
                                LED1 = ON;
                            }
                      }
                  }
                  else if(!PORTD.F1) {
                      Delay_ms(50);
                      if(!PORTD.F1) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg3));
                                runFlag = TRUE;
                                onTimeInMilliSec = 200;
                                offTimeInMilliSec = 400;
                                LED2 = ON;
                            }
                      }
                  }
                  else if(!PORTD.F2) {
                      Delay_ms(50);
                      if(!PORTD.F2) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg4));
                                runFlag = TRUE;
                                onTimeInMilliSec = 167;
                                offTimeInMilliSec = 333;
                                LED3 = ON;
                            }
                      }
                  }
                  else if(!PORTD.F3) {
                      Delay_ms(50);
                      if(!PORTD.F3) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg5));
                                runFlag = TRUE;
                                onTimeInMilliSec = 143;
                                offTimeInMilliSec = 285;
                                LED4 = ON;
                            }
                      }
                  }
                  else if(!PORTC.F4) {
                      Delay_ms(50);
                      if(!PORTC.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg6));
                                runFlag = TRUE;
                                onTimeInMilliSec = 125;
                                offTimeInMilliSec = 250;
                                LED5 = ON;
                            }
                      }
                  }
                  else if(!PORTC.F5) {
                      Delay_ms(50);
                      if(!PORTC.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg7));
                                runFlag = TRUE;
                                onTimeInMilliSec = 111;
                                offTimeInMilliSec = 222;
                                LED6 = ON;
                            }
                      }
                  }
                  else if(!PORTC.F6) {
                      Delay_ms(50);
                      if(!PORTC.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg8));
                                runFlag = TRUE;
                                onTimeInMilliSec = 100;
                                offTimeInMilliSec = 200;
                                LED7 = ON;
                            }
                      }
                  }
                  else if(!PORTC.F7) {
                      Delay_ms(50);
                      if(!PORTC.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg9));
                                runFlag = TRUE;
                                onTimeInMilliSec = 83;
                                offTimeInMilliSec = 166;
                                LED8 = ON;
                            }
                      }
                  }
                  else if(!PORTD.F4) {
                      Delay_ms(50);
                      if(!PORTD.F4) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg10));
                                runFlag = TRUE;
                                onTimeInMilliSec = 72;
                                offTimeInMilliSec = 144;
                                LED9 = ON;
                            }
                      }
                  }
                  else if(!PORTD.F5) {
                      Delay_ms(50);
                      if(!PORTD.F5) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg11));
                                runFlag = TRUE;
                                onTimeInMilliSec = 63;
                                offTimeInMilliSec = 126;
                                LED10 = ON;
                            }
                      }
                  }
                  else if(!PORTD.F6) {
                      Delay_ms(50);
                      if(!PORTD.F6) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg12));
                                runFlag = TRUE;
                                onTimeInMilliSec = 56;
                                offTimeInMilliSec = 112;
                                LED11 = ON;                                                                                           ;
                            }
                      }
                  }
                  else if(!PORTD.F7) {
                      Delay_ms(50);
                      if(!PORTD.F7) {
                            if(runOnceFlag) {
                                LCD_Out(1,1,CopyConst2Ram(msg, msg13));
                                runFlag = TRUE;
                                onTimeInMilliSec = 50;
                                offTimeInMilliSec = 100;
                                LED12 = ON;
                            }
                      }
                  }
                 
                  if(runOnceFlag) {
                      startStopCounter++;
                      toggleOnTimeOffTime = 1;
                      SOLENOID = ON;
                      INTE_bit = ENABLE;
                      INTF_bit = 0;
                      InitTimer1();
                      runOnceFlag = CLEAR;
                  }
          }
         
          if(resetFlag == TRUE) {
                  LCD_Cmd(_LCD_CLEAR);
                  LCD_Out(1,1,CopyConst2Ram(msg, msg14));
                  resetFlag = CLEAR;
                  SOLENOID = OFF;
                  runFlag = CLEAR;
                  TMR1ON_bit = 0;
                  startStopCounter = 0;
                  TurnOffLeds();
                  INTE_bit = ENABLE;
                  INTF_bit = CLEAR;
          }
    }
}
 
Top