完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
亲爱的各位,请用触发中断来帮助我解决每个周期结束时PWM更新占空比的问题。我使用的是DSSPICEP64 GS502。我使用一个变量“i”来计算中断循环,以在每个周期结束时更新占空比,但是一旦我成功更新,占空比将不会在下一个周期内改变。我已经通过改变“i”的值来测试。如果i=0或i=1,它正常工作,但不能中断下一次中断的循环。这是我的代码。请帮帮我。提前感谢!!!!{包括lc.h & gt;//ClultBuffyAtAtditTyx((α-BythTyt*,NoAutoNo.PSV))pWM1Sc除器(){INti=0;//为中断环i=i+1;int=i=2;IF(A==0)//计数偶数{PDC1=300;//更新占空比SDC1=300;IFS5BIT.PWM1IF=0;//清除标志} //计数为奇数{PDC1=1;//更新占空比SDC1=1;IFS5BITS PWM1IF=0;//清除标志} int主(){ACLKCONBITS.FRCSEL=1;/FRC被输入辅助PLL ACLKCONBIT。SelaCLK=1;/ /辅助振荡器提供时钟源ACLKCONTITS。APSTLSCR=7;/ /辅助时钟由1个ACKLCONDONS分配。EnAPLL=1;/ /启用辅助PLL(ACLKCONbits.APLLCK)!=(1);/ /等待辅助锁相环锁定/ *全局中断允许位*/ItCON2BIT.GEE=1;/ /中断和相关的中断使能位是启用/ * PWM配置*//*设置在相控寄存器*/Prime1=600的PWM周期;SPASHE1=600;/*设置任务循环*/PDC1=0;SDC1=0;/*设置PWM模式*/IOCON1=0B1100110000亿;/PWM模块控制PWMXH、L PIN//PWMXH,L引脚在真正的独立PWM输出模式/ PWM中是有源高//PWM I/O。生成器为PWMXH、L//PWMXH提供数据,L引脚被映射到各自的PIN 0 /*集独立时基、边缘对齐模式和独立占空比*/PWMCON1= 0B000 000 01100000;/ /触发事件产生中断请求。//相量/ SPASEX寄存器提供该PWM发生器//PDCx /SDCX寄存器的时基周期,为该PWM发生器/PWM发生器提供占空比信息,使用主主时基同步。并且作为PWM产生逻辑的时钟源/ /边缘对齐模式/外部引脚不影响PWM时基/ /更新到激活的MCD/PDCX/SDCX/PACEX/SPASHEX寄存器是同步器。ON到本地PWM时基/*配置故障*/FCLCON1= 0x00 03//故障输入被禁用/*1:1预分频器*/PTCON2= 0x000;/*中断CONFIG*/TIG1=0;/ /点TrangeReTrGCON1=0;// Trigger每个触发事件的输出/等待0个PWM周期,然后在模块启用IPC23比特。PWM1IP=4;//pWM1中断优先级别=4IFS5BITS;PWM1IF=0;//清除FLAGECEC5BITS;PWM1IE=1;//PWM1中断。Enable TrasiabITS.TISISA3=0;//配置RA3为OutPutsRISABIT.TISISA4=0;//配置RA4为OutPuttA=0x000;//清除端口A/*使PWM模块*/PTCON=0x800 0/(1){};}
以上来自于百度翻译 以下为原文 Dear all, Please help me to figure out my problem with PWM update duty cycle at the end of every period by using trigger interrupt. I am using dsPIC33ep64gs502. I used a variable "i" to count the interrupt loop to update duty cycle at the end of each period But once I updated successfully, the duty cycle will not change for the next period. I already tested by change the value of "i". It worked normally if i=0 or i=1 but can not get out of loop for next interrupt . Here is my code. Please help me. Thanks in advance !!! #include //Interrupt void __attribute__((__interrupt__,no_auto_psv)) _PWM1Interrupt() { int i = 0; //Count for interrupt loop i = i + 1; int a; a = i%2; if (a == 0) //Count is even number { PDC1 = 300; //Update duty cycle SDC1 = 300; IFS5bits.PWM1IF = 0; //Clear flag } else //Count is odd number { PDC1 = 1; //Update duty cycle SDC1 = 1; IFS5bits.PWM1IF = 0; //Clear flag } } int main() { ACLKCONbits.FRCSEL = 1; // FRC is input to Auxiliary PLL ACLKCONbits.SELACLK = 1; // Auxiliary Oscillator provides the clock source ACLKCONbits.APSTSCLR = 7; // Divide Auxiliary clock by 1 ACLKCONbits.ENAPLL = 1; // Enable Auxiliary PLL while(ACLKCONbits.APLLCK != 1); // Wait for Auxiliary PLL to Lock /*Global interrupt enable bit*/ INTCON2bits.GIE = 1; // Interrupts and associated interrupt enable bits are enabled /*PWM CONFIGURATION*/ /* Set PWM Periods on PHASEx Registers */ PHASE1 = 600; SPHASE1 = 600; /* Set Duty Cycles */ PDC1 = 0; SDC1 = 0; /* Set PWM Mode */ IOCON1 = 0b1100110000000000; //PWM module controls the PWMxH,L pins //PWMxH,L pins are active-high //PWM I/O in True independent PWM output mode //PWM generator provides data for PWMxH,L //PWMxH,L pins are mapped to their respective pins 0 /* Set Independent Time Bases, Edge-Aligned mode and Independent Duty Cycles */ PWMCON1 = 0b0000011000000000; //A trigger event generates an Interrupt Request //PHASEx/SPHASEx registers provide the time base period for this PWM generator //PDCx/SDCx registers provides duty cycle information for this PWM generator //PWM generator uses the primary master time base for synchronization and as the clock source for the PWM generation logic //Edge-Aligned mode //External pins do not affect the PWM time base //Update to the active MDC/PDCx/SDCx/PHASEx/SPHASEx registers are synchronized to the local PWM time base /* Configure Faults */ FCLCON1 = 0x0003 //Fault input is disabled /* 1:1 Prescaler */ PTCON2 = 0x0000; /*Interrupts Config*/ TRIG1 = 0; //Point at which triggered TRGCON1 = 0; //Trigger output for every trigger event //Wait 0 PWM cycles before generating the first trigger event after the module is enabled IPC23bits.PWM1IP = 4; //PWM1 interrupt priority level = 4 IFS5bits.PWM1IF = 0; //Clear flag IEC5bits.PWM1IE = 1; //PWM1 interrupt enable TRISAbits.TRISA3 = 0; // Configure RA3 as output TRISAbits.TRISA4 = 0; // Configure RA4 as output PORTA = 0x0000; //Clear port A /* Enable PWM Module */ PTCON = 0x8000 while(1) { }; } |
|
相关推荐
2个回答
|
|
请在代码周围使用[Co De]和[/C.O.E]标签(没有空格)。变量“i”被声明为ISR的本地,这意味着它将在每个中断上再次创建。您可能需要使其静态化。您阅读了数据表的第152.1节,其中是否需要一个解锁序列来写入ICONCOX和FCLCONX寄存器?我在你的代码中看不到。(顺便说一句,我建议你看一下我可以为你做这个解锁的一个编译程序的用户指南,我不知道它们的存在,因为我没有使用那个外围设备和MCU,但是如果你尝试在C中解锁,你会遇到各种各样的问题。如果宏没有这样做。然后使用在线汇编程序)苏珊
以上来自于百度翻译 以下为原文 Please use [ c o d e ] and [ / c o d e ] tags (without the spaces) around your code. The variable 'i' is being declared as local to the ISR which means that it will be created again on each interrupt. You probably need to make that static. Have you read Section 15.2.1 of the data sheet where is says that you need an unlock sequence to write to the IOCONx and FCLCONx registers? I can't see that in your code. (BTW, I would suggest that you look i the compiler user guide for __BUILT_IN_xxx macros that can do this unlocking for you - I have no idea of they exist as I've not used that peripheral and that MCU but you can get into all sorts of problems if you try to do the unlocking in C. If the macros don't exist then use in-line assembler.) Susan |
|
|
|
亲爱的苏珊,谢谢你的建议!我看到了我关于变量‘i’的错误,所以我把它改为全局变量。在我改变它之后,程序以50%的占空比成功中断,但是当我把占空比减少到1.04nS(最大分辨率)时,信号以某种方式中断了错误。时钟源获得1.04个最大分辨率PWM,并利用模拟器看到信号形式。这是FRC还是模拟器造成的问题?我需要连接到DSPIC而不是模拟器,因为我使用的是来自DSPIC的FRC。
以上来自于百度翻译 以下为原文 Dear Susan, Thank you for your suggestion! I see my mistake about variable 'i', so I have change it to global variable. After I changed it, the program interrupt successfully with 50% duty cycle but when I reduced the duty cycle to 1.04ns (maximum resolution), the signal somehow interrupted wrong way. I am using FRC input to Auxiliary PLL as clock source to get 1.04 maximum resolution PWM and use simulator to see the signal form. Is this the problem due to FRC or simulator? Do I need to connect to dspic instead of simulator because I am using FRC from dspic. |
|
|
|
只有小组成员才能发言,加入小组>>
5184 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3179 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2230 浏览 5 评论
739浏览 1评论
626浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
511浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
637浏览 0评论
535浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 13:05 , Processed in 1.282180 second(s), Total 81, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号