//title "Project Zero"
#include <dspic.h>
//PORTB will be the hall effect sensors.
void init(void)
{
TRISB = 1;
PORTB = 0;
PORTD = 0;
TRISD = 0;
PORTE = 0;
}
void main(void)
{
init();
PTCON= 0b1000000000000000; //set PWM mode
PTPER = 0b0000000000010001; //set pwm period
}
Instruction cycle period (TCY) equals four times the input oscillator time base period. All specified values
are based on characterization data for that particular oscillator type under standard operating conditions
with the device executing code. Exceeding these specified limits may result in an unstable oscillator
operation and/or higher than expected current consumption. All devices are tested to operate at “min.”
values with an external clock applied to the OSC1/CLKI pin. When an external clock input is used, the
“Max.” cycle time limit is “DC” (no clock) for all devices
Isn't the Victor884 looking for a servo signal (even though they call it PWM), which would be a 2ms period, not 2us?Hi,
I am trying to figure out how to use the PWM Motor Control Mode for my dsPic30F4011. I am trying to send a signal with a period of 2 microseconds. The problem is I have no idea how. I've poured through the dsPic manual and the motor pwm module manual, but the documentation is just not making the process clear. So my questions are:
#chip 18f1330,4
#config osc=INTIO2,HPOL=HIGH,LPOL=High 'set PWM polarity bit for active low (CA led)
#define Red PortB.1 'PWM1, PWM 1/3/5 are main PWM 0/2/4 are compimentary
#define Green PortB.4 'PWM3
#define Blue PortB.6 'PWM5
#define WaitLed wait 1 s
dim Level as word
dir Red out
dir Green out
dir Blue out
dir PortB out
init_PowerPWM
Level = 16384
Main:
;Yellow
PDC0H = ! Level_H
PDC0L = ! Level
PDC1H = ! Level_H/2
PDC1L = ! Level
PDC2H = ! 0XFF
PDC2L = ! 0XFF
waitled
;Purple
PDC0H = ! Level_H
PDC0L = ! Level
PDC1H = ! 0X00
PDC1L = ! 0X00
PDC2H = ! Level_H/2
PDC2L = ! Level
waitled
goto Main
sub init_PowerPWM
Set PTEN On
'set UDIS Off
PTCKPSO = 0
PTCKPSO = 0
Set PMOD2 Off
Set PMOD1 Off
Set PMOD0 Off ;independent mode
Set PTMOD1 Off
set PTMOD0 Off
'no dead time required!
'DTCON = b;01010100'
PTCON0 = 0X00
'PTCON1 = 0XC0
SET PTEN ON ;PWM TIME BASE IS ON
SET PTDIR ON ;PWM TIME BASE COUNTS DOWN
'PWMCON0 = 0X37
Set PWMEN2 On ;PWM1,3,5 enabled
set PWMEN1 ON
Set PWMEN0 On
'PWMCON1 = 0
PTPERL = 0XAA
PTPERH = 0X01
PDC0H = 0x00
PDC0L = 0X00 'PWM1 DUTY CYCLE
PDC1H = 0X00
PDC1L = 0X00
PDC2H = 0X00
PDC2L = 0X00
end sub
The PWMEN2 bit is part of the PWMCON0 register for the 18f1330, so I would think you would be looking for a PWMCON register for the 16bit dspic? Please note that three bits need to be set to determine which PWM pins are enabled. Notice in my code all three PWMEN2Okay, interesting. I'll try writing the code a bit more like you do. You set a PWMEN2 On, which you comment turns on the PWMs. Do you know if there is something analogous for the dspic30f? One of my above questions was whether there was something I needed to write to to enable the pwm registers. I tried also writing to the ptmr and pwmcon1 registers, but still no signal. I'll repost my own code in a bit after I do a little work on it.
// Init PWM1 for dual pair output.
PWM1CON1bits.PEN1L = 1;
PWM1CON1bits.PEN1H = 1;
PWM1CON1bits.PEN2L = 1;
PWM1CON1bits.PEN2H = 1;
// Setup the dead time periods
P1DTCON1bits.DTBPS = 0;
P1DTCON1bits.DTB = 63;
P1DTCON1bits.DTAPS = 0;
P1DTCON1bits.DTA = 63;
// Set which transitions get which dead times.
P1DTCON2bits.DTS1I = 1;
P1DTCON2bits.DTS1A = 0;
P1DTCON2bits.DTS2I = 1;
P1DTCON2bits.DTS2A = 0;
P1TPER = 1999;
// Enable PWM1
P1TCONbits.PTEN = 1;
// Set the initial duty cycle;
P1DC1 = 1000;
P1DC2 = 1000;
//title "Project Zero"
#include <dspic30f4011.h>
//PORTB will be the hall effect sensors.
void init(void)
{
TRISB = 0b111111111111111;
PORTB = 0b0000000000000000;
PORTD = 0b0000000000000000;
TRISD = 0b0000000000000000;
TRISE = 0b0000000000000000;
}
void main(void)
{
init();
PTCON = 0B1000000000000000; //set PWM mode - free running, time base is on.
PTPER = 0B1001110000111111 //set pwm period to 39999, which should be 2 ms
PWMCON1 = 0B111111111111; //should enable all pins for PWM output
PDC1 = 0b1000; //should set PMW1 duty cycle.
}
;PTPER = (Fcy/Fpwm*(PTMRPrescaler))-1
;Fcy = Fosc/4
;for 20ms or 18ms servo update
;Fpwm = 1/.020 or 1/.018 = 50 or 55.55Hz
;PTPER = (1000000/(50*16))-1 = 1249 (12 bit (i.e. 0xFFF) is max for 18f1330)
PTPER = 1249
;Servo duty cycle is 1-2ms over the 20ms period or,
;1/20*4*(PTPER+1) 'full reverse', 1.5/20*4*(PTPER+1) 'neutral', 2/20ms*4*(PTPER+1) 'full forward'
;to get the correct duty cycle must multiply by 4 because of Q1:Q0 in PTMR register
;PDCx (PDCxH and PDCxL) will be forward = 500, neutral = 375, reverse = 250
//title "Project Zero"
#include <dspic30f4011.h>
#define FOSC 10000000 // 10Mhz
#define FCY (FOSC / 4) // 1 inscruction cycle = 4 clock cycles
void initpwm(void)
{
PTEN = 0; //Disable MCPWM
PWMCON1_PEN1H = 1; //PWM1H enabled for PWM output
PWMCON1_PEN2H = 1; //PWM2H enabled for PWM output
PTCKPS0 = 1; //PTCKPS<1:0> set to 01. 1:4 Prescale.
PTCKPS1 = 0;
PTOPS0 = 0; //PTOPS<3:0> set to 0s. 1:1 Postscale
PTOPS1 = 0;
PTOPS2 = 0;
PTOPS3 = 0;
PTSIDL = 1; // STOP in Idle Mode: Yes
PTMOD0 = 0; //PTMOD<1:0> set to 00. Free running mode.
PTMOD1 = 0;
PTEN = 1; //ENABLE MCPWM
PTPER = 11250;
}
void main(void)
{
initpwm();
while(1) //Infinite loop
{
PDC1 = 1875; //should set PMW1 duty cycle.
}
}
#include <htc.h>
//-----------------------------------------------------------------------------
//Configuration bits
//should be 9.7 mips frc for extended range device dsPIC30F1010, FRC_PLL and FRC_HI_RANGE
__CONFIG (FOSC, CLKSWDIS & FSCMDIS & RANGEH & CLKOACT);
__CONFIG (FOSCEL, FRCPLL);
__CONFIG (FWDT, WDTDIS);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?