Jump to content
Sign in to follow this  
emailcausey

Array Errors

Recommended Posts

The code below is working well. I have added an array to it (see purple bold text) and get expected values (arraypres[0]=100....array[9]=109). However, when I change it to

 

uint16_t arraypres[20]; 

 

and    

 

if(number <20){

    arraypres[number]=100+number;

    number = number+1;

    }

 

I have unexpected results (see attachment). I am perplexed as to why this is happening. Any suggestions greatly appreciated.

 

Code:

 

#include "DSP28x_Project.h" // Device Header file and Examples Include File

 

#include "f2802x_common/include/adc.h"

#include "f2802x_common/include/clk.h"

#include "f2802x_common/include/flash.h"

#include "f2802x_common/include/gpio.h"

#include "f2802x_common/include/pie.h"

#include "f2802x_common/include/pll.h"

#include "f2802x_common/include/pwm.h"

#include "f2802x_common/include/wdog.h"

#include "f2802x_common/include/spi.h"

#include "f2802x_common/include/timer.h"

 

// nokia5110 libraries

#include "font8x8.h"

#include "nokia5110.h"

#include "stdio.h"

 

// Prototype statements for functions found within this file.

interrupt void adc_isr(void);

void Adc_Config(void);

interrupt void cpu_timer0_isr(void);

interrupt void spiRxFifoIsr(void);

void gpio_init(void);

void spi_init(void);

 

// Global variables used in this example:

uint16_t interruptCount=0, val=0, arraymot[5], check=0;

uint16_t j=0,i=0, a=0,AVG_VOLT=0,mV=0,ConversionCount = 0, LoopCount = 0,interval=900, y=0; //default interval is 15 min

int16 level1=0, level2=0, level3=0, slope=16;//0.16 is the avg slope for LMV324 (should be 0.15 for INA333 slope)

uint16_t baseline;

uint16_t arraypres[10];

int16 number=0;

 

long Voltage1[10];

long Voltage2[10];

long Voltage3[10];

long Voltage5[10];

long Voltage6[10];

long Voltage7[10];

long Voltage8[10];

long Voltage9[10];

long Voltage10[10];

long Mean_Volt3=0;

long Mean_Volt7=0;

long Mean_Volt10=0;

long Peqn=0;

long Peqn1=0;

long Peqn2=0;

long Sum_Volt3=0;

long Sum_Volt7=0;

long Sum_Volt10=0;

 

ADC_Handle myAdc;

CLK_Handle myClk;

FLASH_Handle myFlash;

GPIO_Handle myGpio;

PIE_Handle myPie;

SPI_Handle mySpi;

CPU_Handle myCpu;

PLL_Handle myPll;

PWM_Handle myPwm1;

WDOG_Handle myWDog;

TIMER_Handle myTimer;

 

char value0[10];

char value1[10];

char value2[10];

 

void main(void)

