caramoan tour package

caramoan tour package

Author Topic: patulong po sa pagdebug ng dsPIC30f4011  (Read 181 times)

Offline studyante

  • Size AA Battery
  • ****
  • Posts: 105
  • Pogi/Ganda Points: 0
patulong po sa pagdebug ng dsPIC30f4011
« on: March 16, 2011, 07:47:14 PM »
nagkakaproblema po ako sa pagprogram ng dspic30f4011. kapag naginitialize po ako ng motor control pwm module na may interrupt tapos kapag naginitialize po ako ng change notification module na may interrupt naghahang po yung mga interrupts ko kapag may nagchange sa inputs ko..anu po kaya ang problema dito?

eto po ang code ko
Code: [Select]
#include "dspic30f4011.h"
#include "pwmmc.h"
#include "uart.h"
#include "cin.h"
#include "function.h"

void main(){
     spd = 10;
     port_init();
     uart_init();
     pwmmc_init();
     joint4 = 0;
//    move4 = 1;
     cin_init();
     while(1){
          if(joint4 >= 2880){
               PWM1_MC_Stop();
          }
     }
     
     
     /*
     while(1){
          while(fin) {
               if (UART2_Data_Ready()) {
                    UART2_Read_Text(command,".",5);
                    cmd_decode();
               }
          }
          while(fin == 0){
               move4 = 1;
               cin_init();
               if (joint4_hold < mj4){
                    pwm_dir = 1;                                                  //movement is counterclockwise
                    pwmmc_init();
                    angle4 = mj4 - joint4_hold;
                    joint4 = 0;
                    while(joint4_pos < angle4){
                         Ctrl4 = 1;
                         if(UART2_Read() == 's'){
                              PWM1_MC_Stop();
                              fin = 1;
                              cmd++;
                         }
                    }
                    Ctrl4 = 0;
                    PWM1_MC_Stop();
               }
               else if(joint4_hold > mj4){
                    pwm_dir = 0;                                                  //movement is clockwise
                    pwmmc_init();
                    angle4 = joint4_hold - mj4;
                    joint4 = 0;
                    while(joint4_pos < angle4){
                         Ctrl4 = 1;
                         if(UART2_Read() == 's'){
                              PWM1_MC_Stop();
                              fin = 1;
                              cmd++;
                         }
                    }
                    Ctrl4 = 0;
                    PWM1_MC_Stop();
               }
               else {
                    Ctrl4 = 0;
                    joint4 = 0;
                    PWM1_MC_Stop();
               }
               joint4_hold = mj4;
               move4 = 0;
               cin_init();
               fin = 1;
          }
          if(cmd == 7){
               UART2_Write_Text("Adept Cobra 600 Motor Control\r\n");
               cmd = 0;
          }
     }
     */
}

//PWM interrupt subroutine
void pwmmc_isr(void) iv IVT_ADDR_PWMINTERRUPT {
     //increment indeces
     index1++;
     index2++;
     index3++;
     //reset indeces
     if(index1 == 16)   index1 = 0;
     if(index2 == 16)   index2 = 0;
     if(index3 == 16)   index3 = 0;
     //update PWM duty cycles
     if (pwm_dir){
          PWM1_MC_Set_Duty(sine_table[index1], 1);    //initialize PWM1 to index1
          PWM1_MC_Set_Duty(sine_table[index2], 2);    //initialize PWM2 to index2
          PWM1_MC_Set_Duty(sine_table[index3], 3);    //initialize PWM3 to index3
     }
     else {
          PWM1_MC_Set_Duty(sine_table[index1], 1);    //initialize PWM1 to index1
          PWM1_MC_Set_Duty(sine_table[index3], 2);    //initialize PWM2 to index3
          PWM1_MC_Set_Duty(sine_table[index2], 3);    //initialize PWM3 to index2
     }
     IFS2bits.PWMIF = 0;                                              //clear PWM Interrupt flag
}

