📄 s12_atd.c
字号:
//=============================================================================
// File: S12_ATD.C - V1.00
// Rem.: The ACPRD Project Page on the Web -> http://hc12web.de/acprd
//=============================================================================
//-- Includes -----------------------------------------------------------------
#include "datatypes.h"
#include "hcs12dp256.h"
#include "s12_atd.h"
//-- Static Data --------------------------------------------------------------
UINT16 atd_results[ATD_AVERAGE_COUNT][ATD_MAX_CHANNELS];
UINT16 atd_aidx;
//-- Code ---------------------------------------------------------------------
// Func: Initialize ATD module
// Args: -
// Retn: -
//
void initATD0(void) {
// enable ATD module
ATD0CTL2 = BM_ADPU;
// 10 bit resolution, clock divider=12 (allows ECLK=6..24MHz)
// 2nd sample time = 2 ATD clocks
ATD0CTL4 = BM_PRS2 | BM_PRS0;
}
//-----------------------------------------------------------------------------
// Func: Perform single channel ATD conversion
// Args: channel = 0..7
// Retn: unsigned, left justified 10 bit result
//
UINT16 getATD0(UINT8 channel) {
// select one conversion per sequence
ATD0CTL3 = BM_S1C;
// right justified unsigned data mode
// perform single sequence, one out of 8 channels
ATD0CTL5 = BM_DJM | (channel & 0x07);
// wait until Sequence Complete Flag set
// CAUTION: no loop time limit implemented!
while((ATD0STAT0 & BM_SCF) == 0) ;
// read result register
return ATD0DR0;
}
//-----------------------------------------------------------------------------
// start single ATD conversion with all 8 channels
// (conversion results will be collected later)
//
void startATD0(void) {
// select 8 conversions per sequence
ATD0CTL3 = BM_S8C;
// right justified unsigned data mode
// perform single sequence on multiple channels (starting with channel 0)
ATD0CTL5 = BM_DJM | BM_MULT;
}
//-----------------------------------------------------------------------------
// read out conversion results of all 8 channels
// (assuming conversion has been started before and completed successfully)
//
BOOL readATD0(UINT16 *results) {
UINT8 n;
UINT16 *patd;
if((ATD0STAT0 & BM_SCF) == 0) { // check sequence complete flag
return FALSE;
}
patd = (UINT16 *)(&ATD0DR0);
n = ATD_MAX_CHANNELS;
while(n--) { // copy ATD result registers
*results = *patd;
results++;
patd++;
};
return TRUE;
}
//-----------------------------------------------------------------------------
// read out conversion results, start new conversion sequence
// and calculate floating average for all 8 channels
//
void handleATD0(UINT16 *buf) {
UINT16 *src, *dest;
UINT8 n, k;
dest = &atd_results[atd_aidx][0];
readATD0(dest); // read out conversion results
startATD0(); // start new conversion sequence
atd_aidx++; // increment index
if(atd_aidx == ATD_AVERAGE_COUNT) atd_aidx = 0;
k = 0; // calculate sum of measures
src = &atd_results[0][0]; // for each channel
do {
n = 0;
dest = buf;
do {
if(k == 0) *dest = *src;
else *dest += *src;
// add the following line to normalize results
// if(k == (ATD_AVERAGE_COUNT-1)) *dest /= ATD_AVERAGE_COUNT;
dest++;
src++;
n++;
} while(n < ATD_MAX_CHANNELS);
k++;
} while(k < ATD_AVERAGE_COUNT);
}
//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -