(SOLVED) MPLab 8.92 or Program Weird Behavior

Thread Starter

jpanhalt

Joined Jan 18, 2008
11,087
MCU= PIC16F1829
Development: MPLab 8.92 /MPASM (Assembly)

Two slightly different versions of a program created a day apart behave differently. Call the earlier one "old" and the more recent one "new".
Code:
;First section New:
;*******************************************************************************
     call      ClrRam         ;takes care of flag0
POR   
     movlw     0x85           ;command to display LCO on IRQ/INT pin
     movwf     temp           ;include adding 5 caps x 8 pF each
     movlw     0x08           ;register number
     call      Put232
     goto      GetLCO         ;check ant freq & calibrate antenna, returns      |B0
Main
     call      ClrIF          ;call from any bank, returns                      |B0
     call      ClrScrn        ;returns                                          |B0
     StrPrint  "Listening"
     DelayCy   (100*msecs)
     movlw     0xBC
     call      Put232
     btfsc     flag0,4        ;check flag bit to clear after each event
     bra       PutNoStat
     StrPrint  "ClrStat(1)"   ;10 char
;<much more code>
Code:
;Same section Old:
;*******************************************************************************
     call      ClrRam         ;takes care of flag0
POR
     movlw     0x85           ;command to display LCO on IRQ/INT pin
     movwf     temp           ;include adding 5 caps x 8 pF each
     movlw     0x08           ;register number
     call      PutReg         ;returns in                                       |B2
     goto      GetLCO         ;check ant freq & calibrate antenna, returns      |B0
Main
     call      ClrIF          ;call from any bank, returns                      |B0
     call      ClrScrn        ;returns                                          |B0
     StrPrint  "Listening"
     DelayCy   (100*msecs)
     movlw     0xBC
     call      Put232
     btfsc     flag0,4        ;check flag bit to clear after each event
     bra       PutNoStat
     StrPrint  "ClrStat(1)"   ;10 char
Configuration settings:
Code:
    __CONFIG _CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
;__0x09E2
    __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_ON &  _STVREN_OFF & _BORV_19 & _LVP_OFF
;__0x1DFF
This code controls an AMS AS3935 lightning detector that has been mentioned here briefly before. First off, I see no difference between those two sections of code.

Function: On POR, register 0x08 is set to output a tuning frequency on the INT pin (its analog, not digital). User can add or subtract 8 pF capacitors to tune an antenna circuit. Writing 0x85 to register 0x08 is actually 2 commands. 0x80 causes the frequency to be outputted to the pin , adding 0x05 adds 5 capacitors to the circuit. Without the 5 capacitors, the frequency is about 509 kHz. With the capacitors, it is 500 kHz +/- 1kHz.

Problem: When the Old code is installed, it works well. The 5 capacitors are installed, and the frequency is approx. 500 kHz. It can be unplugged, plugged back in, held in reset, released from reset. Whatever I do, it keeps working.

When the New program is installed AFTER the Old program, it also works immediately after programming or after using the reset function of the programmer. Moreover, if I first erase the Old program, the New program still works. However, if power is interrupted to the New program, the reported frequency reverts back what is obtained with no capacitors. All attempts to make it start correctly "cold" fail; however, the subroutines to adjust those capacitors all work as before. If I read register 0x08 after exiting the frequency output part, it shows no capacitors. When working properly, that register shows 5 capacitors.

To repeat, that change with the New program appears to be irreversible without re-programming with the Old program first. That is the part that really troubles me . I have repeated those sequences several times with the same result.

Any ideas?

John
 

joeyd999

Joined Jun 6, 2011
5,283
Are you saying the code -- in its entirety -- is the same, but it built differently on different days?

Highly unlikely!

Run a diff on the two resulting .hex files. You'll quickly see the difference.

Someone is playing a prank on you. Or you made a change you don't remember.

I use Git to track my code changes over time. Works like a charm.
 

Thread Starter

jpanhalt

Joined Jan 18, 2008
11,087
What is the difference between PutReg (line 9 old program) and Put232 (line 9 new program)?
That's it! Both should be PutReg. (PutReg writes to the register identified in W, Put232 just writes to the serial LCD what's in W, and an 0x08 does nothing to that device. So, it was going through POR, basically doing nothing except getting the unadjusted frequency and returning to normal operation.

I am a horrible proof reader, but I don't think I will improve in my time remaining.

What happens to me (maybe others too) is that while editing, the cursor will not be where I think it is. Of course, I usually catch those errors and fix them. This looks like one that escaped me. That's why I posted both pertinent parts of the code, as I needed a new set of eyes. I had looked at it many times and my mind did not see that difference. I don't quite understand why it worked when the old code was programmed first, but at least it works now.

Thank you.

John
 
Last edited:

Thread Starter

jpanhalt

Joined Jan 18, 2008
11,087
Are you saying the code -- in its entirety -- is the same, but it built differently on different days?

Highly unlikely!

Run a diff on the two resulting .hex files. You'll quickly see the difference.

Someone is playing a prank on you. Or you made a change you don't remember.

I use Git to track my code changes over time. Works like a charm.
Not exactly. The pertinent parts of the code seemed the same to me (See: reply to AlbertHall). The code is sort of in compartments -- routine operation and command operation. Command operation is much longer.

Now that the problem has been identified, I remember exactly when it happened. There are two lines that set the cursor position on an LCD.
Code:
     movlw     0x94           ;accomodates POR branch to GetLCO
     call      Put232
