dsPIC30F2020 - interaction between comparator and PWM?

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
womai
Posts: 239
Joined: 16 Apr 2008 07:45

dsPIC30F2020 - interaction between comparator and PWM?

#1 Post by womai » 09 Jul 2009 01:29

I am trying to use comparator 1 on pin RB2, and at the same time produce a PWM signal on pin RE0. On its own the comparator works fine (triggers on signal, with selectable threshold and polarity), and the PWM works fine (produces a 10 kHz square wave on pin RE0).

The problem is - the moment I turn the comparator on, the PWM signal disappears, even though these two functions aren't even using the same pin. Is there any other internal resource that gets used by both and that causes this? I read through the PIC data sheet but couldn't find anything mentioned regarding interaction (or incompatibility) between PWM and comparators.

Thanks

Wolfgang



Here is my PWM setup:

Code: Select all

//Does not work when CMP1 is turned on (CMPCON1bits.CMPON = 1;)
// works fine with CMPCON1bit.CMPON = 0;

    IOCON2bits.PENL = 1; // PWM1L is controlled by PWM module
    IOCON2bits.PENH = 0; // PWM1H is regular I/O

    IOCON2bits.PMOD = 0b01; // Select independent Output PWM mode

    PWMCON2bits.IUE = 1; // instantaneous duty cycle update
    PWMCON2bits.DTC = 2; // disable dead time

    PTPER = 51199; // PWM period counter = 4 * (128 MHz / f_pwm) - 1; f_pwm = 10 kHz

    PDC2 = 25600; // Duty Cycle

    PTCONbits.PTEN = 1; // Enable the PWM Module
And here is my comparator setup code:

Code: Select all

	// initialize trigger comparators

     // disable digital input buffer on comparator pins to avoid excessive current draw
     // RB2 = trigger CH1, RB3 = trigger CH3, RB4 = ext. trigger (treshold 0-5V)
	ADPCFG &= 0xFFE3;

	// CH1 trigger input

     CMPCON1bits.INSEL = 0b10; // input 1C = RB2
     CMPCON1bits.EXTREF = 0; // use internal reference DAC
     CMPCON1bits.RANGE = 1; // high range, max DAC value = VCC/2

     CMPCON1bits.CMPON = 1; // turn comparator on

	IFS1bits.AC1IF = 0; // reset interrupt status flag for comparator 1
	IPC7bits.AC1IP = 7; // analog comparator 1 interrupt priority (7 = highest)

     CMPCON1bits.CMPOL = 0; // output inversion (0 = not inverted = trigger on rising edge, 1 = inverted = trigger on falling edge)
     CMPDAC1bits.CMPREF = 0x0200; // reference DAC value; max = 0x3ff = 1023 = VCC/2

Orcino Borges
Posts: 42
Joined: 17 Apr 2009 13:33

#2 Post by Orcino Borges » 16 Jul 2009 01:20

Hi Wolfgang,

Look the register FCLCONx


Good luck

Orcino

womai
Posts: 239
Joined: 16 Apr 2008 07:45

#3 Post by womai » 12 Aug 2009 21:36

Hi Orcino,

once again I owe you! That register was indeed the troublemaker.

For everybody's benefit, here are the two lines that I had to add to make the PWM output work with comparator 1 turned on:

FCLCON1bits.CLMODE = 0; // current-limit function disabled
FCLCON1bits.FLTMOD = 3; // fault input disabled

Now everything works like a treat.

Wolfgang

bhadresh
Posts: 1
Joined: 05 Jul 2013 09:32

Re: dsPIC30F2020 - interaction between comparator and PWM?

#4 Post by bhadresh » 05 Jul 2013 09:41

hi....i have also same kind of problem. while using uart while using PWM. as when uart is inactive i.e.
U1MODE:L
bit 15 UARTEN:UARTx Enable bit
1= Enable UARTx; UARTx pins are controlled by UARTx module
0= Disable UARTx; UARTx pins are controlled by I/O Port control registers

when this bit is disabled pwm works fine. but as soon as i enable uart pwm signals are vanished.

