dsPIC33F之Negative Deadtime PWM

/**************************************************************************************
*  ?2008 Microchip Technology Inc.                                         
*                                                                           
*  FileName:              Negative Deadtime.c                                    
*  Dependencies:          Header (.h) files if applicable, see below        
*  Processor:             dsPIC33FJ06GS202                                  
*  Compiler:              MPLAB?C30 v3.02 or higher                        
*  IDE:                   MPLAB?IDE v8.02 or later                         
*  Hardware Dependencies: 16-Bit 28-Pin Starter Board			             
*                                                                           
*  SOFTWARE LICENSE AGREEMENT:                                              
* Microchip Technology Incorporated ("Microchip") retains all ownership and 
* intellectual property rights in the code accompanying this message and in all 
* derivatives hereto.  You may use this code, and any derivatives created by 
* any person or entity by or on your behalf, exclusively with Microchip's
* proprietary products.  Your acceptance and/or use of this code constitutes 
* agreement to the terms and conditions of this notice.
*
* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS".  NO 
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
* TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 
* PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP'S 
* PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. 
*
* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER 
* IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), 
* STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, 
* PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF 
* ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN 
* ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT 
* ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO 
* THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO 
* HAVE THIS CODE DEVELOPED.
*
* You agree that you are solely responsible for testing the code and 
* determining its suitability.  Microchip has no obligation to modify, test, 
* certify, or support the code.
*
* Description:
*     This program illustrates the use of Negative Dead-time in the PWM
* module in the dsPIC33F SMPS family with ADC triggers. The 
* PWM is configured with negative deadtime and configured to 
* generate an ADC trigger. PWM1 generates a trigger on ADC channel 5. 
* The converted ADC value is used to update the duty cycle of PWM1. 
* The voltage at AN5 (POT) decides the duty cycle of PWM1.
*    
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#include "p33FJ06GS202.h"

/* Configuration Bit Settings */
_FOSCSEL(FNOSC_FRC)
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON)
_FWDT(FWDTEN_OFF)
_FPOR(FPWRT_PWR128)
_FICD(ICS_PGD2 & JTAGEN_OFF)

void init_PWM();
void init_ADC();

int main()
{
            /* Configure Oscillator to operate the device at 40Mhz
	   Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
 	   Fosc= 7.37*(43)/(2*2)=80Mhz for Fosc, Fcy = 40Mhz */

	/* Configure PLL prescaler, PLL postscaler, PLL divisor */
	PLLFBD=41; 	      /* M = PLLFBD + 2 */
	CLKDIVbits.PLLPOST=0;   /* N1 = 2 */
	CLKDIVbits.PLLPRE=0;    /* N2 = 2 */

    __builtin_write_OSCCONH(0x01);		/* New Oscillator FRC w/ PLL */
    __builtin_write_OSCCONL(0x01);  		/* Enable Switch */
      
    while(OSCCONbits.COSC != 0b001);		/* Wait for new Oscillator to become FRC w/ PLL */  
    while(OSCCONbits.LOCK != 1);		/* Wait for Pll to Lock */

	/* Now setup the ADC and PWM clock for 120MHz
	   ((FRC * 16) / APSTSCLR ) = (7.37 * 16) / 1 = ~ 120MHz*/

	ACLKCONbits.FRCSEL = 1;		/* FRC provides input for Auxiliary PLL (x16) */
	ACLKCONbits.SELACLK = 1;		/* Auxiliary Oscillator provides clock source for PWM & ADC */
	ACLKCONbits.APSTSCLR = 7;		/* Divide Auxiliary clock by 1 */
	ACLKCONbits.ENAPLL = 1;		/* Enable Auxiliary PLL */
	
	while(ACLKCONbits.APLLCK != 1);	/* Wait for Auxiliary PLL to Lock */
	
	init_PWM();
	init_ADC();

	IPC3bits.ADIP = 4;      		/* ADC Interrupt Priority */
	IFS0bits.ADIF = 0;     		/* Clear ADC Interrupt Flag */
	IEC0bits.ADIE = 1;     		/* ADC Interrupt Enable */
    
    ADCONbits.ADON = 1;     	         /* ADC is turned ON */
 	PTCONbits.PTEN = 1;                 /* Turn ON PWM module */

    while(1)
    {
        Nop();
    }
}

void init_PWM()
{
    PTPER = 9615;				/* PTPER = ((10us) / 1.04ns) = 9615, where 10us
					is the PWM period and 1.04ns is PWM resolution. */                           

    PDC1 = 2404;                            /* PWM1 duty cycle (2.5us / 1.04ns) */
                                         
    IOCON1bits.PENH = 1;                    /* PWM1 outputs controlled by PWM */
    IOCON1bits.PENL = 1;                    
    IOCON1bits.PMOD = 0;                    /* Choose Complementary PWM mode */

    PWMCON1bits.DTC = 1;                    /* Negative Dead time enabled */
    
        			                /* Negative deadtime = 130nsec */
    DTR1 = 63;                              /* Deadtime = (65ns / 1.04ns) where 65ns is desired deadtime */
    ALTDTR1 = 63;                           /* ALTDeadtime = (65ns / 1.04ns) where 65ns is desired deadtime */                     
    
    TRGCON1bits.TRGDIV = 1;                 /* Trigger output every 2nd trigger event */
    TRIG1 = 4808;                           /* Trigger generated in the middle of the PWM period */
}

void init_ADC()
{
      ADPCFGbits.PCFG5 = 0;                 /* Choose AN5 as analog input */
      ADCPC1bits.IRQEN2 = 1;                /* Enable interrupt request */
      ADCPC1bits.TRGSRC2 = 4;               /* Trigger ADC pair 2 conversion on PWM1 generator */
      
      ADSTATbits.P2RDY = 0;                 /* Clear pair 2 data ready bit */
                    
      ADCONbits.FORM = 0;                   /* Choose integer data format */
      ADCONbits.ORDER = 0;                  /* Even numbered input is converted first */
      ADCONbits.SEQSAMP = 1;                /* Sequential sampling enabled */
      ADCONbits.ADCS = 7;                   /* ADC clock = FADC/14 */
}

void __attribute__ ((__interrupt__, no_auto_psv)) _ADCInterrupt(void)
{
    unsigned int tmp;
    IFS0bits.ADIF = 0;                      /* Clear ADC interrupt flag */
    ADSTATbits.P2RDY = 0;                   /* Clear pair 2 data ready bit */
  
    tmp = ADCBUF5*8;                        /* Scaling of AN5 voltage */
                                            /* This ensures a significant
                                               change in duty cycle based
                                               on ADC conversion value */
                                             
    if (tmp < 500) tmp = 500;               /* Applying boundary conditions */
                                            /* The boundary conditions ensure
                                               that the duty cycle does not 
                                               become less than deadtime
                                               setting 500/8 >= Deadtime */
    
    PDC1 = tmp;                             /* Update PWM1 duty cycle */
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值