|
|
View previous topic :: View next topic |
Author |
Message |
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
PIC24EP512GP806 PWM SETUP - HELP REQUIRED [SOLVED] |
Posted: Tue Jul 26, 2016 10:29 am |
|
|
Device: from PIC24HJ256GP206 to PIC24EP512GP806
Compiler: 5.026
I've just replaced a PIC24HJ256GP206 with a PIC24EP512GP806 and all seems to be working well except for the PWM.
Setup for either PIC device:
Code: |
#zero_ram
#device PASS_STRINGS=IN_RAM
#fuses HS, WDT, PR, WPOSTS16
#use delay( crystal = 36864000, clock = 73728000 )
#build ( stack=1024 ) |
PWM setup for the HJ:
Code: |
setup_compare( 1, [b]COMPARE_PWM[/b] | COMPARE_TIMER2 );
setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, 287 );
set_pwm_duty( 1, 144 ); |
The result is that pin OC1/RD0 outputs a nice perfect 128KHz 50% duty PWM.
PWM setup for the EP:
1) Re-map the OC1 function to PIN D0 like this:
Code: |
#PIN_SELECT OC1=PIN_D0 |
2) Then it seems that the only difference is the second parameter in the function that needs to be changed from COMPARE_PWM for the HJ to COMPARE_PWM_EDGE for the EP although in both header files, it's the the same value (0x06):
Code: |
setup_compare( 1, [b]COMPARE_PWM_EDGE[/b] | COMPARE_TIMER2 );
setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, 287 );
set_pwm_duty( 1, 144 ); |
The result is that pin D0 is 562.46Hz instead of 128KHz at like less than 1% duty.
Obviously, there is something missing but I am not sure what exactly... As stated in an earlier post from two weeks ago regarding the EP series, this is a new device I never tried before, pin mapping is a new concept although fairly simple, but still, perhaps I missed something along the way.
Other than the PWM, I can recompile the code and everything else works (UART through DMA, I2C, SPI, interrupts etc).
Now, the one question I had in a previous post regarding the EP series was that pin 39 is OSC1/RPi60/RC12. On the HJ series, pin 39 is OSC1/CLKIN/RC12. The difference here is that the EP shows a pin mapping option versus the HJ that explicitely says "CLKIN".
I am wondering if the new EP device is actually using the external 36.864MHz clock or if there's a line I need to add somehow and is now reverting to an internal clock or something?
Because that's the only thing I could see causing the PWM to be much slower.
Other pointer: when I setup the PWM, if I change TMR_DIV_BY_1 to TMR_DIV_BY_256, then the output of the PWM is 2.197Hz. Obviously, it makes sense, it is the result of the DIV_256... so what part of the clocking configuration is wrong?
Thanks!
Benoit
Last edited by benoitstjean on Wed Jul 27, 2016 7:36 am; edited 1 time in total |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Tue Jul 26, 2016 11:43 pm |
|
|
Hi! I can't help much but obviously you know what you're doing and this brings flashbacks to hours and hours of debugging only to find that there are small bugs in the compiler.
If you don't get a better response from the forum or find something yourself, go ahead, read the whole data sheet section about PWM (so you don't miss anything important), then compare the documentation to the actual register contents. I won't be surprised to hear you did everything ok and there is a bug in the compiler... Good luck! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Jul 27, 2016 4:56 am |
|
|
If you look at the data sheet for the output compare module on the two chips, you will see that they are 'chalk and cheese'. On the older chip, the module diagram has just three sections, with a timer, fed to a comparator then to output logic. The later chip, has instead about ten sections. Loads more clocks, trigger and sync sources, extra buffers etc... Then you have comments like:
The Trigger/Sync source is enabled by default and is set to Timer2 as a source. This timer must be enabled for
proper OCx module operation or the Trigger/Sync source must be changed to another source option.
Which seems to suggest that this part of the module needs to be disabled, or re-routed, if you are going to use timer2 as the main clock source. Then we see that the fault logic is far more complex. Ouch...
We can also see the reason for the change in name from CCS. On the older chip, the value '6' in the data sheet, gives "PWM without Fault Protection", while this same value on the newer chip gives "Edge-Aligned PWM mode".
So we need to see what is wanted to get the module to behave the same?.
The old chip refers us to DS70247, while the second one refers to DS70358, so the differences in these are the first places to look. This then gives the comment:
"Note: This is a migration issue for applications. In the OC module without dedicated timers (see “Output Compare” chapter in the specific device data sheet); the OCxRS register served as a double-buffer to OCxR. In this version, both the registers are double-buffered."
This is presumably why the behaviour is different. The diagram (3-13 in the sheet about this), shows that 'sync occurs' at the end of the pulse, rather than the pulse just repeating.
So without spending time to read on, it suggest you need to look into setting the sync source, and disabling this part of the peripheral to get it to work as the older chip. You are going to have to look at what is different in the two chips for this mode of operation. |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Jul 27, 2016 6:16 am |
|
|
Yikes. Ok.
I sent an email to CCS to see what their suggestion is.
I have to admit that I am not the best person to understand all the inner-intricacies of how the PIC works at the register level.... this is why I am using a C compiler rather than assembly language. I _have_ been through many of the registers over the years and understand lots of it but there's also lots of it that I don't understand.
And this PIC being a new device that I've been playing with for only two days, that's why I am not aware of all the changes at the PWM level.
I will look at the docs but guaranteed I will have questions because some stuff might just be above my head.
In the meantime, if anyone knows how to output a 128kHz 50% duty PWM on the PIC24EP-series, then please post your code here!
Thanks,
Ben |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
PIC24EP512GP806 PWM SETUP - HELP REQUIRED [SOLVED] |
Posted: Wed Jul 27, 2016 7:01 am |
|
|
Got it!
The 'COMPARE_TRIG_SYNC_TIMER2' flag had to be added:
setup_compare( 1, COMPARE_PWM_EDGE | COMPARE_TRIG_SYNC_TIMER2 | COMPARE_TIMER2 );
All right, using a 36.864MHz oscillator, to get a 128kHz 50% duty PWM output on OC1 (PIN_D0), this is the code:
PIC24HJ-series:
#use delay( crystal = 36864000, clock = 73728000 )
setup_compare( 1, COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, 287 );
set_pwm_duty( 1, 144 );
PIC24EP-series:
#use delay( crystal = 36864000, clock = 73728000 )
#PIN_SELECT OC1=PIN_D0
setup_compare( 1, COMPARE_PWM_EDGE | COMPARE_TRIG_SYNC_TIMER2 | COMPARE_TIMER2 );
setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, 287 );
set_pwm_duty( 1, 144 );
Thanks for your help!
Ben |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Jul 27, 2016 7:12 am |
|
|
Try using #USE PWM.
So:
Code: |
#PIN_SELECT OC1=PIN_D0
#USE PWM(PWM1, FREQUENCY=128445, PWM_ON)
//then to change the duty
pwm_set_duty(144L);
|
If your serial etc., are working, then it says your clock rate is OK (otherwise this might be the problem), so it is a matter of 'how to configure', and though I don't like this command, since it 'hides' too much, it could be the solution for this... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Jul 27, 2016 7:16 am |
|
|
I see you got a reply while I was posting.
Makes sense. As I said:
"look into setting the sync source". |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Jul 27, 2016 7:40 am |
|
|
I guess I don't have the same compiler version as yours because in the help (5.026), I don't see #USE PWM.
Ben |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Jul 27, 2016 7:55 am |
|
|
It is in the .h file for your version. It's one where the included 'help' is out of date. It's always worth getting the .pdf help.
They came out in Oct 2012, right at the start of V5:
<http://www.ccsinfo.com/newsdesk_info.php?newsdesk_id=147> |
|
|
|
|
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
|