Anda di halaman 1dari 6

Dspic sections

All the information regarding the controller and its sections is given in the
datasheet named dspic33fj128mc202, and the detailed information regarding
configuration of each section is given in the particular named section in the dspic
documentation folder . if you are not getting for which section you should go
directly, write your particular word in the main dspic datasheet find option it will
direct you for your section then open particular section from the folder.
I/O pins: sec10 dsPIC IO ports
We are using all the pins of the dspic except pin no. 2 & 24
Pin 12 and 26 are for the direction control
Pin 16 : QEI A of first motor encoder
Pin 17 Index of first motor encoder
Pin 15 QEI B of first motor encoder
Pin 21 : QEI A of second motor encoder
Pin 18 Index of second motor encoder
Pin 22 QEI B of second motor encoder
Pin 25 and 23 are connected to the pwm i.e. analog voltage
Pin 14 is RX of uart1
Pin 6 is TX of uart1
Pin 11 is RX of uart2
Pin 7 is RX of uart2
Pin 3 is configured as the change notification input for interrupt handling

For each pin configuration as input or output there is one register which controls its
action called TRIS reg. the section will guide you how to set it.
There is no need to connect external pull ups for the dspic , all you need to do is just
enable the internal pull up by setting particular reg. while using the particular pin as
a input.
And while reading the value from the pin or setting the particular pin as high or low
you have bit configured for each pin in the PORTA or PORTB reg. ex. PORTBbits.RB3
See led blinking prog.

#include <p33FJ128MC202.h>
#define FCY (7370000 * (44)/(2 * 4))
#define BAUDRATE 38400
#define BRGVAL ((FCY/BAUDRATE)/16)-1
All above defined directives are for the oscillator configuration all the formulas
needed for these calculations are given in the pdf

We are not using the external clock frequency, for the clock generation of the
controller we are using the internal FRC i.e. fast rc oscillator with PLL .
For that the configuration settings are as follows
PLLFBD=42; // M=44
CLKDIVbits.PLLPOST=0; // N1=2
CLKDIVbits.PLLPRE=0; // N2=2
OSCTUN=0; // Tune FRC oscillator
The controller has its own watchdog timer which will reset after some time so you
need to disable it
RCONbits.SWDTEN=0;
There is also another one bit which controls the enabling and disabling of the watch
dog .
FWDTbits.FWDTEN=0;
You can also disable it through mplab by setting the configuration bits.
And all macros written in the program are defined in the header file of the dspic
which we include at the starting of the program.
You will find that file at the location C:\Program Files\Microchip\MPLAB
C30\support\dsPIC33F\h
PWM
We are using PWM in our prog. for giving analog voltage to the motor controller. The
motor controller needs 0 to 12 volts for its speed variation. But as we dont need the
full speed capacity of the motor we vary it only in the rage of 3 to 5v.
The PWM gives the required voltages but doesnt provide the required current , so
we are using driver IC L298 for its current boost.
So now we give PWM o/p in the range of 0.7V to 1.11V to the driver IC which
converts it to the required range of voltage and current.
For the pwm configuration we need to do the following adjustments.
P1TCONbits.PTMOD = 0b00;/* PWM time base operates in a Free Running mode */
P1TCONbits.PTCKPS = 0b00;/* PWM time base input clock period is TCY (1:1
prescale) */

P1TCONbits.PTOPS = 0b00;/* PWM time base output post scale is 1:1 */


P1TPER = 1999;//PxTPER =(FCY/(FPWM (PxTMR Prescaler)))- 1//MOTOR CONTROL
pwmSECTION 14
PWM1CON1bits.PMOD1 = 1;/* PWM I/O pair 1 in independent mode */
PWM1CON1bits.PMOD2 = 1;/* PWM I/O pair 2 in independent mode */
PWM1CON1bits.PEN1H = 1;//pin 25 enabled for pwm
PWM1CON1bits.PEN2H = 1;// pin 23 enabled for pwm
2H = 1;//pin 23 enabled for pwm
PWM1CON2bits.IUE = 0; /* Immediate update of PWM enabled */
P1OVDCONbits.POVD1H = 1;// PWM I/O pin 25 controlled by PWM Generator
P1OVDCONbits.POVD2H = 1;// PWM I/O pin 23 controlled by PWM Generator
P1TCONbits.PTEN = 1; // pwm timer PTMR enable
Check out the following document for the further details of setting page no. 19
onwards sec15 dsPIC motor control

