dsPIC33EP片内有三路高速PWM,可以设置为互补模式、独立模式等,使用起来很方便。
为了保证PWM操作的安全,可以对dsPIC33EP进行如下配置:
#pragma config PWMLOCK = ON // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
此时,对于PWM相关的某些寄存器,进行写操作时需要严格按照解锁序列进行。
; FLT32 pin must be pulled low externally in order to clear and disable the fault
; Writing to FCLCON1 register requires unlock sequence
mov #0xabcd,w10 ; Load first unlock key to w10 register
mov #0x4321,w11 ; Load second unlock key to w11 register
mov #0x0000,w0 ; Load desired value of FCLCON1 register in w0
mov w10, PWMKEY ; Write first unlock key to PWMKEY register
mov w11, PWMKEY ; Write second unlock key to PWMKEY register
mov w0,FCLCON1 ; Write desired value to FCLCON1 register
; Set PWM ownership and polarity using the IOCON1 register
; Writing to IOCON1 register requires unlock sequencemov #0xabcd,w10 ; Load first unlock key to w10 register
mov #0x4321,w11 ; Load second unlock key to w11 register
mov #0xF000,w0 ; Load desired value of IOCON1 register in w0
mov w10, PWMKEY ; Write first unlock key to PWMKEY register
mov w11, PWMKEY ; Write second unlock key to PWMKEY register
mov w0,IOCON1 ; Write desired value to IOCON1 register
即每次对FCLCONx和IOCONx寄存器进行写操作时,必须按照上述序列进行。
如果使用的是C语言,可以采用如下方法:
asm volatile ("mov #0xabcd,w10"); //PWM1 写保护寄存器的解锁序列
asm volatile ("mov #0x4321,w11");
asm volatile ("mov #0x0000,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 #0xF000,w0");
asm volatile ("mov w10, PWMKEY");
asm volatile ("mov w11, PWMKEY");
asm volatile ("mov w0,IOCON1");
在某一个项目,由于对上述解锁序列的理解不深刻,曾出现多次对FCLCON1和IOCON1进行操作,而只使用了一次解锁序列的情况,这会导致后面的写操作无法成功,产生不可预料的错误。
表现出来的现象为,配置了dsPIC33EP32MC202的PWM1,在有的芯片上能够输出PWM波,在其他的一些芯片却不能输出相应的PWM波,曾为之苦恼了很长时间,最终发现是该问题,终于解决之。