|
|
View previous topic :: View next topic |
Author |
Message |
Hicham
Joined: 09 Feb 2010 Posts: 17
|
Hard to figure out Power control PWM module in pic18F4331 |
Posted: Sat Mar 20, 2010 3:31 pm |
|
|
Hello,
I'm having some problem in setting up the pwm module in pic 18F4331.
So, i was wondering if anybody here can help me out.
Here is my code, please take a look at it.
Code: |
#include "D:\documents\CCS Pic Project\PWM power motion\pwm.h"
#DEFINE postscale 3
#define time_base 1000
#define period 3999
#define compare 0
#define compare_postscale 0
#define dead_time 0
#define duty_cycle0 2000
#define duty_cycle2 1000
#define duty_cycle4 3000
#define duty_cycle6 500
boolean n=0;
unsigned int16 dcycle0=0;
#int_RTCC
void RTCC_isr(void)
{ if (n=0)
output_high(PIN_A0),n=1;
else
output_low(PIN_A0),n=0;
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
// TODO: USER CODE!!
setup_power_pwm (PWM_FREE_RUN,postscale, time_base, period, compare,
compare_postscale, dead_time);
setup_power_pwm_pins(PWM_ODD_ON,PWM_OFF,PWM_OFF,PWM_OFF );
while(1)
{ set_power_pwm0_duty(dcycle0);
if (dcycle0<= 4000)
dcycle0+=100;
else
dcycle0=0;
output_high(PIN_A0);
delay_ms(5);
output_low(PIN_A0);
delay_ms(5);
}
}
|
The output_high/low stuff is just to check if the pic works.
And I also have few questions:
In the datasheet (page 195) they use this formula, to calculate the PWM period:
TPWM =((PTPER + 1) x PTMRPS)(FOSC/4)
and the term PTMRPS appears just in this page.
This term has 2 possibilities
1) PWM Timer Prescale (2,4,16,64) means Fosc/(4,16,64,256)
2) PWM Timer Postscale (1-16)
And I guess its postscale, since in CCS Help they use the term posctscale.
If it is so, where is the prescale ???? in CCS ???
Is it the PWM_CLOCK_DIV (4,16,64,128)??? so the last division is wrong, its 256 not 128 ???
And about the parameter compare (0-225), it doesn't exist in the datasheet so far, the special event trigger has just postscale between (1-16) ???
Here is the help for the function setup_power_pwm
Quote: |
Syntax:
setup_power_pwm(modes, postscale, time_base, period, compare, compare_postscale, dead_time)
Parameters:
Modes values may be up to one from each group of the following:
PWM_CLOCK_DIV_4, PWM_CLOCK_DIV_16,
PWM_CLOCK_DIV_64, PWM_CLOCK_DIV_128
PWM_OFF, PWM_FREE_RUN, PWM_SINGLE_SHOT,
PWM_UP_DOWN, PWM_UP_DOWN_INT
PWM_OVERRIDE_SYNC
PWM_UP_TRIGGER,
PWM_DOWN_TRIGGER
PWM_UPDATE_DISABLE, PWM_UPDATE_ENABLE
PWM_DEAD_CLOCK_DIV_2,
PWM_DEAD_CLOCK_DIV_4,
PWM_DEAD_CLOCK_DIV_8,
PWM_DEAD_CLOCK_DIV_16
Postscale is an integer between 1 and 16. This value sets the PWM time base output postscale.
time_base is an integer between 0 and 65535. This is the initial value of the PWM base.
Period is an integer between 0 and 4095. The PWM time base is incremented until it reaches this number.
Compare is an integer between 0 and 255. This is the value that the PWM time base is compared to, to determine if a special event should be triggered.
compare_postscale is an integer between 1 and 16. This postscaler affects compare, the special events trigger.
dead_time is an integer between 0 and 63. This value specifies the length of an off period that should be inserted between the going off of a pin and the going on of it is a complementary pin.
|
So guys, if you have any idea or had use it before, it will be much appreciated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 21, 2010 1:27 pm |
|
|
It would be easier if you would give a list of the timing and features that
you would like to see in the PWM waveform.
Currently, the way your post is written, you want us to research the
Power PWM module for 1 hour and then write up an improved manual
page. Remember, we don't work for CCS. This forum is all "users".
I would rather that you say that you want, say, 15 KHz PWM frequency,
with "x" duty cycle, and "y" deadtime, etc. Then I would just look at
the manual, make a test program, plug in some reasonable-looking
numbers, compile and run it (in hardware), look at the signal with an
oscilloscope, and then make adjustments if required. If it didn't work, I
would look at the .LST file and make sure the compiler was writing to the
correct registers, with the correct values. (i.e., I would look for compiler
bugs).
But you can do most, or maybe all of that, too. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sun Mar 21, 2010 1:53 pm |
|
|
What compiler version?.
The misprints you mention (spelling of postscale, and /256, rather than /128), don't exist in any of the versions of V4, that are modern enough to actually work....
I went back to 4.070 (about the oldest V4 compiler that has a reasonable chance of working), and both are OK.
Seriously, it sounds as if you have a very old version, and I know that on these the power PWM on the 4331 does not configure properly.
Best Wishes |
|
|
Hicham
Joined: 09 Feb 2010 Posts: 17
|
|
Posted: Mon Mar 22, 2010 2:13 am |
|
|
Thanks PCM programmer,
For the testing, I can't use the lab everyday and I'm waiting my turn .
And I really want to know how it works, not just get the values and put them in my program. And what I really want is to find someone who used this module before, and know the basics of it.
-----------------------------------------
Ttelmah
I'm using v 4.088.
Do you mean, you don't have this configuration in newest version ?
Maybe they didn't updated correctly. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Mar 22, 2010 3:20 am |
|
|
On 4.088, the manual, doesn't have the term 'posctscale', and the includes for the prescale dividers, are:
Code: |
// Constants used for SETUP_POWER_PWM() are:
#define PWM_CLOCK_DIV_4 0x00
#define PWM_CLOCK_DIV_16 0x04
#define PWM_CLOCK_DIV_64 0x08
#define PWM_CLOCK_DIV_128 0x0C
|
No mention of the 256, you refer to.
All you do, is 'or' the one of these you want, with the other stuff for the first value, so:
PWM_CLOCK_DIV_16|PWM_FREE_RUN
Gives you a free running clock, with /16 prescaler.
Are you sure your compiler is correctly loading the include files and manual, for the version you are using, not an older version?. Remember if you 'built' the project originally with (say) 4.030, the include paths in the project, will be for this. You need to go to the project options, and select the include page, delete the current entries, and create new ones pointing to the subdirectory for the current compiler.
The point I am making, is that two of the errors you refer to, _should not be happening with 4.088_, and if they are, it says 'something is wrong with your setup'.
Now, I have the 4331, running a H bridge, and the configurations used were:
Code: |
setup_power_pwm(PWM_CLOCK_DIV_4|PWM_FREE_RUN|PWM_DEAD_CLOCK_DIV_2|PWM_OVERRIDE_SYNC,1,0,255,0,1,0);
setup_power_pwm_pins(PWM_ODD_ON,PWM_ODD_ON,PWM_OFF,PWM_OFF);
|
Best Wishes |
|
|
Hicham
Joined: 09 Feb 2010 Posts: 17
|
|
Posted: Tue Mar 23, 2010 5:10 am |
|
|
@Ttelmah ,
Thanks for your interest, like i said i have CCS compiler 4.088 and the manual is shown in my first post. and this is my first compiler. never used CCS before. do you have a different manual than mine in your version ?
for the prescale ,in the datasheet (p.188) you will find this
Code: |
bit 3-2 PTCKPS1:PTCKPS0: PWM Time Base Input Clock Prescale Select bits
00 = PWM time base input clock is FOSC/4 (1:1 prescale)
01 = PWM time base input clock is FOSC/16 (1:4 prescale)
10 = PWM time base input clock is FOSC/64 (1:16 prescale)
11 = PWM time base input clock is FOSC/[b]256[/b] (1:64 prescale)
|
this means the prescale will divide the the clock by 4,16,64,256 , right ?
but in the CCS manual like you mention it ,the prescale divide the clock by 4,16,64,128 ??
so here is my questions: is there a division by 256 or 128 ?
and, does the prescale effect the frequency or not ?
for the postscale, if you search in the manual "power pwm"
you can find the formula that they use
Frequency = Fosc / (4 * (period+1) *postscale)
----------------------------------
by the way.
i tested the pwm in hardware ,and got pretty good PWM ,but i didn't check the duty,frequency and other stuff..maybe i'll today
I hope you'll take some time and help me with this |
|
|
|
|
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
|