📄 u08adc.c
字号:
//============================================================================
// File: U08ADC.C
// Func: Software ADC for USB08
// Ver.: 1.01
// Auth: (C)2000,2001 by Oliver Thamm, MCT Elektronikladen GbR
// http://hc08web.de/usb08
// Rem.: View/Edit this File with TAB-Size=4
// Jan 02 : Adapted for CodeWarrior by Isabelle GIRAUD, Metrowerks Europe
//============================================================================
#include "hc08jb8.h"
#include "u08adc.h"
#define _CHAR_ADR(x) ((volatile unsigned char*)&(x))
#define _COPY_BYTE(from, to, offset) (*(_CHAR_ADR(to)+(offset))=*(_CHAR_ADR(from)+(offset)))
#define _COPY_16_BITS_HIGH_FIRST(from, to) (_COPY_BYTE(from, to,0),_COPY_BYTE(from, to,1))
//----------------------------------------------------------------------------
void initSADC() {
// disable internal Pull-Ups on PTE
POCR &= ~0x80; // disable PTE20P
}
//----------------------------------------------------------------------------
unsigned scaleSADC(unsigned t1, unsigned t2) {
unsigned retval;
unsigned char n;
//-----------------------------------------------------------
// retval = (t2/t1) * 256 (0 <= retval <= 0xff)
// We are taking care of overflow/accuracy by actually doing:
// retval = (t2 * 2^j) / (t1 / 2^k)
// with j+k = 8 (because 2^8=256)
//-----------------------------------------------------------
for(n=0; n<8; n++) {
if((t2 & 0x8000) == 0) t2 <<= 1;
else t1 >>= 1;
}
retval = t2/t1;
retval *= 135; // *135/128 = *1.055
retval >>= 7;
if(retval > 0x00ff) retval = 0x00ff;
return(retval);
}
//----------------------------------------------------------------------------
void delay025ms(void) {
asm {
LDA #240
__l_01: DBNZA __l_01
}// 3cyc x 333ns x 240+9 = 0,249ms
}
//----------------------------------------------------------------------------
unsigned char getSADC(char channel) {
unsigned int tcal, tcal0, tcal1;
unsigned int tacq, tacq0, tacq1;
unsigned int t;
// convert channel # 1/2/3 to 0x01/0x02/0x04
if(channel == 3) channel++;
PTD &= ~0x78; // all PTD[3..6] = L
PTE |= 0x07; // all PTE[0..2] = H;
// *** calibration cycle ***
DDRD |= 0x78; // Output L on PTD[3..6]
DDRE |= 0x07; // Output H on PTE[0..2]
delay025ms(); // discharge C13..C15
DDRE &= ~0x07; // PTE[0..2] HiZ (PTE[0..2] configured as Input)
TSC |= 0x10 ; // reset Tim counter
_COPY_16_BITS_HIGH_FIRST(TCNTH,tcal0);
while((PTE & channel) != 0) ; // while the threshold level is not reached ....
_COPY_16_BITS_HIGH_FIRST(TCNTH,tcal1);// cal end time
tcal = tcal1 - tcal0;
// *** acquisition cycle ***
DDRD &= ~0x38; // PTD[3..5] = HiZ, PTD[6] = L
DDRE |= 0x07; // Output H on PTE[0..2]
delay025ms(); // discharge C13..C15
DDRE &= ~0x07; // PTE HiZ (Input)
TSC |= 0x10 ; // reset Tim counter
_COPY_16_BITS_HIGH_FIRST(TCNTH,tacq0);// acq start time
while((PTE & channel) != 0) ; // while the threshold level is not reached ....
_COPY_16_BITS_HIGH_FIRST(TCNTH,tacq1);// acq end time
tacq = tacq1 - tacq0;
if(tacq <= tcal) return(0); // underflow
t = tacq - tcal;
if(t >= tcal) return(0xff); // overflow
// *** calculate scaled result ***
t = scaleSADC(tcal,t);
return (unsigned char)t;
}
//============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -