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.

Once the key pressed the while loop is executed even without pressing again

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,580
Helped
122
Reputation
244
Reaction score
114
Trophy points
1,353
Location
India
Activity points
10,382
I have a program to ON and OFF a device , the following program [MPLab Pic16f877a controller Simulation on Proteus)


string("Press to ON ");
__delay_ms(500);
while(PORTDbits.RD7==0);
while(PORTDbits.RD7==1)
{
PORTDbits.RD7==0; // flush again the press

// working code
}

First it will operate normally ,and the second iteration the while loop excuted without pressing my RD7 switch .

what is the reason ?
 

1.jpg



Port Setup
void main(void)
{ start:

TRISD7 = 1; // A as input
TRISD5 = 1; // A as input
TRISD6 = 0;
UD[0]=65,UD[1]=66,UD[2]=67 , UD[3]=89; //Data Packet ABC Y
//OPTION_REG &= 0x7F;
TRISB =0x00;
TRISD = 10100000;
__delay_ms(100);
lcd_init();
TRISC6=0; //Output (TX)
TRISC7=1; //Input (RX)

unsigned char data = 0x31;
unsigned int a = 0x01;
unsigned char ch;
ser_int();
Enlarged Image
 

Attachments

  • 1.jpg
    1.jpg
    181.3 KB · Views: 80

First it will operate normally ,and the second iteration the while loop excuted without pressing my RD7 switch .
We don't see how second iteration can occur because the overall code isn't shown. After button release, code execution continuous below the loop statement.
 

string("Press to ON ");
__delay_ms(500);
while(PORTDbits.RD7==0);
while(PORTDbits.RD7==1)
{
PORTDbits.RD7==0; // changed it to PORTDbits.RD7 = 0; // Typing mistake changed to assignment operator now working

// working code
}

PORTDbits.RD7 = 0; // Typing mistake changed to assignment operator now working
but why the switch is not go to low when realse the button ?
 

Hello,

TRISD7 = 1; // A as input
TRISD5 = 1; // A as input
TRISD6 = 0;
.
.
TRISD = 10100000;

The last line is sufficient and more compact

PORTDbits.RD7==0; // changed it to PORTDbits.RD7 = 0

The == is a very big error, and if RD7 is a simple input port bit without special useage (interrupt or else) this write command has no sense
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top