Is there a wise way to debug a large code?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,123

    Default Is there a wise way to debug a large code?

    Hello.
    My deluxe nixie clock V2.0 project is almost complete, PCBs assembled and tested, enclosures made and software written, however, I'm trying to debug a nasty bug with no success. The issue is as follows, I have option for auto brightness, which gradually decreases brightness starting at 18:00, till 23:00 then keeps at low level till 06:00 and gradually increases it till 10:00. This is done via this subroutine:

    Code:
    DIMMER: 'auto dim routine
    IF Z2=06 THEN HPWM 2,150,2000
    IF Z2=08 THEN HPWM 2,200,2000
    IF Z2=10 THEN HPWM 2,255,2000
    IF Z2=18 THEN HPWM 2,200,2000
    IF Z2=19 THEN HPWM 2,150,2000
    IF Z2=20 THEN HPWM 2,100,2000
    IF Z2=21 THEN HPWM 2,20,2000
    IF Z2=23 THEN HPWM 2,10,2000
    return
    Z2 is decoded from RTC reading (DS3231) and converted to DEC with this:
    Code:
    Z2=(RTCHour >> 4)*10+RTCHour // 16 'decode hours
    But there is a strange problem. After time reaches 18:00, in each minute, clock enters setup mode! I exit setup mode, it works fine, but when minute changes, instead of doing display refresh, as it is defined in software, it goes into setup again. This happens all night long, but during the daytime, after 10 o'clock, it works all fine!

    Setup mode is called via pressing a button, which is connected to ADC:

    Code:
    if ticker>150 and ard<100 then  ticker=1: goto premakro (ticker is delay loop counter for de-bouncing and ARD is ADC input)
    So what I have done:

    Checked Z2 variable, it is "exclusive" and only used in these two code samples posted above.
    "premakro" subroutine is being called from only one place in the code and there are no other entries referring to it.

    Any ideas?
    I can post complete code, but it is over 700 lines long, so I guess no one will bother reading it...

  2. #2
    Join Date
    Feb 2013
    Posts
    1,123


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    Forgot to mention, IC is 16F887A and compiler gives no errors, except warning "crossing page boundaries, ensure bits are set".
    Compiled code is about 7k.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    If you want goto premakro to run when ticker>=150, perhaps you need to reframe that part with the else....endif

    I wouldn't mind looking over the code if you want.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,123


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    "premakro" works fine, when I press a button, it gets called.
    The issue as I see, that somehow, HPWM statement, when duty cycle is less than 100%, disrupts the code.
    I'm using hardware PWM generators at PORTC.1 for brightness control PWM, and PORTC.2 for alarm buzzer sound

  5. #5
    Join Date
    Feb 2013
    Posts
    1,123


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    I modified DIMMER subroutine and set it to always make 255 duty cycle - then nothing fails. So how to relate these things together?

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    How often is dimmer called? Every hour, every minute, every time thru the main loop?
    What if you make it always outout a dutycycle of 200, which is what it's getting set to 18.00 when the problem starts? Perhaps you have a hardware issue?

    Also, as a test try to get rid of the "one-liners" and do:
    Code:
    IF Z2=06 THEN
      HPWM 2,150,2000
    ENDIF
    
    IF Z2=08 THEN
      HPWM 2,200,2000
    ENDIF
    /Henrik.

  7. #7
    Join Date
    Feb 2013
    Posts
    1,123


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    DIMMER gets called every second. I can fix this, but issue happens each minute, not second.
    I tried inserting HPWM 1,200,2000 at random places in main loop - no issues. They only happen when that DIMMER sub is being called.
    For the one-liners, they're great way to save yourself from a lot of scrolling, I'm writing code in that way for last 40 years, so I don't think that now is the time to change.
    For the hardware issue, ADC reading is done at PORTA.0 and PWM output is at PORTC.1, so I don't think this can be a RMW issue.

    The main loop can be divided into the following sections:

    1. Get current time (read RTC)
    2. If oldminute<>currentminute, then go seganime (animate all segments at each 1 minute change)
    3. Convert hours into Z2
    4. If autodim is enabled, then goto dimmer
    5. alarm check and call subroutine
    6. 12/24 hour check and conversion subroutine
    7. Display current time routine
    8. Keyboard handling code (ADC reading and debouncing)
    9. middle dot blinking

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    Quote Originally Posted by CuriousOne View Post
    Forgot to mention, IC is 16F887A
    i got lost at this point


    THEN HPWM 2,150,2000 ?? 16F887A does not exist 16F887 has 1 ccp module

    then today


    I tried inserting HPWM 1,200,2000 at random places in main loop


    mind boggles
    Warning I'm not a teacher

  9. #9
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    Not that this would fix your issue, but you have:
    Code:
    DIMMER: 'auto dim routine
    IF Z2=06 THEN HPWM 2,150,2000
    IF Z2=08 THEN HPWM 2,200,2000
    IF Z2=10 THEN HPWM 2,255,2000
    In this case I would have used:
    Code:
    Dimmer:
    IF Z2 = 06 THEN
     HPWM 2,150,2000
    ELSEIF Z2 = 08 THEN...
    What happens if Z2 = 07? Maybe use:
    Code:
    IF Z2 <= 06 THEN
     HPWM 2,150,2000
    ELSEIF Z2 <= 08 THEN...

  10. #10
    Join Date
    Feb 2013
    Posts
    1,123


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    Well, there is no GOTO there is GOSUB, this was just a literal description of how it works.

    16F887 has two PWM modules, which can be output at 4 different channels, read the datasheet. You can adjust duty cycle individually, just frequency will be the same.

    From datasheet: - 10-bit PWM with 1, 2 or 4 output channels, programmable “dead time”, max. frequency 20 kHz.


    IF Z2=07 then nothing should happen and nothing happens

    For the cleanliness of experiment, I've built a completely new PCB today, and will check code for it at weekend.

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: Is there a wise way to debug a large code?

    16F887 has two PWM modules
    apologies so it does
    Warning I'm not a teacher

Similar Threads

  1. DEBUG DEC DatVar vs DEBUG #DatVar
    By netstranger.nz in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th March 2011, 21:26
  2. Replies: 0
    Last Post: - 18th November 2010, 18:50
  3. How does everyone debug their code?
    By HankMcSpank in forum General
    Replies: 30
    Last Post: - 18th June 2009, 06:41
  4. SERIN from a large string
    By earltyso in forum Serial
    Replies: 5
    Last Post: - 2nd May 2008, 22:23
  5. large displays
    By George in forum Off Topic
    Replies: 2
    Last Post: - 18th March 2007, 21:56

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts