11 種濾波算法程序大全(含源碼)

1、限幅濾波法(又稱程序判斷濾波法)

/*
A、名稱:限幅濾波法(又稱程序判斷濾波法)
B、方法:
    根據經驗判斷,確定兩次採樣允許的最大偏差值(設爲A),
    每次檢測到新值時判斷:
    如果本次值與上次值之差<=A,則本次值有效,
    如果本次值與上次值之差>A,則本次值無效,放棄本次值,用上次值代替本次值。
C、優點:
    能有效克服因偶然因素引起的脈衝干擾。
D、缺點:
    無法抑制那種週期性的干擾。
    平滑度差。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;
int Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
  Value = 300;
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Value = Filter_Value;          // 最近一次有效採樣的值,該變量爲全局變量
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 限幅濾波法(又稱程序判斷濾波法)
#define FILTER_A 1
int Filter() {
  int NewValue;
  NewValue = Get_AD();
  if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A))
    return Value;
  else
    return NewValue;
}

2、中位值濾波法

/*
A、名稱:中位值濾波法
B、方法:
    連續採樣N次(N取奇數),把N次採樣值按大小排列,
    取中間值爲本次有效值。
C、優點:
    能有效克服因偶然因素引起的波動干擾;
    對溫度、液位的變化緩慢的被測參數有良好的濾波效果。
D、缺點:
    對流量、速度等快速變化的參數不宜。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 中位值濾波法
#define FILTER_N 101
int Filter() {
  int filter_buf[FILTER_N];
  int i, j;
  int filter_temp;
  for(i = 0; i < FILTER_N; i++) {
    filter_buf[i] = Get_AD();
    delay(1);
  }
  // 採樣值從小到大排列(冒泡法)
  for(j = 0; j < FILTER_N - 1; j++) {
    for(i = 0; i < FILTER_N - 1 - j; i++) {
      if(filter_buf[i] > filter_buf[i + 1]) {
        filter_temp = filter_buf[i];
        filter_buf[i] = filter_buf[i + 1];
        filter_buf[i + 1] = filter_temp;
      }
    }
  }
  return filter_buf[(FILTER_N - 1) / 2];
}

3、算術平均濾波法

/*
A、名稱:算術平均濾波法
B、方法:
    連續取N個採樣值進行算術平均運算:
    N值較大時:信號平滑度較高,但靈敏度較低;
    N值較小時:信號平滑度較低,但靈敏度較高;
    N值的選取:一般流量,N=12;壓力:N=4。
C、優點:
    適用於對一般具有隨機干擾的信號進行濾波;
    這種信號的特點是有一個平均值,信號在某一數值範圍附近上下波動。
D、缺點:
    對於測量速度較慢或要求數據計算速度較快的實時控制不適用;
    比較浪費RAM。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 算術平均濾波法
#define FILTER_N 12
int Filter() {
  int i;
  int filter_sum = 0;
  for(i = 0; i < FILTER_N; i++) {
    filter_sum += Get_AD();
    delay(1);
  }
  return (int)(filter_sum / FILTER_N);
}

4、遞推平均濾波法(又稱滑動平均濾波法)

/*
A、名稱:遞推平均濾波法(又稱滑動平均濾波法)
B、方法:
    把連續取得的N個採樣值看成一個隊列,隊列的長度固定爲N,
    每次採樣到一個新數據放入隊尾,並扔掉原來隊首的一次數據(先進先出原則),
    把隊列中的N個數據進行算術平均運算,獲得新的濾波結果。
    N值的選取:流量,N=12;壓力,N=4;液麪,N=4-12;溫度,N=1-4。
C、優點:
    對週期性干擾有良好的抑制作用,平滑度高;
    適用於高頻振盪的系統。
D、缺點:
    靈敏度低,對偶然出現的脈衝性干擾的抑制作用較差;
    不易消除由於脈衝干擾所引起的採樣值偏差;
    不適用於脈衝干擾比較嚴重的場合;
    比較浪費RAM。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 遞推平均濾波法(又稱滑動平均濾波法)
#define FILTER_N 12
int filter_buf[FILTER_N + 1];
int Filter() {
  int i;
  int filter_sum = 0;
  filter_buf[FILTER_N] = Get_AD();
  for(i = 0; i < FILTER_N; i++) {
    filter_buf[i] = filter_buf[i + 1]; // 所有數據左移,低位仍掉
    filter_sum += filter_buf[i];
  }
  return (int)(filter_sum / FILTER_N);
}

5、中位值平均濾波法(又稱防脈衝干擾平均濾波法)

/*
A、名稱:中位值平均濾波法(又稱防脈衝干擾平均濾波法)
B、方法:
    採一組隊列去掉最大值和最小值後取平均值,
    相當於“中位值濾波法”+“算術平均濾波法”。
    連續採樣N個數據,去掉一個最大值和一個最小值,
    然後計算N-2個數據的算術平均值。
    N值的選取:3-14。
C、優點:
    融合了“中位值濾波法”+“算術平均濾波法”兩種濾波法的優點。
    對於偶然出現的脈衝性干擾,可消除由其所引起的採樣值偏差。
    對週期干擾有良好的抑制作用。
    平滑度高,適於高頻振盪的系統。
D、缺點:
    計算速度較慢,和算術平均濾波法一樣。
    比較浪費RAM。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 中位值平均濾波法(又稱防脈衝干擾平均濾波法)(算法1)
#define FILTER_N 100
int Filter() {
  int i, j;
  int filter_temp, filter_sum = 0;
  int filter_buf[FILTER_N];
  for(i = 0; i < FILTER_N; i++) {
    filter_buf[i] = Get_AD();
    delay(1);
  }
  // 採樣值從小到大排列(冒泡法)
  for(j = 0; j < FILTER_N - 1; j++) {
    for(i = 0; i < FILTER_N - 1 - j; i++) {
      if(filter_buf[i] > filter_buf[i + 1]) {
        filter_temp = filter_buf[i];
        filter_buf[i] = filter_buf[i + 1];
        filter_buf[i + 1] = filter_temp;
      }
    }
  }
  // 去除最大最小極值後求平均
  for(i = 1; i < FILTER_N - 1; i++) filter_sum += filter_buf[i];
  return filter_sum / (FILTER_N - 2);
}


//  中位值平均濾波法(又稱防脈衝干擾平均濾波法)(算法2)
/*
#define FILTER_N 100
int Filter() {
  int i;
  int filter_sum = 0;
  int filter_max, filter_min;
  int filter_buf[FILTER_N];
  for(i = 0; i < FILTER_N; i++) {
    filter_buf[i] = Get_AD();
    delay(1);
  }
  filter_max = filter_buf[0];
  filter_min = filter_buf[0];
  filter_sum = filter_buf[0];
  for(i = FILTER_N - 1; i > 0; i--) {
    if(filter_buf[i] > filter_max)
      filter_max=filter_buf[i];
    else if(filter_buf[i] < filter_min)
      filter_min=filter_buf[i];
    filter_sum = filter_sum + filter_buf[i];
    filter_buf[i] = filter_buf[i - 1];
  }
  i = FILTER_N - 2;
  filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是爲了四捨五入
  filter_sum = filter_sum / i;
  return filter_sum;
}*/

