CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PID controller PWM duty cycle....

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PID controller PWM duty cycle....
PostPosted: Tue Oct 09, 2018 12:44 pm     Reply with quote

Hi All,

I'm working on a PID controller project that will control the temperature in a thermal chamber. I'm at the point now where I'm ready to actually start controlling the chamber heaters, so I need to think about converting my PID output to a PWM duty cycle. I believe I want the duty cycle to vary from 0% to 100% depending on the magnitude of the PID output, so it seems that I need to calculate the maximum PID output in order to calculate a duty cycle fraction. This will determine the duty cycle value. But, it seems that if my kp, ki, and kd terms change during tuning, this must be a real time calculation?

PIC is a 18F44k22, and I'm running the PWM in 10 bit mode at 1KHz.

Am I thinking about this correctly?

Jack
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Oct 09, 2018 1:03 pm     Reply with quote

Basically yes, except don't think about fractions. Think of parts of an integer.

You can use an int32, effectively as a 'scaled integer', with the low 16bits being the 'fractional part'. Scale your factors to result in a number that is (say) 1024*65536 for the 'maximum', then just put the high word of this into the PWM.
guy



Joined: 21 Oct 2005
Posts: 291

View user's profile Send private message Visit poster's website

PostPosted: Thu Oct 18, 2018 8:52 pm     Reply with quote

If I'm not mistaken, note that the PWM duty cycle cannot reach 100% (only 1023/1024). To get 100% you would need to either
a. disable the PWM module and output_high
b. invert the PWM output if using the ECCP module
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Oct 19, 2018 12:58 am     Reply with quote

You are mistaken.
If you put out a value equal to the maximum or higher into the pwm duty, the output goes permanently on.
guy



Joined: 21 Oct 2005
Posts: 291

View user's profile Send private message Visit poster's website

PostPosted: Fri Oct 19, 2018 1:53 am     Reply with quote

I'm not helping much am I? Sad
This was my experience from some PICs I worked with. Maybe things changed?
So please explain Ttelmah - how can a 10-bit register (1024 combinations) cover 1025 combinations? Assuming 0 gives 0/1024 duty cycle, 1 gives 1/1024, etc. so 1023 will output 1023/1024...?
Theoretically 1024 should output 100% but it is not a valid value for a 10-bit register.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Oct 19, 2018 4:48 am     Reply with quote

First of all, this all depends on if you are using all 1024 cycles.
The point is that if you use less than this the PWM is specified so that if the duty cycle exceeds the period it will go permanently high. Use PR2=254, and 1020 cycles, and you can get 100%.

Then it does depend on the chip. The older chips which have the split duty cycle register (8bits +2), can only give 1023/1024, if you use the maximum PR2 value. However the ones that have a separate duty cycle register do support going to full on. Several of the newer PIC18's, and the PIC24's.

There is also an issue if you are using 8bit mode, since here there are two 'unchanged' bits. In this mode you either have a little bit 'on' at zero, or 'off' at maximum.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 19, 2018 5:14 am     Reply with quote

10 bits is 1024 combinations, of which 0b0000000000 is a valid entry, so 1023 is the max and 1023/1023 is 1, so 100% 'on'. Same as in 8 bit mode where 255/255=1 and is 100% 'on'.
It may be that if the compiler see a value greater than the max allowed for the mode, that it stores the max. IE in 8 bit mode 255 is the max...your program tries to use 257. The compiler is smart and stores 255.
Decades ago when using the 16F877 for a realtime helicopter, I coded max and min values to be sure my 'math' didn't exceed legal PWM values.

Back to the original post though, you will find that 'temperature control' is a very lazy function, IE slow.... You'll probably fine you can just have preset values, say 10, 20, 30% 'levels' and have very good control over the temperature. Modern stoves do that, using 8 bit mode. It's quick, easy, saves memory, allows for cheaper controller but still has acceptable performance.
While you don't say how big the 'thermal chamber' is, you'll need multiple temperature sensors and a fan or two. BTDT !
Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group