大家好,我有一个奇怪的问题-当我配置RB8的开路漏控制,所有PWM发生器从外围断开。我使用DSPIC33 EP64 GS506和MPLAB与XC16编译器。当我尝试使用I2C2模块进行
通信时,我首先注意到这一点(RB8是SDA2)。我尝试了所有其他的I2C引脚,它们不影响PWM发生器。这里是一个最小的(非)工作例子:
以上来自于百度翻译
以下为原文
Hi everyone,
I have a bizarre problem - when I configure RB8 for open-drain control, all PWM generators are disconnected from the peripheral. I use dsPIC33EP64GS506, and MPLAB with xc16 compiler.
I've first no
ticed this when I tried to use I2C2 module for communication (RB8 is SDA2). I tried with all other I2C pins, they don't affect PWM generators.
Here is a minimum (non)working example:
#pragma config BWRP=OFF, BSS=DISABLED, BSEN=OFF, GWRP=OFF, GSS=DISABLED, CWRP=OFF, CSS=DISABLED, AIVTDIS=OFF
#pragma config FNOSC=FRC, IESO=OFF
#pragma config POSCMD=XT, OSCIOFNC=OFF, IOL1WAY=ON, FCKSM=CSECMD, PLLKEN=ON
#pragma config WDTPOST=PS32768, WDTPRE=PR128, WDTEN=OFF, WINDIS=OFF, WDTWIN=WIN25
#pragma config ICS=PGD1, JTAGEN=OFF, BTSWP=OFF
#pragma config CTXT1=OFF, CTXT2=OFF
#pragma config PWMLOCK=OFF, DBCC=OFF
// Use only one of these two configurations:
// OFF - I2C module is mapped to SDA/SCL pins
// ON - I2C module is mapped to ASDA/ASCL pins
#pragma config ALTI2C1=OFF, ALTI2C2=ON
#include
#define FCY 30000000UL
#include
int main(void)
{
//=== INITIALIZE OSCILLATOR ===//
// 10 MHz -> PLL -> 120 MHz (FPLL)
// FCY is 60 MHz (30 MIPS)
// PLL parameters
_PLLPRE = 0; // N1=2
PLLFBD = 22; // M=24
_PLLPOST = 0; // N2=2
// Initiate clock switch sequence
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(OSCCON | 0x01);
// Wait until clock switch occurs
while (OSCCONbits.COSC != OSCCONbits.NOSC);
while (OSCCONbits.LOCK == 0);
// PWM clock source is FPLL
ACLKCONbits.SELACLK = 0;
ACLKCONbits.APSTSCLR = 7; // divide-by-1
while (ACLKCONbits.APLLCK == 0);
//=== INITIALIZE PORTS ===//
// Configure all I2C ports for open-drain output
// Configure all other ports for digital output
// All ports are digital (not analog)
ANSELA = 0x0000;
ANSELB = 0x0000;
ANSELC = 0x0000;
ANSELD = 0x0000;
// Initialize all LAT ports to low state
LATA = 0x0000;
LATB = 0x0000;
LATC = 0x0000;
LATD = 0x0000;
// Configure all I2C ports
// You don't need to use I2C modules
// to see what is the problem here
// SCL2
_LATB15 = 1;
_ODCB15 = 1;
// // SDA2 (problem)
// _LATB8 = 1;
// _ODCB8 = 1;
// SDA1
_LATB7 = 1;
_ODCB7 = 1;
// SCL1
_LATB6 = 1;
_ODCB6 = 1;
// ASCL2
_LATB3 = 1;
_ODCB3 = 1;
// ASDA2
_LATC15 = 1;
_ODCC15 = 1;
// ASCL1
_LATC8 = 1;
_ODCC8 = 1;
// ASDA1
_LATC7 = 1;
_ODCC7 = 1;
// All other ports are digital outputs
TRISA = 0x0000;
TRISB = 0x0000;
TRISC = 0x0000;
TRISD = 0x0000;
//=== INITIALIZE PWM MODULE ===//
// Configure for 50 kHz operation with 50% duty-cycle
// Use a master time base for all PWM generators
PTCON = 0x0000;
PTCON2 = 0x0000; // divide-by-1
// PWM timing configuration
PTPER = 19192; // 50 kHz, 20 us
PDC1 = 9600; // 50%
PDC2 = 9600; // 50%
PDC3 = 9600; // 50%
PDC4 = 9600; // 50%
PDC5 = 9600; // 50%
_PTEN = 1; // enable PWM
//=== INITIALIZE I2C MODULE ===//
// I2C1 module configuration
// This one doesn't affect PWM generators,
// regardless on ALTI2C1 configuration
I2C1CONL = 0x9040;
I2C1ADD = 0x50;
I2C1MSK = 0x0000;
// I2C2 module configuration
// Note that this one will affect PWM generators
// if you don't set ALTI2C2=ON
I2C2CONL = 0x9040;
I2C2ADD = 0x50;
I2C2MSK = 0x0000;
//=== INFINITE LOOP ===//
while (1);
return 0;
}
EDIT: In addition to this, it seems that I cannot configure RB8 as digital input either. If I configure RB8 as digital input, PWM stops working.