#include <stdio.h> #include <stdlib.h> #include<p33FJ64MC506.h> #define BIT7 0x0080 #define IGBTEN_ON PORTD=LATD&(~BIT7) //配置文件 _FOSCSEL(FNOSC_FRC) //初始振荡器源选择:000=FRC振荡器 _FOSC(FCKSM_CSECMD & OSCIOFNC_ON) //时钟切换模式:01=使能时钟切换,禁止故障保护时钟监视器 //OSC2引脚功能位(XT和HS模式除外):0=OSC2为通用数字I/O引脚 _FWDT(FWDTEN_OFF) //看门狗定时器使能:0=通过用户软件使能/ 禁止看门狗定时器 _FPOR(FPWRT_PWR128) //上电复位定时器值选择位:111=PWRT=128ms _FICD(ICS_PGD1 & JTAGEN_OFF) //ICD通信通道选择使能位:11=在PGEC1和PGED1上进行通信 //JTAG使能位:0=禁止JTAG void delay_us(int n) { int j; while(n--) for(j=0;j<10;j++); } void delay_ms(int n) { while(n--) delay_us(1000); } void init_PWM(void); void pwm1_init(); int Input_caputre(void) { IC1CONbits.ICM = 0b00; IC1CONbits.ICTMR = 1; //Select Timer2 as the IC1 Time base IC1CONbits.ICI = 0b01; // Interrupt on every fourth capture event IC1CONbits.ICM = 0b011; //Capture mode, every 4th rising edge IPC0bits.IC1IP = 3; // Setup IC1 interrupt priority level IFS0bits.IC1IF = 0; // Clear IC1 Interrupt Status Flag IEC0bits.IC1IE = 1; // Enable IC1 interrupt return 1; } int timer2_init() { T2CONbits.TON = 0; // 确保T2关闭,再进行初始化 T2CONbits.TCKPS = 0; T2CONbits.T32 = 0; T2CONbits.TCS = 0; T2CONbits.TON = 1; return 1; } /************************************************************************************** * FunctionName : main() * Description : 主函数 * EntryParameter : NO * ReturnValue : N0 **************************************************************************************/ int main() { /******** Configure Oscillator to operate the device at 39.6288Mhz(MIPS) Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 //Fosc= 7.3728*(43)/(2*2)=79.2576Mhz for Fosc, Fcy = 40Mhz *********/ /* Configure PLL prescaler, PLL postscaler, PLL divisor */ PLLFBD=41; /* M=PLLFBD+2=43 */ CLKDIVbits.PLLPOST=0; /* N2=PLLPOST+2=2 */ CLKDIVbits.PLLPRE=0; /* N1=PLLPRE+2=2 */ __builtin_write_OSCCONH(0x01); /* 快速RC振荡器(FRC)→带PLL的快速RC振荡器(FRC) */ __builtin_write_OSCCONL(0x01); /* Enable Switch */ // Wait for Clock switch to occur while(OSCCONbits.COSC != 0b001); /* Wait for new Oscillator to become FRC with PLL */ while(OSCCONbits.LOCK != 1); /* Wait for Pll to Lock */ AD1PCFGL=0xFFFF; //PWM模块IO初始 PORTD = LATD | 0x2038; PORTD = LATD & 0xFF7F; TRISD &= 0xdf47; //IGBTEN初始为低,配置为输出引脚pwm6H、6L、7H、7L配置为输出引脚,初始高电平 //pwm1_init(); timer2_init(); Input_caputre(); TRISB = 0x0000; pwm1_init(); while(1) /* Infinite Loop */ { //delay_ms(100); } } /************************************************************************************** * FunctionName : init_PWM() * Description : 初始化PWM设置 * EntryParameter : NO * ReturnValue : N0 **************************************************************************************/ void init_PWM() { PTPER=47188; /* PTPER = ((1 / 20kHz) / 1.0596ns) = 47188, where 20kHz is the desired switching frequency and 1.0596ns is PWM resolution. */ PWM1CON1bits.PEN1H = 1; /* 1 = PWM模块控制PWM1H引脚 */ PWM1CON1bits.PEN1L = 0; /* 0 = GPIO模块控制PWM1L引脚 */ PWM1CON1bits.PMOD1 = 1; /* PWM I/O引脚对处于真正独立输出模式 */ PDC1 = 10874; /* Initial Duty cycle 40%(最终输出60%)*/ /*~~~~~~~~~~~~~~~~~~~~~~~ PWM7 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ PWM1CON1bits.PEN2H = 1; /* PWM2H is controlled by PWM module */ PWM1CON1bits.PEN2L = 0; /* PWM2L is controlled by GPIO module */ PWM1CON1bits.PMOD2 = 1; /* Select Independent Output PWM mode */ PDC2 = 28313; /* Initial Duty cycle 60(最终输出40%)*/ PTCONbits.PTEN = 1; /* Enable the PWM Module */ } void pwm1_init() { PTPER=47188; PTCONbits.PTEN = 1; //PWM time base is on PTCONbits.PTMOD = 0; // operates in a Free-Running mode PTCONbits.PTCKPS = 0; // PWM time base input clock period is TCY (1:1 prescale) PWMCON1bits.PMOD1 = 1; //PWM I/O pin pair is in the Independent PWM Output mode PWMCON1bits.PEN1H = 1; //PWMxH pin is enabled for PWM output PWMCON1bits.PEN1L = 1; //PWMxL pin is enabled for PWM output PWMCON2bits.SEVOPS = 0; //PWM Special Event Trigger Output Postscale PWMCON2bits.IUE = 1; // Updates to the active PDC registers are immediate PDC1 = 20000; } void __attribute__((__interrupt__,no_auto_psv))_IC1Interrupt(void) { IFS0bits.IC1IF=0; LATB=(PORTB ^ 0x01); }
举报
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
9
41
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号