6、限幅平均濾波法

/*
A、名稱:限幅平均濾波法
B、方法:
    相當於“限幅濾波法”+“遞推平均濾波法”;
    每次採樣到的新數據先進行限幅處理,
    再送入隊列進行遞推平均濾波處理。
C、優點:
    融合了兩種濾波法的優點;
    對於偶然出現的脈衝性干擾,可消除由於脈衝干擾所引起的採樣值偏差。
D、缺點:
    比較浪費RAM。
E、整理:shenhaiyu 2013-11-01
*/

#define FILTER_N 12
int Filter_Value;
int filter_buf[FILTER_N];

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
  filter_buf[FILTER_N - 2] = 300;
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 限幅平均濾波法
#define FILTER_A 1
int Filter() {
  int i;
  int filter_sum = 0;
  filter_buf[FILTER_N - 1] = Get_AD();
  if(((filter_buf[FILTER_N - 1] - filter_buf[FILTER_N - 2]) > FILTER_A) || ((filter_buf[FILTER_N - 2] - filter_buf[FILTER_N - 1]) > FILTER_A))
    filter_buf[FILTER_N - 1] = filter_buf[FILTER_N - 2];
  for(i = 0; i < FILTER_N - 1; i++) {
    filter_buf[i] = filter_buf[i + 1];
    filter_sum += filter_buf[i];
  }
  return (int)filter_sum / (FILTER_N - 1);
}

