|
|
View previous topic :: View next topic |
Author |
Message |
Donatello
Joined: 21 Oct 2016 Posts: 22
|
High Speed PWM, how to configure it? |
Posted: Fri Oct 21, 2016 10:19 am |
|
|
Dear programmers,
For many days, I try to use High Speed PWM functions, but no PWM is generated. I use these instructions:
Code: |
setup_hspwm_unit(1, HSPWM_ENABLE | HSPWM_FAULT_MODE_DISABLED | HSPWM_H_POLAIRTY_HIGH | HSPWM_L_POLARITY_LOW | HSPWM_COMPLEMENTARY | HSPWM_TIME_BASE_FROM_PHASE_REGS);
set_hspwm_phase(1,500);
set_hspwm_duty(1,224);
|
Please, can someone help me?
Thank you
Best Regards,
Donatello |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19511
|
|
Posted: Fri Oct 21, 2016 10:42 am |
|
|
Does nobody try looking at the examples....
ex_hspwm.c
You are setting up the 'unit', but not actually setting up the clock or enabling the pwm
setup_hspwm |
|
|
Donatello
Joined: 21 Oct 2016 Posts: 22
|
|
Posted: Sat Oct 22, 2016 5:07 am |
|
|
Hello Ttelmah,
Thank you for reply.
I tried looking in the examples (v5.061) but I could not find any HSPWM examples.
My dspic is 33EP512GM604, and I use these lines:
Code: |
setup_hspwm (HSPWM_ENABLED | HSPWM_CLOCK_DIV_BY_1, 1000);
setup_hspwm_unit (1 HSPWM_ENABLE | HSPWM_FAULT_MODE_DISABLED | HSPWM_H_POLAIRTY_HIGH | HSPWM_L_POLARITY_LOW | HSPWM_COMPLEMENTARY | HSPWM_TIME_BASE_FROM_PHASE_REGS);
set_hspwm_phase (1,130);
set_hspwm_duty (1,224);
|
but without any PWM signal.
I also also included these lines:
Code: |
while(1)
{
output_high(pin_led1);
delay_ms(1000);
output_low(pin_led1);
delay_ms(1000);
} |
and the LED blinks as expected then the clock is corrected and set to 140MHz.
Last edited by Donatello on Sun Oct 23, 2016 2:49 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Oct 22, 2016 7:16 am |
|
|
while I don't use that PIC...
is this line...
setup_hspwm_unit (1 HSPWM_ENABLE | HSPWM_FAULT_MODE_DISABLED | HSPWM_H_POLAIRTY_HIGH | HSPWM_L_POLARITY_LOW | HSPWM_COMPLEMENTARY | HSPWM_TIME_BASE_FROM_PHASE_REGS);
correct as far as syntax( proper 'format') goes ??
I ask as the first program has a comma between 1 and the next option.
Jay |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Oct 22, 2016 10:00 am |
|
|
The ex_hspwm.c referenced by TTelmah was apparently added
in 5.064 because I didn't have it either until I upgraded.
The example uses the 33EP512MC502. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19511
|
|
Posted: Sun Oct 23, 2016 1:21 am |
|
|
Hadn't realised that example was quite so recent....
I'd simply ask CCS for it. They will normally give an example like this, and then you have a 'starting point' to work from.
As further comments/questions, what chip?. What compiler version?. What frequency do you want to generate, on what pins?. etc. etc.. |
|
|
Donatello
Joined: 21 Oct 2016 Posts: 22
|
|
Posted: Sun Oct 23, 2016 2:48 am |
|
|
I use dspic33EP512GM604@140MHz (8MHz crystal) with PCWHD v5.061, my goal is to generate a 1MHz complementary signals on PWM1H (pin14) and PWM1L (pin15).
I used an LED (pin10) that flashes to check if the PIC is working properly. I can check the PWM signal with an oscilloscope. The watchdog is disabled.
If I use these line:
setup_hspwm(HSPWM_ENABLED | HSPWM_CLOCK_DIV_BY_4, 0x1000);
00596: MOV #2,W4
00598: MOV W4,C02
0059A: MOV C00,W0
0059C: MOV #580F,W1
0059E: AND W0,W1,W0
005A0: MOV #8000,W1
005A2: IOR W0, W1,W0
005A4: MOV W0,C00
005A6: MOV #1000,W4
005A8: MOV W4,C04
the dsPIC freezes and the LED remains off. I tried changing the settings (prescaler or period value) but without success.
If I use these line:
setup_hspwm(HSPWM_DISABLED | HSPWM_CLOCK_DIV_BY_4, 0x1000);
00596: MOV #2,W4
00598: MOV W4,C02
0059A: MOV C00,W0
0059C: MOV #580F,W1
0059E: AND W0,W1,W0
005A0: MOV #0,W1
005A2: IOR W0, W1,W0
005A4: MOV W0,C00
005A6: MOV #1000,W4
005A8: MOV W4,C04
The LED is blinking but the HSPWM module is disabled. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19511
|
|
Posted: Sun Oct 23, 2016 3:06 am |
|
|
The reason for this, is the setup for the module must be after the setup for the unit.
Problem is that the chips always wake with the fault enabled. This is done so that the PWM won't start till it is configured. The fault has to be cleared first, and the setup_hspwm_unit code does this.
So what is happening is when you enable the PWM module it immediately errors.
I think given that the example really ought to be there on the earlier compiler, I'll post the three setup lines from this:
Code: |
setup_hspwm_unit(1, HSPWM_ENABLE | HSPWM_REDUNDANT | HSPWM_TIME_BASE_FROM_PHASE_REGS | //Enable PWM1L and PWM1H with same signal and faults disabled.
HSPWM_FAULT_MODE_DISABLED); //Frequency set with phase register, independent from Master Period register (PTPER).
//Duty cycle set with duty cycle register, independent from Master Duty Cycle register (MDC).
setup_hspwm(HSPWM_ENABLED | HSPWM_CLOCK_DIV_BY_2, 0xFFFF); //HSPWM enabled, PWM Clock = Fosc/2
set_hspwm_phase(1, PwmPeriod - 1); //Set the frequency of the PWM signal.
|
PwmPeriod, is 3200, for 5KHz in the example (running at 32Mhz). |
|
|
Donatello
Joined: 21 Oct 2016 Posts: 22
|
|
Posted: Wed Oct 26, 2016 4:20 am |
|
|
Thanks for the advices everyone. I solved by inserting these lines:
Code: | setup_hspwm_unit(1, HSPWM_ENABLE | HSPWM_REDUNDANT | HSPWM_TIME_BASE_FROM_PHASE_REGS | HSPWM_FAULT_MODE_DISABLED|HSPWM_CENTER_ALIGN_MODE);
setup_hspwm_unit(2,HSPWM_DISABLED);
setup_hspwm_unit(3,HSPWM_DISABLED);
setup_hspwm_unit(4,HSPWM_DISABLED);
setup_hspwm_unit(5,HSPWM_DISABLED);
setup_hspwm_unit(6,HSPWM_DISABLED);
set_hspwm_phase(1, 22); //Set the frequency of the PWM signal.
set_hspwm_duty(1,10);
setup_hspwm(HSPWM_ENABLED | HSPWM_CLOCK_DIV_BY_1 , 0x0000);
|
The instructions order is important. Disable unused units, otherwise it can not be used for I/O ports. |
|
|
shirke
Joined: 08 Nov 2016 Posts: 11 Location: Hungary
|
HSPWM |
Posted: Sun Dec 04, 2016 3:02 pm |
|
|
Hi,
Sorry, I don't understand it. How can I set the PWM frequency and duty cycle? Can you show me an example?
Best Regards,
Shirke _________________ shirke |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Dec 04, 2016 3:35 pm |
|
|
shirke
1) read the datasheet for the PIC, especially the 'PWM' section
2) read the compiler manual, again lookup PWM...
3) look at the example that CCS supplies....
4) look at the PIC 'header' to see what options are used...
5) cut minimal code and see what happens
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19511
|
|
Posted: Mon Dec 05, 2016 2:32 am |
|
|
3) here is particularly important. If you read the example that I refer to earlier, it has notes in the 'comments' explaining how the figures are calculated. |
|
|
shirke
Joined: 08 Nov 2016 Posts: 11 Location: Hungary
|
|
Posted: Mon Dec 05, 2016 6:27 am |
|
|
Ttelmah wrote: | 3) here is particularly important. If you read the example that I refer to earlier, it has notes in the 'comments' explaining how the figures are calculated. |
Ok. And where can I find that example? In the example folder? _________________ shirke |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Dec 05, 2016 7:51 am |
|
|
Yes, the examples are in the examples folder...
The program names may not be real informative, but generally do say what's inside....
ie ex_sisr.c is the ex(ample) of the s(erial) i(nterrupt) s(ervice) r(outine).
Also while some are to demonstrate 'something', they usually contain information about 'other' features of the PICs and compiler.
It is very good time spent opening and looking at ALL of the examples !
and...
if you press F11 while your project is open, the CCS manual 'magically' appears.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19511
|
|
Posted: Mon Dec 05, 2016 8:02 am |
|
|
and as a general comment, even people who have used the PIC's for years will sometimes find themselves referring back to the samples, and (of course), a simple 'search', using 'HSPWM' as the search parameter, will quickly find the ones that apply to this.
There are some 'old favourites', where the examples can mislead (unfortunately, CCS omit 'ERRORS' on quite a few of the RS232 setups, use 'ADC_CLOCK_INTERNAL', where really they shouldn't, etc...), but 90% show the basics of how to use just about every feature. |
|
|
|
|
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
|