How to work with arrays ( Mikroc )

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Hi all of you :) .



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 and so on in a loop .

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 :

Rich (BB 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++;
delay_ms(500);
 if(i==6){
 i=0;
 }
}
}

 

DumboFixer

Joined Feb 10, 2009
217
There is no loop in your code so the start button will only get checked once before coming to the end of your program. It will then probably continue executing the rest of the code memory before coming back round to your code again. Enclosing the main part of your code in a loop should improve things. I've not compiled this code but it should be ok :)

Rich (BB code):
i=1;
 
do
{
     if( start==1){
          delay_ms(200);
          portb= Leds;
          I++;
          delay_ms(500);
          if(i==6){
               i=0;
          }
     }
} while (1);


The problem your code has at the moment is that if you keep the button pressed the incrementing will happen repeatedly.

Try changing the button check so that it only responds to a leading edge of the button push. The 200ms and 500ms delays should mean that there's no switch bounce problem.
 
Top