7、一階滯後濾波法

/*
A、名稱:一階滯後濾波法
B、方法:
    取a=0-1,本次濾波結果=(1-a)*本次採樣值+a*上次濾波結果。
C、優點:
    對週期性干擾具有良好的抑制作用;
    適用於波動頻率較高的場合。
D、缺點:
    相位滯後,靈敏度低;
    滯後程度取決於a值大小;
    不能消除濾波頻率高於採樣頻率1/2的干擾信號。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;
int Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
  Value = 300;
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 一階滯後濾波法
#define FILTER_A 0.01
int Filter() {
  int NewValue;
  NewValue = Get_AD();
  Value = (int)((float)NewValue * FILTER_A + (1.0 - FILTER_A) * (float)Value);
  return Value;
}

8、加權遞推平均濾波法

/*
A、名稱:加權遞推平均濾波法
B、方法:
    是對遞推平均濾波法的改進,即不同時刻的數據加以不同的權;
    通常是,越接近現時刻的數據,權取得越大。
    給予新採樣值的權係數越大,則靈敏度越高,但信號平滑度越低。
C、優點:
    適用於有較大純滯後時間常數的對象,和採樣週期較短的系統。
D、缺點:
    對於純滯後時間常數較小、採樣週期較長、變化緩慢的信號;
    不能迅速反應系統當前所受干擾的嚴重程度,濾波效果差。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 加權遞推平均濾波法
#define FILTER_N 12
int coe[FILTER_N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};    // 加權係數表
int sum_coe = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12; // 加權係數和
int filter_buf[FILTER_N + 1];
int Filter() {
  int i;
  int filter_sum = 0;
  filter_buf[FILTER_N] = Get_AD();
  for(i = 0; i < FILTER_N; i++) {
    filter_buf[i] = filter_buf[i + 1]; // 所有數據左移,低位仍掉
    filter_sum += filter_buf[i] * coe[i];
  }
  filter_sum /= sum_coe;
  return filter_sum;
}

9、消抖濾波法

/*
A、名稱:消抖濾波法
B、方法:
    設置一個濾波計數器,將每次採樣值與當前有效值比較:
    如果採樣值=當前有效值,則計數器清零;
    如果採樣值<>當前有效值,則計數器+1,並判斷計數器是否>=上限N(溢出);
    如果計數器溢出,則將本次值替換當前有效值,並清計數器。
C、優點:
    對於變化緩慢的被測參數有較好的濾波效果;
    可避免在臨界值附近控制器的反覆開/關跳動或顯示器上數值抖動。
D、缺點:
    對於快速變化的參數不宜;
    如果在計數器溢出的那一次採樣到的值恰好是干擾值,則會將干擾值當作有效值導入系統。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;
int Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
  Value = 300;
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 消抖濾波法
#define FILTER_N 12
int i = 0;
int Filter() {
  int new_value;
  new_value = Get_AD();
  if(Value != new_value) {
    i++;
    if(i > FILTER_N) {
      i = 0;
      Value = new_value;
    }
  }
  else
    i = 0;
  return Value;
}

10、限幅消抖濾波法

/*
A、名稱:限幅消抖濾波法
B、方法:
    相當於“限幅濾波法”+“消抖濾波法”;
    先限幅,後消抖。
C、優點:
    繼承了“限幅”和“消抖”的優點;
    改進了“消抖濾波法”中的某些缺陷,避免將干擾值導入系統。
D、缺點:
    對於快速變化的參數不宜。
E、整理:shenhaiyu 2013-11-01
*/

