View previous topic :: View next topic |
Author |
Message |
mbalderas Guest
|
PWM, who is wrong? |
Posted: Sun Oct 11, 2009 10:31 pm |
|
|
With a PIC18F2550, I want to create a PWM.
I don't know what is going on... I just want to know if this is an issue of the compiler or, what's wrong. I want to pass the value of the duty cycle through a variable.
Like in:
Code: |
int16 var;
var=100;
set_pwm1_duty(var);
|
But this doesn't work, the compiler doesn't find errors but in the real world, the output is always low. If I do it with a constant it works fine.
Code: |
set_pwm1_duty(100);
|
Has anyone experienced something similar? How to know if the compiler's wrong? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 11, 2009 10:38 pm |
|
|
Quote: |
int16 var;
var=100;
set_pwm1_duty(var);
|
In this case you are using 10-bit mode for the PWM, because you
declared 'var' as 16 bits.
Quote: | set_pwm1_duty(100); |
In this case you are using 8-bit mode.
Quote: |
int8 var;
var=100;
set_pwm1_duty(var);
|
To make the 1st case be the same as the 2nd case, you need to
declare 'var' as 8 bits, as shown above in bold. |
|
|
mbalderas Guest
|
|
Posted: Sun Oct 11, 2009 11:06 pm |
|
|
Thanks PCM Prog, but it still doesn't go... Is there any way to test it assigning the values directly to the CCPR1L and CCP1CON? Because I'm guessing the compiler is the issue.
Thanks for your time |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
mbalderas Guest
|
|
Posted: Sun Oct 11, 2009 11:25 pm |
|
|
4.038 |
|
|
mbalderas Guest
|
|
Posted: Sun Oct 11, 2009 11:42 pm |
|
|
Look:
Code: |
#use delay(clock=8000000)
void main()
{
int8 var;
set_tris_a(0xFF);
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);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,255,16);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
var=50;
set_pwm1_duty(var);
while(1);
}
} |
But if I assign it directly:
Works. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 12:18 am |
|
|
1. Post your fuses statement.
2. Are you testing this on a board, or is it in Proteus ? |
|
|
mbalderas Guest
|
|
Posted: Mon Oct 12, 2009 9:21 am |
|
|
Code: | #include <18F2550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES MCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES XINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL2 //Divide By 2(8MHz oscillator input)
#FUSES CPUDIV1 //No System Clock Postscaler
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#use delay(clock=8000000) |
I'm using Proteus before going to the board.
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 11:36 am |
|
|
Don't use XINST. It will make your program behave erratically.
Always use NOXINST. |
|
|
mbalderas Guest
|
|
Posted: Mon Oct 12, 2009 2:10 pm |
|
|
Gosh, you're a genius! It's working now!
What is that fuse for? I didn't write it, it came from the Wiz... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 2:14 pm |
|
|
XINST enables the Extended Instruction Set in the 18F2550. But the
CCS compiler doesn't support it. It's a bug in the Wizard to enable it. |
|
|
|