Promgram time out on Rabbit 4000

Hello,

I am running a program that records values from one sensor onto memory. It consists of two functions and main. The first function converts the analog value from the sensor to a digital voltage (mV). The second function creates two buffers and dumps them to memory. The program runs (with some data loss) when set to record from two sensors. However, when recording from only one lead the program times out after about 20 seconds.

I have attached a relatively short version of the program below. I would appreciate any insight.

Thank you.

#class auto

#define DO_FAT // Define if use FAT filesystem
#memmap xmem

#define INPUT_COMPRESSION_BUFFERS 4
#define FAT_USE_FORWARDSLASH
#define FAT_BLOCK
#use “fat.lib”

#use RCMM40xx.LIB // Pin configuration is defined here
// RCMM40xx.lib is the Lib file to be used when using the PCB
#define ADC_SCLKBAUD 115200ul
#define NUMSAMPLES 10 //Ten samples per sec (10Hz).
#define STARTCHAN 0
#define ENDCHAN 1 // End channel 1 is the maximum
#define GAINSET GAIN_1 // Input = output
#define PERIOD 100 // Define the Period here in mSec w/100 10 /sec 600/min)
#define BUFFER_COUNT 2 // create buffers
#define BUFFER_SIZE 128 // Buffer size.

/* *********************************************
Begin ADC conversion and sampling here
***********************************************/
nodebug
unsigned int sample_ad(int channel, int num_samples)
{
auto unsigned long rawdata;
auto unsigned int sample;
auto unsigned int cmd;

//convert channel and gain to ADS7870 format
// in a direct mode
cmd = 0x80|(GAINSET*16+(channel|0x08));

rawdata = (long) anaInDriver(cmd);
return ((unsigned int) rawdata);

}
/*************************************************
END ADC SAMPLING HERE
*************************************************/

// 1st mode is FTP tick, FTP is always running, mode idel is not necesssary
// Second mode is logging
enum E_MODE
{
MODE_IDLE = 0,
MODE_LOGGER = 1,
MODE_ERROR = -1
} mode;

unsigned int buf[BUFFER_COUNT][BUFFER_SIZE];
unsigned int buf_num; // buffer currently being filled
unsigned int buf_len; // number of elements in the current buffer
int buf_full; // index of the full buffer, -1 if none are full
unsigned int sample_time; // time of last conversion

void init_logger(void)
{
buf_num = 0;
buf_len = 0;
buf_full = -1;

anaIn(0, SINGLE, GAINSET); // do first conversion… may take a while

sample_time = (unsigned int)MS_TIMER; // initial conversion time
}
/***********************************************
Begin logging routine
***********************************************/
void logger()
{
unsigned int the_time;
unsigned int chan;
unsigned int flag;

the_time = (unsigned int)MS_TIMER; //Stores the actual time to “the_time”.

//last conversion was over 100 ms ago. Earlier sampling frequency was set to 10Hz.
if(the_time - sample_time >= PERIOD)
{
// this is a marker which will be inserted to recognize each new line.
flag = 2000;

  sample_time += PERIOD; // Time stamp.

  //When channel number is less than 1, it moves to next channel.
  for(chan = STARTCHAN; chan <= ENDCHAN; chan++)
  {

  //Every time it encounters the first channel, it inserts flag before it.
  	if ( chan == STARTCHAN )
     {
        //Whenever sampling from first lead, flag and time are inserted.
        buf[buf_num][buf_len++] = flag;
        buf[buf_num][buf_len++] = sample_time;
     }

     // Other wise, it records the value of the channel
     else
     {
    		 buf[buf_num][buf_len++] = sample_ad(chan-1, NUMSAMPLES);
     }

     if(buf_len >= BUFFER_SIZE) //  When buffer is full
     {
        if(buf_full >= 0)
        {
        	mode = MODE_ERROR; // both buffers full!
        }

        buf_full = buf_num;
        buf_len = 0;
        buf_num++;
        if(buf_num >= BUFFER_COUNT)
           buf_num = 0;
     }
  }

}
}

/* ***********************************
End logging routine
*************************************/

/* ***********************************
Begin Main Program and call routines in the main program
***********************************/
void main()
{
auto int rc, i, uid, handle, sw;
unsigned int the_time;
char fname[12];
long prealloc;
FATfile file, countfile;
fat_part *part;

brdInit();

mode = MODE_IDLE;

// Note: sspec_automount automatically initializes all known filesystems.

rc = sspec_automount(SSPEC_MOUNT_ANY, NULL, NULL, NULL);
if (rc)
// If failed
part = NULL; // find first mounted partition
for ( i = 0; i < num_fat_devices * FAT_MAX_PARTITIONS; ++i )
{
if ( ( part = fat_part_mounted[ i ] ) != NULL )
{
break; // found a mounted partition, so use it
}
}

for( ; ; )
{
switch(mode)
{
sw = BitRdPortI(PBDR,4); // Check for switch 4 and store the value in SW
while(sw==1){

	   mode = MODE_LOGGER;
      break;
      }
     break;

   case MODE_LOGGER:
       init_logger();

			prealloc = 0;
			// Open the count  file, if there is no count file create a file called count and store
			// the information of a number to begin with, If the file already exists
			// read the file and increase the count by 1
			fat_Open(part, "COUNT.TXT", FAT_FILE, FAT_CREATE, &amp;countfile, &amp;prealloc);
			fat_Read(&amp;countfile, (char*)&amp;count, 2);
			count++;
			utoa(count, fname);
		   strcat(fname, ".TXT");
			fat_Seek(&amp;countfile, 0, SEEK_SET);
		   fat_Write(&amp;countfile, (char*)&amp;count, 1);
		   fat_Close(&amp;countfile);
		prealloc = 0;
     rc = fat_Open(part, fname, FAT_FILE, FAT_CREATE,
                   &amp;file, &amp;prealloc);

      while(mode == MODE_LOGGER)
     {

         logger();
        if(buf_full != -1)
        {

				the_time = (unsigned int)MS_TIMER;
           rc = fat_Write(&amp;file, (char*)buf[buf_full], BUFFER_SIZE * 2);
           buf_full = -1;
        }
        the_time = (unsigned int)MS_TIMER;
        ftp_tick();

       	sw = BitRdPortI(PBDR,4); // Check for switch 4 and store the value in SW
        if(sw==0)
        {
          fat_Close(&amp;file);
          mode = MODE_IDLE;
      	}
    }


      break;

   default:
     exit(1);
      break;
   }

}

}
}

//END MAIN.

The time out issue was resolved by playing around with the code. It may have just been un-matched braces.