Announcement

Collapse
No announcement yet.

XC8 C code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XC8 C code

    I've ported George's Mikro-C code to the Microchip XC8 compiler. Originally I intended to do a direct port with few code changes but I ended up pulling some concepts from the way I'm writing C code for ITMD3, which follows from C++ code I've done elsewhere. As such, it looks a lot different structurally but when you look at the functions most of the code has not actually changed. Here are the differences:

    1. Code is more modularized. Modules are
    • Voodoo (main)
    • VoodooADC
    • VoodooDSP
    • VoodooEEPROM
    • VoodooTiming
    • VoodooUI
    • LCD

    2. The LCD code was ported from my C++ efforts elsewhere and can be used independently. It is not quite finished as scrolling is not implemented, but that's not used in Voodoo.

    3. Converted everything to CamelCase, that's just my programming preference.

    4. The detector continues to operate while in the menus. Timing is live-changed on every button press so you can see pulse changes on an oscope on the bench or can hear effects on target responses while in the field. Obviously, the screen remains in the menu so the meter is not available.

    5. When you exit the menu and re-enter, you re-enter where you left off.

    6. There is a new menu item called DISC INVERT which inverts the polarity of the Disc signal. I noted in one of my test coils that the RX coil was phase-inverted so that non-ferrous produced negative bars. This setting allows you to correct for that without having to make major changes to the Disc sample delay.

    7. Timers are a little different:
    • Tmr1 interrupt runs every half-period.
    • At one Tmr1 interrupt, the sequence of Tmr0-based pulses is started, beginning with the TX pulse. The PSSync pin is also set high.
    • At the next Tmr1 interrupt the PSSync pin is set low. This can potentially happen in the middle of the Tmr0 pulses but that should not matter.
    • This results in a PSSync pulse running at the TX frequency and with a 50% duty cycle.

    8. I noticed jitter in the Hybrid mode; this is due to the imbalanced number of switch/case evaluations as it alternates between PI & Disc. I found this behavior to be bizarre, it should not be happening as it is interrupt-driven. But adding 2 dummy cases to the Disc side eliminates the jitter.

    I've only very loosely tested this code so I post it here with absolutely no guarantee of usability. Please post feedback & bug reports to this thread.

    Voodoo.X.2.00.zip Version 2.0
    Voodoo.X.2.01.zip Version 2.01
    Voodoo.X.2.02.zip Version 2.02
    Last edited by Carl-NC; 07-30-2021, 06:31 PM.

  • #2
    Sounds great Carl. Thanks.
    I will look over this new code.

    Comment


    • #3
      Thanks Carl for this mammoth effort.

      Programmed Voodoo with this new code, but found some strange behaviour ... as follows:

      When I switched on the detector it wasn't detecting targets correctly, and the meter was only moving by one block in either direction.
      Entered the Menu (Hybrid Mode) and found the Meter Zero Limit was set to zero. I then increased the value to 20 and exited the menu.
      Switched off the detector and on again, but the meter Zero Limit was back at zero.
      Tried pressing the Down button, and the value changed to 255. The Meter Zero Limit should have a range from 5 to 20.
      Still not working correctly.

      Re-programed the detector to start from scratch.
      Set Meter Zero Limit to 20.
      Detector was working back to front. Beeps on ferrous and not on non-ferrous, and meter was backwards.
      Changed Disc Invert to ON. No difference.
      Changed Disc Invert back to OFF. Now working correctly!
      Switched detector off and on again, and everything went back to beeping on ferrous targets.

      At the moment it appears that I have to enter the menu every time and toggle the Disc Invert to get correct operation.
      Also there's something screwy with the Meter Zero Limit.

      Comment


      • #4
        Sounds like I may have introduced a bug when I added the DiscInvert option. Probably in EEPROM code, I'll look it over today.

        Comment


        • #5
          As I suspected I botched adding DiscInvert to the EEPROM code. I got it and MeterZeroLimit swapped in the read/write functions. V2.01 is now posted (post #1).

          This reminded me of another change I made... PI & Disc thresholds are saved as their true setting values (0-20) and are offset by 511 in the DSP code. Since I store them as bytes instead of ints in EEPROM I skipped bytes 3 & 13 to keep the EEPROM data aligned with the MikroC version, so if you program the XC8 code into a chip that had MikroC code the menu settings will still be correct. Then I inexplicably stuck DiscInvert in the middle instead of the end, breaking compatibility. 2.01 places DiscInvert at the end where it should have been.

          Edit: It occurs to me that the MikroC values for both thresholds will be read incorrectly because I'm storing the XC8 bytes in the MikroC high-byte locations. Should have stored them in the low-byte locations. Easy fix, change locations 2 -> 3 and 12 -> 13 in the Read/Write functions. Or just readjust the thresholds in the menu and they will be saved correctly to EEPROM.

          Comment


          • #6
            The EEPROM compatibility problem bothered me enough that I went ahead and fixed it. See V2.02.

            Comment


            • #7
              Flashed V2.02 into Voodoo detector today.
              Everything seems to be working as advertised.

              Comment

              Working...
              X