Help me with the code!

Status
Not open for further replies.

Nuwan Tharaka

New Member
C:
run();
reverse();
void main()
{
   trisb=0;
   }
   run()
   {
   portb=1;
   delay_ms(100);
   do{
   portb=portb<<1;
   if(portb.f4>=1)
   {
   delay_ms(100);
   reverse();
    }
   }
   while(1);
   }
   reverse()
   {
   portb=0b00010000;
   delay_ms(100);
   do{
   portb=portb>>1;
   if(portb.f0<=1)
   {
   delay_ms(100);
   main();
   }
   }
   while(1);
    }

this code was written with mikro c for led chaser to light up leds one by one in forward direction and then backwards using pic16f877.it compiles well in the compiler but when i simulated it in proteus the leds are not lit as expected,random leds are lit instead.so what could be the reason for that?please someone explain me the mistakes in it.
 
Last edited by a moderator:
Ok, couple of things.

1) There are code tags for a reason. Surround your code with code tags to keep the formatting. It's very difficult to read without the proper spacing:



2) You need to tell us what problems you're running into. If something isn't correct, the compiler will tell you where the error is, and generally tell you what it is. What does it say when you compile it?

3) You need to tell us what you've already tried to fix the problem.

4) You need to tell us exactly what you expect it to do. There are many types of LED chasers, so you need to be more specific.

If you expect help, you need to give us more information first. Help us help you.

Regards,
Matt
 
There is no way that c code compiled... One of the quirks of mikroC is that a clean build and a non build look very similar.

The main is completely screwed up!!

It should look more like this
C:
void run();
void reverse();

void main(){
     trisb=0;
   while(1){
     run();
     reverse();
     }
}
   
void run(){
   portb=1;
   while(!portb == 0x10){
     portb=portb<<1;
     delay_ms(100);
   }
}

void reverse(){
   portb=0b00010000;
   while(!portb == 0x1){
     portb=portb>>1;
     delay_ms(100);
   }
  }
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…