{

    // Initialize all the handles needed for this application

    myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));

    myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));

    myCpu = CPU_init((void *)NULL, sizeof(CPU_Obj));

    myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));

    myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));

    myPie = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));

    myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));

    myPwm1 = PWM_init((void *)PWM_ePWM1_BASE_ADDR, sizeof(PWM_Obj));

    myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));

    myTimer = TIMER_init((void *)TIMER0_BASE_ADDR, sizeof(TIMER_Obj));

    mySpi = SPI_init((void *)SPIA_BASE_ADDR, sizeof(SPI_Obj));

 

    // Perform basic system initialization

    WDOG_disable(myWDog);

    CLK_enableAdcClock(myClk);

    (*Device_cal)();

 

    //Select the internal oscillator 1 as the clock source

    CLK_setOscSrc(myClk, CLK_OscSrc_Internal);

 

    // Setup the PLL for x12 /2 which will yield 60Mhz = 10Mhz * 12 / 2

    PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);

 

    PIE_disable(myPie);

    PIE_disableAllInts(myPie);

    CPU_disableGlobalInts(myCpu);

    CPU_clearIntFlags(myCpu);

 

    // If running from flash copy RAM only functions to RAM

    #ifdef _FLASH

    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);

    #endif

 

    // Setup a debug vector table and enable the PIE

    PIE_setDebugIntVectorTable(myPie);

    PIE_enable(myPie);

 

    // Register interrupt handlers in the PIE vector table

    PIE_registerPieIntHandler(myPie, PIE_GroupNumber_10, PIE_SubGroupNumber_1, (intVec_t)&adc_isr);

    PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_7, (intVec_t)&cpu_timer0_isr);

 

    // Configure CPU-Timer 0 to interrupt every second:

    // 60MHz CPU Freq, 1 second Period (in uSeconds)

    //ConfigCpuTimer(&CpuTimer0, 60, 1000000);

    TIMER_stop(myTimer);

    TIMER_setPeriod(myTimer, 60*1000000);

    TIMER_setPreScaler(myTimer, 0);

    TIMER_reload(myTimer);

    TIMER_setEmulationMode(myTimer, TIMER_EmulationMode_StopAfterNextDecrement);

    TIMER_enableInt(myTimer);

 

    TIMER_start(myTimer);

 

    // Initialize the ADC

    ADC_enableBandGap(myAdc);

    ADC_enableRefBuffers(myAdc);

    ADC_powerUp(myAdc);

    ADC_enable(myAdc);

    ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);

 

    // Enable ADCINT1 in PIE

    PIE_enableAdcInt(myPie, ADC_IntNumber_1);

    // Enable CPU Interrupt 1

    CPU_enableInt(myCpu, CPU_IntNumber_10);

    // Enable CPU INT1 which is connected to CPU-Timer 0:

    CPU_enableInt(myCpu, CPU_IntNumber_1);

    // Enable Global interrupt INTM

    // Enable TINT0 in the PIE: Group 1 interrupt 7

    PIE_enableTimer0Int(myPie);

    CPU_enableGlobalInts(myCpu);

    // Enable Global realtime interrupt DBGM

    CPU_enableDebugInt(myCpu);

 

    // Configure ADC

    //Note: Channel ADCINA4  will be double sampled to workaround the ADC 1st sample issue for rev0 silicon errata

    ADC_setIntPulseGenMode(myAdc, ADC_IntPulseGenMode_Prior);               //ADCINT1 trips after AdcResults latch

    ADC_enableInt(myAdc, ADC_IntNumber_1);                                  //Enabled ADCINT1

    ADC_setIntMode(myAdc, ADC_IntNumber_1, ADC_IntMode_ClearFlag);          //Disable ADCINT1 Continuous mode

    ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC2);                 //setup EOC2 to trigger ADCINT1 to fire

    ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A4);    //set SOC0 channel select to ADCINA4 - Interval

    ADC_setSocChanNumber (myAdc, ADC_SocNumber_1, ADC_SocChanNumber_A4);    //set SOC1 channel select to ADCINA4 - Interval

    ADC_setSocChanNumber (myAdc, ADC_SocNumber_2, ADC_SocChanNumber_A7);    //set SOC2 channel select to ADCINA7 - Pressure

    ADC_setSocChanNumber (myAdc, ADC_SocNumber_3, ADC_SocChanNumber_A3);    //Motor

    ADC_setSocTrigSrc(myAdc, ADC_SocNumber_0, ADC_SocTrigSrc_EPWM1_ADCSOCA);    //set SOC0 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1

    ADC_setSocTrigSrc(myAdc, ADC_SocNumber_1, ADC_SocTrigSrc_EPWM1_ADCSOCA);    //set SOC1 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1

    ADC_setSocTrigSrc(myAdc, ADC_SocNumber_2, ADC_SocTrigSrc_EPWM1_ADCSOCA);    //set SOC2 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1, then SOC2

    ADC_setSocTrigSrc(myAdc, ADC_SocNumber_3, ADC_SocTrigSrc_EPWM1_ADCSOCA);

    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_64_cycles);   //set SOC0 S/H Window to 64 ADC Clock Cycles, (6 ACQPS plus 1)

    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_1, ADC_SocSampleWindow_64_cycles);   //set SOC1 S/H Window to 64 ADC Clock Cycles, (6 ACQPS plus 1)

    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_2, ADC_SocSampleWindow_64_cycles);   //set SOC2 S/H Window to 64 ADC Clock Cycles, (6 ACQPS plus 1)

    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_3, ADC_SocSampleWindow_64_cycles);

 

    // Enable PWM clock

    CLK_enablePwmClock(myClk, PWM_Number_1);

 

    // Setup PWM

    PWM_enableSocAPulse(myPwm1);                                         // Enable SOC on A group

    PWM_setSocAPulseSrc(myPwm1, PWM_SocPulseSrc_CounterEqualCmpAIncr);   // Select SOC from from CPMA on upcount

    PWM_setSocAPeriod(myPwm1, PWM_SocPeriod_FirstEvent);                 // Generate pulse on 1st event

    PWM_setCmpA(myPwm1, 0x0080);                                         // Set compare A value

    PWM_setPeriod(myPwm1, 0xFFFF);                                       // Set period for ePWM1

    PWM_setCounterMode(myPwm1, PWM_CounterMode_Up);                      // count up and start

 

    CLK_enableTbClockSync(myClk);

 

    gpio_init();

    spi_init();

    nokiaInit();

 

    clear();

 

    myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));

 

    GPIO_setMode(myGpio, timeandreset, GPIO_19_Mode_GeneralPurpose);

    GPIO_setDirection(myGpio, timeandreset, GPIO_Direction_Output);

    GPIO_setHigh(myGpio, timeandreset); //Default is that the Omron is off

 

    GPIO_setMode(myGpio, LED1, GPIO_28_Mode_GeneralPurpose);

    GPIO_setDirection(myGpio, LED1, GPIO_Direction_Output);

    GPIO_setMode(myGpio, LED2, GPIO_29_Mode_GeneralPurpose);

    GPIO_setDirection(myGpio, LED2, GPIO_Direction_Output);

    GPIO_setMode(myGpio, LED3, GPIO_34_Mode_GeneralPurpose);

    GPIO_setDirection(myGpio, GPIO_Number_34, GPIO_Direction_Output);

    GPIO_setMode(myGpio, buzzer, GPIO_0_Mode_GeneralPurpose);

    GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);

 

    GPIO_setLow(myGpio, LED1);

    GPIO_setLow(myGpio, LED2);

    GPIO_setLow(myGpio, LED3);

    GPIO_setLow(myGpio, buzzer);

 

