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.

[SOLVED] Help me out with the following PIC code MikroC

Status
Not open for further replies.

Nuwan Tharaka

Newbie level 4
Joined
Nov 23, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Sri Lanka
Activity points
57
I am a beginner to Pic and wrote this code for Pic16f877 using mikroc. I want to know the error,this was written for led chaser forward and reverse.This code won't simulate in proteus.Please look into this!


Code:
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);
    }
 
Last edited by a moderator:

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,417
Helped
14,251
Reputation
28,763
Reaction score
12,940
Trophy points
1,393
Location
Bochum, Germany
Activity points
280,184
The code makses no sense, the braces are misplaced or unmatched. It won't compile at all.

Please check.
 

hexreader

Advanced Member level 2
Joined
Apr 7, 2011
Messages
537
Helped
194
Reputation
392
Reaction score
196
Trophy points
1,323
Location
England
Activity points
4,828
The code makses no sense
Agreed :)

the braces are misplaced or unmatched. It won't compile at all.
Amazingly, the code does compile, even though it does nothing of any use.

Here is the same code formatted a bit better. Hopefully it will become obvious that the code serves no purpose.

Code:
// some garbage I found on the internet
// for PIC16F877 and MikroC compiler

// a poor attempt at function prototypes
run();
reverse();

// set PORTB as all outputs and do absolutely nothing else !!!
void main(){
   TRISB=0;                            // PORTB all output
}


// no idea what this function is for - but it is not called - so no problem
// while(1) ensures that it would never return anyway
run(){
   PORTB=1;
   delay_ms(100);
   do{
       PORTB = PORTB << 1;          // no idea why ??
       if(PORTB.f4 >= 1){           // no idea why ??
           delay_ms(100);           // no idea why ??
           reverse();               // no idea why ??
       }
   }
   while(1);                  // loop forever - but I don't know why
}

// no idea what this function is for - but it is not called - so no problem
reverse(){
   PORTB = 0b00010000;
   delay_ms(100);
   do{
       PORTB = PORTB >> 1;         // no idea why ??
       if(PORTB.f0 <= 1){            // no idea why ??
           delay_ms(100);          // no idea why ??
           main();                 // calling main is NOT a valid thing to do
       }
   }
   while(1);                       // loop forever - but I don't know why
}

@Nuwan Thuraka,
Instead of guessing at how to code in MikroC, try examining the links in this post:

https://www.mikroe.com/forum/viewtopic.php?f=147&t=55027

I have not followed the links, but I would guess that somebody already worked out how to do it ;-)

Hope the link helps....

... oh and please.... format your code properly to make it readable and provide comments to help you and others when reading the code
 

Nuwan Tharaka

Newbie level 4
Joined
Nov 23, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Sri Lanka
Activity points
57
@hexreader
thanks for your advice!
Code:
run(); // function declare
reverse();
void main()
{
   trisb=0; // portb all outputs
   }
   run() // call run function
   {
   portb=1;// LSB of port b is high
   delay_ms(100); // wait a while
   do{ // do
   portb=portb<<1; // shift LSB of portb by one
   if(portb.f4>=1) // if 4th bit is 1,
   {
   delay_ms(100);// then wait a while and
   reverse();  // call function reverse,
    }
   }
   while(1);// endless loop
   }
   reverse() // reverse function
   {
   portb=0b00010000; // 4th bit is hight
   delay_ms(100);   // wait  a while
   do{   // do,
   portb=portb>>1; //shift 4th bit of portb by one to the right 
   if(portb.f0<=1) // if LSB is high
   {
   delay_ms(100); // wait a while

   main(); // call main
   }
   }
   while(1); // endless loop
    }
 

hexreader

Advanced Member level 2
Joined
Apr 7, 2011
Messages
537
Helped
194
Reputation
392
Reaction score
196
Trophy points
1,323
Location
England
Activity points
4,828
Looks like Ian Rogers has tidied up your code for you on the other forum
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,417
Helped
14,251
Reputation
28,763
Reaction score
12,940
Trophy points
1,393
Location
Bochum, Germany
Activity points
280,184
Unfortunately the comment is misleading, no action performing functions are called at all.
Execution stops after clearing TRISB.

I feel a lack of basic C coding concepts:

- program structure
- declaring functions
- calling functions
 

Nuwan Tharaka

Newbie level 4
Joined
Nov 23, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Sri Lanka
Activity points
57
@FvM & hexreader

I've been trying to do the same task by another method,it doesnt work either!whats the wrong here??
Code:
unsigned int i;
void main() {
trisb=0;   // portb all outputs
portb=0;
while(1)   // loop
{
for(i=1;i<=128;i*2) // leds forward
 {
 portb=i;
 delay_ms(100);
 }
 for(i=128;i>=1;i/2)  // leds reverse
 {
 portb=i;
delay_ms(100);
 }
}



}

Picture2.jpg
 

Nuwan Tharaka

Newbie level 4
Joined
Nov 23, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Sri Lanka
Activity points
57
Fvm
thank you! thank you! thank you!!
:D :D
finally it worked !!
thank you very much guys all of you!1=
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top