![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
acm45
Joined: 29 Dec 2008 Posts: 18 Location: Italy
|
PWM and interrupts |
Posted: Sun Jun 02, 2013 10:48 am |
|
|
Hi,
I'm using the 18F4550, tring to use the CCP2 on PWM mode but
I'm having some troubles with the following simple code: the code configure CCP2 in PWM mode with the frequency and duty cycle that I need.
After the configuration the code make the 2 led blink then... after 1s the pic reboots, I can't understand that behaviour.
If I comment the Timer0 interrupt the problem is solved but I need it as I want to update the pwm duty cycle periodically.
I think I'm missing something about the configuration of timers, but what?
Thank you for any suggestion
Regards
Code: |
#include <18F4550.h>
#device adc=8
#device ICD=TRUE
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES PLL5 //Divide By 5(20MHz oscillator input)
#FUSES CPUDIV4 //System Clock by 1
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES HS//HSPLL //High Speed Crystal/Resonator with PLL enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES VREGEN //USB voltage regulator enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
//#FUSES ICPRT //ICPRT enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODEBUG //Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPB //No Boot Block code protection
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES ICSP1
// CCP2 on pin B3
#fuses CCP2B3
#use delay(clock=5000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)
// LED Definition
#define LED1 PIN_B4
#define LED2 PIN_B5
#INT_RDA
void serial_isr()
{
return;
}
#int_TIMER0
void TIMER0_isr(void)
{
return;
}
void main()
{
SET_TRIS_B( 0x00 ); // B all outputs
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
setup_timer_2(T2_DIV_BY_16, 192, 1);
setup_ccp1(CCP_OFF); // Configure CCP1 as disabled
setup_ccp2(CCP_PWM); // Configure CCP2 as PWM
set_pwm2_duty(64);
for (index = 0; index < 3; index++)
{
output_high(LED1);
output_low(LED2);
delay_ms(500);
output_high(LED2);
output_low(LED1);
delay_ms(500);
}
setup_comparator(NC_NC_NC_NC);
//Enable interrupts
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while (1)
{
} // Close Main
|
|
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sun Jun 02, 2013 11:23 am |
|
|
Code: |
void main() {
SET_TRIS_B( 0x00 ); // B all outputs
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
setup_timer_2(T2_DIV_BY_16, 192, 1);
setup_ccp1(CCP_OFF); // Configure CCP1 as disabled
setup_ccp2(CCP_PWM); // Configure CCP2 as PWM
set_pwm2_duty(64);
setup_comparator(NC_NC_NC_NC);
//Enable interrupts
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while (1) {
for (index = 0; index < 3; index++) {
output_high(LED1);
output_low(LED2);
delay_ms(500);
output_high(LED2);
output_low(LED1);
delay_ms(500);
} //close for
} //close 'while'
} // Close Main
|
better now ???
BTW if not really using ICD leave it OFF !!!!! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Sun Jun 02, 2013 11:29 am |
|
|
There are quite a few things 'potentially problematical'.
First you are enabling INT_RDA, but your handler for this does not read the character. If the serial input line goes high, the chip will hang. Unless the character is read, the interrupt cannot be cleared.
Then you are using a lot of pins on PORTB, but have PBADEN in the fuses, setting these all as analog inputs. You do not change this in the code.
You have the brownout voltage set at 2v, but the minimum for the USB regulator is 4v. Doesn't give confidence...
You have NODEBUG set, but select the ICSP pins, and ICD=true.
Using a crystal, you really should have PUT selected.
You _must_ always have 'ERRORS' when using the hardware RS232, unless you handle errors yourself. Otherwise the UART can become hung.
Generally, you don't need to touch TRIS if you are using standard_io.
Then the code as posted, will never compile. index is not declared, and the bracket count does not match. |
|
![](templates/subSilver/images/spacer.gif) |
acm45
Joined: 29 Dec 2008 Posts: 18 Location: Italy
|
|
Posted: Sun Jun 02, 2013 1:10 pm |
|
|
Hi all,
thank you for your fast answer, with the new fuses configuration it's working!
Thank you again
Regards |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|