int SBP;

SBP = 120;

 

int DBP;

DBP = 110;

 

    while(1)

        {

 

       //arraypres[val] = 1;

 

    asm(" NOP");

 

    val = interruptCount;

 

      //baseline pressure taken during first 5 seconds

 

        if (val==5){ //0 V pulse sent for 3 seconds

        GPIO_setLow(myGpio, timeandreset);

        }

 

        else if(val==8){ //Device pumps up for 40 seconds

        GPIO_setHigh(myGpio, timeandreset);

        }

 

        else if(val==48){ //At this point a 0 V pulse sent for 3 seconds to turn the Omron off

        GPIO_setLow(myGpio, timeandreset);

        }

 

        else if(val==51){ //Omron rests until interval is reached - LED remains turned on until interval is reached

        GPIO_setHigh(myGpio, timeandreset);

 

                if(SBP >= 160 || DBP >= 110)

                {

                GPIO_setHigh(myGpio, LED1);

                }

 

                if(SBP >= 140 && SBP < 160 || DBP >= 90 && DBP < 110)

                {

                GPIO_setHigh(myGpio, LED2);

                }

 

                if(SBP < 140 && DBP < 90)

                {

                GPIO_setHigh(myGpio, LED3);

                }

        }

 

        //buzzer sounds if higher level is reached

        if(val>51 && val<53 && (SBP >= 160 || DBP >= 110))

            {

            level1=1;

 

            if(level1+level2+level3>2)

            {

            GPIO_setHigh(myGpio, buzzer);

            level2=0;

            level3=0;

            }

            }

 

            if(val>51 && SBP < 160 && DBP < 110 && (SBP >= 140 || DBP >= 90))

            {

            level1=0;

            level2=1;

 

            if(level2+level3>2)

            {

            GPIO_setHigh(myGpio, buzzer);

            level3=0;

            }

            }

 

            if(val>51 && SBP < 140 && DBP < 90)

            {

            level1=0;

            level2=0;

            level3=1;

            }

 

            //buzzer turns off after 10s

            if(val>53){

            GPIO_setLow(myGpio,buzzer);

            }

 

        if(val==interval){

        interruptCount = 0;

        GPIO_setLow(myGpio, LED1);

        GPIO_setLow(myGpio, LED2);

        GPIO_setLow(myGpio, LED3);

        }

 

            }

}

 

