PWM with PIC 16F1788 - Page 1
Forum section: electronics / general

author: backd00r
author_rank: Contributor
timestamp: April 12, 2015, 03:36:41 am
content: I wrote some PWM functions for PIC16f1788 that are part of a much bigger code. I have a function that does ADC from a pot input and feeds that as a duty cycle to both of my PWM functions. As I am testing my code I found that the PWM output would jerk randomly. I can see in the scope that it works as expected . The duty cycle varies as I am varying the resistance of the pot and it is quite accurate too. But the problem is once in a while the duty cycle would change for a second or two then it goes back to its original value. I can't figure out whats wrong with my code at all. I believe I am missing something in the PWM functions that is causing this abrupt change in duty to happen. Please take a look at my PWM functions attached.

author: senso
author_rank: Frequent Contributor
timestamp: April 12, 2015, 05:35:45 pm
content: Noise from the pot?Try applying simple averaging to the data read from the ADC.

author: backd00r
author_rank: Contributor
timestamp: April 13, 2015, 01:39:43 am
content: guys this is the output I get I think you can see here the how noisy my signal is. I have no idea why it is this way. I put a new pic on and same thing happens I have this project due very soon and I would like to get your help. Thanks you.

author: ludzinc
author_rank: Unknown
timestamp: April 13, 2015, 02:18:23 am
content: I'd also look at determining if the pot is a source of error.Change your code to ignore the pot, and use a set PWM value. Does it jitter then? If you can change the PWM value by serial do so and see if that effect your jitter.

author: backd00r
author_rank: Contributor
timestamp: April 13, 2015, 02:21:44 am
content: yes I tried setting a constant duty cycle in the software independent of the hardware and it does jitter I think it is the way the pwm functions are set I might be missing a minor thing that makes them jitter that way. I noticed that even when the duty cycle is set to zero I still get a small square wave as output. Can you please take a look at my PWM functions and see what I am missing. Mainly the pwm_l and pwm_r functions of the Controller_PWM.h file. Thank you

author: BlueBill
author_rank: Regular Contributor
timestamp: April 13, 2015, 04:50:04 am
content: Schematic and picture of your assembled circuit would help.Do you have proper supply decoupling, 0.1uF across the PICs power pins?

author: SL4P
author_rank: Super Contributor
timestamp: April 13, 2015, 06:04:53 am
content: Without looking at the code in detail, or the container main() & isr() functions...I'd be suspicious of re-initialising the PWM registerseverytime you call the blink() function.Chances are you are seeing some aliasing between the 'blinking' and the servo PWM overflow - with the PWM counter being reset as a consequence - halfway through a PWM cycle...Just 2c worth, but where I'd start looking. comment outallthe calls to blink() and then test to isolate the cause.

author: backd00r
author_rank: Contributor
timestamp: April 14, 2015, 04:23:06 am
content: Quote from: Mechatrommer on April 13, 2015, 06:38:04 amI am using 10k pots

author: backd00r
author_rank: Contributor
timestamp: April 14, 2015, 04:24:38 am
content: Quote from: SL4P on April 13, 2015, 06:04:53 amI was thinking the same thing actually I think the overflow is happening between the other functions because I am actually not using the blink function anymore I removed it totally. Still can't figure out whats wrong in that PWM codethanks for the input though

author: Brutte
author_rank: Frequent Contributor
timestamp: April 14, 2015, 03:45:47 pm
content: That does not look like a minimal test case. Poorly commented. Why *.h?QuoteAre you a subcontractor working for Toyota?

author: backd00r
author_rank: Contributor
timestamp: April 14, 2015, 06:50:57 pm
content: Haha I wish that I worked for Toyota no this is my capstone project we are building a two channel BLDC controller lol

author: backd00r
author_rank: Contributor
timestamp: April 14, 2015, 06:53:12 pm
content: I think it is a register switching problem it seems like some kind of overflow happening. I am lost because I never had to deal with so many pwm outputs before I will have to talk to a microprocessor professor at the University and see what he thinks. For the mean time any inputs are welcome

author: MarkF
author_rank: Super Contributor
timestamp: June 02, 2015, 02:04:02 am
content: I had similar problems with a pic16f876.The fastest way to determine if it's the pot would be to ignore the pot and hard code a PWM width. It your PWM is stable, it's the pot.Also, the pic16f876 datasheet clearly states that the maximum resistance for the A/D inputs is 2.5K ohm. I imagine your pic has the same limit.

author: Stupid Beard
author_rank: Regular Contributor
timestamp: June 02, 2015, 02:17:45 am
content: Quote from: MarkF on June 02, 2015, 02:04:02 amThe maximum impedance for the PIC16F1788 A/D inputs is 10Kohm; see section 17.4 of the datasheet. I assumed it was the same for all of them, but I just double checked the datasheet for the 16F87X and that does indeed say 2.5K.I guess that just serves as a reminder to always check the datasheet.

author: mikerj
author_rank: Super Contributor
timestamp: June 02, 2015, 09:45:55 am
content: Quote from: Stupid Beard on June 02, 2015, 02:17:45 amA 10k pot gives a maximum 2.5k impedance, so you should be ok. The impedance requirements are important to meet the accuracy spec. in the datasheet, a higher impedance will still work but leakage and S&H currents will degrade accuracy.In terms of your code:1) Why are you reconfiguring the CCP peripherals every single time you update the duty cycle? This simply isn't required, and could well be causing problems. Additionaly, do you really need to calculate PR2 value at run time, and especially every time you update the duty cycle? Does your PWM frequency have to change regularly?2) Why are you using floating point? This will bloat your code and incur a hefty execution time penalty. There is rarely any need to use floating point math in simple functions like this.

author: JanJansen
author_rank: Frequent Contributor
timestamp: June 05, 2015, 04:41:53 pm
content: Search for Tad in the manual!, your sampling to fast, let it wait for all the bits.