Thread: How to work with simple array ( MIKROC compiler for uC )

  1. #1
    Registered User THEp's Avatar
    Join Date
    Mar 2014
    Posts
    5

    Post How to work with simple array ( MIKROC compiler for uC )

    Hi all on this nice board .

    I am a beginner on micro-controller programming , and since the type am working with uses basic C# language i though it would be a nice idea to hit the pros here on the cboard forum .

    I have a hard time using this program witch consists of controlling the lightning of 4 leds with the press of a button , so when a button is pressed first led goes on and with the next press its the turn of the second one to light up .

    Unfortunatly the program compiles very well but fails when i simulate the uC on ISIS , am by the way using the PIC16F84A , and using porta.b0 as input for the button , portb.b0 to portb.b3 as outputs for the 4 leds .

    Here is the program script :

    Code:
    #define start porta.b0
    #define led1  portb.b0
    #define led2  portb.b1
    #define led3  portb.b2
    #define led4  portb.b3
    
    
    int Leds[6]= {0x00,0x01,0x02,0x04,0x08};
    int i=0 ;
    
    
    void main() {
    
    
    porta=0x00;
    portb=0x00;
    trisa=0xff;
    trisb=0x00;
    i=1;
    
    
    if( start==1){
    
    
    delay_ms(200);
    portb= Leds[i];
    i++;
    delay_ms(500);
     if(i==6){
     i=0;
     }
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if( start==1)
    Unless the button is down at the start, you only get one shot at this.
    Try using a while loop.

    Most embedded programs want a main which looks like this.
    Code:
    while ( 1 ) {
      // do everything, forever
    }
    Code presentation is important, both for you understanding what is going on, and getting help from other people.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User THEp's Avatar
    Join Date
    Mar 2014
    Posts
    5

    Post

    Hi salem , i have already found the problem , it was a missing loop .

    Here is the new code :

    Code:
    #define start porta.b0
    #define led1  portb.b0
    #define led2  portb.b1
    #define led3  portb.b2
    #define led4  portb.b3
    
    
    int Leds[6]= {0x00,0x01,0x02,0x04,0x08};
    int i=0 ;
    
    
    void main() {
    
    
    porta=0x00;
    portb=0x00;
    trisa=0xff;
    trisb=0x00;
    i=0;
    
    
    while(1) {
    if( start==1){
    delay_ms(200);
    i++;
    portb= Leds[i];
    delay_ms(500);
    }
    if(i==4){
    i=0;
    }
    }
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What about the indentation?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User THEp's Avatar
    Join Date
    Mar 2014
    Posts
    5
    Quote Originally Posted by Salem View Post
    What about the indentation?
    Ah , will take care of that of course .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can not get MingW compiler to work
    By compiler77 in forum C Programming
    Replies: 15
    Last Post: 08-05-2012, 09:39 AM
  2. Compiler/Linker work around
    By erry in forum C Programming
    Replies: 4
    Last Post: 06-23-2010, 10:34 PM
  3. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  4. help with making Borland C++ compiler work
    By Masada in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2003, 11:02 AM
  5. Replies: 17
    Last Post: 09-28-2002, 07:35 AM

Tags for this Thread