/* functions */

 

void gpio_init(void){

 

// set up GPIO's for nokia

//GPIO_setMode(myGpio, nokiaVcc, GPIO_0_Mode_GeneralPurpose);

GPIO_setMode(myGpio, nokiaRst, GPIO_0_Mode_GeneralPurpose);

GPIO_setMode(myGpio, nokiaDc, GPIO_0_Mode_GeneralPurpose);

GPIO_setMode(myGpio, nokiaBlight, GPIO_0_Mode_GeneralPurpose);

GPIO_setMode(myGpio, nokiaSce, GPIO_0_Mode_GeneralPurpose);

 

//GPIO_setDirection(myGpio, nokiaVcc, GPIO_Direction_Output);

GPIO_setDirection(myGpio, nokiaRst, GPIO_Direction_Output);

GPIO_setDirection(myGpio, nokiaDc, GPIO_Direction_Output);

GPIO_setDirection(myGpio, nokiaBlight, GPIO_Direction_Output);

GPIO_setDirection(myGpio, nokiaSce, GPIO_Direction_Output);

 

//GPIO_setPullUp(myGpio, nokiaVcc, GPIO_PullUp_Disable);

GPIO_setPullUp(myGpio, nokiaRst, GPIO_PullUp_Disable);

GPIO_setPullUp(myGpio, nokiaDc, GPIO_PullUp_Disable);

GPIO_setPullUp(myGpio, nokiaBlight, GPIO_PullUp_Disable);

GPIO_setPullUp(myGpio, nokiaSce, GPIO_PullUp_Disable);

 

return;

}

 

void spi_init(void){

 

// enable Spia clock

CLK_enableSpiaClock(myClk);

//set up GPIO's for SPI

GPIO_setPullUp(myGpio, nokiaMosi, GPIO_PullUp_Disable);

    GPIO_setPullUp(myGpio, nokiaClk, GPIO_PullUp_Disable);

 

    GPIO_setQualification(myGpio, nokiaMosi, GPIO_Qual_ASync);

    GPIO_setQualification(myGpio, nokiaClk, GPIO_Qual_ASync);

 

    GPIO_setMode(myGpio, nokiaMosi, GPIO_16_Mode_SPISIMOA);

    GPIO_setMode(myGpio, nokiaClk, GPIO_18_Mode_SPICLKA);

 

    // Resets the serial peripheral interface (SPI)

    SPI_reset(mySpi);

    // Initializes the serial peripheral interface (SPI) object handle

    SPI_enable(mySpi);

    // Enables the serial peripheral interface (SPI) transmit and receive channels

    SPI_enableChannels(mySpi);

    // Enable master mode

    SPI_setMode(mySpi, SPI_Mode_Master);

    // Enables the serial peripheral interface (SPI) masater/slave transmit mode

    SPI_enableTx(mySpi);

    // Sets the serial peripheral interface (SPI) character length

    SPI_setCharLength(mySpi, SPI_CharLength_8_Bits);

    // Sets the serial peripheral interface (SPI) clock phase

    SPI_setClkPhase(mySpi, SPI_ClkPhase_Normal);

    // Sets the serial peripheral interface (SPI) clock polarity

    SPI_setClkPolarity(mySpi, SPI_ClkPolarity_OutputFallingEdge_InputRisingEdge);

    // Enables the serial peripheral interface (SPI) interrupt

    SPI_enableInt(mySpi);

    // Sets the serial peripheral interface (SPI) baud rate

    SPI_setBaudRate(mySpi, (SPI_BaudRate_e)0x00);

    // enable the SPI

    SPI_enable(mySpi);

    // Set so breakpoints don't disturb xmission

    SPI_setPriority(mySpi, SPI_Priority_FreeRun);

 

    PIE_registerPieIntHandler(myPie, PIE_GroupNumber_6, PIE_SubGroupNumber_1, (intVec_t)&spiRxFifoIsr);

    PIE_enableInt(myPie, PIE_GroupNumber_6, PIE_InterruptSource_SPIARX);

    CPU_enableInt(myCpu, CPU_IntNumber_6);

 

    return;

}

 

