14M2 head scratcher

rob53

New Member
Hi folks, for some inexplicable reason, all four of my through hole 14M2 chips are having some anomalous behavior executing some simple code to control a Yellow/Blue bi-colored common anode LED. Here is the snippet of code they're having trouble executing correctly. I should also add the fact that this is not new code, I have been using it for years with no problems until now. I also have this same code written for the 20M2 that is still working just fine.

Code:
; ====== Deflector Dish CTRL ========================

Start1:
  Do
    Suspend 1
    buttonA = buttonA + 1
  Select Case buttonA
      Case 1
        PwmOut PWMDIV4, C.0, 99, 0
        For pwmA = 0 To 400
        PwmDuty C.0, pwmA
        Pause 2
        Next
      Case 2
        PwmOut PWMDIV4, C.2, 99, 0
        For pwmA = 0 To 400
        PwmDuty C.2, pwmA
        Pause 2
        Next
        For pwmA = 400 To 0 Step -1
        PwmDuty C.0, pwmA
        Pause 2
        Next
        PwmOut C.0, off
      Case 3
        For pwmA = 400 To 0 Step -1
        PwmDuty C.2, pwmA
        Pause 2
        Next
        PwmOut C.2, off
        buttonA = 0
     End Select
 Loop

Pin C.0 controls the yellow LED and C.2 controls the blue LED. It's Case 3 that is having the problem. The blue LED is supposed fade out, but it does not fade out, it goes out all at once, as if I had switched off the power. As far as I can tell, that code is the correct instructions to make the LED fade out when I press the momentary switch for the third time. (i.e "Case 3"). I removed the 14M2 from the breadboard and replaced it with a 20M2, then changed the outputs for the 20M2 code from C.0 and C.2 to C.2 (Yellow) and C.3 (blue) and it fades out just like it's supposed to. Here is the Case 3 snippet from the 20M2 code

Code:
Case 3
        For pwmA = 400 To 0 Step -1
        PwmDuty C.3, pwmA
        Pause 2
        Next
        PwmOut C.3, off
        buttonA = 0
     End Select
 Loop

As you can see, there's no difference in the instructions. Anyone have any ideas as to what's going on here? I've tried reprogramming them, also tried erasing them, then reprogramming them. Two of the 14M2s are brand new, never having been programmed with any other code.
 
Last edited:
One thing that comes to mind, given that you have not posted all of your code, are you using the servo/servopos commands in the same program? PWM on C.0, C.2 and servo share Timer2 in the 14M2.
 
Hi,

PWM is effectively a "Silicon (chip) Hardware" facility, so not all PICaxes behave in the same way! As Pete (and the On-line Command Syntax says), the PWM pins C.0 and C.2 of the 14M2 share one of the timers, whilst it is C.3 and C.5 on the 20M2 (so C.2 and C.3 use different timers). Problems may arise even if Servos are not being used, because the same timer is also used for the "time" variable in M2 chips, (even if a DISABLETIME command is executed).

Also, note the paragraph after section 9) in the Command Syntax, the requirement :
"To stop pwmout on a pin it is necessary to issue a 'pwmout pin, off' command. Note that this stops all pwm channels sharing that timer (e.g. both C.1 and C.2 will stop together on a 28X2 part). To just stop one channel use 'pwmduty pin, 0 ."

Cheers, Alan.
 
No, there are no servos in this circuit. This is just strictly LEDs. Here's the entire code, but keep in mind, the only thing connected to the 14M2 is the Yellow/Blue common anode LED. It's the only LED on the breadboard I'm using separate 2N2222 transistors to control them, as opposed to an array. I don't see how C.0 and C.2 sharing a timer would matter here, since the only thing functioning on Case 3 is the blue LED, the yellow LED was faded off in Case 2. The only thing left for the 14M2 to do is fade off the blue LED, and reset the button count. Like I said before, I've been using this program for years on my larger scale Enterprise Refit models. It's never given me a minutes trouble, until now, AND it's not just one 14M2 that's malfunctioning, it's all four of them, two of which are brand new, this being the only program I've ever loaded on them. This is totally weird, almost spooky.

Code:
#Picaxe 14M2 USS Enterprise NCC-1701 Refit Lighting FX