I didn't like the flow of the code at that point and was playing with various options. Tried them in the POR, then switched to the subroutine, GetLCO. Apparently the error happened when I excised them from the POR.

Thanks for looking.

John
 

Thread Starter

jpanhalt

Joined Jan 18, 2008
11,087
And I'm surprised to see this:

DelayCy (100*msecs).

I thought you were better than that.
That is a macro from Mike McLaren K8LH. The inline part is quite short. I use it for short delays when there are several different periods that are not used repeatedly. Long (e.g, 2-second) delays are handled as ordinary calls.

I will try Git later. Have to run into Cleveland this morning. Up till now, I use Word doc compare, which is not easy to do for code.

John
 

joeyd999

Joined Jun 6, 2011
5,283
If the uC has nothing better to do then it might as well be timing a delay as sitting in a loop twiddling its thumbs.
My code tends to grow (substantially) into doing things that I never expected/planned for.

In addition, I write my code in reusable modules. I may not expect to use a particular function/hardware device again in the future, but if it turns out I have to, it is much easier to drop an old module into new code than to write new code all over again.

With this in mind, I always write non-blocking code. This means a) no hardware waits, and b) no static timing loops.

That my code idles most of the time in the main loop is an advantage. It means I've got "daylight" into which to add new functionality without disrupting the already-present code.
 

joeyd999

Joined Jun 6, 2011
5,283
A good example was this project.

I had an existing project, already with some 8 or 9 thousand lines of code, and I had to integrate a Bluetooth module into it. Aside from the Bluetooth programming itself, integrating into the main app was a breeze (resulting in 4 thousand new lines of code). And I didn't have to modify the main app except to add a few #includes.

As a benefit, the same Bluetooth code has been integrated into a number of my projects -- again very easily because I write all my code using the same structure and with the same rule set.
 

Thread Starter

jpanhalt

Joined Jan 18, 2008
11,087
That 100-ms delay is part of the "Listening" display. Tried to make a video (5 sec) but not sure anyone can open it. I am laboring with the native programs of Windows 7 and some ancient version of Office.

The reason for the effect of switching off the power hit me about the time I was at the end of my driveway. The AS3935 is protected from the programming voltages. Thus, once it was set with the "Old" program, the capacitors were already installed. New program did nothing to them, as described above, so it appeared to work. Once power was interrupted, the AS3935 settings went to default. Dumb errors like that are the hardest to find.

See below (Post #17 ) for MP4 file. Didn't realize wimp wasn't a real video. It was awfully small.
 
Last edited:

joeyd999

Joined Jun 6, 2011
5,283
That 100-ms delay is part of the "Listening" display. Tried to make a video (5 sec) but not sure anyone can open it. I am laboring with the native programs of Windows 7 and some ancient version of Office.

The reason for the effect of switching off the power hit me about the time I was at the end of my driveway. The AS3935 is protected from the programming voltages. Thus, once it was set with the "Old" program, the capacitors were already installed. New program did nothing to them, as described above, so it appeared to work. Once power was interrupted, the AS3935 settings went to default. Dumb errors like that are the hardest to find.
No, your movie didn't come through. Just a wimp script.

I'd like to see some more details on your application. The idiots at AMS want me to register to get a copy of the datasheet. No thanks...got enough junk mail and sales calls already.
 
That 100-ms delay is part of the "Listening" display. Tried to make a video (5 sec) but not sure anyone can open it. I am laboring with the native programs of Windows 7 and some ancient version of Office.

The reason for the effect of switching off the power hit me about the time I was at the end of my driveway. The AS3935 is protected from the programming voltages. Thus, once it was set with the "Old" program, the capacitors were already installed. New program did nothing to them, as described above, so it appeared to work. Once power was interrupted, the AS3935 settings went to default. Dumb errors like that are the hardest to find.
The WPLF file that you attached is just the movie project file created by Windows Live Movie Maker...the movie is the file C:\Users\XXX\Desktop\Andonstar\AS3935\MVI_1540 I know you know what an .avi file is, I just want to make sure you also know the information in the WPLF file.
 

Thread Starter

jpanhalt

Joined Jan 18, 2008
11,087
No, your movie didn't come through. Just a wimp script.

I'd like to see some more details on your application. The idiots at AMS want me to register to get a copy of the datasheet. No thanks...got enough junk mail and sales calls already.
OK, I will put a zipped MP4 (posting movies is new to me).
Also here is the DigiKey link to datasheet: http://media.digikey.com/pdf/Data Sheets/Austriamicrosystems PDFs/AS3935.pdf I did not have to register to get it. That's a pain. Here's another link to the Demo board manual with schematics: http://ams.com/eng/Support/Demoboar.../Wireless-Sensor-Connectivity/AS3935-Demo-Kit Just tried that and did not need to register for it either. I can post, but it is about a 4 MB pdf.
 

Attachments

Last edited:

joeyd999

Joined Jun 6, 2011
5,283
OK, I will put a zipped MP4 (posting movies is new to me).
Also here is the DigiKey link to datasheet: http://media.digikey.com/pdf/Data Sheets/Austriamicrosystems PDFs/AS3935.pdf I did not have to register to get it. That's a pain. Here's another link to the Demo board manual with schematics: http://ams.com/eng/Support/Demoboar.../Wireless-Sensor-Connectivity/AS3935-Demo-Kit Just tried that and did not need to register for it either. I can post, but it is about a 4 MB pdf.
Got it.

Yeah. I'd want my project to do something else while waiting for lightning to strike.

Maybe make some coffee or walk the dog.
 
Top