• AVR Freaks

Simulink+Microchip Code Blocks: How to setup PWM/Output Compare

Author
stephen_newvistas
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2016/07/08 15:27:44
  • Location: 0
  • Status: offline
2016/08/23 10:01:21 (permalink)
0

Simulink+Microchip Code Blocks: How to setup PWM/Output Compare

Issue: How do you setup a pwm output using the dsPIC33FJ256GP710A's output compare/input capture peripheral? The Microchip Code Blocks contain an Output Compare block, but there is no documentation as to the expected input on the block nor any detailed information on the type of modes available.
 
Background: The Microchip Code Blocks also contain some High Speed PWM blocks, but these are only supported on devices with more advanced PWM peripherals (dsPICs with "MC", or Motor Control, are an example). General Purpose PICs, like the dsPIC33FJ256GP710A have a peripheral that uses the Output Compare pins (OC1:OC8) and is able to set them up and use them as PWM.
 
Software:
Matlab 2016a
Simulink+Matlab Coder+Simulink Coder+Embedded Coder
Microchip Code Blocks
Windows 10
MPLAB X v3.35
XC16 Compiler v1.26
 
Hardware:
Microchip Explorer 16 devBoard w/ dsPIC33FJ256GP710A
ICD 3
 
Thanks for the help,
Stephen
#1
Lubin
Moderator
  • Total Posts : 353
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: Simulink+Microchip Code Blocks: How to setup PWM/Output Compare 2016/08/23 13:17:27 (permalink)
0
Hi Stephen,
 
You might few input from the Quick Start Guide (ref is "DS50002238A") provided with the blockset. It is copied within the blockset folder, typically: C:\Program Files (x86)\Microchip\MatlabTools\docs
 
There are also few thread which might already help. Let us know if they can provide you with the information you are looking for:
Input capture works in a quite similar way.
 
Input Capture and Output Compare blocks allows precise measurement and generation of pulses / PWM signals. We might provide further examples if you provide more details about what you are willing to do.
#2
stephen_newvistas
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2016/07/08 15:27:44
  • Location: 0
  • Status: offline
Re: Simulink+Microchip Code Blocks: How to setup PWM/Output Compare 2016/08/29 07:22:29 (permalink)
0
Lubin,
 
Thank you for the response. I have looked over the documents and suggested forum posts. The issue still needs some clarification.
 
It looks like the Output Compare has a few settings/inputs that correspond with how the peripheral would be set up traditionally using C to assign values to the PIC's registers. Typically, at the very basic level you would modify these values in the PIC's registers:
 
Period
Rise/fall count
Prescaler
 
We have found that the code blocks for the Output Compare are associated with these traditional settings by:
 
Period = _per
Rise/fall count = _up
Prescaler = ??
 
It seems that OCNmax time is somehow related to prescaler, but in an indirect way. From what I have found, I have not been able to understand exactly what OCNmax is and how it relates to the prescaler. What exactly is maximum time? Is it the time of a timer tick?
 
Thanks,
Stephen
#3
Lubin
Moderator
  • Total Posts : 353
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: Simulink+Microchip Code Blocks: How to setup PWM/Output Compare 2016/09/02 00:55:33 (permalink)
3 (1)
Hi Stephen,
 
Peripheral blocks from the dsPIC blockset handle low level peripheral setting based on the high level block’s user interface options; this include prescalers. For the OC block:
The parameters “Max Time for all channels” or “Max time / channels” define the maximum timing event for the output:
  • If you are generating a periodic PWM, it defines the maximum period.
  • If you are generating a pulse, it defines the maximum pulse width.
The peripheral prescaler will be set based on that input to provide the maximum resolution compatible with this maximum timing constraint.
 
Block input are the raw registers values for the Period/Pulse/DutyCycle… Thus block input values need to be scaled using your own Simulink blocks (gain, offset...). The peripehral GUI creates OC{x}max variable within the matlab workspace which allows performing such scaling: OC{x}max represent the maximum timing event. This value is always below 65535 (16 bit registers).
 
Let’s take an example:
OC block is set so as to have two block input: up & period.
- Max time /channel set to 20e-3 (20ms)
=> OC1max in the matlab workspace take a value, for example 17999 which will code for 20ms
- Block input “Period” value 17999 will provide a 20ms period.
- Block input “up” value 8999 will provide a 50% duty cycle.
Duty cycle from 0 to 100% with are represented by block input “up” value from 0 to 17999 (OC{x}max).
You should not use a value higher than OC{x}max.
 
Lubin
#4
apriddis
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2016/06/13 10:30:10
  • Location: 0
  • Status: offline
Re: Simulink+Microchip Code Blocks: How to setup PWM/Output Compare 2016/11/16 15:33:18 (permalink)
0
Can you explain the different modes of the Output Compare module? For example in the HW Output Compare there is the Set & Reset & period mode. What exactly do the Set and Reset inputs do? In the SW Output Compare, how does Toggle work?
 
My goal is to created a specific number of pulses. Do you have any suggestions on creating a discreet number of pulses? Output Compare seems like the right initial approach but I'm not sure how to make this happen with a uniform pulse for and generate a certain number of pulses.
-Andrew
#5
Jump to:
© 2019 APG vNext Commercial Version 4.5