Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Ludo dice problem of pic microcontroller

Status
Not open for further replies.

khatus

Member level 3
Joined
Oct 7, 2017
Messages
56
Helped
1
Reputation
2
Reaction score
0
Trophy points
6
Activity points
441
Question) This is the problem of a pic microcontroller. In this problem, PIC16f877A Microcontroller Pin 0-5 of port B, indicates the value from 1 to 6 of a ludo dice. PIN-0 of PORTC is taken as a pushbutton input. when the push button is pressed At that moment the LEDs connected to Pin 0-5 of port B will display the indicated value of the ludo dice.
My problem is when I press the push button (in Proteus simulation) only one LED is on. But the LED is not displaying the value indicated by the LED at that moment. What's wrong with my code?

Code:
void main()
{
 static unsigned int var;
 TRISC.F0=1;
 TRISB=0x00;
 PORTB=0x00;
 while(1)
 {
   for ( var =1 ; var <=63; var = (var<<1) +1)
    {
     PORTB = var;
     delay_ms(500);
     PORTB = 0;
     delay_ms(500);
         if( !PORTC.F0)
          {
             PORTB = var;
             break;
          }
         else
          continue;
    }
  }
}
dice.PNG
 

My guess is that you are expecting the LEDs to display as long as you have the push button pushed.
However what will happen is, when the push button is low and your 'if' statement becomes true, then you will display the value of 'var' on the PORTB pins but the next 'break' statement will take you out of the 'for' loop and the outer 'while loop' will take you straight back into the start of the 'for' loop with 'var' set back to 1.
What you need to do is to detect when the button is pushed (making sure that you have debounced it first), display the 'var' number on the LEDs and then wait until the button is released.
Also the 'else continue' is not doing anything for you as you are at the end of the loop and it will 'continue' anyway.
Susan
 

Hi,
As @Aussie Susan mentioned, for loop after break might be the issue. try the following code change and check:
Code:
void main()
{
 static unsigned int var;
 TRISC.F0=1;
 TRISB=0x00;
 PORTB=0x00;
 while(1)
 {
    if( !PORTC.F0)
    {
        PORTB = var;
    }
    else{
        for ( var =1 ; var <=63; var = (var<<1) +1)
        {
            PORTB = var;
            delay_ms(500);
            PORTB = 0;
            delay_ms(500);
        }
    }
  }
}
 

Thanks. This code works!!
Code:
void main()
{
 static unsigned int var;
 TRISC.F0=1;
 TRISB=0x00;
 PORTB=0x00;
 var =1  ;
 while(1)
 {
   while( var <=63)
    {
     PORTB = var;
     delay_ms(500);
     PORTB = 0;
     delay_ms(500);
         if( !PORTC.F0)
          {
             PORTB = var;
             break;
          }
     if(var==63)
     {
      var = 1;
      }
      else
      var = (var<<1) +1;
    }
  }
}
 

Now that you were given the answer, do you understand WHY that works and yours didn't?
If you have not taken this step then you have not learned anything.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top