0
\$\begingroup\$

So I implemented a simple PWM on a PIC 12F617. Had to use the Compare peripheral with interrupts since I couldn't generate 50hz with the PWM module (too low frequency). I set a value to the CCPR1 register and when TMR1 reaches that value, an interrupt is firing in which I either set a pin to 1 or 0 to generate the duty cycle I want.

Now I also bitbang I2C in the main loop and here's the weird thing. I tested both PWM and I2C separately and they work fine. Once I enable them both, I2C gets completely messed (can see it on the scope), it looks like it can't pull the line low so it basically is oscillating a few millivolts below VCC when it wants to pull it down, while on other occassions it is just oscillating all over the place.

And now the even weirdest thing. While I2C alone works and stops when I enable PWM, if I disable PWM again, it won't go back to where it was before! At first I thought it was slave device acting up, but power cycling everything wont do anything. After a few hours of fiddling here and there looking for ghosts, breaking the whole circuit and assembling it again it starts to work again. So I enable PWM again and sure enough, everything is screwed again! I disable PWM and back to square 1, everything is messed and just doesn't want to work. I spent all day trying to find out whats going on, I've done this 4 times already and everytime the same story.

Anyone had this happen to them? Any advice on how to prevent PWM making everything go crazy?

\$\endgroup\$
8
  • 1
    \$\begingroup\$ From your description I can deduce only that your code is bad. \$\endgroup\$
    – Eugene Sh.
    Jul 24, 2017 at 21:24
  • 1
    \$\begingroup\$ Show the code. It's probably not bad, just misbehaving... \$\endgroup\$ Jul 24, 2017 at 21:29
  • 2
    \$\begingroup\$ @ChrisKnudsen Code is never misbehaving. It is doing exactly what it was told to. \$\endgroup\$
    – Eugene Sh.
    Jul 24, 2017 at 21:40
  • 4
    \$\begingroup\$ @E Yes, exactly. However, that doesn't make it so much bad... just misguided. The next philosophical question: Who is the victim: the code, or the creator? Or perhaps the hardware on which it lives? I suppose we're all victims. \$\endgroup\$ Jul 24, 2017 at 21:59
  • 2
    \$\begingroup\$ John - "While I2C alone works and stops when I enable PWM, if I disable PWM again, it won't go back to where it was before! [...] power cycling everything wont do anything" If only software bugs were involved in your problem, then power-cycling everything would restore normal operation. Therefore based on your story so far, there might be more to it than just software - providing an accurate schematic would help. If I had this situation, I would work to make (a) a minimal software build & set of actions to reproduce the problem, and (b) a minimal set of hardware to go with that. \$\endgroup\$
    – SamGibson
    Jul 24, 2017 at 22:14

2 Answers 2

1
\$\begingroup\$

From my comment above:

If only software bugs were involved in your problem, then power-cycling everything would restore normal operation. Therefore based on your story so far, there might be more to it than just software - providing an accurate schematic would help.

It has now been confirmed that the problem is more than a software problem, so I'm "promoting" this comment to an answer.

In your follow-up question "Is my GPIO pin fried?", a hardware fault has now been confirmed with at least GPIO pin GP2 on the PIC.

\$\endgroup\$
2
\$\begingroup\$

Most of what you describe sounds to me as though your ic2 is throwing unmasked interrupts that you are not handling. Enabling interrupts for you PWM would then make your main loop go wonky.

\$\endgroup\$
2
  • \$\begingroup\$ Yes definitely, investigate the interrupts. Both are using interrupts, and it sounds like some contention between the two. \$\endgroup\$
    – rdtsc
    Jul 25, 2017 at 0:14
  • \$\begingroup\$ I implemented a bitbanged i2c since this uC doesn't have one. \$\endgroup\$
    – John
    Jul 25, 2017 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.