CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Filling Arrays??~??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jacqueskleynhans



Joined: 10 Apr 2008
Posts: 109
Location: Cape Town, South Africa

View user's profile Send private message

Filling Arrays??~??
PostPosted: Sun Aug 31, 2008 6:46 am     Reply with quote

Hi just want to know if my sequence of filling arrays is right and if there is a better way of doing this??

Code:
    case FILLARRAYS:
         if(n < 32)
        {
          n++;
          //Add acceleromter values to the arrays
          XArray16[n]      = Xaxis;
          YArray16[n]      = Yaxis;
          ZArray16[n]      = Zaxis;

          //Point each pointer in the array at the Acceleromter values in the data arrays above
          PointerX16[n]    = &XArray16[n];
          PointerY16[n]    = &YArray16[n];
          PointerZ16[n]    = &ZArray16[n];
        }else if(n = 32)
        {
           State0 = PRINTARRAYS;
        }
          break;
     
      case PRINTARRAYS:
         printf("\n\rSInt16 Data Array:");
         
        for(n = 0; n < 32; n++)
           {
             printf("%ld ",XArray16[n]);
             printf("%ld ",YArray16[n]);
             printf("%ld ",ZArray16[n]);
           }
             break;              

_________________
"THE ONLY EASY DAY WAS YESTERDAY"
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Aug 31, 2008 7:50 am     Reply with quote

Try to post short and complete code examples.
Where do you fill the arrays? This code is incomplete, you only show a few assignments in the array but there is no loop.
The code for printing the arrays has nothing to do with your question and could have been left out.

A few remarks though:
- the 'n++' at the top means you can never write to Array16[0] but you will write (the non existing?) Array16[32]. Seems like a bug.
Code:
}else if(n = 32)
- Bug: change '=' to '=='.
- You are using a pointer array. Why? My guess is you want to do some (speed) optimization. The PIC is different from many other processors in that it is inefficient in using pointers. Pointers are a great programming technique but use them with knowledge.

As a learning exercise: study the generated list file for your function (*.lst file). Even when you don't understand assembly language you will see from the number of assembly lines if your code is efficient or not.
jacqueskleynhans



Joined: 10 Apr 2008
Posts: 109
Location: Cape Town, South Africa

View user's profile Send private message

**
PostPosted: Sun Aug 31, 2008 9:15 am     Reply with quote

I am using the arrays in a state machine after i read new adc value
i was trying to do it the old fastion way but could not suceed eg
Code:
void FillArrays(void)
{
   for(n = 0; n < 32; n++)
   {
      //Set the elements in each data array to zero
      DataArray16[n]       = n;
      DataArray8[n]       =  n;
      //Set the poiters to point at a zero valued variable
      PointerArray16[n]    = &Default16;
      PointerArray8[n]    = &Default8;

   }
}


thats when i tried it in my state machine
Code:
#include <18F452.h>
#device *=16 ADC=10 ICD=true
#fuses HS,NOWDT,NOPROTECT, NOPUT, NOLVP, NOBROWNOUT

#include <hardware18F452.h>
#include <global.h>
#include <Timer.h>

#use delay(clock=OSC_FREQ,RESTART_WDT)
#use rs232(baud=BAUD_RATE,xmit=TX,rcv=RX,parity=N,bits=8,RESTART_WDT,STREAM=streamed)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)

//this gets the total size of the program memory from the compiler
#define PROGRAM_MEMORY_SIZE  getenv("PROGRAM_MEMORY")
//Define the total amount of memory I want
#define FLASH_DATA_SIZE      8192
//Define the end point of my block of flash (the very last byte of flash available)
#define FLASH_DATA_END      PROGRAM_MEMORY_SIZE-1
//Now define where the start of my data will be by subtracting the amount I want from the
//size of the memory
#define FLASH_DATA_START    (PROGRAM_MEMORY_SIZE - FLASH_DATA_SIZE)
//Reserve space
#org FLASH_DATA_START, FLASH_DATA_END {}

#include <Timer.c>
//#include <Flash Storing.c>
#include <Adc_Cal_Data.c>
#include <Cal_Constant.c>
#include <Adc_Get_Data.c>


#inline
void nop()
{
#asm
  NOP
#endasm
}

//Function declarations
void Adc_Get_Data();
void Cal_Constant();
void Adc_Cal_Data();

