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.

[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:

The code makses no sense, the braces are misplaced or unmatched. It won't compile at all.

Please check.
 
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
 
@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
    }
 

Looks like Ian Rogers has tidied up your code for you on the other forum
 

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
 
@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
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top