//PWM initialize function
void pwmmc_init(void) {
     TRISE = 0x0100;                                                  //initialize PWM pins as outputs and FLTA as input
     switch(spd){
          case 1:   volt = 0.1;
                    break;
          case 2:   volt = 0.2;
                    break;
          case 3:   volt = 0.3;
                    break;
          case 4:   volt = 0.4;
                    break;
          case 5:   volt = 0.5;
                    break;
          case 6:   volt = 0.6;
                    break;
          case 7:   volt = 0.7;
                    break;
          case 8:   volt = 0.8;
                    break;
          case 9:   volt = 0.9;
                    break;
          default:  volt = 1.0;
                    break;
     }
     pwm_period = PWM1_MC_Init(320, 0x00, 0xFF, 0x0000);             //initialize to 12kHz PWM, 0 prescaler, 0 postscaler, and complementary mode
     if (pwm_dir){
          PWM1_MC_Set_Duty(sine_table[index1], 1);    //initialize PWM1 to index1
          PWM1_MC_Set_Duty(sine_table[index2], 2);    //initialize PWM2 to index2
          PWM1_MC_Set_Duty(sine_table[index3], 3);    //initialize PWM3 to index3
     }
     else {
          PWM1_MC_Set_Duty(sine_table[index1], 1);    //initialize PWM1 to index1
          PWM1_MC_Set_Duty(sine_table[index3], 2);    //initialize PWM2 to index3
          PWM1_MC_Set_Duty(sine_table[index2], 3);    //initialize PWM3 to index2
     }
     DTCON1 = 0x0002;                                                 //~1us deadtime
     IFS2bits.PWMIF = 0;                                              //clear PWM Interrupt flag
     IPC9bits.PWMIP = 1;                                              //priority set to 7
     IEC2 |= 0x0080;                                                  //enable PWM interrupt
     PWM1_MC_Start();                                                 //enable all PWM pins
}

//encoder update function
void cin_isr(void) iv IVT_ADDR_CNINTERRUPT {
     if (pwm_dir){
          //joint1 encoder
          if(QEA1){
               if(QEB1)  joint1++;
               else      joint1 = joint1;
          }
          else {
               if(QEB1)  joint1 = joint1;
               else      joint1++;
          }
          //joint2 encoder
          if(QEA2){
               if(QEB2)  joint2++;
               else      joint2 = joint2;
          }
          else {
               if(QEB2)  joint2 = joint2;
               else      joint2++;
          }
          //joint3 encoder
          if(QEA3){
               if(QEB3)  joint3++;
               else      joint3 = joint3;
          }
          else {
               if(QEB3)  joint3 = joint3;
               else      joint3++;
          }
          //joint4 encoder
          if(QEA4){
               if(QEB4)  joint4++;
               else      joint4 = joint4;
          }
          else {
               if(QEB4)  joint4 = joint4;
               else      joint4++;
          }
     }
     else {
          //joint1 encoder
          if(QEA1){
               if(QEB1)  joint1 = joint1;
               else      joint1++;
          }
          else {
               if(QEB1)  joint1++;
               else      joint1 = joint1;
          }
          //joint2 encoder
          if(QEA2){
               if(QEB2)  joint2 = joint2;
               else      joint2++;
          }
          else {
               if(QEB2)  joint2++;
               else      joint2 = joint2;
          }
          //joint3 encoder
          if(QEA3){
               if(QEB3)  joint3 = joint3;
               else      joint3++;
          }
          else {
               if(QEB3)  joint3++;
               else      joint3 = joint3;
          }
          //joint4 encoder
          if(QEA4){
               if(QEB4)  joint4 = joint4;
               else      joint4++;
          }
          else {
               if(QEB4)  joint4++;
               else      joint4 = joint4;
          }
     }
     Ctrl1 = ~Ctrl1;
     //joint1_pos = joint1*joint1_res;
     //joint2_pos = joint2*joint2_res;
     //joint3_pos = joint3*joint3_res;
     //joint4_pos = joint4*joint4_res;
}

void cin_init(){
     /*
     if (move1){
          CNEN1.CN6IE = 1;
          CNPU1.CN6PUE = 1;
     }
     else if (move2){
          CNEN1.CN2IE = 1;
          CNPU1.CN2PUE = 1;
     }
     else if (move3){
          CNEN1.CN4IE = 1;
          CNPU1.CN4PUE = 1;
     }*/
     //else if (move4){
          CNEN1.CN1IE = 1;
          CNPU1.CN1PUE = 1;
     /*
     }
     else {
          CNEN1.CN1IE = 0;
          CNEN1.CN2IE = 0;
          CNEN1.CN4IE = 0;
          CNEN1.CN6IE = 0;
          CNPU1.CN1PUE = 0;
          CNPU1.CN2PUE = 0;
          CNPU1.CN4PUE = 0;
          CNPU1.CN6PUE = 0;
     }
     */
     IFS0bits.CNIF = 0;
     //IPC3bits.CNIP = 7;
     IEC0bits.CNIE = 1;
}

