Help me correct my LED blinking problem

Status
Not open for further replies.

nandini88

Junior Member level 2
Joined
Nov 9, 2011
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,439
why the LED keep blinking even thought i not press any button. pls anybody correct the code for me. thank you

Code:
unsigned short  i;
void main()
{

TRISB= 0xFF;
PORTB=0x00;
TRISD = 0x00; // PORTB as ouput
PORTD = 0x00; // Set port output as all 0's

while(1){
if(TRISB.F0==1) {  // Detect logical one to zero
for(i=0;i <50; i++){ //repeat 50 times
    PORTD.F0 = 0;   //switch on LED at PORTB pin 0
    delay_ms(150);  //short delay
    PORTD.F0 = 1;   //switch off LED
    delay_ms(150);
    }
 }

if(TRISB.F1==1) {  // Detect logical one to zero
for(i=0;i < 100; i++){ //repeat 100 times
    PORTD.F0 = 0;   //switch on LED at PORTB pin 0
    delay_ms(150);  //short delay
    PORTD.F0 = 1;   //switch off LED
    delay_ms(150);
    }
 }

if(TRISB.F2==1) {  // Detect logical one to zero
  for(i=0; i<1000; i++){ //repeat 1000 times
    PORTD.F0 = 0;   //switch on LED at PORTB pin 0
    delay_ms(150);  //short delay
    PORTD.F0 = 1;   //switch off LED
    delay_ms(150);
    }
 }
}
}
 
Last edited:

Re: led blinking problem

What does TRISB.F1==1 do? I don't understand that part of the code. Not familiar with this code.
I think you must write if(PORTB.F1 == 1) not TRISB and so the others.
 
Re: led blinking problem

You are testing TRIS which sets the port direction instead of testing the PORT.

Keith
 
Re: led blinking problem

thank alot for ur kind help. i know my mistake d.

---------- Post added at 10:09 ---------- Previous post was at 10:07 ----------

But it still same. keep blinking once supply is on. pls
 

Re: led blinking problem

What do you buttons do and where are they connected and how? You are detecting a 1 but buttons are often connected to 0V so you detect a zero.

Keith
 
Re: led blinking problem

have you changed it also to F2 and F0?
can you post your circuit schematic?
 
Re: led blinking problem

here i attached the schematic. if anything wrong pls correct it. im new learner.



thank you.
 

Re: led blinking problem

The problem is that your buttons have pullups so are always logic 1. Change you software to detect a zero on the button inputs instead of 1.

Keith
 
Re: led blinking problem

how to change. i not get u. pls... where i should change?

---------- Post added at 13:18 ---------- Previous post was at 13:12 ----------

ok ok. i got what u mean by. i changed d. now functioning well. thanks alot

---------- Post added at 14:26 ---------- Previous post was at 13:18 ----------

i having one more problem. now i want to count the number of led blinking by connecting it to optocoupler. so i think it involve interrupt code. so can u help me regarding to interrupt? every 1000 led blinking the count value have to increase by one and display the number of count in lcd.

thank you
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…