Hi, I'm trying to drive a motor using a dspic33. I'm using PWM to control the motor and CANbus to communicate with PC (that's a school project, I have to use these things). Both pwm and CAN work fine, but when I try to send the duty cycle value with CAN, pwm doesn't work. I've used debugger and that's what i've found:
- CAN buffer is read by DMA
- Firmware read DMA and set the duty cycle register
- DMA buffer is cleared
- PWM module clear the duty cycle register too
That's the main:
while(1)
{
duty=ecan1msgBuf[1][3];
if(duty!=0)
{
P2DC1=duty*100;
}
}
And here pwm initialization:
P2TCONbits.PTEN=0; //bisable pwm
P2TCONbits.PTOPS=0b0000; //postscaler 1:1
P2TCONbits.PTCKPS=0b00; //prescaler 1:1
P2TCONbits.PTMOD=0b00; //freerunning mode
P2TPER=499; //pwm period value bits
P2FLTACONbits.FAEN1=0;
PWM2CON1bits.PEN1H=0;
PWM2CON1bits.PEN1L=1;
PWM2CON1bits.PMOD1=1;
PWM2CON2bits.IUE=0;
PWM2CON2bits.OSYNC=0;
PWM2CON2bits.UDIS=0;
P2DC1=0;
P2OVDCONbits.POVD1H=0;
P2OVDCONbits.POVD1L=1;
P2TCONbits.PTEN=1;
- CAN buffer is read by DMA
- Firmware read DMA and set the duty cycle register
- DMA buffer is cleared
- PWM module clear the duty cycle register too
That's the main:
while(1)
{
duty=ecan1msgBuf[1][3];
if(duty!=0)
{
P2DC1=duty*100;
}
}
And here pwm initialization:
P2TCONbits.PTEN=0; //bisable pwm
P2TCONbits.PTOPS=0b0000; //postscaler 1:1
P2TCONbits.PTCKPS=0b00; //prescaler 1:1
P2TCONbits.PTMOD=0b00; //freerunning mode
P2TPER=499; //pwm period value bits
P2FLTACONbits.FAEN1=0;
PWM2CON1bits.PEN1H=0;
PWM2CON1bits.PEN1L=1;
PWM2CON1bits.PMOD1=1;
PWM2CON2bits.IUE=0;
PWM2CON2bits.OSYNC=0;
PWM2CON2bits.UDIS=0;
P2DC1=0;
P2OVDCONbits.POVD1H=0;
P2OVDCONbits.POVD1L=1;
P2TCONbits.PTEN=1;