I tried to find out conflicts with other functions on pin like PGD/PGC and PWM4H/4L. but no clues for the same. I tried out FCLCON also . but no effect :( here is my code




#include "p30f2020.h"


/* Configuration Bit Settings */
/*
_FOSCSEL(FRC_PLL)
_FOSC(PRIOSC_OFF & CSW_FSCM_OFF & FRC_HI_RANGE & OSC2_IO)
_FPOR(PWRT_128)*/

_FOSCSEL(PRIOSC_PLL)
_FOSC(HS & OSC2_CLKO & CSW_FSCM_OFF) //OSC2 PIN HAS clk FUNCTION,HS selected
_FPOR(PWRT_128)


/*****************************************************************************/
/* STANDARD PWM INITIALIZATION CODE */
/*****************************************************************************/
void init_PWM()
{


//~~~~~~~~~~~~~~~~~~~~~~~~~ PWM Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PTPER = 1920;

PDC1 = 1000;
PDC2 = 960;

DTR1 = 64; // 67.2 nsec dead time
DTR2 = 64; // 67.2 nsec dead time

ALTDTR1 = 64; // 67.2 nsec dead time
ALTDTR2 = 64; // 67.2 nsec dead time

IOCON1bits.PENH = 1; // PWM1 outputs controlled by PWM module
IOCON1bits.PMOD = 0b01;
IOCON1bits.PENL = 1;

IOCON2bits.PENH = 1; // PWM2 outputs controlled by PWM module
IOCON2bits.PMOD = 0b01;
IOCON2bits.PENL = 1;

IOCON4bits.PENH = 1;
IOCON4bits.PENL = 1;

PWMCON1bits.IUE = 1; //update from pdc immediate
PWMCON2bits.IUE = 1;

PTCONbits.PTEN = 1; // Turn ON PWM module
//~~~~~~~~~~~~~~~~~~~~~~ End PWM Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}






/******************************************************************************/
/* MAIN ROUTINE */
/******************************************************************************/
int main()
{

/* Wait for PLL to lock */
while(OSCCONbits.LOCK!=1) {};

/*Configure Oscillator to operate the device at 40MIPS*/

//TRISB = 0b1111111111110000; //set B0-B3 to output
//TRISA = 0b00000; //set A0-A4 as ouput
OSCCONbits.COSC = 0b011;





FCLCON1bits.CLMODE = 0; // current-limit function disabled
FCLCON1bits.FLTMOD = 3; // fault input disabled
FCLCON2bits.CLMODE = 0; // current-limit function disabled
FCLCON2bits.FLTMOD = 3; // fault input disabled
FCLCON3bits.CLMODE = 0; // current-limit function disabled
FCLCON3bits.FLTMOD = 3; // fault input disabled
FCLCON4bits.CLMODE = 0; // current-limit function disabled
FCLCON4bits.FLTMOD = 3; // fault input disabled


/* Set Up UART */
U1BRG=156; /* 9600Baud for 24MHz UxBRG = (Fcy/16.Baud_rate)-1 Fcy=24MHz*/
/* Change U1BRG to suit your clock frequency */
U1MODE=0b0000000000000000; /*Enable,8data,no parity,1 stop,use alternate pins for uart */
U1STA =0x8400; /* Interrupt generated when a character is transferred to
* the Transmit Shift register and the transmit buffer
* becomes empty*/


INTCON1bits.NSTDIS=1; //interrupt nesting is disabled
INTCON2 = 0b0000000000000111; //set all three interrupts int0,int1.int2 are set as negative edge detector
IEC0 = 0b0110111100000001; //int0 enable bit,uart,i2c,adc,spi interrupts are enabled
IEC1 = 0b0000000000000011; //int2 and int1 enable bit
IEC2 = 0b0000011111100000; //adc0 to adc5 pairs conversions complete interrupts
IPC0 = 0b0000000000000111; //SETTING INTO AS 7TH (highest priority)
IPC1 = 0b0000000000000110; //SETTING INT1 AS 6TH (2nd highest priority)
IPC2 = 0b0000000001010000; //SETTING INT2 AS 5TH (3rd highest priority)

/* Disable Watch Dog Timer */
RCONbits.SWDTEN=0;


/* Set Up UART */
U1BRG=194; /* 9600Baud for 16MIP (See FRM Tables) *///103
/* Change U1BRG to suit your clock frequency */
U1MODE=0x8000; /* Enable, 8data, no parity, 1 stop */
U1STA =0x8400; /* Enable TX */


init_PWM();
while(1); //controller should be in active executable mode



}

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”