|
|
View previous topic :: View next topic |
Author |
Message |
Billy Guest
|
Pic18f8722 PWM Problem |
Posted: Sat Mar 03, 2007 12:50 pm |
|
|
I'm trying to get a PIC 81f8722 to produce PWM on CCP4 and 5 but nothing is happening. The code is below (Mostly generated by the PIC wizard) any ideas what I'm doing wrong?
Code: | #include <18F8722.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES H4 //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT //Code not protected from reading
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES PUT //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 NOCPB //No Boot Block code protection
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOEBTR //Memory not protected from table reads
#FUSES CCP2C1 //CCP2 input/output multiplexed with RE7
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode enabled
#FUSES MCU //Microcontroller Mode
#use delay(clock=32000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
void main()
{
int16 Power;
setup_adc_ports(AN0_TO_AN7|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_2);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_spi2(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_4,255,1);
setup_timer_3(T3_INTERNAL|T3_DIV_BY_2);
setup_timer_4(T4_DIV_BY_4,255,1);
//setup_ccp1(CCP_PWM,CCP_PWM_HALF_BRIDGE);
//setup_ccp2(CCP_PWM,CCP_PWM_HALF_BRIDGE);
//setup_ccp3(CCP_PWM,CCP_PWM_HALF_BRIDGE);
setup_ccp4(CCP_PWM);
setup_ccp5(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_RDA);
//enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
power = 0;
while(1)
{
set_pwm4_duty( power );
power++;
if(power > 1023) power = 0;
delay_ms(10);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 04, 2007 2:37 pm |
|
|
Quote: | enable_interrupts(INT_RDA);
//enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL); |
In the code above, you're enabling interrupts but you don't have an
#int_rda routine. What happens if you get an interrupt ? The PIC will
jump to the interrupt vector, but there won't be interrupt dispatcher
there and there also won't be an int_rda isr. The program will crash
or behave erratically. Delete those lines.
What you really need to do, is to create a very small test program.
In your posted code, you're trying to do too many things. You're
trying to run in the 4x PLL mode. You're setting up all sorts of
PIC modules that are not related to PWM. You're trying to increment
the duty cycle over a period of time. All of this is irrelevant to
the problem at hand, which is to just get the CCP4 pin to produce
any PWM output. Once you get that working, then you can proceed
with adding in all the other stuff.
Here is a test program for CCP4. I don't have an 18F8722 to test.
But I compiled this program with PCH vs. 4.025 and looked at the
.LST file and it looks OK. I think it will work. It should produce a
square wave on pin G3 of the 18F8722.
Code: |
#include <18F8722.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=8000000)
//========================
void main()
{
setup_timer_2(T2_DIV_BY_1, 255, 1);
setup_ccp4(CCP_PWM);
set_pwm4_duty(128); // 50% duty cycle
while(1);
} |
|
|
|
Billy Guest
|
|
Posted: Mon Mar 05, 2007 4:09 am |
|
|
Ok, thanks and yes I should have tried simpler code (There was an interrupt function I just forgot to paste it in)
It turns out that it is because I'm using V4.017 and there is a fix listed in 4.018 for CCP problems on some parts (I presume mine is one of them) I tried compiling with my pre 4.0 version and it worked fine. |
|
|
|
|
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
|