Thursday, December 17, 2009

ADCTASK.C

/******************************************************************************
�����������������������������������������������������������������������������͸
�                                                                             �
�                    ANALOG-TO-DIGITAL CONVERTING TASK                        �
�                                                                             �
�                        Copyright (c) June, 1995                             �
�                           by Lutfi Shahab                                   �
�               Instrumentation and Controls Laboratory                       �
�                     Dept. of Engineering Physics                            �
�                   Faculty of Industrial Technology                          �
�                      Institut Teknologi Bandung                             �
�                                                                             �
�  Date:      20/11/94                                                        �
�                                                                             �
�  Platform : Motorola 68HC16 Microcontroller                                 �
�  Compiler : Whitesmith HC16 Compiler                                        �
�  Target   : EPROM-targeted binary code                                      �
�                                                                             �
�  Description: -Task and routines to access ADC-module of 68HC16             �
�                                                                             �
�  Functions:                                                                 �
�                                                                             �
�   Summary:                                                                  �
�   The ADC module is mapped into 32 words of address space. Five words are   �
�   control/status registers, one word is digital port data, and 24 words     �
�   provide access to the results of ADC conversion (eight addresses for each �
�   type of converted data). Two words are reserved for expansion.            �
�   The ADC module base address is determined by the value of the MODMAP bit  �
�   int the system integration module configuration register (SIMMCR).        �
�   The base address is normaly $FFF700 in the MC68HC16Z1.                    �
�       Internally, the ADC has both a differential data bus and a buffered   �
�   IMB data bus. Registers not directly associated with AD conversion        �
�   functions, such as the MCR, the MTR, and the PDR, reside on the bufferd   �
�   bus, while conversion registers and result registers reside on the        �
�   differential bus.                                                         �
�                                                                             �
�   Registers that must be set prior operation:                               �
�     ADMCR:                                                                  �
�       STOP = 0 (normal operation)                                           �
�       FRZ  = 0                                                              �
�       SUPV = 1 (supervisory mode)                                           �
�     ADCTL0:                                                                 �
�       PRS   = 1 (ADC clock = system clock/4 = 16.667 MHz/4)                 �
�       STS   = 0 (4 A/D clock periods in the sample time)                    �
�       RES10 = 1 (10-bit conversion)                                         �
�     ADCTL1:                                                                 �
�       SCAN  = 1 (continuous conversion)                                     �
�       MULT  = 1 (sequential conversion of four or eight channels            �
�                  selected by [CD:CA])                                       �
�       S8CM  = 1 (eight-conversion sequence)                                 �
�       CD    = 0 (measured data at each channel stored into his register)    �
�                                                                             �
�����������������������������������������������������������������������������;
******************************************************************************/
#define _FAR_

#include <wslxa.h>
#include "_adc.h"
#include "mcx16.h"
#include "sysdef.h"

#define CLICK_T     (16.256E-3) /* 1 clock tick = 16.256 mS */
#define TICK1       (1000/16) /* 1 second */
#define TICK2       (1000/16)

/*
    It's difficult to find out how time constant of thermocouple is,
    but for bare thermocouple, however, extensive reseach and testing
    have provided semi-empirical formulas that allow calculation of the time
    constant with fair accuracy. One such relation useful for temperatures
    from 160 - 1600 F, wire diameter 0.016 - 0.051 in, mass velocity 3 - 50
    lbm/(ft3.s), and static pressure of 1 atm is:

        t = (3500*rho*c*d^1.25 * pow(G, -15.8* sqrt(T)))/T, where

    rho = mass density of two thermocouple materials, lbm/ft3
    c   = average specific heat of two thermocouple materials, Btu/(lbm.F)
    d   = wire diameter (in)
    G   = flow mass velocity, lbm/(ft2.s)
    T   = stagnation temperature, R

    (adapted from Doebelin, MEASUREMENT SYSTEMS,...)
*/

/*********
    THIS JUST FOR CLARITY !
    ADC has been initialized by STARTUP.S
*******/
/*****
void ADC_init(void)
{
    admcr.reg = ADMCR_SUPV;
    admcr.reg = 0x0010;
    adctl0.reg = 0x0083;
    adctl1.reg = 0x0077;
}
******/


WORD adcdata[N_ADC_CHANNELS];

void ADC_Task(void)
{
   int i;

   _mcx_timer(TICK1, TICK2, SAMP_SEMA); /* install timer */
   FOREVER
   {
      /* wait until sample period begin */
      _mcx_wait(SAMP_SEMA);

      /* SCF is cleared when ADCTL1 is written and a conversion sequence begin */
       adctl1.reg = 0x0077;
      do {
      } while (adstat.w.ccf==0); /* all channels completely converted ? */

  /*******
    if last channel scanned
        send data to mailbox if polling has reached last channel.
        signalling other task with ADC_SEMA
    rescan ADC channels, starting from first channel
  *******/

      for(i=ADC_1; i<=N_ADC_CHANNELS; i++)
            adcdata[i] = ur_adc_rslt[i];
      _mcx_send_mbx(ADC_MBX, &adcdata, ADC_SEMA);
   }
}

No comments:

Post a Comment