Trying to make a program in MIKROC

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Hello everyone here .

The program is like this :
Using a microcontroller (pic16f84a) , 4 leds on port b , and one button on port a .

when i press the 1st time , the led1 lights up , when i push 2nd time , led2 lights up and the first shuts down , and so on for the 4th one , after that when i press the button everything is off , and the cycle starts again .

This is my vision of the program , but unfortunately it didn't work out :
Please share your ideas .



Rich (BB code):
#define start porta.b0
#define led1  portb.b0
#define led2  portb.b1
#define led3  portb.b2
#define led4  portb.b3
 
int x0,x1,x2,x3;

void main() {
porta=0x00;
portb=0xff;
trisa=0xff;
trisb=0x00;
x0=1;x1=0;x2=0;x3=0;

while(1){


if((start==1)&&(x0==1)){
x0=0;
x1=1;
led1=0;

}

if((start==1)&&(x1==1)){
x1=0;
x2=1;
led1=1;
led2=0;

}

if((start==1)&&(x2==1)){
x2=0;
x3=1;
led2=1;
led3=0;

}

if((start==1)&&(x3==1)){
x3=0;
x0=1;
led3=1;
led4=0;

}
}
}
 
Last edited:

t06afre

Joined May 11, 2009
5,934
MikroC do have a software debugger? In cases like this, the software debugger is one of your best friends. And you should learn how to use it, like do single-stepping, setting breakpoints, adding watches, and stimulus. Will work much faster than this forum also. But of course this forum will always be open for your question if you struggle
 

ErnieM

Joined Apr 24, 2011
8,377
... but unfortunately it didn't work out
From the overabundance of fault data provided it is an inescapable conclusion that the code is broken.

You may also want to look up the concept of switch debouncing, as this program is a prime example of where one is required.
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Actually i need to read some c books to get better .
But for now i really need to get this done before monday .

Just give some hints and i will try to get this on the road .
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
From the overabundance of fault data provided it is an inescapable conclusion that the code is broken.

You may also want to look up the concept of switch debouncing, as this program is a prime example of where one is required.
Thanks fr this info .
As far as i know , w have only studied how to use debounce witch i guess is ' Anti rebond ' in french ? using a SR latch , to perform a perfect transition from 1 to 0 .

But how should i use this debouncing in the program ?
 

t06afre

Joined May 11, 2009
5,934
The concept of switch debouncing, is not required in the perfect world of the Proteus simulator, which i guess you have been used to test your program. However in the real world. It is a very important factor. Causing a lot of problems then not handled correct
 

THE_RB

Joined Feb 11, 2008
5,438
The MikroC inbuilt libraries have a button() function which performs some debouncing, it is fine for such a simple button sequence task.

Check in the compiler help file. :)
 

takao21203

Joined Apr 28, 2012
3,702
Can i do it using delays or case switchs or variables ????
:) I'd say you need to use variables no matter what.

Yes you can use delays but it is not good. Only beginners use delays for keys in their first programs.

If you don't debounce then at best, you get a row of reactions when you press the button just once.
 

t06afre

Joined May 11, 2009
5,934
@LETTITROLL!
As long as you use the ISIS Proteus simulator to test run your code. Switch debouncing is not something you have to worry about. Your problem is in the code. Therfor use your time on learning how to use the MikroC debugger instead.
 

takao21203

Joined Apr 28, 2012
3,702
I have looked at the code, while the idea is there, the issue here is wishful thinking.

The PIC does not have any idea how much delay you want. So at best the LEDs would light up very very fast.

Also, you don't test for the button to become released at all.

This is the process to learn programming- to transform a human language into a programming language and in the end, machine language.

C language often has many different ways to solve it, and assembler allows a lot of creativity, but they are unforgiving.

They are binary languages, if there is a tiny bit wrong, everything can fail and the chip does not bother.

While humans, if you talk to someone and it is expressed a little bit wrongly, you are still able to understand.

It is helpful to use the single step debugger and really observe what each line is doing. Later you can write more complex expression or even more than one into each line.

Don't worry, even after years of practice there is still the phenomena of wishful thinking- when you program some tricky idea.

It just takes much less time to correct it and you'll have some routine to know where to look for issues and what to check.

Experienced programmers often write seperate test programs to try out some idea (the main program might be to messy for debugging).
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
You should learn how to use the debugging tools available to you. If you do not have any hardware debugger. Use the software simulator. http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/avr/help/software_simulator.htm If it is book worth reading it must be the manual
Ok lets try to make this simple idea on a program by C language :

1 button , two leds
- first press , first led lights up .
- second press , second led lights up .
- third press everything is cleared .
and so on .
What are the possibilities to make this , without using the timer only with the debounce technic ( without the button library only using the delays )and single variable
 

t06afre

Joined May 11, 2009
5,934
Ok lets try to make this simple idea on a program by C language :

1 button , two leds
- first press , first led lights up .
- second press , second led lights up .
- third press everything is cleared .
and so on .
What are the possibilities to make this , without using the timer only with the debounce technic ( without the button library only using the delays )and single variable
You have some code written. And it compiles. That is good. But still the program might not work as you want it. And that is then the debugger comes in. Why are you so reluctant using it ;) As suggested try to detect the releasing of the button. Just add a simple while statement in in state. Something like this
Rich (BB code):
}

if((start==1)&&(x1==1)){
x1=0;
x2=1;
led1=1;
led2=0;
while(start==1); /*Do nothing until button released*/
}
Also then working with micros. The basic data type for variables like x1 is signed or unsigned char. As the char data type only take one byte. Int is a two byte variable. Not that using the int type is causing your problems
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Agreed , i should put char : memory savings .

So as far as i understand , the bouncing causes the leds to light up at random times while am bushing the button , its like am pushing the button too many times during that first press .

So according to you i should put :

while(start==1){
portb=0xff; // all leds are off during that the press
}


??
 

t06afre

Joined May 11, 2009
5,934
The human eye is quite slow. So if the blink is very short you will not see it at all. Using something like this may cause a blink that is to short so the LEDs will appear to off all the time
Rich (BB code):
while(start==1){
portb=0xff; // all leds are off during that the press 
}
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
OK i will think more about this , and i hope ill find a solution .

I will get back to you later , i appreciate your time
 
Top