• AVR Freaks

dsPIC33EP512MU810 Matrix Multiplication Problem

Author
syntax3
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/01/02 09:23:44
  • Location: 0
  • Status: offline
2020/01/14 23:59:56 (permalink)
0

dsPIC33EP512MU810 Matrix Multiplication Problem

Hi,
 
I am trying to use matrix multiplication function of dsp.h library. I defined fractional variables, my matrices and converted matrices to fractional data type then send it to "MatrixMultiply" function after that I send the results through serial channel and read it on serial terminal.
 
However, whatever the input the output of matrix multiplication function is zero. Below I shared my code.
 
//----------------CONFIG BITS----------------//

// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)

// FOSCSEL
#pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection Bits (Primary Oscillator (XT, HS, EC) with PLL)
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)

// FOSC
#pragma config POSCMD = EC // Primary Oscillator Mode Select bits (EC (External Clock) Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch will not wait for the PLL lock signal.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = ON // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)

// FICD
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)

// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)

//-------------------------------------------//

#define FCY 70000000UL //Defining Expected Clock frequency for __delay_ functions


#include <xc.h>
#include <libpic30.h> //Delay Function Library
#include <dsp.h>


//int sine_data[100] = {2048,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844,2047,3251,3995,3995,3251,2048,844,100,100,844};
int i;
/*
float vandMat[400] = {-1.31937808286819e-06,-1.15945346676295e-06,-1.00768826984676e-06,-8.63914256672415e-07,-7.27963191792718e-07,-5.99666839760472e-07,-4.78856965128483e-07,-3.65365332449552e-07,-2.59023706276483e-07,-1.59663851162080e-07,-6.71175316591451e-08,1.87834876795183e-08,9.82074423011061e-08,1.71322567652816e-07,2.38297099181843e-07,2.99299272335387e-07,3.54497322560642e-07,4.04059485304806e-07,4.48153996015075e-07,4.86949090138647e-07,5.20613003122718e-07,5.49313970414484e-07,5.73220227461143e-07,5.92500009709891e-07,6.07321552607925e-07,6.17853091602442e-07,6.24262862140638e-07,6.26719099669711e-07,6.25390039636857e-07,6.20443917489272e-07,6.12048968674154e-07,6.00373428638699e-07,5.85585532830104e-07,5.67853516695566e-07,5.47345615682282e-07,5.24230065237447e-07,4.98675100808260e-07,4.70848957841917e-07,4.40919871785614e-07,4.09056078086550e-07,3.75425812191918e-07,3.40197309548918e-07,3.03538805604744e-07,2.65618535806596e-07,2.26604735601670e-07,1.86665640437159e-07,1.45969485760264e-07,1.04684507018181e-07,6.29789396581044e-08,2.10210191272345e-08,-2.10210191272337e-08,-6.29789396581052e-08,-1.04684507018182e-07,-1.45969485760265e-07,-1.86665640437160e-07,-2.26604735601669e-07,-2.65618535806597e-07,-3.03538805604744e-07,-3.40197309548918e-07,-3.75425812191919e-07,-4.09056078086549e-07,-4.40919871785615e-07,-4.70848957841920e-07,-4.98675100808262e-07,-5.24230065237446e-07,-5.47345615682284e-07,-5.67853516695567e-07,-5.85585532830107e-07,-6.00373428638699e-07,-6.12048968674153e-07,-6.20443917489273e-07,-6.25390039636858e-07,-6.26719099669712e-07,-6.24262862140640e-07,-6.17853091602441e-07,-6.07321552607927e-07,-5.92500009709891e-07,-5.73220227461145e-07,-5.49313970414485e-07,-5.20613003122716e-07,-4.86949090138647e-07,-4.48153996015074e-07,-4.04059485304805e-07,-3.54497322560641e-07,-2.99299272335387e-07,-2.38297099181845e-07,-1.71322567652819e-07,-9.82074423011061e-08,-1.87834876795187e-08,6.71175316591461e-08,1.59663851162077e-07,2.59023706276483e-07,3.65365332449548e-07,4.78856965128485e-07,5.99666839760475e-07,7.27963191792720e-07,8.63914256672415e-07,1.00768826984676e-06,1.15945346676296e-06,1.31937808286819e-06,
                     0.000229006338669266,0.000203012876958733,0.000178291585262902,0.000154816975911525,0.000132563561234350,0.000111505853561127,9.16183652216061e-05,7.28756085455364e-05,5.52520958626680e-05,3.87223395027503e-05,2.32608517955333e-05,8.84214507076644e-06,-4.55926834180045e-06,-1.69688761124177e-05,-2.84121659113357e-05,-3.89146254088046e-05,-4.85017422750749e-05,-5.71990041803967e-05,-6.50318987950205e-05,-7.20259137891965e-05,-7.82065368331751e-05,-8.35992555972064e-05,-8.82295577515410e-05,-9.21229309664289e-05,-9.53048629121207e-05,-9.78008412588665e-05,-9.96363536769168e-05,-0.000100836887836522,-0.000101427931407932,-0.000101434972061397,-0.000100883497467168,-9.97989952954944e-05,-9.82069532166273e-05,-9.61328589008169e-05,-9.36022000183133e-05,-9.06404642393668e-05,-8.72731392342279e-05,-8.35257126731467e-05,-7.94236722263735e-05,-7.49925055641586e-05,-7.02577003567529e-05,-6.52447442744057e-05,-5.99791249873679e-05,-5.44863301658899e-05,-4.87918474802219e-05,-4.29211646006140e-05,-3.68997691973165e-05,-3.07531489405800e-05,-2.45067915006546e-05,-1.81861845477908e-05,-1.18168157522390e-05,-5.42417278424890e-06,9.66256685928837e-07,7.32898498804341e-06,1.36385244518453e-05,1.98693874070839e-05,2.59960861835086e-05,3.19931331108697e-05,3.78350405189163e-05,4.34963207373983e-05,4.89514860960657e-05,5.41750489246678e-05,5.91415215529542e-05,6.38254163106752e-05,6.82012455275798e-05,7.22435215334183e-05,7.59267566579397e-05,7.92254632308942e-05,8.21141535820315e-05,8.45673400411010e-05,8.65595349378526e-05,8.80652506020359e-05,8.90589993634008e-05,8.95152935516965e-05,8.94086454966735e-05,8.87135675280806e-05,8.74045719756681e-05,8.54561711691852e-05,8.28428774383827e-05,7.95392031130088e-05,7.55196605228138e-05,7.07587619975471e-05,6.52310198669597e-05,5.89109464607998e-05,5.17730541088181e-05,4.37918551407632e-05,3.49418618863851e-05,2.51975866754348e-05,1.45335418376606e-05,2.92423970281214e-06,-9.65580739935930e-06,-2.32320871391067e-05,-3.78300871866778e-05,-5.34752952123239e-05,-7.01931988862958e-05,-8.80092858788434e-05,-0.000106949043860217,-0.000127037960500666,-0.000148301523470442,-0.000170765220439795,
                     -0.0116508624020593,-0.0104768957763848,-0.00935675637284791,-0.00828940935838932,-0.00727381989994982,-0.00630895316447015,-0.00539377431889107,-0.00452724853015331,-0.00370834096519763,-0.00293601679096477,-0.00220924117439549,-0.00152697928243053,-0.000888196282010641,-0.000291857340076571,0.000263072376430936,0.000777627700571130,0.00125284346540326,0.00168975450398659,0.00208939564938035,0.00245280173464381,0.00278100759283621,0.00307504805701681,0.00333595796024487,0.00356477213557962,0.00376252541608033,0.00393025263480624,0.00406898862481662,0.00417976821917069,0.00426362625092774,0.00432159755314699,0.00435471695888772,0.00436401930120916,0.00435053941317057,0.00431531212783120,0.00425937227825030,0.00418375469748714,0.00408949421860094,0.00397762567465098,0.00384918389869649,0.00370520372379675,0.00354671998301099,0.00337476750939846,0.00319038113601843,0.00299459569593012,0.00278844602219283,0.00257296694786577,0.00234919330600821,0.00211815992967941,0.00188090165193859,0.00163845330584504,0.00139184972445799,0.00114212574083669,0.000890316188040407,0.000637455899128380,0.000384579707159870,0.000132722445194107,-0.000117081053709630,-0.000363795956492092,-0.000606387430094035,-0.000843820641456201,-0.00107506075751934,-0.00129907294522422,-0.00151482237151156,-0.00172127420332212,-0.00191739360759666,-0.00210214575127589,-0.00227449580130060,-0.00243340892461151,-0.00257785028814940,-0.00270678505885499,-0.00281917840366902,-0.00291399548953228,-0.00299020148338548,-0.00304676155216937,-0.00308264086282474,-0.00309680458229226,-0.00308821787751277,-0.00305584591542696,-0.00299865386297557,-0.00291560688709938,-0.00280567015473912,-0.00266780883283556,-0.00250098808832941,-0.00230417308816147,-0.00207632899927243,-0.00181642098860309,-0.00152341422309415,-0.00119627386968642,-0.000833965095320575,-0.000435453066937415,2.97048522336987e-07,0.000474320084117923,0.000987650872908587,0.00154132424795360,0.00213637504231218,0.00277383808904368,0.00345474822120720,0.00418014027186205,0.00495104907406757,0.00576850946088288,
                     0.159999999999997,0.147878787878785,0.136252319109459,0.125111667909898,0.114447908497980,0.104252115091583,0.0945153619085860,0.0852287231668666,0.0763832730843034,0.0679700858787747,0.0599802357681590,0.0524047969703346,0.0452348437031797,0.0384614501845729,0.0320756906323923,0.0260686392645163,0.0204313702988234,0.0151549579531917,0.0102304764454997,0.00564899999362573,0.00140160281544804,-0.00252064087115492,-0.00612665684830482,-0.00942537089812343,-0.0124257088027323,-0.0151365963442531,-0.0175669593048075,-0.0197257234665171,-0.0216218146115037,-0.0232641585218889,-0.0246616809797943,-0.0258233077673415,-0.0267579646666524,-0.0274745774598484,-0.0279820719290514,-0.0282893738563828,-0.0284054090239644,-0.0283391032139179,-0.0280993822083649,-0.0276951717894269,-0.0271353977392259,-0.0264289858398833,-0.0255848618735209,-0.0246119516222602,-0.0235191808682229,-0.0223154753935308,-0.0210097609803055,-0.0196109634106686,-0.0181280084667417,-0.0165698219306465,-0.0149453295845048,-0.0132634572104382,-0.0115331305905682,-0.00976327550701667,-0.00796281774190516,-0.00614068307735521,-0.00430579729548858,-0.00246708617842700,-0.000633475508292236,0.00118610893279431,0.00298274136271098,0.00474749599933583,0.00647144706054742,0.00814566876422423,0.00976123532824441,0.0113092209704862,0.0127806999088280,0.0141667463611484,0.0154584345453255,0.0166468386792377,0.0177230329807636,0.0186780916677808,0.0195030889581684,0.0201890990698045,0.0207271962205673,0.0211084546283353,0.0213239485109868,0.0213647520864002,0.0212219395724536,0.0208865851870257,0.0203497631479946,0.0196025476732387,0.0186360129806364,0.0174412332880660,0.0160092828134058,0.0143312357745342,0.0123981663893295,0.0102011488756701,0.00773125745143427,0.00497956633450064,0.00193714974274667,-0.00140491810594817,-0.00505556299370619,-0.00902371070264924,-0.0133182870148981,-0.0179482177125754,-0.0229224285778021,-0.0282498453926999,-0.0339393939393911,-0.0399999999999965
};
*/
//fractional vandMatF[400];
//float data_y[100];
//fractional data_yF[100];

