PIC 16F877A controller On mikroC compiler

JohnInTX

Joined Jun 26, 2012
4,499
if you change if statement to while and use logical comparison like
JohnInTX said your program look good and many merits too. i will send a demo.
Your post is confusing. If you are saying that I thought the TS's code 'looked good', that is not true. My last post was to advise the TS to look at where his 'break' was, study up on what 'break' actually did and to flow out his code and post a flow chart so that we could tell what he in fact wanted his program to do. Besides 'it doesn't work' he did not provide any useful information. But if you can help, bring it.
 
Last edited:

arunpradh

Joined Dec 11, 2013
54
Your post is confusing. If you are saying that I thought the TS's code 'looked good', that is not true. My last post was to advise the TS to look at where his 'break' was, study up on what 'break' actually did and to flow out his code and post a flow chart so that we could tell what he in fact wanted his program to do. Besides 'it doesn't work' he did not provide any useful information. But if you can help, bring it.
sorry for the trouble. iasked him to avoid if statement and use while loop.
 

JohnInTX

Joined Jun 26, 2012
4,499
@arunpradh You can upload .ZIP files but it is better to post the code using CODE tags. A lot of folks won't open zips plus zips make it harder on casual viewers. When editing your post, click on the toolbar INSERT icon and select INSERT CODE. Paste the source into the box. When you are back in the post editor, you can edit the code tag to say CODE=C for better formatting.
 

arunpradh

Joined Dec 11, 2013
54
Code:
void main()
{
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
TRISD.F1 = 1; //Configure 1st bit of PORTD as input
TRISD.F2 = 1; //Configure 1st bit of PORTD as input
TRISD.F3 = 1; //Configure 1st bit of PORTD as input
TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin

    while(1) //Infinite Loop
   {
    if(PORTD.F1 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 1; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(2000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    }
    if(PORTD.F0 == 0)   //If the switch is pressed
    {
    PORTB.F1 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(2000); //1 Second Delay
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF

    }
    if(PORTD.F3 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(5000); //1 Second Delay
    }
    if(PORTD.F2 == 0)   //If the switch is pressed
    {
    while(PORTD.F2 == 0);
    while(PORTD.F3 == 1&&PORTD.F2 == 1&&PORTD.F0 == 1&&PORTD.F1 == 1) //Infinite Loop
  {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED ON
    Delay_ms(500); //1 Second Delay
    PORTB.F3 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(500); //1 Second Delay   second leg Left leg
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(500); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(1000); //1 Second Delay
    }
    }
    }
   }
Mod edit: Fixed code tag
 
Last edited by a moderator:

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
Code:
void main()
{
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
TRISD.F1 = 1; //Configure 1st bit of PORTD as input
TRISD.F2 = 1; //Configure 1st bit of PORTD as input
TRISD.F3 = 1; //Configure 1st bit of PORTD as input
TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin

    while(1) //Infinite Loop
   {
    if(PORTD.F1 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 1; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(2000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    }
    if(PORTD.F0 == 0)   //If the switch is pressed
    {
    PORTB.F1 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(2000); //1 Second Delay
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF

    }
    if(PORTD.F3 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(5000); //1 Second Delay
    }
    if(PORTD.F2 == 0)   //If the switch is pressed
    {
    while(PORTD.F2 == 0);
    while(PORTD.F3 == 1&&PORTD.F2 == 1&&PORTD.F0 == 1&&PORTD.F1 == 1) //Infinite Loop
  {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED ON
    Delay_ms(500); //1 Second Delay
    PORTB.F3 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(500); //1 Second Delay   second leg Left leg
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(500); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(1000); //1 Second Delay
    }
    }
    }
   }
This works same as my program
 
Last edited by a moderator:

dannyf

Joined Sep 13, 2015
2,197
This is the flow diagram of my source code
I think your idea of flow chart is quite different from everyone else's.

Since your code is working, I would suggest that you stay to it as much as you can, and think in terms of "logic":

Code:
  if (switch1 is pressed) { do something}
  if ((switch 2 is pressed) and (switch 1 is not pressed)) {do something}
  while (switch 3 is pressed) {do something repeatedly}
  do {something repeatedly} while (switch 4 is pressed}
Or whatever you want to do.
 

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
I think your idea of flow chart is quite different from everyone else's.

Since your code is working, I would suggest that you stay to it as much as you can, and think in terms of "logic":

Code:
  if (switch1 is pressed) { do something}
  if ((switch 2 is pressed) and (switch 1 is not pressed)) {do something}
  while (switch 3 is pressed) {do something repeatedly}
  do {something repeatedly} while (switch 4 is pressed}
Or whatever you want to do.
Yes
if (switch1 is pressed) { do the function which is defined to that switch only}
if ((switch 2 is pressed) and (switch 1 is not pressed)) {do the function which is defined to that switch only}
while (switch 3 is pressed) {do the function which is defined to that switch only but no repeatedly}
But when switch 4 press do {do the function repeatedly which is defined to that switch only} while (switch 4 is pressed}
 
Top