interrupt void spiRxFifoIsr(void)

{

GPIO_setHigh(myGpio, nokiaSce); //set Sce high

    SPI_read(mySpi);

 

    // Issue PIE ack

    PIE_clearInt(myPie, PIE_GroupNumber_6);

 

    return;

}

 

interrupt void cpu_timer0_isr(void)

{

interruptCount = interruptCount++;

 

// Acknowledge this interrupt to receive more interrupts from group 1

    PIE_clearInt(myPie, PIE_GroupNumber_1);

}

 

interrupt void adc_isr(void)

{

Voltage1[ConversionCount] = ADC_readResult(myAdc, ADC_ResultNumber_1); //interval

Voltage2[ConversionCount] = Voltage1[ConversionCount]*3300;

Voltage3[ConversionCount] = Voltage2[ConversionCount]/4096;

 

Voltage5[ConversionCount] = ADC_readResult(myAdc, ADC_ResultNumber_2); //pressure

Voltage6[ConversionCount] = Voltage5[ConversionCount]*3300;

Voltage7[ConversionCount] = Voltage6[ConversionCount]/4096;

 

Voltage8[ConversionCount] = ADC_readResult(myAdc, ADC_ResultNumber_3); //motor

Voltage9[ConversionCount] = Voltage8[ConversionCount]*3300;

Voltage10[ConversionCount] = Voltage9[ConversionCount]/4096;

 

Sum_Volt3=Voltage3[ConversionCount]+Sum_Volt3; //interval

Sum_Volt7=Voltage7[ConversionCount]+Sum_Volt7; //pressure

Sum_Volt10=Voltage10[ConversionCount]+Sum_Volt10; //motor

 

//Clears screen periodically

if(LoopCount == 99)

{

LoopCount = 0;

clear();

}

else LoopCount++;

 

// If 30 conversions have been logged, start over

if(ConversionCount == 29)

{

   ConversionCount = 0;

   Mean_Volt3=Sum_Volt3/30; //Mean of interval

   Mean_Volt7=Sum_Volt7/30; //Mean of pressure

   Mean_Volt10=Sum_Volt10/30; //Mean of motor

   Sum_Volt3=0;

   Sum_Volt7=0;

   Sum_Volt10=0;

 

if (val<5){ //baseline measured

baseline=Mean_Volt7;

}

 

   Peqn = (slope*Mean_Volt7-slope*baseline)/100;

 

}

else ConversionCount++;

 

    if(number <10){

    arraypres[number]=100+number;

    number = number+1;

    }

 

    writeString(1,0,"Pressure:");

    writeString(35,1,"mmHg");

    writeString(1,2,"Voltage:");

    writeString(35,3,"mV");

    writeString(1,4,"Interval:");

 

int t=Mean_Volt7; //voltage from pressure

sprintf(value0,"%d",t);

writeString(1,3,value0);

 

int y_2=Peqn; //pressure

sprintf(value2,"%d",y_2);

writeString(1,1,value2);

 

if(Mean_Volt3<100){

 

writeString(1,5,"15 min");

interval=15*60;

}

 

else if(Mean_Volt3>=100 && Mean_Volt3<500){

writeString(1,5,"30 min");

interval=30*60;

}

 

else if(Mean_Volt3>=500 && Mean_Volt3<900){

writeString(1,5,"1 hr  ");

interval=60*60;

}

 

else if(Mean_Volt3>=900 && Mean_Volt3<1300){

writeString(1,5,"2 hrs ");

interval=120*60;

}

 

else if(Mean_Volt3>=1300){

writeString(1,5,"3 hrs  ");

interval=240*60;

}

 

// Clear ADCINT1 flag reinitialize for next SOC

ADC_clearIntFlag(myAdc, ADC_IntNumber_1);

// Acknowledge interrupt to PIE

PIE_clearInt(myPie, PIE_GroupNumber_10);

 

i++;

 

return;

}

 

post-1078-14264717220377_thumb.jpg

Share this post


Link to post
Share on other sites

My guess is that the problem is related to signed/unsigned integers.  The variable number is signed while you're array is not.  I would try viewing the results as hex and see if you can see a pattern...

 

You might also want to try stepping through and looking at the variables in the arithmetic (as hex values)...I think you'll see the problem.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...