Little problem with my 1st PIC code..Please Help!!!

Discussion in 'Embedded Systems and Microcontrollers' started by Eric007, Aug 28, 2011.

  1. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    Hi All!

    I wrote my very 1st code with PIC16F690, built my circuit on breadbord and programmed it with Pickit3!

    Here's what it is intended to do:

    - initially all LEDs off
    - On 1st press: LEDs flashes in this way:
    LED1, LED2 flash every 1 second
    LED3, LED4 flash every 2 second
    - on 2nd press: all LEDs are turned off
    - On 3r press: flash LEDs again as above
    And so on

    I have connected my pushbutton as pullup configuration with a 10 k resistor...

    Here's how my code behave:
    - initially on LEDs are off
    - on first press: all LEDs flashes as required
    And that's it!!!:(
    On the 2nd press, the LEDs keep flashing instead of being turned off
    Basically, only the firt press work, all the other press does not change anything...

    I didn't include all my code but the necessary part for you to help me...it has my pins configuration, constants and variable definition and the main code

    Please have a look at my attachement!

    And please help!!!
     
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,674
    2,724
    For starters, 'push' is undefined...and you have something called 'button' that is defined but not used.
     
  3. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    Ok I noticed that I fixed that...coz I remember I got a message about it when building it...and I corrected it...just assume push is button! I replaced all the push with button in the constant section...ima check it out again...

    Anything else wrong?
     
  4. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    ****!! U are right...lemme correct it and see how it behaves and I'll get back to you...I thought I corrected it!
     
  5. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    I changed BUT still behaving the same way...

    Have a look at the change in the attachement!!

    Why are the LEDs not off on second press? Its like the program recognise only the 1st press

    Please help!!!
     
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,674
    2,724
    Have you tried holding the button down for longer than 2.5 seconds?

    Couple of notes:

    the 'goto flashLEDs' prior to the flashLEDs label is superfluous...though it won't hurt anything...likewise, the 'goto turnLEDsoff' prior to the 'turnLEDsoff' label. I only mention this as extraneous code makes things difficult to read.

    Also, the 'clrf LEDPORT' after 'turnLEDsoff' doesn't do anything...your program logic already insures the port is clear by the time it reaches that point in the code.

    Aside from 2.5 seconds between testing the button during the LED flashing phase, I cannot find anything that pops out at me. Have you tried MPLAB SIM to step through the code?
     
  7. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,674
    2,724
    BTW, you're LEDs will cycle off 1/2 second prior to reading the push button, and the enter the 'turnLEDsoff' code. Then, 1/2 second later, if the push button is still down, it will jump back to the flashLEDs.

    So, your code may be working, *BUT* you need to hold your button down for exactly the right amount of time and at the right time for the leds to actually stop cycling.

    IOW, this is pretty bad code! But this is how we all learn, I guess. Reconsider your pushbutton logic. You not only want to advance on a press, but you also don't want to advance on a press without having released it first.
     
  8. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    Can u please edit my code the way u think it should have been...maybe I'll have a clear picture of what u saying...

    I'll try again reconsidering what u mention...also if I understand u well u want to press n hold it down for 2.5 second...
     
  9. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    @joeyd999: you are absolutely right!!! Its all about pressing the button for the right amount of time and at the right time!!!

    Thanks so much...

    But well I'm still tryna improve it but it works...
    Any advice on the improvement?
     
  10. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    @joeyd999: you are absolutely right!!! Its all about pressing the button for the right amount of time and at the right time!!!

    Thanks so much...

    But well I'm still tryna improve it but it works...
    Any advice on the improvement?
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I guess you MPLAB. How are your debugging skills. They are as important as any programming skills. And a important tool to. That you better learn.
    Take a look here http://forum.allaboutcircuits.com/showthread.php?t=44852
     
    Eric007 likes this.
  12. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,674
    2,724
    Since you asked, I will try to cobble something together that shows how I would do it...gimme some time...I'll post later.
     
  13. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    My debbuging skill...NOT GOOD!!! Still have to improve...

    Thx!
     
  14. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    @joeyd999: OK! I can't wait to see that...if u need my delay routine I can give it to u...
    I'm also working on how to turn LEDs off when pressing and holding for less than 2.5 seconds...

    I know I can use timers and interrups but that will require lots of reading and I don't really have time...

    Thx again
     
  15. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    I see lots of progress. good job.

    To make the button presses more responsive, move the check button press routine into a subroutine/procedure. Calling the sub prior to the delay subs, or better yet, at the top of each delay routine. That would get the response down to a half second. To gain further response, shorten up the delays to say 0.1 sec and loop thru them 5 times in Main. Now you are down to a much smaller response window.
     
    Eric007 likes this.
  16. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    @nickelflipper: u are absolutely right and that's exactly the solution I had in mind in order to make the push button response much quicker...by creating a subroutine and call it...I'm happy that I'm starting to think well...

    Lemme implement that first before trying with the 0.1 second...

    Thx all you guyz are opening up my mind with PIC programming...
     
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Take your time learn. As I understand you make progress. And that is all that counts
     
  18. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    @t06afre: you are right...and I'm willing to take what it takes (time) to learn and become a senior member and be able to help others with whatever problem they do regarding PIC programing...

    Thx
     
  19. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    Hey guyz!

    I improved my design and the responsive time of the pushbutton is much better (0.5 second)...Also notice how I changed my turnLEDsoff ...

    It now works like a charm but still wana improve the response time to 0.1 second...

    Please have a look at my attachement

    Thx guyz!
     
  20. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I checked your code and it's quite neat and well written. :)

    To give the best response time, you can follow this sequence;
    1. if button pressed, instantly perform the event
    2. then wait for button released as a debounce (after the event)

    In psuedo code that looks a bit like this;

    if button1 pressed, call LEDS_on1
    call debounce

    if button2 pressed, call LEDS_on2
    call debounce

    Then debounce is a function that simply waits for ALL buttons to be released for (say) 20mS. Something like this;

    *debounce*
    delay 1mS
    count = count+1
    if ANY buttons pressed count=0
    if count > 20 return
    else repeat

    That whole setup gives you instant response for LEDs tuning on, and needs minimum 20mS between button presses, so the whole system will be very fast response overall.
     
    Eric007 likes this.
Loading...