pic16f877a and mikroc problem

Thread Starter

wali78

Joined Dec 29, 2014
28
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
Perfect, I simulated this code and it worked very well, I'll try it on the hardware and let you know the result.
Many thanks for your cooperation and for ErnieM and everybody gave an advice, highly appreciated.
 

Thread Starter

wali78

Joined Dec 29, 2014
28
Perfect, I simulated this code and it worked very well, I'll try it on the hardware and let you know the result.
Many thanks for your cooperation and for ErnieM and everybody gave an advice, highly appreciated.
Hello JohnInTX, how are you? I hope that everything is going well
I was very busy ;So! I couldn't try this code on hardware; last days I've tried it but unfortunately it doesn't work! I don't know why! I've simulated it as I mentioned earlier and worked well but practically! Not. (I'm very sure of the hardware).
Please advice.
Thanks in advance.
 

ErnieM

Joined Apr 24, 2011
8,415
A wise man one said “If it looks funny, Record Amount of Funny.”

As we have mentioned several times before " it doesn't work!" is an inadequate description of your problem.

What are your complete observations?
 

Thread Starter

wali78

Joined Dec 29, 2014
28
A wise man one said “If it looks funny, Record Amount of Funny.”

As we have mentioned several times before " it doesn't work!" is an inadequate description of your problem.

What are your complete observations?
I wanted to do a circuit with two buttons; every button do an operation; JohnInTX done the code and when I simulated it! it worked well;
but on hardware it doesn't work. Please any advise would be appreciated. Thanks in advance
 

JohnInTX

Joined Jun 26, 2012
4,787
A wise man one said “If it looks funny, Record Amount of Funny.”
That's pretty funny..


When you move from simulation to actual hardware you have to make sure that all of the hardware issues are resolved - many sims will indicate good results when the setup won't run on actual hardware.

Things to look for are:

Make sure the configuration bits are set up for your oscillator, MCLR select etc. Review the data book and make sure you understand each setting and have it set correctly in MicroC.
Make sure you have a good MCLR/ circuit to reset the processor on power up or disable it in config.
Make sure your oscillator is running - amazing how some sims don't check for this. If you don't have a scope you can measure OSC1 / OSC2 with a DVM. You should read about 1/2 Vdd if its running.
A good source of power with decoupling caps is necessary
Double check your wiring on the prototype board - bent pins? Incorrect wiring? You can test subsystems (relays, LEDs etc) by removing the processor and applying +5V or GND as required to ensure that when the port turns on, the output circuit will comply.
Be sure that your programmer is set to program the config bits.

To echo ErnieM, post a current schematic, program settings etc etc so that we don't have to dust off the crystal ball.

Good luck.
 
Top