CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM and interrupts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
acm45



Joined: 29 Dec 2008
Posts: 18
Location: Italy

View user's profile Send private message

PWM and interrupts
PostPosted: Sun Jun 02, 2013 10:48 am     Reply with quote

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

asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun Jun 02, 2013 11:23 am     Reply with quote

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 !!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Sun Jun 02, 2013 11:29 am     Reply with quote

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.
acm45



Joined: 29 Dec 2008
Posts: 18
Location: Italy

View user's profile Send private message

PostPosted: Sun Jun 02, 2013 1:10 pm     Reply with quote

Hi all,

thank you for your fast answer, with the new fuses configuration it's working!
Thank you again


Regards
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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