UART section:
Dspic has two uarts which can be configured differently. We are using the first uart
for the communication with the ros. And another for communication with the
secondary pic.
Refer pdf sec17 dsPIC UART and dsPIC UART.
U1MODEbits.STSEL = 0; // 1-stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
U1MODEbits.ABAUD = 0; // Auto-Baud Disabled
U1MODEbits.BRGH = 0; // Low Speed mode
U1BRG = BRGVAL; // BAUD Rate Setting for 9600

U1STAbits.URXISEL = 0; // Interrupt after one RX character is received;//interrupt


flag bit is set when char received
IEC0bits.U1RXIE = 0; // Enable UART Rx interrupt

U1STAbits.UTXISEL0 = 0; // Interrupt after one Tx character is transmitted


U1STAbits.UTXISEL1 = 0;
IEC0bits.U1TXIE = 1; // Enable UART Tx interrupt

U1MODEbits.UARTEN = 1; // Enable UART


U1STAbits.UTXEN = 1; // Enable UART Tx

These are the settings which you need to follow for uart configuration. And these
are only for one uart
For the two different we configure as
RPINR18bits.U1RXR = 5;//(pin 14)Assign UART1 Receive (U1RX) to the
corresponding RPn pin
RPOR1bits.RP2R = 3;//(pin 6) RPn tied to UART1 Transmit

RPINR19bits.U2RXR = 4;//(pin 11)Assign UART2 Receive (U2RX) to the


corresponding RPn pin
RPOR1bits.RP3R = 5;//(pin 7) RPn tied to UART2 Transmit
And you need to set all bits for each one separately .

QEI section:
The dspic has its own QEIs Quadrature Encoder Interface (QEI) .
You can refer sec16 dsPIC QEI in pic manuals.
The QEI uses two methods for encoding the data from motor encoder. The pins
coming from the encoder are mainly QEI A, QEI B, and INDEX. The index pulse gives
one pulse after its each rotation so this is the first method by wich you can
calculate the speed of motor otherwise another is by the reading of the phase
values i.e. QEI A and QEI B. when we dont use the index pulse method we calculate
position and speed. Of the motor by max count register.

The direction of the motor is obtained by the diff. between the phases i.e. if phase A
leads the phase B then motor is moving clockwise you can get more idea at
graphical representation in the section pdf. At the page no. 2
QEI settings are as follows
ADPCFG |= 0x0BB0; // Configure QEI pins as digital inputs
QEI1CONbits.QEIM = 0; // Disable QEI Module
QEI1CONbits.POSRES = 1; // Index pulse DOES NOT resets position counter
QEI1CONbits.PCDOUT = 0; // Normal I/O pin operation
QEI1CONbits.SWPAB = 0; // QEA and QEB not swapped
QEI1CONbits.QEISIDL = 0; // Continue operation during sleep
QEI1CONbits.CNTERR = 0; // Clear any count errors
DFLT1CONbits.CEID = 1; // Count error interrupts disabled
DFLT1CONbits.QEOUT = 0; // Digital filters output enabled for QEn pins
DFLT1CONbits.QECK = 0; // 1:1 clock divide for digital filter for QEn
QEI1CONbits.QEIM = 5; // X4 mode with position counter reset by Maxcount
POS1CNT =0;// Reset position counter
MAX1CNT=9999;
//ConfigIntQEI
IEC3bits.QEI1IE = 1;

// Enable QEI interrupt

For each max count overflow interrupt is generated. And we handle that interrupt in
its subroutine.
The subroutine is nothing but the routines written after the main() function looks
like macros in the each macro definition you need to write , no_auto_psv check
prog. for ex. As controller will not go inside subroutine until you guide him, it will
assume auto subroutine and will discard your settings.

Each section of the program is written inside the while loop as we need to receive
and transmit characters continuously as well as we require the p1dc1 regi. Value
continuously in the run.

The main logic steps of the program are as follows.


1)
2)
3)
4)
5)
6)
7)

Check handshake signals from the ros .


Then compare it for the confirmation.
Then initialize the motor p1dc1 values.
Check next stings coming from ros.
Convert the required bits in the integer.
Give respective value to the p1dc1 reg. for speed variation.
Encode direction from the received string and then rotate motor in resp.
direction
8) Check the values from the encoder phase.
9) Calculate its direction and encoder values and send it to ros.
10)
Check prog for the further details.

Many times the prog . Resets due to some internal interrupts ,so you need to figure
out by which interrupt !!. for that dspic contain its own register which stores the
respective bit according to reset occurred named RCON bit. So as long as the power
is on you can check the cause of the reset. By checking these bits. check the prog.
named rstwthled.c

Anda mungkin juga menyukai