使用整數計算方式,順便記下一階濾波器程式
#define K 0.01
#define Q_K 16
#define Const_Q_K (int)(K*(1<<Q_K))
#define K_MUL(x) (((x)*Const_Q_K)>>Q_K)
#define Q_ADC 8
#define INT_Q(x,a) ((x)<<( a))
#define Q_INT(x,a) ((x)>>( a))
_IO ADC = 0x40001000;
extern int Read_IO(unsigned int address);
int ADC_IN()
{
static int Output; // Q_ADC variable
int Last_Output; // Q_ADC variable
Last_Output = Output;
Output += K_MUL(INT_Q(Read_IO(ADC),Q_ADC) - Output );
if( Q_INT(Last_Output,Q_ADC) != Q_INT(Output,Q_ADC) ) TFT_string( Q_INT(Output,Q_ADC));
return Q_INT(Output,Q_ADC);
}
沒有留言:
張貼留言