int matrix1[9] = {1,2,3,1,2,3,1,2,3};
int vector[3] = {1,2,3};
int resultV[3];

fractional matrix1F[9];
fractional vectorF[3];
fractional resultVF[3];

void Initialization(void){
    //----CLOCK SETUP----//
    // Crystal Frequency * (PLLDIV+2)
    //Fosc = --------------------------------
    // (PLLPOST + 2) * (PLLPRE + 2)
    //FCY = Fosc/2
    
    PLLFBDbits.PLLDIV = 54;
    CLKDIVbits.PLLPOST = 0;
    CLKDIVbits.PLLPRE = 0;
    
    //----PORT SETUP----//
    TRISB = 0x000; //10 bits, All ports set to OUTPUT for PORTB
    ANSELB = 0x000; //10 bits, All channels are set to digital port.
    AD1CON1 = 0x0000; // ADC1 Turned off;
    AD2CON1 = 0x0000; // ADC2 Turned off;
    TRISGbits.TRISG6 = 0; //Output
    
    
       //----USART SETUP----//

    
    U1MODEbits.UARTEN = 0;
    U1BRG = 455; //9600
    RPOR13bits.RP118R = 0b00001;
    U1MODEbits.UARTEN = 1;
    U1STAbits.UTXEN = 1;
    __delay_us(105);
    
    
    
}


void UART_Send_Char(unsigned char *data)
{

    while(*data != '\0') // Loop until end of the string
    {

        U1TXREG = *data; // Place data into TX Buffer
        data++; // Increment data address
        while(!(U1STAbits.TRMT)); //Wait until transmission is over
    }
    
    
}



unsigned char * int2str(unsigned int value)
{
    //Converting 4 digit (Since 10 bit = 1024 and 12 bit = 4096) integer value into string
    static unsigned char str[5]; //Number of digits + 1 since '\0' is needed at end of string
    unsigned int k = 0;
    const unsigned int digits[4] = {1000, 100, 10,1};//Add 10^n to set for desired digits
    while(k != 4)
    {
        str[k] = '0';

        while (value >= digits[k])
        {
            value -= digits[k];
            str[k]++;
        }

        k++;
    }
    str[4] = '\0';

    return str;

}




int main(void) {
    
    Initialization();

    for(i=0;i<9;i++)
    {
        matrix1F[i] = (signed) matrix1[i] >> 1;
    }

    for(i=0;i<3;i++)
    {
        vectorF[i] = (signed) vector[i] >> 1;
    }
 
    
    
    MatrixMultiply(3,3,1,resultVF,matrix1F,vectorF);
    
    
    
    
    while(1){
        
        for(i = 0; i<3 ; i++)
        {
            UART_Send_Char(int2str(resultVF[i]*2));
        }
        
        __delay_ms(1000);
        
    }
    
    return 0;
}

 
I would really appreciate any help. I am new to dspic familiy.  
#1

0 Replies Related Threads

    Jump to:
    © 2020 APG vNext Commercial Version 4.5