//system configuration
#define Fosc       8000000
#define Fcy        Fosc/4

//resolution per encoder count
#define joint1_res 0.00045
#define joint2_res 0.00072
#define joint3_res 0.0015
#define joint4_res 0.03125

//control pins for motor joints
#define Ctrl1      PORTFbits.RF1
#define Ctrl2      PORTFbits.RF6
#define Ctrl3      PORTDbits.RD0
#define Ctrl4      PORTDbits.RD2

//encoder pins for motor joints
#define QEA2       PORTBbits.RB0
#define QEB2       PORTBbits.RB1
#define QEA3       PORTBbits.RB2
#define QEA1       PORTBbits.RB4
#define QEB1       PORTBbits.RB5
#define limit1     PORTBbits.RB3
#define limit2     PORTBbits.RB6
#define limit3     PORTBbits.RB7
#define QEB3       PORTBbits.RB8
#define QEA4       PORTCbits.RC13
#define QEB4       PORTCbits.RC14
#define INDX4      PORTDbits.RD1

//variable declarations
static char command[5];

static int cfg, mj1, mj2, mj3, mj4, spd;
static int index1 = 0, index2 = 5, index3 = 11;
static int cmd = 0, pwm_dir = 0, nest = 0, fin = 1;
static int move1 = 0, move2 = 0, move3 = 0, move4 = 0;
static int angle1 = 0, angle2 = 0, angle3 = 0, angle4 = 0;
static int joint1 = 0, joint2 = 0, joint3 = 0, joint4 = 0;
static int joint1_pos, joint2_pos, joint3_pos, joint4_pos;
static int joint1_hold, joint2_hold, joint3_hold, joint4_hold;
static int state4 = 0;
unsigned int pwm_period;

static float volt;
static float table[16];
static float volt_table[10] = {0.10,0.20,0.30,0.40,0.50,0.60,0.70,0.80,0.90,1.00};
static float sine_table1[16] = {1.00,1.38,1.70,1.92,2.00,1.92,1.70,1.38,1.00,0.62,0.30,0.08,0.00,0.08,0.30,0.62};
static int sine_table[16] = {5750,7935,9775,11040,11500,11040,9775,7935,5750,3565,1725,460,0,460,1725,3565};

void port_init(){
     ADPCFG = 0xFFFF;                                                 //set PORTB as digital I/O
     TRISBbits.TRISB0 = 1;
     TRISBbits.TRISB1 = 1;
     TRISBbits.TRISB2 = 1;
     TRISBbits.TRISB3 = 1;
     TRISBbits.TRISB4 = 1;
     TRISBbits.TRISB5 = 1;
     TRISBbits.TRISB6 = 1;
     TRISBbits.TRISB7 = 1;
     TRISBbits.TRISB8 = 1;
     TRISCbits.TRISC13 = 1;
     TRISCbits.TRISC14 = 1;
     TRISDbits.TRISD1 = 1;
     TRISDbits.TRISD0 = 0;
     TRISDbits.TRISD2 = 0;
     TRISFbits.TRISF1 = 0;
     TRISFbits.TRISF6 = 0;
}

//UART receive interrupt subroutine
void UARTRX_isr(void) iv IVT_ADDR_U1RXINTERRUPT {

     IFS1bits.U2RXIF = 0;
}

//UART transmit interrupt subroutine
void UARTTX_isr(void) iv IVT_ADDR_U1TXINTERRUPT {

     IFS1bits.U2TXIF = 0;                                          //clear the UART1 transmitter interrupt flag
}

//uart initialize function
void uart_init(){
     UART2_Init(9600);
     delay_ms(100);
     if (UART2_Tx_Idle() == 1){
          UART2_Write_Text("Adept Cobra 600 Motor Control\r\n");
     }
}

Philippine Electronics Forum

patulong po sa pagdebug ng dsPIC30f4011
« on: March 16, 2011, 07:47:14 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com