Arduino UNO R3 使用自建C函式庫[How to call C functions from Arduino sketch?]
Arduino UNO R3 使用自建C函式庫[How to call C functions from Arduino sketch?]
資料來源: https://tomyam-yang.blogspot.com/2018/07/arduino-uno-r3-c.html
https://arduino.stackexchange.com/questions/946/how-to-call-c-functions-from-arduino-sketch
GITHUB: https://github.com/jash-git/Jash-good-idea-20220101-001/tree/main/Arduino%20UNO%20R3%20%E4%BD%BF%E7%94%A8%E8%87%AA%E5%BB%BAC%E5%87%BD%E5%BC%8F%E5%BA%AB
實際測試紀錄:
main.ino
//---
//引用純C的方法 ~ C:\Users\student(windows使用者)\Documents\Arduino\libraries\crc16
extern "C"{
#include "crc16.h"
};
//---引用純C的方法
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
uint16_t data = CalculateCRC16("<09M", 4);
String StrData="CalculateCRC16(\"<09M\", 4) = ";
Serial.print(StrData);
Serial.println(data);
delay(5000);
}
crc16.h
#pragma once//重點:一定要加入
#include <stdio.h>
#include <stdint.h>
uint16_t crctable[2] ={ 0x0000, 0x1189};
uint16_t CalculateCRC16( // Call example CalculateCRC16("<09M", 4);
const void *c_ptr, // Pointer to byte array to perform CRC on
size_t len) // Number of bytes to CRC
{
uint16_t crc = 0xFFFF; // Seed for CRC calculation
const uint8_t *c = (const uint8_t *)c_ptr;
while (len--)
crc = (crc << 8) ^ crctable[((crc >> 8) ^ *c++)];
return crc;
}