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.

[help] how to solve the while loop problem ? ccs compiler . PIC16f877A.

Status
Not open for further replies.

sfchew7

Junior Member level 2
Joined
Sep 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,423
Can anyone tell me how to solve the while loop problem? I dont want the printF keep looping. if I remove the while(1) then the LED will no longer turn on. Is that anyway I can make this code to work when the LED turns on and will show the message ("available")only once and turn off will show the message ("Not available") once. thanks I am using PIC16F877A and ccs compiler.

#include "16f877a.h"
#fuses HS,PROTECT,NoWDT,put,brownout
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

void main()
{
while(1)
{
if(input(pin_a0)==1)
{
output_low(pin_b0);
printf("Not available");
}
else
{
output_high(pin_b0);
printf("Available");
}


if(input(pin_a1)==1)
{
output_low(pin_b1);
printf("Not available");
}
else
{
output_high(pin_b1);
printf("Available");
}

}
}
 

delay a while after printf, then clear it
 

I'm not sure what you are trying to do, your code is currently reflecting the input pin state, when it changes the output changes too.

Do you want to have a toggle effect so that once it is enabled it doesn't turn off, you can use a flag variable for that.
If you just want for the output to stay activated for a while you can use a delay.

Alex

---------- Post added at 15:56 ---------- Previous post was at 15:38 ----------

delay a while after printf, then clear it

I woudn't use a delay this way because this would freeze the execution and the second condition would not be evaluated during that.
I would use a variable counter as delay


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void main()
{
    unsigned int d1=0,d2=0;
 
    while(1)
    {
        if (d1==0) {  // only evaluate the following condition when 0
            if (input(pin_a0)==1)
            {
                output_low(pin_b0);
                printf("Not available");
            }
            else
            {
                output_high(pin_b0);
                printf("Available");
                d1=10000;  // set the value to start delay
            }
        }
        else d1--;
 
        if (d2==0) {
            if(input(pin_a1)==1)
            {
                output_low(pin_b1);
                printf("Not available");
            }
            else
            {
                output_high(pin_b1);
                printf("Available");
                d2=10000;
            }
        }
        else d2--;
 
    }
}



Alex
 

If you mean to make your code act on input changes rather that input states, you have to write something that can detect input changes.

e.g.
Code:
int1 pin_a1_prev;
...
if (input(pin_a1) != pin_a1_prev)
{
  // handle input change
  // ..
  pin_a1_prev = input(pin_a1);
}
 

OKAY. THANKS FOR THE HELP. I go to try now.:p
 

I'm not sure what you are trying to do, your code is currently reflecting the input pin state, when it changes the output changes too.

Do you want to have a toggle effect so that once it is enabled it doesn't turn off, you can use a flag variable for that.
If you just want for the output to stay activated for a while you can use a delay.

Alex

---------- Post added at 15:56 ---------- Previous post was at 15:38 ----------



I woudn't use a delay this way because this would freeze the execution and the second condition would not be evaluated during that.
I would use a variable counter as delay


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void main()
{
    unsigned int d1=0,d2=0;
 
    while(1)
    {
        if (d1==0) {  // only evaluate the following condition when 0
            if (input(pin_a0)==1)
            {
                output_low(pin_b0);
                printf("Not available");
            }
            else
            {
                output_high(pin_b0);
                printf("Available");
                d1=10000;  // set the value to start delay
            }
        }
        else d1--;
 
        if (d2==0) {
            if(input(pin_a1)==1)
            {
                output_low(pin_b1);
                printf("Not available");
            }
            else
            {
                output_high(pin_b1);
                printf("Available");
                d2=10000;
            }
        }
        else d2--;
 
    }
}



Alex








I have edited the code to this way and I used PROTEUS 7.5 TO RUN THE SIMULATION.
THE SIMULATION LCD RESULT CANNOT SHOW WHAT THE RESULT THAT I WANT. THERE ARE SOME UNWANTED CHARACTERS APPEAR.
IT IS MY CODE HAS PROBLEM ??
HERE IS MY CODE

Code:
#include "16f877a.h"
    #fuses HS,PROTECT,NoWDT,put,brownout
    #use delay(clock=4000000)
    #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include "lcd.c"

    int1 fiA0=0; // Flag
    int1 fiA1=0;

    void main()
    {
lcd_init();
lcd_gotoxy(1,1);

       while(1)
       {

          if(fiA0!=input(pin_a0)) // Test if Flag is change status
          {
             fiA0=input(pin_a0); // lock Flag
             if(input(pin_a0))
             {
                output_low(pin_b0);
                printf("No1\n\r");

				lcd_putc("\fone");

             }
             else
             {
                output_high(pin_b0);
                printf("yes1\n\r");
			
				lcd_putc("\ftwo");
             }
          }      
          
          if(fiA1!=input(pin_a1)) // Test Flag
          {
             fiA1=input(pin_a1); // lock Flag
             if(input(pin_a1)==1)
             {
                output_low(pin_b1);
                printf("No2\n\r");

				lcd_putc("\f\nthree");
             }
             else
             {
                output_high(pin_b1);
                printf("yes2\n\r");
	
				lcd_putc("\f\nfour");

	             }
          }
       }
    }


THANKS!
 
Last edited by a moderator:

Did you set the Proteus PIC model oscillator accordingly? as well as the baud rate for the virtual terminal?
 

Post your terminal output showing the characters in question in relation to the expected output.

Also, what is the purpose of using the Form Feed (\f) escape sequence?

BigDog
 

Note that if your input is from a button then your code may execute multiple times because you haven't included any delay to apply debounce (unless you have a hardware debounce) but for simulation in proteus this will not be a problem.

Alex
 

problem.JPG
here is the picture.
I am using /f is because the characters will overlap if without "/f". is it true?
thanks
 

Note that if your input is from a button then your code may execute multiple times because you haven't included any delay to apply debounce (unless you have a hardware debounce) but for simulation in proteus this will not be a problem.

Alex

actually I have replaced the LDR(light dependent resistors) with the switches because the LDRs didn't work in PROTEUS 7.5.I don't know what caused the LDR failed to work.thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top