int Filter_Value;
int Value;

void setup() {
  Serial.begin(9600);       // 初始化串口通信
  randomSeed(analogRead(0)); // 產生隨機種子
  Value = 300;
}

void loop() {
  Filter_Value = Filter();       // 獲得濾波器輸出值
  Serial.println(Filter_Value); // 串口輸出
  delay(50);
}

// 用於隨機產生一個300左右的當前值
int Get_AD() {
  return random(295, 305);
}

// 限幅消抖濾波法
#define FILTER_A 1
#define FILTER_N 5
int i = 0;
int Filter() {
  int NewValue;
  int new_value;
  NewValue = Get_AD();
  if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A))
    new_value = Value;
  else
    new_value = NewValue;
  if(Value != new_value) {
    i++;
    if(i > FILTER_N) {
      i = 0;
      Value = new_value;
    }
  }
  else
    i = 0;
  return Value;
}

11、卡爾曼濾波(非擴展卡爾曼)

#include <Wire.h> // I2C library, gyroscope

// Accelerometer ADXL345
#define ACC (0x53)    //ADXL345 ACC address
#define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)


// Gyroscope ITG3200 
#define GYRO 0x68 // gyro address, binary = 11101000 when AD0 is connected to Vcc (see schematics of your breakout board)
#define G_SMPLRT_DIV 0x15   
#define G_DLPF_FS 0x16   
#define G_INT_CFG 0x17
#define G_PWR_MGM 0x3E

#define G_TO_READ 8 // 2 bytes for each axis x, y, z


// offsets are chip specific. 
int a_offx = 0;
int a_offy = 0;
int a_offz = 0;

int g_offx = 0;
int g_offy = 0;
int g_offz = 0;
////////////////////////

////////////////////////
char str[512]; 

void initAcc() {
  //Turning on the ADXL345
  writeTo(ACC, 0x2D, 0);      
  writeTo(ACC, 0x2D, 16);
  writeTo(ACC, 0x2D, 8);
  //by default the device is in +-2g range reading
}

void getAccelerometerData(int* result) {
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  byte buff[A_TO_READ];
  
  readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
  
  //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  //thus we are converting both bytes in to one int
  result[0] = (((int)buff[1]) << 8) | buff[0] + a_offx;   
  result[1] = (((int)buff[3]) << 8) | buff[2] + a_offy;
  result[2] = (((int)buff[5]) << 8) | buff[4] + a_offz;
}

//initializes the gyroscope
void initGyro()
{
  /*****************************************
  * ITG 3200
  * power management set to:
  * clock select = internal oscillator
  *     no reset, no sleep mode
  *   no standby mode
  * sample rate to = 125Hz
  * parameter to +/- 2000 degrees/sec
  * low pass filter = 5Hz
  * no interrupt
  ******************************************/
  writeTo(GYRO, G_PWR_MGM, 0x00);
  writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF
  writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19
  writeTo(GYRO, G_INT_CFG, 0x00);
}


void getGyroscopeData(int * result)
{
  /**************************************
  Gyro ITG-3200 I2C
  registers:
  temp MSB = 1B, temp LSB = 1C
  x axis MSB = 1D, x axis LSB = 1E
  y axis MSB = 1F, y axis LSB = 20
  z axis MSB = 21, z axis LSB = 22
  *************************************/

  int regAddress = 0x1B;
  int temp, x, y, z;
  byte buff[G_TO_READ];
  
  readFrom(GYRO, regAddress, G_TO_READ, buff); //read the gyro data from the ITG3200
  
  result[0] = ((buff[2] << 8) | buff[3]) + g_offx;
  result[1] = ((buff[4] << 8) | buff[5]) + g_offy;
  result[2] = ((buff[6] << 8) | buff[7]) + g_offz;
  result[3] = (buff[0] << 8) | buff[1]; // temperature
  
}


