Stuffed-Bit
Joined: 07 Jul 2006 Posts: 9 Location: US
|
PWM Duty won't drop to 0% |
Posted: Sat Jan 09, 2010 11:35 am |
|
|
Hoping some of the experts may be able to explain this to me. I'm probably missing something simple. I searched but there are so many PWM post I couldn't find an answer, sorry if it has already been answered. In the sample code line 51 sets the PWM to 100%, this works. But in lines 58 and 66 the PWM does not drop all the way back to 0%. Both LSB's stays set. But in line 74 the PWM does drop back to 0%, all bits cleared. So what is the difference between an 8-bit zero and a 16-bit zero? The manual states "If value is an 8 bit item, it is shifted up with two zero bits in the lsb positions to get 10 bits" but this appears not to happen when I use 0x00 as a value, only the upper 8 bits are cleared (CCPR2L). Shouldn't an 8-bit or 16-bit zero clear the two LSB's? Stepping through the code in MPLAB the CCP2CON<5:4> bits are only cleared when using a 16-bit zero. Am I missing something?
NOTE: I'm sending the PWM to RB3 with fuse CCP2B3
Hardware: 18F252
Compiler: 4.092
MPLAB: 8.30 w/ICD2
Code: |
#include <18F252.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //Power Up Timer
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES DEBUG //Debug mode for use with 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 NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOCPD //No EE protection
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES CCP2B3 //CCP2 input/output multiplexed with RB3
#use delay(clock=20000000,RESTART_WDT)
int8 temp8;
int16 temp16;
void main(){
setup_adc_ports(NO_ANALOGS);
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,196,16);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_PWM);
disable_interrupts(INT_TIMER2);
disable_interrupts(GLOBAL);
while(true){
output_low(pin_a2); //mark time
delay_ms(250); //mark time long pulse top
output_high(pin_a2); //mark time
set_pwm2_duty(1023); //Set PCM to 100%
delay_cycles(1); //nop breakpoint location for use in MPLAB
delay_ms(5000); //wait to view results
output_low(pin_a2); //mark time
delay_ms(50); //mark time
output_high(pin_a2); //mark time
set_pwm2_duty(0x00); //Set PCM to 0% <<<<<<<< Does NOT go to 0%
delay_cycles(1); //nop breakpoint location for use in MPLAB
delay_ms(5000); //wait to view results
output_low(pin_a2); //mark time
delay_ms(50); //mark time
output_high(pin_a2); //mark time
temp8=0; //set 8-bit variable to 0
set_pwm2_duty(temp8); //Set PCM to 8-bit 0% <<<<<<<< Does NOT go to 0%
delay_cycles(1); //nop breakpoint location for use in MPLAB
delay_ms(5000); //wait to view results
output_low(pin_a2); //mark time
delay_ms(50); //mark time
output_high(pin_a2); //mark time
temp16=0; //set 16-bit variable to 0
set_pwm2_duty(temp16); //Set PCM to 16-bit 0% <<<<<<<< DOES go to 0%
delay_cycles(1); //nop breakpoint location for use in MPLAB
delay_ms(5000); //wait to view results
} //end while(true)
} //end main()
|
|
|