View previous topic :: View next topic |
Author |
Message |
hunterdd
Joined: 29 Mar 2014 Posts: 6
|
using power control module in pic 18F2431 |
Posted: Sat Mar 29, 2014 11:03 am |
|
|
hi everybody
I was wondering if anbody had used this module and if possible he can provide me with an exemple written in C to use it.
Actually I came to make it work (pwm0 activated and pwm2 is deactivated), but when I try to invert (pwm2 activated and pwm0 deactivated) it doesn't work and I don't know why??!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
hunterdd
Joined: 29 Mar 2014 Posts: 6
|
|
Posted: Wed Apr 02, 2014 4:08 am |
|
|
The version of my compiler is 4.140 and here's my test program :
Code: |
#include <18F2431.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
set_tris_b(ALL_out);
int16 pos;
#define POWER_PWM_PERIOD 999 // 1 KHz pwm freq with 8 MHz osc.
#int_IC3DR
void IC3DR_isr(void)
{
}
#int_IC2QEI
void IC2QEI_isr(void)
{
}
void avance()
{
setup_power_pwm_pins(PWM_ODD_ON,PWM_OFF,PWM_OFF,PWM_OFF);
set_power_pwm0_duty(100);
set_power_pwm2_duty(0);
}
void recul()
{
setup_power_pwm_pins(PWM_ODD_ON,PWM_ODD_ON,PWM_OFF,PWM_OFF);
set_power_pwm0_duty(0);
set_power_pwm2_duty(100);
}
void PID(int x)
{
}
void main()
{
setup_timer_5(T5_INTERNAL | T5_DIV_BY_4);
enable_interrupts(INT_IC3DR);
enable_interrupts(INT_IC2QEI);
enable_interrupts(GLOBAL);
// Setup the 4 Power PWM channels as ordinary pwm channels.
setup_qei(QEI_MODE_X4_RESET_ON_MATCH | QEI_VELOCITY_MODE_ENABLED | QEI_VELOCITY_PULSE_DIV_4 ,QEI_FILTER_ENABLE_QEB | QEI_FILTER_ENABLE_QEA,50000);
setup_power_pwm(PWM_FREE_RUN, 1, 0, POWER_PWM_PERIOD, 0, 1,50);
while(TRUE)
{
pos=qei_get_count(QEI_GET_POSITION_COUNT);
output_low(PIN_C0);
avance();
if (pos>16)
{
delay_ms(50);
recul();
}
}
}
|
The main problem is in the "recul()" fonction ??!! |
|
|
hunterdd
Joined: 29 Mar 2014 Posts: 6
|
|
Posted: Mon Apr 07, 2014 4:12 pm |
|
|
any suggestion ?? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 07, 2014 4:44 pm |
|
|
Quote: | #include <18F2431.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
set_tris_b(ALL_out);
int16 pos;
|
Where are your #fuses ? They're missing. Is this Proteus ? Proteus
doesn't care about fuses. That suggests it is actually Proteus.
2nd thing. You have a CCS function (set_tris_b) just stuck out in space.
It won't do anything there. Functions have to be within main() or some
other function. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Apr 07, 2014 6:05 pm |
|
|
what is the thinking behind your two ISR servicing routines ?
as is,
there is "no there , there "
|
|
|
hunterdd
Joined: 29 Mar 2014 Posts: 6
|
|
Posted: Mon Apr 07, 2014 11:57 pm |
|
|
thank's for replying
first of all for the fuses they are included in the .h file so no need to do the same work over and over
2nd for the set_trib_b() yes it's a mistake but even with that my program work as expected exept the handeling of the pwm module that i cant come to correctly configurate it !! :(
for the other answer ISR() fonctions here are not needed in this phase !!!
i just want to make the pwm work correctly and then i will see the other features of the algorithm ;) |
|
|
hunterdd
Joined: 29 Mar 2014 Posts: 6
|
|
Posted: Tue Apr 08, 2014 12:05 am |
|
|
So the main problem is that i can't shutdown pwm0 and make only pwm2 work because this combination is not supported with this compiler and i don't know why and even if set the duty cycle of pwm0 to have it shut!!! It does give an output signal instead:
setup_power_pwm_pins(PWM_OFF,PWM_BOTH_ON,PWM_OFF,PWM_OFF); |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Tue Apr 08, 2014 12:45 am |
|
|
Don't blame the compiler.
Always start by reading the data sheet. Is what you want to do supported by the chip?.
Code: |
111 =All odd PWM I/O pins enabled for PWM output.
110 =PWM1, PWM3 pins enabled for PWM output.
101 =All PWM I/O pins enabled for PWM output.
100 =PWM0, PWM1, PWM2, PWM3, PWM4 and PWM5 pins enabled for PWM output.
011 =PWM0, PWM1, PWM2 and PWM3 I/O pins enabled for PWM output.
010 =PWM0 and PWM1 pins enabled for PWM output.
001 =PWM1 pin is enabled for PWM output.
000 =PWM module disabled. All PWM I/O pins are general purpose I/O.
|
PWMCON0 register settings for the chip.
As you can see if you look at the list there is no option that enables PWM2 and not PWM0.
Always start with what the chip can do. The compiler can't do anything the chip can't.
What you can do, is use the 'output override' ability to turn off output 0.
set_power_pwm_override(0,TRUE,0);
Will disable the PWM0 output. |
|
|
hunterdd
Joined: 29 Mar 2014 Posts: 6
|
|
Posted: Thu Apr 10, 2014 3:17 am |
|
|
Thanks Ttelmah I actually managed to do what i want using the duty cycle of the PWM as my weapon 3
But still the overide function would be better in this case as you suggested ;)
thanks to your answer i knew how to use it correctly :D |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Apr 10, 2014 5:24 am |
|
|
hunterdd wrote: | first of all for the fuses they are included in the .h file so no need to do the same work over and over | Ehhh...
You modified the original CCS supplied header file to be efficient???
This is bad practice!
Have you considered what will happen on installing the next compiler update?
What will happen when you start a 2nd project that requires other fuse settings?
What will happen when you hand your project to someone else?
Please keep all project specific settings inside your project directory. Don't modify the original compiler supplied files. |
|
|
|