View previous topic :: View next topic |
Author |
Message |
andys
Joined: 23 Oct 2006 Posts: 175
|
3 PWM |
Posted: Sun Jan 21, 2007 12:37 pm |
|
|
I wrote the code bellow to generate 3 channel PWM. Is anyone who knows what is going wrong, because does not work ?
Code: |
#include <18F4550.h>
#device ADC=16
#USE delay(CLOCK=4000000)
#fuses XT,NOWDT,NOPROTECT,NOLVP
void main()
{
long adc_result;
while(1){
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_div_16);
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
setup_ccp1(CCP_PWM_H_H);
setup_timer_2(T2_DIV_BY_1, 255, 1);
delay_ms(1);
adc_result=read_adc();
delay_ms(1);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_div_16);
set_adc_channel(1);
delay_ms(1);
adc_result=read_adc();
setup_ccp1(CCP_PWM_H_L);
setup_timer_2(T2_DIV_BY_1, 255, 1);
delay_ms(1);
adc_result=read_adc();
delay_ms(1);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_div_16);
set_adc_channel(2);
setup_ccp1(CCP_PWM_L_H );
setup_timer_2(T2_DIV_BY_1, 255, 1);
delay_ms(1);
adc_result=read_adc();
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 21, 2007 2:56 pm |
|
|
You can't get 3 PWM channels with a different duty cycle for each
channel with the 18F4550. Basically, the 18F4550 has two PWM
modules available. The CCP1 module can be used to run a Half-Bridge
or Full-Bridge motor controller, and uses 4 output pins on the PIC.
The CCP2 can have a single PWM output.
Here's an example program. This program runs a full H-Bridge
on pins P1A, P1B, P1C, and P1D. It switches between running
the H-Bridge in the forward direction and then in reverse.
At the same time, the CCP2 is used to run a normal single PWM
channel on the CCP2 pin. Both PWM modules run off Timer2,
so they have the same frequency.
Code: |
#include <18F4550.h>
#fuses XT,NOWDT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
//=================================
void main()
{
// Setup the ECCP for PWM in full bridge mode.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty(100);
// Setup CCP2 for PWM output on pin C1.
setup_ccp2(CCP_PWM);
set_pwm2_duty(50);
// Switch the H-Bridge outputs between forward
// and reverse every 5 seconds.
while(1)
{
// Run forward for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
delay_ms(5000);
// Run in reverse for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE_REV | CCP_PWM_H_H);
delay_ms(5000);
}
} |
|
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
3 pwm |
Posted: Mon Jan 22, 2007 4:53 am |
|
|
I try to use the code you write but i have this error:
Undefined identifier CCP_PWM_FULL_BRIDGE
Undefined identifier CCP_PWM_FULL_BRIDGE
Undefined identifier CCP_PWM_FULL_BRIDGE_REV
I use the PCH compiler. How to write the code for this compiler. Please help |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
3 pwm |
Posted: Mon Jan 22, 2007 6:17 am |
|
|
I use pwm with a low pass filter to generate the analog signal.I want 3 PWM because i have 3 diferend signal input.Can i managment the PWM like a Full-Bridge motor controller??????? |
|
|
Ttelmah Guest
|
|
Posted: Mon Jan 22, 2007 8:55 am |
|
|
No.
Simplest solution, is probably to add an external I2C, or SSP driven PWM controller.
The chip _only has two PWM's available_. One supports up to four outputs, but these outputs will have the same frequency/width (except for a small adjustment introduced to ensure both drivers are not on at once). So though you can generate five 'signals', the available timings and widths, only give two output values supported.
Look at something like the DS1050, if you do not need a huge resolution.
Best Wishes |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
3 PWM |
Posted: Wed Jan 24, 2007 5:47 pm |
|
|
Quote: |
You can't get 3 PWM channels with a different duty cycle for each
channel with the 18F4550. Basically, the 18F4550 has two PWM
modules available. The CCP1 module can be used to run a Half-Bridge
or Full-Bridge motor controller, and uses 4 output pins on the PIC.
The CCP2 can have a single PWM output.
Here's an example program. This program runs a full H-Bridge
on pins P1A, P1B, P1C, and P1D. It switches between running
the H-Bridge in the forward direction and then in reverse.
At the same time, the CCP2 is used to run a normal single PWM
channel on the CCP2 pin. Both PWM modules run off Timer2,
so they have the same frequency.
Code:
#include <18F4550.h>
#fuses XT,NOWDT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
//=================================
void main()
{
// Setup the ECCP for PWM in full bridge mode.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty(100);
// Setup CCP2 for PWM output on pin C1.
setup_ccp2(CCP_PWM);
set_pwm2_duty(50);
// Switch the H-Bridge outputs between forward
// and reverse every 5 seconds.
while(1)
{
// Run forward for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
delay_ms(5000);
// Run in reverse for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE_REV | CCP_PWM_H_H);
delay_ms(5000);
}
} |
Hi PCM Programmer ,
I tried to used this code but is not working. Is this all code or I must to but and other code???? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 24, 2007 5:57 pm |
|
|
Post your compiler version. This will be a number such as 3.191, 3.249,
or 4.023, etc. You can find it at the top of the .LST file (after a successful
compilation). The .LST file will be in your project directory.
Just post the version number. Don't post any numbers that come after it. |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
3 PWM |
Posted: Thu Jan 25, 2007 4:34 am |
|
|
Compiler version is 3.187 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 25, 2007 1:16 pm |
|
|
I installed PCH 3.187 and it has no support for the 18F4550. It doesn't
have a .H file for it. |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
3 pwm |
Posted: Thu Jan 25, 2007 1:33 pm |
|
|
So I can't use as example your code. Is it possible to modify to work on this compiler or is not possibility to work this code ? |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
3 pwm |
Posted: Fri Jan 26, 2007 4:36 am |
|
|
Hello PCM programmer,now i have the 3.216 compiler but this code does not work |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 28, 2007 12:33 pm |
|
|
I don't have that version. The closest I have after that one is 3.230.
So I can't really help you with problems with that version. |
|
|
|