void init()
{
  // Start watchdog timer with default configuration
  restart_wdt();
   
  // Configure all Ports for ADC
  Setup_ADC_Ports(AN0_AN1_AN2_AN4_VSS_VREF);
   
  // Initialize the port output data latches
   PORT_A = 0;
   PORT_B = 0;
   PORT_C = 0;
   PORT_D = 0;
   PORT_E = 0;

   // Setup data direction registers (1 = input)
   Set_Tris_A(0b111111);
   Set_Tris_B(0b00000000);
   Set_Tris_C(0b10000000);
   Set_Tris_D(0b00000000);
   Set_Tris_E(0b111);
   
  port_b_pullups(true);
 
   // Initialize and restart watchdog timer
  SETUP_WDT(WDT_ON);   // Assign prescaler (DIV 2) to WDT
  restart_wdt();

  // Setup AD clock
  SETUP_ADC( ADC_CLOCK_DIV_8);

   //Usart Asysnchronous registers setup
   SPBRG = 25;
   TXSTA = 0b00100100;
   
   Timer0_init();

}

void main()
{
   init();
   
   enable_interrupts(GLOBAL);
     
  // Housekeeping
   restart_wdt();
   

//Setting Initial values   
   State0 = CAL;
   State1 = XPG;
   n = defaultn;
 
   while(true)
  {
     switch(State0)
     {
 
        case CAL:
        
            Adc_Get_Data();
           Cal_Constant();
           State0 = WAIT;
           
           break;

      case WAIT:
         
         if(Timer0Done)
            {
               State0 = SAMPLE1;
               Timer0Done = false;
            }
            
             break;
                
      case SAMPLE1:
         
             Adc_Cal_Data();
             State0 = FILLARRAYS;
         
             break;
     
      case FILLARRAYS:
         if(n < 32)
        {
          n++;
          //Add acceleromter values to the arrays
          XArray16[n]      = Xaxis;
          YArray16[n]      = Yaxis;
          ZArray16[n]      = Zaxis;

          //Point each pointer in the array at the Acceleromter values in the data arrays above
          PointerX16[n]    = &XArray16[n];
          PointerY16[n]    = &YArray16[n];
          PointerZ16[n]    = &ZArray16[n];
        }else if(n = 32)
        {
           State0 = PRINTARRAYS;
        }
          break;
     
      case PRINTARRAYS:
         printf("\n\rSInt16 Data Array:");
         
        for(n = 0; n < 32; n++)
           {
             printf("%ld ",XArray16[n]);
             printf("%ld ",YArray16[n]);
             printf("%ld ",ZArray16[n]);
           }
             break;              
     }
  }
}    
   
     


What i need to accomplish is after every 250us int from tmr0 i must jump from case WAIT into SAMPLE then after SAMPLE store the value in the array and increment n when n == 32 i must print the array. I am only using it like this for testing because in the end i must fill the arrays with floats but at this stage i dont know how to do it.

Kind regards
Jacques Kleynhans
_________________
"THE ONLY EASY DAY WAS YESTERDAY"
Ttelmah
Guest







PostPosted: Sun Aug 31, 2008 10:34 am     Reply with quote

Two major bugs have already been pointed out. Correct them.
You carefully omit important parts, that make answering difficult:
How are all the variables declared?.
_Simplify_, till you have code that just shows the problem. You will probably see the fault for yourself when you do so. If not, the code will be smaller, and contain just the part that matters.

Best Wishes
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sun Aug 31, 2008 10:43 am     Reply with quote

Quote:
What i need to accomplish is after every 250us int from tmr0 i must jump from case WAIT into SAMPLE then after SAMPLE store the value in the array and increment n when n == 32 i must print the array.


This might be being ambitious, since as well as getting the ADC reading you call:
Quote:
Code:
Adc_Cal_Data();

Do you *know* you have 250uS available?

Also dont see where you 'kick' the watchdog in your state nachine, unless you do it in the timer ISR (that you havent shown)
jacqueskleynhans



Joined: 10 Apr 2008
Posts: 109
Location: Cape Town, South Africa

View user's profile Send private message

**
PostPosted: Sun Aug 31, 2008 11:08 am     Reply with quote

Thx i value the comments i will try and fix my code and if i get stuck ill post a smaller section.

Kind Regards
_________________
"THE ONLY EASY DAY WAS YESTERDAY"
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group