float xz=0,yx=0,yz=0;
float p_xz=1,p_yx=1,p_yz=1;
float q_xz=0.0025,q_yx=0.0025,q_yz=0.0025;
float k_xz=0,k_yx=0,k_yz=0;
float r_xz=0.25,r_yx=0.25,r_yz=0.25;
  //int acc_temp[3];
  //float acc[3];
  int acc[3];
  int gyro[4];
  float Axz;
  float Ayx;
  float Ayz;
  float t=0.025;
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  initAcc();
  initGyro();
  
}

//unsigned long timer = 0;
//float o;
void loop()
{
  
  getAccelerometerData(acc);
  getGyroscopeData(gyro);
  //timer = millis();
  sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]);
  
  //acc[0]=acc[0];
  //acc[2]=acc[2];
  //acc[1]=acc[1];
  //r=sqrt(acc[0]*acc[0]+acc[1]*acc[1]+acc[2]*acc[2]);
  gyro[0]=gyro[0]/ 14.375;
  gyro[1]=gyro[1](-14.375);
  gyro[2]=gyro[2]/ 14.375;
  
   
  Axz=(atan2(acc[0],acc[2]))*180/PI;
  Ayx=(atan2(acc[0],acc[1]))*180/PI;
  /*if((acc[0]!=0)&&(acc[1]!=0))
    {
      Ayx=(atan2(acc[0],acc[1]))*180/PI;
    }
    else
    {
      Ayx=t*gyro[2];
    }*/
  Ayz=(atan2(acc[1],acc[2]))*180/PI;
  
  
 //kalman filter
  calculate_xz();
  calculate_yx();
  calculate_yz();
  
  //sprintf(str, "%d,%d,%d", xz_1, xy_1, x_1);
  //Serial.print(xz);Serial.print(",");
  //Serial.print(yx);Serial.print(",");
  //Serial.print(yz);Serial.print(",");
  //sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]);
  //sprintf(str, "%d,%d,%d",gyro[0],gyro[1],gyro[2]);
    Serial.print(Axz);Serial.print(",");
    //Serial.print(Ayx);Serial.print(",");
    //Serial.print(Ayz);Serial.print(",");
  //Serial.print(str);
  //o=gyro[2];//w=acc[2];
  //Serial.print(o);Serial.print(",");
  //Serial.print(w);Serial.print(",");
  Serial.print("\n");

  
  //delay(50);
}
void calculate_xz()
{

 xz=xz+t*gyro[1];
 p_xz=p_xz+q_xz;
 k_xz=p_xz/(p_xz+r_xz);
 xz=xz+k_xz*(Axz-xz);
 p_xz=(1-k_xz)*p_xz;
}
void calculate_yx()
{
  
  yx=yx+t*gyro[2];
  p_yx=p_yx+q_yx;
  k_yx=p_yx/(p_yx+r_yx);
  yx=yx+k_yx*(Ayx-yx);
  p_yx=(1-k_yx)*p_yx;

}
void calculate_yz()
{
  yz=yz+t*gyro[0];
  p_yz=p_yz+q_yz;
  k_yz=p_yz/(p_yz+r_yz);
  yz=yz+k_yz*(Ayz-yz);
  p_yz=(1-k_yz)*p_yz;
 
}


//---------------- Functions
//Writes val to address register on ACC
void writeTo(int DEVICE, byte address, byte val) {
   Wire.beginTransmission(DEVICE); //start transmission to ACC 
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}


//reads num bytes starting from address register on ACC in to buff array
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  Wire.beginTransmission(DEVICE); //start transmission to ACC 
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(DEVICE); //start transmission to ACC
  Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC
  
  int i = 0;
  while(Wire.available())    //ACC may send less than requested (abnormal)
  { 
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/YniPFWc3ecZSiuxO5J1YZQ