# Promgram time out on Rabbit 4000

**URL:** <https://forums.digi.com/t/promgram-time-out-on-rabbit-4000/9303>\
**Category:** Rabbit\
**Created:** [September 8, 2008, 1:02am UTC](https://forums.digi.com/t/promgram-time-out-on-rabbit-4000/9303 "2008-09-08T01:02:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ershadf](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@Ershadf](https://forums.digi.com/u/Ershadf)\
**Post date:** [September 8, 2008, 1:02am UTC](https://forums.digi.com/t/promgram-time-out-on-rabbit-4000/9303/1 "2008-09-08T01:02:57Z")

</div>

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 &lt;= 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 &gt;= BUFFER_SIZE) // When buffer is full
     {
        if(buf_full &gt;= 0)
        {
        	mode = MODE_ERROR; // both buffers full!
        }

        buf_full = buf_num;
        buf_len = 0;
        buf_num++;
        if(buf_num &gt;= 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.

---

<div class="post-metadata">

**Author:** ![Ershadf](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@Ershadf](https://forums.digi.com/u/Ershadf)\
**Post date:** [September 24, 2008, 2:55am UTC](https://forums.digi.com/t/promgram-time-out-on-rabbit-4000/9303/2 "2008-09-24T02:55:30Z")

</div>

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