issue about button in mikro c

Thread Starter

abo zaynab

Joined May 1, 2018
35
i ve an issue to make this line which is the start button shut down every thing again
if(portc.f0==0&&btn==0&&portc.f6!=1){delay_ms(200);btn=1; //power push button
also i want to know if i ve an event which is occuring and i want a button to pause this event n continue after i release the button how do i make it?
 

Attachments

jayanthd

Joined Jul 4, 2015
945
Try this.

If your button is PORTB.F0 and button press level = 0 then

Code:
LED = On;

if(PORTB.F0 == 0) {
  do {
       //LED = Off //event that has to be paused or stopped
  }while(PORTB.F0 == 0)
}
But this is a blocking code. There are other better methods.
 

Thread Starter

abo zaynab

Joined May 1, 2018
35
Try this.

If your button is PORTB.F0 and button press level = 0 then

Code:
LED = On;

if(PORTB.F0 == 0) {
  do {
       //LED = Off //event that has to be paused or stopped
  }while(PORTB.F0 == 0)
}
But this is a blocking code. There are other better methods.
other methods like what?
 

Thread Starter

abo zaynab

Joined May 1, 2018
35
Need to see your full code to answer. Have to see what you are trying to control and what you want to disable when a button is pressed.
C:
unsigned int seg[]={0Xc0, //Hex value to display the number 0

  0Xf9, //Hex value to display the number 1

  0Xa4, //Hex value to display the number 2

  0Xb0, //Hex value to display the number 3

  0X99, //Hex value to display the number 4

  0X92, //Hex value to display the number 5

  0X83, //Hex value to display the number 6

  0Xf8, //Hex value to display the number 7

  0X80, //Hex value to display the number 8

  0X98  //Hex value to display the number 9

  }; //End of Array for displaying numbers from 0 to 9

float deg;float degree1;

int r_deg;int r_degree1;int sec0=0;int sec1=0;int sec2=0;int sec3=0;

unsigned short btn=0,btn1=0,btn2=0,btn3=0,btn4=0,btn5=0, button1=0,m=0,a=0,b=0,x=0,y=0;

char real_degree[7]; char real_degree1[7];

//Timer1

//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms

void InitTimer1(){

  T1CON  = 0x31;

  TMR1IF_bit  = 0;

  TMR1H  = 0x0B;

  TMR1L  = 0xDC;

  TMR1IE_bit  = 1;

  INTCON  = 0xC0;

}


void Interrupt(){

  if (TMR1IF_bit){

  TMR1IF_bit = 0;

  TMR1H  = 0x0B;

  TMR1L  = 0xDC;

  sec1++;sec2++;sec3++;sec0++;


  }

}


void config(){

  CMCON=7;

adcon1=0b0100;

trisa=3;trisb=0x00;trisc=0x0f;trisd=0x0f;trise=0;

porta=3;portb=0x00;portc=0x0f;portd=0x0f;porte=0;

adc_init();

  }

void main() {

config();

InitTimer1();

while(1){

  if(portc.f0==0&&btn==0&&portc.f6!=1){delay_ms(200);btn=1;  //power push button

  while(1){ loop:

  portc.f4=1; //power indicator lamp is on &showing heating degrees on screen

  deg=adc_read(0);degree1=adc_read(1);

  r_deg=(deg)*(0.48828125);r_degree1=(degree1)*(0.48828125);

  inttostr(r_deg,real_degree);inttostr(r_degree1,real_degree1);

  a=r_deg%10;

  b=((r_deg/10)%10);

  x=r_degree1%10;

  y=((r_degree1/10)%10);

  portb=seg[B];porte.f0=1;delay_ms(5);porte=0;

  portb=seg[a];porte.f1=1;delay_ms(5);porte=0;

  portb=seg[y];porte.f2=1;delay_ms(5);porte=0;

  portb=seg[x];porta.f2=1;delay_ms(5);porta.f2=0;

  if(portd.f0!=0){portd.f7=1;} //openinng water solnoide for filling the booster when its not completly filled

  else if(portd.f0==0){portd.f7=0;}//close solnoide when the booster is filled with water

  //d0=booster water level //d1=box watwer level // d2=door switch //d3=get rid pump switch

  if(r_deg<=89&&portd.f0==0){portc.f7=1;} //connectting the heaters when it reaches a certain degree

  else if(r_deg>=90&&portd.f0==0||portd.f0==1){portc.f7=0;}  //booster heaters

  if(r_deg<=89&&portd.f1==0){sec0=0;}//resetting the timer for filling the box with water from booster

  if(r_deg>=90&&portd.f1!=0&&sec0<=19){portd.f4=1;}  //filling pump

  else if(r_deg>=70&&sec0==20||portd.f1==0){portd.f4=0;sec0=0;}// stop filling pump after a certain time or when water level stops it

  if(sec3==3){sec3=0;}  //resetting timer for lamp blinking

  if (portd.f1==1&&btn==1&&sec3==2){portc.f5=~portc.f5;}//turns off indicator lamp if the machine is not ready yet

  else if(portd.f1==0){portc.f5=1;} //indicator lamp if the machine is ready

  if(portd.f1==0&&r_degree1<=69){portc.f6=1;}  // box heaters

  else if(portd.f1==0&&r_degree1>=70||portd.f0==1){portc.f6=0;} //disconnectting box heaters when it reaches a certain degree

  if(portc.f1==0&&portd.f1==0&&btn1==0&&btn2==0&&btn3==0&&portd.f2!=1){ btn1=1;sec1=0;portd.f6=1;} //washing pump for 1 min

  else if(btn1==1&&portd.f1==0&&sec1==120){btn1=0;portd.f6=0;}  //end of washing

  if(portc.f2==0&&portd.f1==0&&btn1==0&&btn2==0&&btn3==0&&portd.f2!=1){ btn2=1;sec1=0;portd.f6=1;} //washing pump for 2 min

  else if(btn2==1&&portd.f1==0&&sec1==240){btn2=0;portd.f6=0;}  //end of washing

  if(portc.f3==0&&portd.f1==0&&btn1==0&&btn2==0&&btn3==0&&portd.f2!=1){ btn3=1;sec1=0;portd.f6=1;} //washing pump for 1 min

  else if(btn3==1&&portd.f1==0&&sec1==480){btn3=0;portd.f6=0;}  //end of washing

  if(btn1==1&&sec1==119||btn2==1&&sec1==239||btn3==1&&sec1==479){button1=1;sec2=0;} //resetting timer for getting rid of water

  if(button1==1&&sec2==3){portd.f5=1;}

  if(button1==1&&sec2==13){portd.f5=0;}

  if(portd.f2==1&&btn1==1||portd.f2==1&&btn2==1||portd.f2==1&&btn3==1||portc.f0==0&&btn==1){btn1=0;btn2=0;btn3=0;portd.f6=0;portd.f5=0;portd.f4=0;}

  if(btn1==0&&btn2==0&&btn3==0){if(portd.f3==0&&btn5==0&&portd.f2==0){btn5=1;sec3=0;portd.f5=1;} // get rid of water button

  else if(sec3==60&&btn5==1||portd.f2==1){portd.f5=0;btn5=0;}

  }

  }

  }

  }

  }
i want if this button pressed:if(portc.f0==0&&btn==0&&portc.f6!=1){delay_ms(200);btn=1; //power push button,which is portc.f0>>>what has been operated stop it 1st then if pressed again & nothing is operating turn every thing off.
Mod edit: code tags
 
Last edited by a moderator:

djsfantasi

Joined Apr 11, 2010
9,156
unsigned int seg[]={0Xc0, //Hex value to display the number 0

0Xf9, //Hex value to display the number 1

0Xa4, //Hex value to display the number 2

0Xb0, //Hex value to display the number 3

0X99, //Hex value to display the number 4

0X92, //Hex value to display the number 5

0X83, //Hex value to display the number 6

0Xf8, //Hex value to display the number 7

0X80, //Hex value to display the number 8

0X98 //Hex value to display the number 9

}; //End of Array for displaying numbers from 0 to 9

float deg;float degree1;

int r_deg;int r_degree1;int sec0=0;int sec1=0;int sec2=0;int sec3=0;

unsigned short btn=0,btn1=0,btn2=0,btn3=0,btn4=0,btn5=0, button1=0,m=0,a=0,b=0,x=0,y=0;

char real_degree[7]; char real_degree1[7];

//Timer1

//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms

void InitTimer1(){

T1CON = 0x31;

TMR1IF_bit = 0;

TMR1H = 0x0B;

TMR1L = 0xDC;

TMR1IE_bit = 1;

INTCON = 0xC0;

}


void Interrupt(){

if (TMR1IF_bit){

TMR1IF_bit = 0;

TMR1H = 0x0B;

TMR1L = 0xDC;

sec1++;sec2++;sec3++;sec0++;


}

}


void config(){

CMCON=7;

adcon1=0b0100;

trisa=3;trisb=0x00;trisc=0x0f;trisd=0x0f;trise=0;

porta=3;portb=0x00;portc=0x0f;portd=0x0f;porte=0;

adc_init();

}

void main() {

config();

InitTimer1();

while(1){

if(portc.f0==0&&btn==0&&portc.f6!=1){delay_ms(200);btn=1; //power push button

while(1){ loop:

portc.f4=1; //power indicator lamp is on &showing heating degrees on screen

deg=adc_read(0);degree1=adc_read(1);

r_deg=(deg)*(0.48828125);r_degree1=(degree1)*(0.48828125);

inttostr(r_deg,real_degree);inttostr(r_degree1,real_degree1);

a=r_deg%10;

b=((r_deg/10)%10);

x=r_degree1%10;

y=((r_degree1/10)%10);

portb=seg;porte.f0=1;delay_ms(5);porte=0;

portb=seg[a];porte.f1=1;delay_ms(5);porte=0;

portb=seg[y];porte.f2=1;delay_ms(5);porte=0;

portb=seg[x];porta.f2=1;delay_ms(5);porta.f2=0;

if(portd.f0!=0){portd.f7=1;} //openinng water solnoide for filling the booster when its not completly filled

else if(portd.f0==0){portd.f7=0;}//close solnoide when the booster is filled with water

//d0=booster water level //d1=box watwer level // d2=door switch //d3=get rid pump switch

if(r_deg<=89&&portd.f0==0){portc.f7=1;} //connectting the heaters when it reaches a certain degree

else if(r_deg>=90&&portd.f0==0||portd.f0==1){portc.f7=0;} //booster heaters

if(r_deg<=89&&portd.f1==0){sec0=0;}//resetting the timer for filling the box with water from booster

if(r_deg>=90&&portd.f1!=0&&sec0<=19){portd.f4=1;} //filling pump

else if(r_deg>=70&&sec0==20||portd.f1==0){portd.f4=0;sec0=0;}// stop filling pump after a certain time or when water level stops it

if(sec3==3){sec3=0;} //resetting timer for lamp blinking

if (portd.f1==1&&btn==1&&sec3==2){portc.f5=~portc.f5;}//turns off indicator lamp if the machine is not ready yet

else if(portd.f1==0){portc.f5=1;} //indicator lamp if the machine is ready

if(portd.f1==0&&r_degree1<=69){portc.f6=1;} // box heaters

else if(portd.f1==0&&r_degree1>=70||portd.f0==1){portc.f6=0;} //disconnectting box heaters when it reaches a certain degree

if(portc.f1==0&&portd.f1==0&&btn1==0&&btn2==0&&btn3==0&&portd.f2!=1){ btn1=1;sec1=0;portd.f6=1;} //washing pump for 1 min

else if(btn1==1&&portd.f1==0&&sec1==120){btn1=0;portd.f6=0;} //end of washing

if(portc.f2==0&&portd.f1==0&&btn1==0&&btn2==0&&btn3==0&&portd.f2!=1){ btn2=1;sec1=0;portd.f6=1;} //washing pump for 2 min

else if(btn2==1&&portd.f1==0&&sec1==240){btn2=0;portd.f6=0;} //end of washing

if(portc.f3==0&&portd.f1==0&&btn1==0&&btn2==0&&btn3==0&&portd.f2!=1){ btn3=1;sec1=0;portd.f6=1;} //washing pump for 1 min

else if(btn3==1&&portd.f1==0&&sec1==480){btn3=0;portd.f6=0;} //end of washing

if(btn1==1&&sec1==119||btn2==1&&sec1==239||btn3==1&&sec1==479){button1=1;sec2=0;} //resetting timer for getting rid of water

if(button1==1&&sec2==3){portd.f5=1;}

if(button1==1&&sec2==13){portd.f5=0;}

if(portd.f2==1&&btn1==1||portd.f2==1&&btn2==1||portd.f2==1&&btn3==1||portc.f0==0&&btn==1){btn1=0;btn2=0;btn3=0;portd.f6=0;portd.f5=0;portd.f4=0;}

if(btn1==0&&btn2==0&&btn3==0){if(portd.f3==0&&btn5==0&&portd.f2==0){btn5=1;sec3=0;portd.f5=1;} // get rid of water button

else if(sec3==60&&btn5==1||portd.f2==1){portd.f5=0;btn5=0;}

}

}

}

}

}

i want if this button pressed:if(portc.f0==0&&btn==0&&portc.f6!=1){delay_ms(200);btn=1; //power push button,which is portc.f0>>>what has been operated stop it 1st then if pressed again & nothing is operating turn every thing off

You really have to learn how to post code by using code tags. Click on the insert code icon (looks like a code listing with a down arrow. It’s to the left of the floppy disk icon).
 

jayanthd

Joined Jul 4, 2015
945
Your code is using a lot of buttons and the project is a dish washer.

Your buttons don't have a debounce code.

Post your circuit here as PDF or Proteus simulation file and mention the logic of operation and then I can provide you here a small and working mikroC code.

I also see that you are using a 7 Segment display common anode type to display the wash duration (count down).

All you need is a single int or long variable in the timer isr which counts for the required delay count for the current wash cycle and then controls the wash cycle state or goes to the next wash cycle after it expires.

Also, why you are using unsigned int variable (array) for the seven-segment mask values. They are byte values and can be easily stored in a char variable (array) and also they need to be stored in a "const code char" type array so that they are stored in ROM to save RAM space.

Why don't you name the buttons like

Code:
sbit Start_Button at PORTB0_bit;
sbit Refresh_Button at PORTB1_bit;
If you use a 4 MHz oscillator then you will easily get a 500ms timer interrupt delay which you can use for your timer.

Use two variables in timer isr like

Code:
ISR {
    if(++delayCounter >= delayCount) {
        delayCounter = 0;
        washCycleState++;
    }
}
If your timer interrupt is for 500ms then if a wash cycle is for 10 minutes then

10 minutes = 10 * 60 seconds = 600 seconds.

500ms = 1 timer interrupt count or delayCount

So, for 600 seconds it is 1200.

Like this you calculate delayCount values for each wash cycle and load it to the delayCount variable in the state machine when a previous wash cycle finishes in the ISR.

The delayCount variable is loaded with the delay count value for the next wash cycle when a previous wash cycle finishes.

washCycleState is the wash cycle state counter.

Use a

switch() {

}

statement in the while(1) loop to make your wash cycle state machine.

Post your exact circuit here and mention your dishwasher logic of operation and I will post the code for you here.

You should also use a byte of internal EEPROM of the PIC to hold the value of the current wash cycle that is washCycleState variable so that if there is a power reset or watch dog timer reset then it will continue from the last wash cycle.

Better to use another say Timer2 at 2 ms interrupt to drive the 7 Segment display.


Why two LM35 temperature sensors? You need just one for monitoring the water temperature.

Are you also using steam wash?

dish dry cycle?
 
Last edited:

Thread Starter

abo zaynab

Joined May 1, 2018
35
i ve changed my code ,i ve interfaced 7segment with max 7219 but im facing a timming problem just when i interface seperate 7segments but when i use 4 7segments together it just work fine
 

Attachments

Top