Trying to make a program in MIKROC

Discussion in 'Embedded Systems and Microcontrollers' started by LETITROLL, Nov 28, 2013.

  1. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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 .

    [​IMG]

    Code ( (Unknown Language)):
    1.  
    2. #define start porta.b0
    3. #define led1  portb.b0
    4. #define led2  portb.b1
    5. #define led3  portb.b2
    6. #define led4  portb.b3
    7.  
    8. int x0,x1,x2,x3;
    9.  
    10. void main() {
    11. porta=0x00;
    12. portb=0xff;
    13. trisa=0xff;
    14. trisb=0x00;
    15. x0=1;x1=0;x2=0;x3=0;
    16.  
    17. while(1){
    18.  
    19.  
    20. if((start==1)&&(x0==1)){
    21. x0=0;
    22. x1=1;
    23. led1=0;
    24.  
    25. }
    26.  
    27. if((start==1)&&(x1==1)){
    28. x1=0;
    29. x2=1;
    30. led1=1;
    31. led2=0;
    32.  
    33. }
    34.  
    35. if((start==1)&&(x2==1)){
    36. x2=0;
    37. x3=1;
    38. led2=1;
    39. led3=0;
    40.  
    41. }
    42.  
    43. if((start==1)&&(x3==1)){
    44. x3=0;
    45. x0=1;
    46. led3=1;
    47. led4=0;
    48.  
    49. }
    50. }
    51. }
    52.  
    53.  
     
    Last edited: Nov 28, 2013
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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.
     
  4. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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 .
     
  5. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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 ?
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  7. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    Can i use an SR latch as a debounce for this circuit ??
     
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
  9. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    It is not difficult to do software debouncing. But you need a timer interrupt.
     
  10. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    Can i do it using delays or case switchs or variables ????
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    :) 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.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    @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.
     
  13. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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).
     
  14. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    So how to transform wishful thinking into a program ??? any books i should read ??
     
    Last edited: Nov 29, 2013
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  16. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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
     
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
    Code ( (Unknown Language)):
    1. }
    2.  
    3. if((start==1)&&(x1==1)){
    4. x1=0;
    5. x2=1;
    6. led1=1;
    7. led2=0;
    8. while(start==1); /*Do nothing until button released*/
    9. }
    10.  
    11.  
    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
     
  18. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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
    }


    ??
     
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
    Code ( (Unknown Language)):
    1. while(start==1){
    2. portb=0xff; // all leds are off during that the press
    3. }
     
  20. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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
     
Loading...