Symbol switchA = pinC.4
Symbol switchB = pinC.3

Symbol buttonA = b0
Symbol ButtonB = b1
Symbol pwmA = w1
Symbol pwmB = w2
              
; ====== Button Handler ===================

Start0:
  Do
    If switchA = 1 Then : Resume 1 : End If
    If switchB = 1 Then : Resume 2 : End If
    Pause 50
 Loop

; ====== Deflector Dish CTRL ========================

Start1:
  Do
    Suspend 1
    buttonA = buttonA + 1
  Select Case buttonA
      Case 1
        PwmOut PWMDIV4, C.0, 99, 0
        For pwmA = 0 To 400
        PwmDuty C.0, pwmA
        Pause 2
        Next
      Case 2
        PwmOut PWMDIV4, C.2, 99, 0
        For pwmA = 0 To 400
        PwmDuty C.2, pwmA
        Pause 2
        Next
        For pwmA = 400 To 0 Step -1
        PwmDuty C.0, pwmA
        Pause 2
        Next
        PwmOut C.0, off
      Case 3
        For pwmA = 400 To 0 Step -1
        PwmDuty C.2, pwmA
        Pause 2
        Next
        PwmOut C.2, off
        buttonA = 0
     End Select
 Loop

; ====== Torpedo ===========================

Start2:
  Do
    Suspend 2
    buttonB = buttonB + 1
   Select Case buttonB
     Case 1
        PwmOut PWMDIV4, B.2, 99, 0
        For pwmB = 0 To 200
        PwmDuty B.2, pwmB
          Pause 2
       Next
        High B.1
        Pause 50
        Low B.1
        Pause 50
        High B.1
        Pause 50
        Low B.1
        Pause 50
        High B.1
        Pause 50
        Low B.1
        For pwmB = 200 To 0 Step -1
       PwmDuty B.2, pwmB
        Pause 2
        Next
        PwmDuty B.2, 0
     Case 2
        PwmOut PWMDIV4, B.4, 99, 0
        For pwmB = 0 To 200
          PwmDuty B.4, pwmB
          Pause 2
       Next
        High C.1
        Pause 50
        Low C.1
        Pause 50
        High C.1
        Pause 50
        Low C.1
        Pause 50
        High C.1
        Pause 50
        Low C.1
      For pwmB = 200 To 0 Step -1
        PwmDuty B.4, pwmB
        Pause 2
        Next
        PwmDuty B.4, 0
        buttonB = 0
    End Select
 Loop

; ===== Nav Lights =======================

Start3:
  Do
    High B.3
    Pause 1500
    Low B.3
    Pause 1500
  Loop

; ======== Strobes =======================

Start4:
  Do
    High B.5
    Pause 20
    Low B.5
    Pause 1000
  Loop

Here's a Youtube video I made years ago using an early iteration of this program. I has evolved a bit since then. I found a better sound module that can rapid edge re-trigger (WT-588D) so the torpedoes can fire three times in rapid succession. This is what it evolved into Youtube Video 2
 
Last edited:
Code:
      Case2        ......
        PwmOut C.0, off
      Case 3
        For pwmA = 400 To 0 Step -1
        PwmDuty C.2, pwmA
Hi,

So it appears that you are stopping the (shared) PWM timer at the end of Case2, but NOT starting it again at the beginning of case3, because this has no PWMOUT, only a PWMDUTY, command. It presumably works with the 20M2 because C.2 and C.3 use separate PWM timers in the 20M2.

Cheers, Alan.
 
I don't see how C.0 and C.2 sharing a timer would matter here, since the only thing functioning on Case 3 is the blue LED, the yellow LED was faded off in Case 2
rob53,

I checked the manual section that AllyCat referred to and my understanding is the same as his.
  • C.0 and C.2 sharing a timer will matter because when you issue the command "PwmOut C.0, off" in case 2 the manual states that you switch off PWM for both the pins that share this timer to generate their PWM pulses.

It would be pretty quick to test to see if it resolves your problem:
Replace the command "PwmOut C.0, off" with "PwmDuty C.0, 0" in case 2
Replace the command "PwmOut C.2, off" with "PwmDuty C.2, 0" in case 3
 
Last edited:
Back
Top