
/************************************************************************

       BDTEMP.C        
       
       BL11xx/Little Giant

       Functions for testing type of A/D converter, setting
       temperature coefficient, and reading temperature from on-board
       temperature reference.

*************************************************************************/

int setad ();
float bdtemp_init ();
int bdtemp ();
float settemp ();

main ()
{
   int k;

   settemp ();                                /* calculate and store tcoef */
   bdtemp_init ();                           /* initialize bdtemp variables */

   while (1)
   {
      k = bdtemp ();                         /* get temp */
      printf ("%5.1f\n", (float) k / 10.0);  /* make into floating point */
      for (k = 0; k < 10000; ++k) hitwd();   /* pause */
   }
}


/***** setad function *******************/

/*** Beginheader setad, bdtemp_init, bdtemp, settemp  ***/

int setad ();
float bdtemp_init ();
int bdtemp ();
float settemp ();

/*** Endheader */

/*
;
;  SETAD
;
; Trys to determine the type of A/D converter on board based on
; characteristic return values from reading the temperature sensor.
;
; Any time the A/D converter is changed the A/D system must be recalibrated
; and a correction value stored into the EEPROM or used as a constant
; in the user program.
;
; Sets a function pointer (adfun) to the correct A/D read function.
;
;     RETURNS: 0 if was able to determine type of A/D
;             -1 if could not (sets adfunc to ad_rd ())
;
*/

int (*adfunc) ();

int setad ()
{
   int rd10, rd12;

   /* determine type of A/D chip installed */
   if (ad_rd12a (15) > 4095)                      /* is it a 1290? */
   {
      /* NO! it must be a 1094 or a 1294 */
      rd10 = ad_rd (15);
      rd12 = ad_rd12 (15);

      if ((rd10 >> 2) > rd12)   /* is it a 1094? */
        adfunc =  ad_rd;
      else if (rd10 == 2044)
        adfunc = ad_rd12;
      else                      /* don't know what it is, try 1094 */
      {
        adfunc = ad_rd;
        return -1;             /* and return an error */
      }
   }
   else
      adfunc = ad_rd12a;        /* 1290 pretending to be 1294 */

   return 0;
}

/***** bdtemp_init function *******************/

/*
;
;  BDTEMP_INIT
;
; For reading on board temperature and displaying it on PC
; Determines type of A/D converter on board BL11xx/Little Giant
; Gets value coefficient value from eeprom
;
;     RETURNS: Temperature coefficient or
;              -1.0 if eeprom value not correct
;
*/

#define TCOEF     0.001993             /* value good for Version E, 1094 */
shared float tcoef;                    /* TCOEF ~1 degree C for other A/D */

float bdtemp_init ()
{
   char *cp;

   setad ();                        /* figure out type A/D converter */
   cp = (char *) &tcoef;            /* point to temp coefficient */

   *cp++ = ee_rd (0x110);             /* and read it back from eeprom */
   *cp++ = ee_rd (0x111);
   *cp++ = ee_rd (0x112);
   *cp = ee_rd (0x113);

   if (tcoef <= 0.003 && tcoef > 0.0) /* test for valid coefficient */
     return (tcoef);
   else
   {
     tcoef = TCOEF;                   /* give it a reasonable value */
     return (-1.0);                   /* and return an error */
   }
}

/***** bdtemp function *******************/

/*
;
;  BDTEMP
;
; For reading on board temperature and returning as an integer value
; Gets value coefficient value from eeprom
;
;     RETURNS: integer value of centigrade temperature to 0.1 degree
;
*/

int bdtemp ()
{
   float temp;
   int ff;

   if (tcoef > 0.03 || tcoef <= 0)
     tcoef = TCOEF;                   /* put in a reasonable value */

   ff = (*adfunc) (15);               /* get the value */
   temp = (ff * 0.105e-3 + 0.366) / tcoef - 277.0;
   return ((int) (temp * 10.0));      /* round and return */
}

/***** settemp function *******************/

/*
;
;  SETTEMP
;
; Sets the temperature coeficient in eeprom.
;
; Board should be powered-up for at least 30 minutes within
; the enclosure in which it will be installed.  This allows the
; board to reach a stable temperature.  DO NOT USE THIS ROUTINE FROM
; EPROM without modifying the I/O routines!
;
;     RETURNS: temperature coefficient
;              -1.0 if try to write to protected EEPROM
*/

float settemp ()
{
    unsigned int c1, c2;
    float tcoef, troom, advalue;
    char *cp;
    char buf [11];
    int pflag;

    while (kbhit ()) {
    	getchar ();                   /* flush characters in */
    	hitwd();
    }

    puts ("Press F4 (DOS only) then enter room temperature in centigrade: ");

    *buf = 0;
    pflag = 0;
    for (c2 = c1 = 0; c1 < 9 && c2 != '\r';)
    {
    	hitwd();
       while (!kbhit ()) hitwd();
       c2 = getchar ();
       if (c2 >= '0' && c2 <= '9')
       {
         buf [c1] = c2;                /* its a number */
         putchar (c2);
         ++c1;
       }
       else
       {
         switch (c2)
         {
            case '-':
            case '+':
               if (c1 == 0)
               {
                  buf [c1] = c2;       /* only as first character */
                  putchar (c2);
                  ++c1;
               }
               break;

            case '\b':
               if (c1)                 /* erase a character if there is one */ 
               {
                  --c1;
                  if (buf [c1] = '.') pflag = 0;
                  putchar (c2);
                  putchar (' ');
                  putchar ('\b');
               }
               break;

            case '.':
               if (!pflag)             /* only one point */
               {
                  pflag++;
                  buf [c1] = c2;
                  putchar (c2);
                  ++c1;
               }
            case '\r':                 /* let's be economical */
               break;

            default:
               putchar ('\a');         /* beep beep */
         }
       }
    }
    buf [c1] = 0;

    troom = atof (buf);
    printf ("\nRoom temperature = %f\n", troom);

    setad ();
    advalue = 0.0;
    for (c1 = 0; c1 < 512; c1++)      /* sample 512 times */
         advalue += (*adfunc) (15);
    advalue /= 512.0;        /* calc average value */

    tcoef = (0.105e-3 * advalue + 0.366) / (troom + 277.0);
    printf ("tcoef = %f\n", tcoef);
    puts ("Save tcoef in eeprom? [Y]/N ");
    c1 = 0;
    
    while (!c1)
    {
    	hitwd();
      while (!kbhit ()) hitwd();
      c1 = getchar ();

      if (c1 == 'Y' || c1 == 'y')
      {
         cp = (char *) &tcoef;            /* use a char pointer to save */

         if (ee_wr (0x110, *cp++) == -2)
         {
            puts ("\nEEPROM protection is enabled, move J21 to position 2-3!\n");
            return -1.0;
         }
         ee_wr (0x111, *cp++);
         ee_wr (0x112, *cp++);
         ee_wr (0x113, *cp);
      }
      else if (c1 == 'N' || c1 == 'n')
         c1 = 'N';
      else
         c1 = 0;
   }
   puts ("\n");

   bdtemp_init ();
   c1 = bdtemp ();
   printf ("%4.1f\n", c1 / 10.0);
   return tcoef;
}

/**********************************************End of BDTEMP.C *****/
