Author Topic: PWM with PIC 16F1788  (Read 5374 times)

0 Members and 1 Guest are viewing this topic.

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
PWM with PIC 16F1788
« on: April 12, 2015, 01:36:41 pm »
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.

 

Offline senso

  • Frequent Contributor
  • **
  • Posts: 830
  • Country: pt
    • My AVR tutorials
Re: PWM with PIC 16F1788
« Reply #1 on: April 13, 2015, 03:35:45 am »
Noise from the pot?
Try applying simple averaging to the data read from the ADC.
 

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
Re: PWM with PIC 16F1788
« Reply #2 on: April 13, 2015, 11:39:43 am »
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.

 

Offline ludzinc

  • Supporter
  • ****
  • Posts: 453
  • Country: au
    • My Misadventures In Engineering
Re: PWM with PIC 16F1788
« Reply #3 on: April 13, 2015, 12:18:23 pm »
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.
 

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
Re: PWM with PIC 16F1788
« Reply #4 on: April 13, 2015, 12:21:44 pm »
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
 

Offline BlueBill

  • Regular Contributor
  • *
  • Posts: 167
  • Country: ca
Re: PWM with PIC 16F1788
« Reply #5 on: April 13, 2015, 02:50:04 pm »
Schematic and picture of your assembled circuit would help.

Do you have proper supply decoupling, 0.1uF across the PICs power pins?
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2026
  • Country: au
  • There's more value if you figure it out yourself!
Re: PWM with PIC 16F1788
« Reply #6 on: April 13, 2015, 04:04:53 pm »
Without looking at the code in detail, or the container main() & isr() functions...

I'd be suspicious of re-initialising the PWM registers every time 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 out all the calls to blink() and then test to isolate the cause.
Don't ask a question if you aren't willing to listen to the answer.
 

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
Re: PWM with PIC 16F1788
« Reply #7 on: April 14, 2015, 02:23:06 pm »
And what is trimpot r value? Smaller trimpot may burn due to passing electron, that was my newbie mistake. If it burns it will not give nice n contigous reading n degrade overtime until u have no more reading.

I am using 10k pots
 

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
Re: PWM with PIC 16F1788
« Reply #8 on: April 14, 2015, 02:24:38 pm »
Without looking at the code in detail, or the container main() & isr() functions...

I'd be suspicious of re-initialising the PWM registers every time 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 out all the calls to blink() and then test to isolate the cause.

I 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 code  |O  thanks for the input though
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 500
Re: PWM with PIC 16F1788
« Reply #9 on: April 15, 2015, 01:45:47 am »
That does not look like a minimal test case. Poorly commented. Why *.h?

Quote
"applies a PWM waveform with a specified duty cycle to the throttle intercept MOSFET"
"applies a PWM waveform with the specified duty cycle to the cruise control MOSFET"
Are you a subcontractor working for Toyota?
 

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
Re: PWM with PIC 16F1788
« Reply #10 on: April 15, 2015, 04:50:57 am »
Haha I wish that I worked for Toyota no this is my capstone project we are building a two channel BLDC controller lol
 

Offline backd00r

  • Contributor
  • Posts: 32
  • Country: us
Re: PWM with PIC 16F1788
« Reply #11 on: April 15, 2015, 04:53:12 am »
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
 

Offline MarkF

  • Frequent Contributor
  • **
  • Posts: 664
  • Country: us
Re: PWM with PIC 16F1788
« Reply #12 on: June 02, 2015, 12:04:02 pm »
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.
« Last Edit: June 02, 2015, 12:12:53 pm by MarkF »
 

Offline Stupid Beard

  • Regular Contributor
  • *
  • Posts: 221
  • Country: gb
Re: PWM with PIC 16F1788
« Reply #13 on: June 02, 2015, 12:17:45 pm »
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.

The 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.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 1595
  • Country: gb
Re: PWM with PIC 16F1788
« Reply #14 on: June 02, 2015, 07:45:55 pm »
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.

The 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.

A 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.
 

Offline JanJansen

  • Frequent Contributor
  • **
  • Posts: 339
  • Country: nl
Re: PWM with PIC 16F1788
« Reply #15 on: June 06, 2015, 02:41:53 am »
Search for Tad in the manual!, your sampling to fast, let it wait for all the bits.
My ±100€ lab : old scope, peaktech 6225a, aneng 8008
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf