楼主是一个纯新手,也就看了一段时间的书和程序。然后试着编写一个利用pwm主特殊事件产生中断的程序。
用的是dsPIC33EP32MC202的开发板。再加中断之前pwm波一切正常,加了中断后pwm波形都出现异常。自己怎么也琢磨不出原因,求大神指教~~~
不知道问题出在哪
#include <P33ep32mc202.h>
//配置位
_FGS(0xff);//写保护关闭
_FOSCSEL(0xfb);//双速振荡器启动使能 PWM锁定 带PLL的主振荡器
_FOSC(0xdd);//禁止时钟切换 外设引脚多次配置 SOC2数字IO XT晶振模式
_FWDT(0x7f);//看门狗禁止
_FPOR(0xff);//I2C1被映射到SAD1/SCL1引脚 欠压复位_
_FICD(0x03);//禁止JTAG 通过PGEC1 PGED1通讯
void Delay_1ms(unsigned int t)//t = 1000大概1s
{
unsigned int i,j;
for(i = 0;i < t;i ++)
for(j = 0;j < 2200;j ++);
}
//振荡器配置
void System_Colck(void)
{
//产生Fosc = 140MHz 70MIPS
CLKDIVbits.PLLPRE = 0;//N1 = 2 7.3728MHz/2= 2MHz
PLLFBDbits.PLLDIV = 74;//M = 76 3.6864*76 = 279.68MHz
CLKDIVbits.PLLPOST = 0;//N2 = 2 279.68/2 = 139.84MHz
while (OSCCONbits.COSC!= 0b011)
while (OSCCONbits.LOCK!= 1) {};//PLL 处于锁定状态
}
void System_Init(void)
{
TRISB &= ~0xfc00;//RB10-RB15 清零
ANSELB = 0x0000;//数字引脚
//IOCON1 = 0x0000;//GPIO 模块控制 PWMxH PWMxL引脚
}
void PWM_Init(void)
{
//互补 PWM 模式 独立占空比和相位,固定主周期,边沿对齐
//PWM解锁
asm volatile ("mov #0xabcd,w10");
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0x0003,w0");//禁止故障输入
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,FCLCON1");
asm volatile ("mov #0xabcd,w10");
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0xC000,w0");//互补输出模式,高电平有效
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,IOCON1");
asm volatile ("mov #0xabcd,w10");
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0x0003,w0");
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,FCLCON2");
asm volatile ("mov #0xabcd,w10");
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0xC000,w0");
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,IOCON2");
asm volatile ("mov #0xabcd,w10");
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0x0003,w0");
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,FCLCON3");
asm volatile ("mov #0xabcd,w10");
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0xC000,w0");
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,IOCON3");
PTPER = 7000;//主控时基周期寄存器=Fosc/(Fpwm*PCLRDIV(2:0)) PWM频率20K
PHASE1 = 0;
PHASE2 = 2333;
PHASE3 = 4667;//PWM 主相移寄存器 (调节相移,也就是波形之间的偏置)
PDC1 = 3500;
PDC2 = 3500;
PDC3 = 3500;//PWM 发生器占空比寄存器 1/2
DTR1 = DTR2 = DTR3 = 25;//PWM 死区寄存器
ALTDTR1 = ALTDTR2 = ALTDTR3 = 25;//PWM 备用死区寄存器
PWMCON1 = 0x0400;
PWMCON2 = 0x0400;
PWMCON3 = 0x0400;//PWM 控制寄存器 边沿对齐 正死区
PTCON2bits.PCLKDIV = 0;//PWM 时钟分频比选择寄存器 一分频
SEVTCMP = 0;//特殊事件比较值
//使能PWM 允许每次发生匹配事件时产生特殊事件中断 频率20K
PTCONbits.SEIEN = 1;
PTCONbits.SEVTPS = 0;
while (PTCONbits.SESTAT == 1);
//TRIG1 = 0;
//TRGCON1bits.TRGDIV = 0;
//TRGCON1bits.TRGSTRT = 0;
//PWMCON1bits.TRGIEN = 1;
//while (PWMCON1bits.TRGSTAT == 1);
PTCONbits.PTEN = 1;
}
void Interrupt_Int1_Init(void)
{
INTCON1bits.NSTDIS = 0;//中断嵌套使能
CORCONbits.IPL3 = 0;
SRbits.IPL = 0;//设置CPU优先级为0
IPC14bits.PSEMIP = 5;//PWM特殊事件 中断优先级
IFS3bits.PSEMIF = 0;//PWM特殊事件中断标志清零
IEC3bits.PSEMIE = 1;//允许中断
//IPC23bits.PWM1IP = 6;//PWM1触发事件 中断优先级6
//IFS5bits.PWM1IF = 0;//PWM1中断标志清零
//IEC5bits.PWM1IE = 1;//允许中断
INTCON2bits.GIE = 1;//全局中断允许
}
void __attribute__((__interrupt__, auto_psv)) _INT1Interrupt(void)
{
//IFS5bits.PWM1IF = 0;//PWM1中断标志清零
IFS1bits.INT1IF = 0;//外中断标志清零
}
int main()
{
System_Colck();
System_Init();
PWM_Init();
Interrupt_Int1_Init();
while(1)
{
}
}
|
|