pic16f877a and mikroc problem

JohnInTX

Joined Jun 26, 2012
4,787
I don't know how to write the button's code in shadowing!
Buttons are inputs so you don't have to worry about shadowing. Read them normally. They can share the same port as outputs if you want - just be sure to read the PORT itself (not the shadow image).
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Buttons are inputs so you don't have to worry about shadowing. Read them normally. They can share the same port as outputs if you want - just be sure to read the PORT itself (not the shadow image).
thanks, I'll try it and let you know what I got
 

Thread Starter

wali78

Joined Dec 29, 2014
28
thanks, I'll try it and let you know what I got
delay_ms(10) //// in compile is uncleared expression /// invalid expression !!!

unsigned char PORTBimage;

void main(){
{
TRISB=0b00000000;
PORTB=0;
PORTBimage = 0; // must start the same

RB0_bit=1;
if (RB0_bit==0){
delay_ms(10);

//RB1_bit=1;
PORTBimage.B1 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B1 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB2_bit=1;
PORTBimage.B2 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B2 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB3_bit=1;
PORTBimage.B3 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B3 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB7_bit=1;
PORTBimage.B7 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
}//for main
}
 

JohnInTX

Joined Jun 26, 2012
4,787
Delay_ms not delay_ms - case sensitive

You have an extra brace { in main.

RB0_bit = 1 is not necessary or desired - RB0 is an input whose value is controlled by the button.
 

JohnInTX

Joined Jun 26, 2012
4,787
OK
First, init TRISD with 0xFF to ensure its an input.
EDIT: Your button is connected to RD1 but you are polling RD0.
Next, you need some sort of LOOP to test the button in - you are only testing it once.

Code:
void main (void) {

while (1){  // do main forever
  ..stuff to do
   while(RD1_bit==1);  // wait for button
  .. button pressed - proceed
}// close while(1) loop
}// close main
And PLEASE use code tags, not screen dumps. That way we can actually load your code and help you debug it.

See EDIT above
 

Thread Starter

wali78

Joined Dec 29, 2014
28
OK
First, init TRISD with 0xFF to ensure its an input.
EDIT: Your button is connected to RD1 but you are polling RD0.
Next, you need some sort of LOOP to test the button in - you are only testing it once.

Code:
void main (void) {

while (1){  // do main forever
  ..stuff to do
   while(RD1_bit==1);  // wait for button
  .. button pressed - proceed
}// close while(1) loop
}// close main
And PLEASE use code tags, not screen dumps. That way we can actually load your code and help you debug it.

See EDIT above
Compilation is OK, but simulation NO. When I press the button the program is not starting!
E
Code:
        unsigned char PORTBimage;

void main(){
{

      PORTD=0XFF ;

     TRISB=0b00000000;     PORTB=0;
     PORTBimage = 0;        // must start the same
     
      while (1){  // do main forever

      while(RD1==1);           // wait for button

      //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port

        }// close while(1) loop
        }// close main
        }
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Compilation is OK, but simulation NO. When I press the button the program is not starting!
E
Code:
        unsigned char PORTBimage;

void main(){
{

      PORTD=0XFF ;

     TRISB=0b00000000;     PORTB=0;
     PORTBimage = 0;        // must start the same
    
      while (1){  // do main forever

      while(RD1==1);           // wait for button

      //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port

        }// close while(1) loop
        }// close main
        }
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
A couple of things were wrong.
You set PORTD to oxFF instead of TRISD - that was not why it did not work.
while(RD1==1); should have been while(RD1_bit==0); RD1 needed to be called out as a _bit and I see from your drawing that RD1 is normally low and pushed high when the button is pressed (its usually the other way around).

I edited the code (thanks for using tags) - you should be able to debug things like this .. When you add something that doesn't work, take it out and see if that is what broke things. Then put it back and debug it.
I did not simulate it but I DID look at the assembler output. Calling the input RD1 instead of RD1_bit caused NO button test code to be generated - and no errors/warnings. It should have just started up like before.
Anyway, give this a try.

Code:
unsigned char PORTBimage;

void main()
{
     TRISD=0b11111111 ;  // PORTD is all input

     TRISB=0b00000000;    
     PORTB=0;
     PORTBimage = 0;        // must start the same

      while (1){  // do forever

        while(RD1_bit == 0);   // wait for button (1 = pressed)

        //RB1_bit=1;
        PORTBimage.B1 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port
        Delay_ms(3000);

        PORTBimage.B1 = 0; // set bit in shadow register
        PORTB = PORTBimage;  // write it to the port
        Delay_ms(2000);

        //RB2_bit=1;
        PORTBimage.B2 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port
        Delay_ms(3000);

        PORTBimage.B2 = 0; // set bit in shadow register
        PORTB = PORTBimage;  // write it to the port
        Delay_ms(2000);

        //RB3_bit=1;
        PORTBimage.B3 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port
        Delay_ms(3000);

         PORTBimage.B3 = 0; // set bit in shadow register
        PORTB = PORTBimage;  // write it to the port
        Delay_ms(2000);

        //RB7_bit=1;
        PORTBimage.B7 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port

        }// close while(1) loop
}// close main
 

Thread Starter

wali78

Joined Dec 29, 2014
28
A couple of things were wrong.
You set PORTD to oxFF instead of TRISD - that was not why it did not work.
while(RD1==1); should have been while(RD1_bit==0); RD1 needed to be called out as a _bit and I see from your drawing that RD1 is normally low and pushed high when the button is pressed (its usually the other way around).

I edited the code (thanks for using tags) - you should be able to debug things like this .. When you add something that doesn't work, take it out and see if that is what broke things. Then put it back and debug it.
I did not simulate it but I DID look at the assembler output. Calling the input RD1 instead of RD1_bit caused NO button test code to be generated - and no errors/warnings. It should have just started up like before.
Anyway, give this a try.

Code:
unsigned char PORTBimage;

void main()
{
     TRISD=0b11111111 ;  // PORTD is all input

     TRISB=0b00000000;   
     PORTB=0;
     PORTBimage = 0;        // must start the same

      while (1){  // do forever

        while(RD1_bit == 0);   // wait for button (1 = pressed)

        //RB1_bit=1;
        PORTBimage.B1 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port
        Delay_ms(3000);

        PORTBimage.B1 = 0; // set bit in shadow register
        PORTB = PORTBimage;  // write it to the port
        Delay_ms(2000);

        //RB2_bit=1;
        PORTBimage.B2 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port
        Delay_ms(3000);

        PORTBimage.B2 = 0; // set bit in shadow register
        PORTB = PORTBimage;  // write it to the port
        Delay_ms(2000);

        //RB3_bit=1;
        PORTBimage.B3 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port
        Delay_ms(3000);

         PORTBimage.B3 = 0; // set bit in shadow register
        PORTB = PORTBimage;  // write it to the port
        Delay_ms(2000);

        //RB7_bit=1;
        PORTBimage.B7 = 1; // set bit in shadow register
        PORTB = PORTBimage;  // write the new pattern to the port

        }// close while(1) loop
}// close main
this is perfect, compiled and simulated without any issues, I add another button to the diagram and the code; first button worked and the loop was ended but the second button is not connected the circuit.
Below the diagram and the code I wrote:

unsigned char PORTBimage;

void main()
{
TRISD=0b11111111 ; // PORTD is all input

TRISB=0b00000000;
PORTB=0;
PORTBimage = 0; // must start the same

while (1){ // do forever

while(RD1_bit == 0); // wait for button (1 = pressed)

//RB1_bit=1;
PORTBimage.B1 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B1 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB2_bit=1;
PORTBimage.B2 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B2 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB3_bit=1;
PORTBimage.B3 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B3 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);
}// close while(1) loop

while (1){ // do forever

while(RD2_bit == 0); // wait for button (1 = pressed)

//RB1_bit=1;
PORTBimage.B1 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B1 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB5_bit=1;
PORTBimage.B5 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B5 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB2_bit=1;
PORTBimage.B2 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B2 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB3_bit=1;
PORTBimage.B3 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B3 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB1_bit=1;
PORTBimage.B1 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B1 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB4_bit=1;
PORTBimage.B4 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(2000);

PORTBimage.B4 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB5_bit=1;
PORTBimage.B5 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B5 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB2_bit=1;
PORTBimage.B2 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B2 = 0; // set bit in shadow register
PORTB = PORTBimage; // write it to the port
Delay_ms(2000);

//RB5_bit=1;
PORTBimage.B5 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B5 = 0; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(2000);

//RB3_bit=1;
PORTBimage.B3 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(3000);

PORTBimage.B3 = 0; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
Delay_ms(2000);


//RB7_bit=1;
PORTBimage.B7 = 1; // set bit in shadow register
PORTB = PORTBimage; // write the new pattern to the port
} // close while(1) loop
}// close main
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
Glad it works so far... but

It will never get to the second part because the while(1) needs to cover BOTH parts. Keep in mind that the while(1) is there to make the whole program repeat after each cycle.
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Glad it works so far... but

It will never get to the second part because the while(1) needs to cover BOTH parts. Keep in mind that the while(1) is there to make the whole program repeat after each cycle.
Well, I'll try it and let you know the result.
Thanks in advance.
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Well, I'll try it and let you know the result.
Thanks in advance.
I failed to do it, I tried with {IF & ELSE}, I'm not expert in this, I just try to learn.
I would be appreciated if you complete it with me.
I want to make two sequences by two buttons.
 

JohnInTX

Joined Jun 26, 2012
4,787
Post your code and we can take a look at it.
I want to make two sequences by two buttons.
I am not sure what that means. Do you want to have 2 different sequences that you select by pushing one of the 2 buttons? A basic flow chart would help too.
 

ErnieM

Joined Apr 24, 2011
8,377
Shadowing is for the read-modify-WRITE problem. There is no similar issue when just reading a port.

In the hardware you need to add these two buttons and connect to free port pins. If you have free pins with weak pull ups (see the data sheet) you can use those otherwise add pull up resistors (10K or 100K not critical) to the pins and a normally open button to ground. This gives a ONE when not pressed, and a ZERO when pressed.

At the beginning of main() you almost have to do nothing as port pins default to inputs (once you disable any analog functions).

For simplicity I’ve #defined the input port pins as ButtonA and ButtonB. You will need to fix these up to where the buttons actually do connect. If you do the buttons another way you also need to change the PRESSED and NOT_PRESSED #defines.

While loops to check the buttons have been added. These simply loop in place until a button is pressed.
I put the whole package inside another loop so you can run a second load of clothes. <g>

Code:
      #define ButtonA PortX1
      #define ButtonB PortX2
 
      #define PRESSED as 0
      #define NOT_PRESS as 1
   
 
// Example of shadowing PORTB. An image of the port is maintained in RAM.
// Changes to the port are done by first modifying the image then writing
// the whole byte to the PORT and avoiding r-m-w problems.
 
unsigned char PORTBimage;
 
void main()
{
     TRISB=0b00000000;
     PORTB=0;
     PORTBimage = 0; // must start the same
 
  while(1)
  {
 
      while(ButtonA = NOT_PRESSED);  // loop here until first button down.
 
      // all the first part functions
   
 
 
//////////////////////////////////////////////////////////////////////////////////////////////////
 
      while(ButtonB= NOT_PRESSED);   // loop here until second button down.
 
      // all the second part functions
 
 
  }  // end of while, loop for another load.
} // end of main
Note the button checking while() loops have a terminating ; and not {brackets} That just means there is no other code in the while loop but the loop itself.
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Post your code and we can take a look at it.
I am not sure what that means. Do you want to have 2 different sequences that you select by pushing one of the 2 buttons? A basic flow chart would help too.
Exactly, I want to divide the the operation to 2 operations by 2 buttons; and select the operation I want by pushing its button.
Operation No.1: [BUTTON 1] Washing with cold water. (after it finish, should wait until detergent added ).
Operation No.2: [BUTTON 2] Washing with hot water, Polishing, etc...
Code:
    // Example of shadowing PORTB. An image of the port is maintained in RAM.
// Changes to the port are done by first modifying the image then writing
// the whole byte to the PORT and avoiding r-m-w problems.

unsigned char PORTBimage;

void main()
{
     TRISB=0b00000000;
     PORTB=0;
     PORTBimage = 0; // must start the same
/////////////////////////////////[I][B][COLOR=#ff0000]Operation No. 1 [BUTTON 1][/COLOR]////////////////////////////[/B][/I]
      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait 
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(240000);

      PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

       PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(60000);
///////////////////////////[COLOR=#ff0000][B][I]Operation No. 2 [BUTTON 2][/I][/B][/COLOR]/////////////////////////////
      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(600000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(420000);

      PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

      PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB4_bit=1;
      PORTBimage.B4 = 1; // Polishing oil
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B4 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(600000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(420000);

       PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(300000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);
     
      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

       PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1; //Finish [Green LED is ON]
      PORTB = PORTBimage;

    } //for
 //main


This code is working very good.
Thanks in advance.
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Exactly, I want to divide the the operation to 2 operations by 2 buttons; and select the operation I want by pushing its button.
Operation No.1: [BUTTON 1] Washing with cold water. (after it finish, should wait until detergent added ).
Operation No.2: [BUTTON 2] Washing with hot water, Polishing, etc...
Code:
    // Example of shadowing PORTB. An image of the port is maintained in RAM.
// Changes to the port are done by first modifying the image then writing
// the whole byte to the PORT and avoiding r-m-w problems.

unsigned char PORTBimage;

void main()
{
     TRISB=0b00000000;
     PORTB=0;
     PORTBimage = 0; // must start the same
/////////////////////////////////[I][B][COLOR=#ff0000]Operation No. 1 [BUTTON 1][/COLOR]////////////////////////////[/B][/I]
      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(240000);

      PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

       PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(60000);
///////////////////////////[COLOR=#ff0000][B][I]Operation No. 2 [BUTTON 2][/I][/B][/COLOR]/////////////////////////////
      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(600000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(420000);

      PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

      PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB4_bit=1;
      PORTBimage.B4 = 1; // Polishing oil
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B4 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(600000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(420000);

       PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(300000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);
    
      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

       PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1; //Finish [Green LED is ON]
      PORTB = PORTBimage;

    } //for
//main


This code is working very good.
Thanks in advance.
Shadowing is for the read-modify-WRITE problem. There is no similar issue when just reading a port.

In the hardware you need to add these two buttons and connect to free port pins. If you have free pins with weak pull ups (see the data sheet) you can use those otherwise add pull up resistors (10K or 100K not critical) to the pins and a normally open button to ground. This gives a ONE when not pressed, and a ZERO when pressed.

At the beginning of main() you almost have to do nothing as port pins default to inputs (once you disable any analog functions).

For simplicity I’ve #defined the input port pins as ButtonA and ButtonB. You will need to fix these up to where the buttons actually do connect. If you do the buttons another way you also need to change the PRESSED and NOT_PRESSED #defines.

While loops to check the buttons have been added. These simply loop in place until a button is pressed.
I put the whole package inside another loop so you can run a second load of clothes. <g>

Code:
      #define ButtonA PortX1
      #define ButtonB PortX2

      #define PRESSED as 0
      #define NOT_PRESS as 1
  

// Example of shadowing PORTB. An image of the port is maintained in RAM.
// Changes to the port are done by first modifying the image then writing
// the whole byte to the PORT and avoiding r-m-w problems.

unsigned char PORTBimage;

void main()
{
     TRISB=0b00000000;
     PORTB=0;
     PORTBimage = 0; // must start the same

  while(1)
  {

      while(ButtonA = NOT_PRESSED);  // loop here until first button down.

      // all the first part functions
  


//////////////////////////////////////////////////////////////////////////////////////////////////

      while(ButtonB= NOT_PRESSED);   // loop here until second button down.

      // all the second part functions


  }  // end of while, loop for another load.
} // end of main
Note the button checking while() loops have a terminating ; and not {brackets} That just means there is no other code in the while loop but the loop itself.
Please have a look to the code below, I wrote it according to to your advice, it works but I didn't used (ButtonA = NOT_PRESSED) where the compiler told me that's uncleared so I used (RD1_bit=1), the problem now is! I can't begin with Button No.2 directly, I have to press Button No.1 and wait until it finish after that I can press Button No.2. [I want to begin the operation I want directly (Operation No.1 or Operation No.2)].
Thanks in advance.
Code:
  // Example of shadowing PORTB. An image of the port is maintained in RAM.
// Changes to the port are done by first modifying the image then writing
// the whole byte to the PORT and avoiding r-m-w problems.
    unsigned char PORTBimage;
#define ButtonA PortD1
#define ButtonB PortD2

#define PRESSED as 0
#define NOT_PRESS as 1
void main() {
{

  TRISD= 1; //Configure 1st bit of PORTD as input
  PORTD=0b11111111;
  TRISB=0;  PORTB=0;
  PORTBimage = 0;   PORTB = PORTBimage;
   
    while(1)
  {

      while(RD1_bit=1);  // loop here until first button down//Operation No.1.


      // all the first part functions


      //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);
      {
      while(RD2_bit=1);  // loop here until first button down//Operation No.2.

   //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B5 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB4_bit=1;
      PORTBimage.B4 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);

       PORTBimage.B4 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B5 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B5 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);

       //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1;
      PORTB = PORTBimage;
        }
       }
      }
    } //for //main
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Please have a look to the code below, I wrote it according to to your advice, it works but I didn't used (ButtonA = NOT_PRESSED) where the compiler told me that's uncleared so I used (RD1_bit=1), the problem now is! I can't begin with Button No.2 directly, I have to press Button No.1 and wait until it finish after that I can press Button No.2. [I want to begin the operation I want directly (Operation No.1 or Operation No.2)].
Thanks in advance.
Code:
  // Example of shadowing PORTB. An image of the port is maintained in RAM.
// Changes to the port are done by first modifying the image then writing
// the whole byte to the PORT and avoiding r-m-w problems.
    unsigned char PORTBimage;
#define ButtonA PortD1
#define ButtonB PortD2

#define PRESSED as 0
#define NOT_PRESS as 1
void main() {
{

  TRISD= 1; //Configure 1st bit of PORTD as input
  PORTD=0b11111111;
  TRISB=0;  PORTB=0;
  PORTBimage = 0;   PORTB = PORTBimage;
  
    while(1)
  {

      while(RD1_bit=1);  // loop here until first button down//Operation No.1.


      // all the first part functions


      //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);
      {
      while(RD2_bit=1);  // loop here until first button down//Operation No.2.

   //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B5 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB1_bit=1;
      PORTBimage.B1 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B1 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB4_bit=1;
      PORTBimage.B4 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);

       PORTBimage.B4 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B5 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B2 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

      PORTBimage.B5 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);

       //RB3_bit=1;
      PORTBimage.B3 = 1; // set bit in shadow register
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B3 = 0; // set bit in shadow register
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1;
      PORTB = PORTBimage;
        }
       }
      }
    } //for //main
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
Something like this then? Note how each cycle (operation) are broken into individual functions which are called in when buttons are pushed. The button loop is simple and compact. Breaking code into functional blocks is and important step to writing clear, workable code.

Code:
//============= DISHWASHER CONTROL  =====================================
// ..add info about the program here..

unsigned char PORTBimage;
// IO definitions with correct syntax
#define ButtonA RD1_bit
#define ButtonB RD2_bit

#define PRESSED 0     // 0 = pressed, 1 = not pressed
#define NOT_PRESSED 1

//====================== Operation No. 1 ==============================
void run_OperationA(void)
{
      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(240000);

      PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

       PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(60000);
}

//===================== Operation No. 2 ==========================
void run_OperationB(void)
{
        //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(600000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(420000);

      PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

      PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB1_bit=1;
      PORTBimage.B1 = 1; // Filling water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(32000);

      PORTBimage.B1 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB4_bit=1;
      PORTBimage.B4 = 1; // Polishing oil
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(3000);

       PORTBimage.B4 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(600000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB2_bit=1;
      PORTBimage.B2 = 1; // Washing
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(420000);

       PORTBimage.B2 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB5_bit=1;
      PORTBimage.B5 = 1; // Heating water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(300000);

      PORTBimage.B5 = 0; // Wait
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(2000);

      //RB3_bit=1;
      PORTBimage.B3 = 1; // Empty water
      PORTB = PORTBimage;  // write the new pattern to the port
      Delay_ms(30000);

       PORTBimage.B3 = 0; // Wait
      PORTB = PORTBimage;  // write it to the port
      Delay_ms(2000);

      //RB7_bit=1;
      PORTBimage.B7 = 1; //Finish [Green LED is ON]
      PORTB = PORTBimage;
}

//======================= PROGRAM STARTS HERE  ==========================
void main()
{
  TRISD = 0b00000110; //Configure RD1, RD2 as input
  PORTD = 0b00000110; // Unused PORTD are output 0
  TRISB=0; 
  PORTB=0;
  PORTBimage = 0;  
  PORTB = PORTBimage;
 
  //-----------------  START OPERATION ON BUTTONS  -----------------------
  // Scan the 2 buttons
  // 1) Wait for no buttons pressed (idle)
  // 2) Scan the 2 buttons and start cycle when one pushed by calling the
  //    cycle function
  // 3) 'continue' bypasses next button check, after cycle complete,
  //    returns to top of while loop - step 1

  // Note: this does NOT completely do everything needed to reliably
  // do the button input but is a start.
  // Buttons use the IO definitions above to make more readable code and
  // make changes easier
 
  while(1){          // scan button loop
 
     while((ButtonA == PRESSED) || (ButtonB == PRESSED)) ; // wait for no buttons
    
      if(ButtonA == PRESSED){   // call OperationA iff button A pressed
        run_OperationA();
        continue;               // cycle complete, don't check the other button yet
      }
      if(ButtonB == PRESSED){   // call OperationB iff button B pressed
        run_OperationB();
        continue;               // really not needed unless more buttons are added
      }
  }// while scan buttons
}//main
 
Top