Maker Pro
Maker Pro

Problems generating PWM signal with dsPIC30F3014

alexsoad

Apr 4, 2011
12
Joined
Apr 4, 2011
Messages
12
I need to generate a PWM signal to command a DC motor via L298N driver.
I user dsPIC30F3014 to do this, but i cannot generate it and i need some help from you.
I user C programming language.
So:
_FOSC(CSW_FSCM_OFF & FRC_PLL16); //using internal oscillator
//this is for an external interrupt INT0, from a button, to switch on/off the motors.
int activ;
void __attribute__ ((interrupt, no_auto_psv)) _INT0Interrupt(void)
{
if( activ == 1)
activ = 0;
else
activ = 1;
IFS0bits.INT0IF = 0
}

//this is the interrupt from Timer 2
void __attribute__((interrupt, no_auto_psv)) _T2Interrupt (void)
{
IFS0bits.T2IF = 0;
}

// the function to enable the PWM, from OC1 pin
void enable_pwm()
{
PR2=0x0F90; // pwm period
OC1CON=0x0007; // seting OC1 to "simple PWM" mode
OC1RS=0x07C8; // duty cycle
T2CONbits.TON=1; // starting the timer 2

}

// main

int main(void)
{
RCONbits.SWDTEN = 0; //disable WDT
T2CON=0x0000; //set the presclare to 1:1
TRISB = 0x000000; //portB as output
TRISD=0x0000; //portd as output
IFS0bits.INT0IF = 0; //reset the flag from INT0
IFS0bits.T2IF = 0; //reset the flag from T2
IEC0bits.INT0IE = 1; //enable INT0
IEC0bits.T2IE = 1; //enable T2 interrupt
INTCON2 = 0x0001; //enable INT0 from global interrupts
activ = 0; //setting activ to 0 , => motors don't start at reset
while(1)
{
if (activ==1)
{
enable_pwm(); //enables the PWM
LATB=0x0033; //settings extra bits from port B to 1
PORTB=LATB;
}
else
{
LATB=0x0030;
PORTB=LATB;
}
}
}


The motor is controller via L298N. This driver needs 3 pins for a motor. 2 for direction (1 logic and 0 logic: i take them from PORTB). The last pin is for enable. If i take it from a 1 logic pin, the motor does works. If i take it from PWM pin, it doesn't works. So, the PWM isn't generated corretly.

Alex.
 
Top