忘记放配置文件了 配置文件放在TF卡里面 通过修改XXXXXXXXXi文件来设置
校准长按板载按键(b站有校准使用教程 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
先放图
adc使用HX711同公司(海芯科技)生产的hx717芯片
因为同公司 所以代码甚至可以直接用hx711的代码 芯片对比
hx717
hx711
hx717使用2个引脚来修改速率
s1 和 s0
1 高点平
0 低电平
PCB制版文件(Gerber)
原理图(很早之前画的 有点丑 )
主控328p qfn28
代码仓库
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
代码 挺烂的 (
#include <Arduino.h>
#include <SPI.h>
#include <SdFat.h>
#include <stdlib.h>
#include "HX711.h"
#define BUZZER_PIN 3
#define LOADCELL_DOUT_PIN 5
#define LOADCELL_SCK_PIN 6
#define LED_PIN 15
uint32_t before;
float calibrate = -23.8846;
double time_now;
uint32_t time_start = 0;
uint16_t start_weight = 10;
int16_t stop_weight = 0;
int Now_data;
uint8_t open;
uint32_t data;
uint8_t cache[160] = {};
uint16_t left,right=0;//left开始标记 right缓存下标
uint16_t temp = 0;
uint8_t stop;
uint8_t Output_mode = 1;
String Name = "bmd";
bool led;
int Long_press = 500;
long long button_start_time = 0;
bool button_last = false;
bool button_pressed = false;
bool button_now = false;
const char bmd[] PROGMEM = "\n /$$ /$$"\
"\n| $$ | $$"\
"\n| $$$$$$$ /$$$$$$/$$$$ /$$$$$$$"\
"\n| $$__ $| $$_ $$_ $$/$$__ $$"\
"\n| $$ \\ $| $$ \\ $$ \\ $| $$ | $$"\
"\n| $$ | $| $$ | $$ | $| $$ | $$"\
"\n| $$$$$$$| $$ | $$ | $| $$$$$$$"\
"\n|_______/|__/ |__/ |__/\_______/";
const char error[] PROGMEM = "$$$$$$$$\\ "\
"\n$$ _____| "\
"\n$$ | $$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ "\
"\n$$$$$\\ $$ __$$\\$$ __$$\\$$ __$$\\$$ __$$\\ "\
"\n$$ __| $$ | \\__$$ | \\__$$ / $$ $$ | \\__|"\
"\n$$ | $$ | $$ | $$ | $$ $$ | "\
"\n$$$$$$$\\$$ | $$ | \\$$$$$$ $$ | "\
"\n\\________\\__| \\__| \\______/\\__| ";
HX711 scale;
SdFat sd;
SdFile config;
const uint8_t SD_CS_PIN = 10;
void ERROR(int);
void(* reset) (void) = 0;
long AD_Res_Last;
#define Lv_Bo 0.254
long get_weight(){
float out,c;
out = AD_Res_Last;
c = scale.get_units();
out = out*Lv_Bo + c*(1-Lv_Bo);
AD_Res_Last = out;
return AD_Res_Last;
}
// long AD_Res_Last1 = 0;
// long AD_Res_Last2 = 0;
// #define Lv_Bo 0.254
// long get_weight(){
// float out1, out2, c;
// c = scale.get_units();
// out1 = AD_Res_Last1;
// out1 = out1 * Lv_Bo + c * (1 - Lv_Bo);
// AD_Res_Last1 = out1;
// out2 = AD_Res_Last2;
// out2 = out2 * Lv_Bo + out1 * (1 - Lv_Bo);
// AD_Res_Last2 = out2;
// return AD_Res_Last2;
// }
void Calibration(){
tone(BUZZER_PIN,880);
delay(250);
noTone(BUZZER_PIN);
delay(200);
tone(BUZZER_PIN,659);
delay(250);
noTone(BUZZER_PIN);
delay(200);
tone(BUZZER_PIN,880);
delay(250);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN,LOW);
delay(500);
while(1){
if(!config.open("calibrate.csv",O_WRITE | O_CREAT)){
ERROR(2);
}
config.println(F("calibrate = Known weight / (weight_old - zero)"));
config.println(F("zero,weight_old"));
Serial.println(F("calibrate = Known weight / (weight_old - zero)"));
Serial.println(F("zero weight_old"));
tone(BUZZER_PIN,622);
delay(250);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN,LOW);
delay(5000);
scale.set_scale();
scale.tare();
unsigned long zero = scale.read_average(1000);
config.print(zero);
Serial.print(zero);
tone(BUZZER_PIN,1480);
delay(250);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN,LOW);
delay(5000);
config.print(",");
Serial.print(" ");
unsigned long weight_old = scale.read_average(1000);
config.println(weight_old);
Serial.println(weight_old);
config.close();
reset();
}
}
bool button(int button_pin){
while(1){
delay(50);
button_now = digitalRead(button_pin);
if (button_now == 1 && button_last == 0){
button_start_time = millis();
button_pressed = 1;
}
if (button_now == 0 && button_last == 1 && button_pressed == 1){
unsigned long pressDuration = millis() - button_start_time;
if (pressDuration < Long_press){
return 0;
}
else{
return 1;
}
button_pressed = 0;
}
button_last = button_now;
}
}
void Read_config(){
read :
if(sd.exists("config.ini")){
Serial.println(F("Check config file: OK"));
}
else{
Serial.println(F("Check config file: NO"));
if(config.open("config.ini",O_WRITE | O_CREAT)){
Serial.println(F("Create config file: OK"));
config.close();
goto read;
}
else{
ERROR(1);
}
}
config.open("config.ini",O_READ);
char buffer[128];
while (config.fgets(buffer, sizeof(buffer))){
String line = buffer;
line.trim();
if (line.startsWith(";") || line == "") continue;
int equalPos = line.indexOf('=');
if (equalPos > 0){
String key = line.substring(0, equalPos);
String value = line.substring(equalPos + 1);
key.trim();
value.trim();
if (key == "File name"){
Name = value;
}
else if(key == "calibrate"){
calibrate = atof(value.c_str());
}
else if(key == "start_weight"){
start_weight = atof(value.c_str());
}
else if(key == "stop_weight"){
stop_weight = atof(value.c_str());
}
else if(key == "Output_mode"){
Output_mode = atof(value.c_str());
}
}
}
config.close();
Serial.println(F("Read configuration file: OK"));
Serial.println(F("--------------------Config---------------------"));
Serial.println("File name: " + Name);
Serial.print(F("calibrate: "));
Serial.println(calibrate,5);
Serial.print(F("start_weight: "));
Serial.println(start_weight);
Serial.print(F("stop_weight: "));
Serial.println(stop_weight);
Serial.print(F("Output_mode(1:serial:+TF 2:TF 3:serial): "));
Serial.println(Output_mode);
}
void TF_init(){
if(sd.begin(SD_CS_PIN, SPI_HALF_SPEED)){
Serial.println(F("SD_init: OK"));
}
else{
Serial.println(F("SD_init: NO!!!"));
ERROR(1);
}
Read_config();
}
void HX717_init(){
Serial.println(F("--------------------ADC init-----------------------"));
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN,64);
Serial.println(F("ADC_PIN: OK"));
scale.set_scale(calibrate);
Serial.println(F("Set calibrate: OK"));
scale.power_down();
Serial.println(F("Set ADC down: OK"));
delay(100);
scale.power_up();
Serial.println(F("Set ADC up: OK"));
scale.tare();
Serial.println(F("Weight clearing: OK"));
AD_Res_Last = scale.get_units(300);
}
void LED(bool a2){
digitalWrite(LED_PIN,a2);
}
void ERROR(int num){
Serial.println(F("--------------------Error----------------------"));
for (int i = 0; i < sizeof(error); i++){
char c = pgm_read_byte(&error[i]);
if (c == '\0') break;
Serial.print(c);
}
switch(num){
case 1: tone(BUZZER_PIN,4000);
delay(2000);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN,LOW);
while(1){
Serial.print(F("\nCheck if TF is inserted or formatted to a right file system"));
delay(500);
}
case 2: tone(BUZZER_PIN,4000);
delay(1000);
noTone(BUZZER_PIN);
delay(500);
tone(BUZZER_PIN,4000);
delay(1000);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN,LOW);
while(1){
Serial.print(F("\nCreate weight file: NO"));
delay(500);
}
//case 3:
}
}
void setup() {
Serial.begin(115200);
Serial.println("\n");
Serial.println(F("--------------------Welcome--------------------"));
for (int i = 0; i < sizeof(bmd); i++) {
char c = pgm_read_byte(&bmd[i]);
if (c == '\0') break;
Serial.print(c);
}
Serial.print("\n");
Serial.println(F("Welcome to use 320Hz capture card By:bbbbmmdddd"));
Serial.println(F("Version: V1.0"));
Serial.println(F("---------------------Init----------------------"));
pinMode(LED_PIN,OUTPUT);
Serial.println(F("LED_pin: OK"));
pinMode(BUZZER_PIN,OUTPUT);
Serial.println(F("BUZZER_pin: OK"));
TF_init();
HX717_init();
Serial.println(F("------------------Choose mode------------------"));
if(button(16)){
Calibration();
}
Serial.println(F("Choose mode: use mode"));
Serial.println(F("---------------------Start---------------------"));
tone(BUZZER_PIN,1000);
delay(500);
tone(BUZZER_PIN,2000);
delay(500);
tone(BUZZER_PIN,4000);
delay(1000);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN,LOW);
digitalWrite(LED_PIN,HIGH);
pinMode(16,INPUT);
pp:
while(scale.get_units() < start_weight) delay(1);
open ++;
if(!(open >= 3)) goto pp;
if(!config.open((Name + ".csv").c_str(),FILE_WRITE)){
ERROR(2);
}
config.println(F("Time (s),Weight (g)"));
time_start = millis();
// while(1){
// // cache[right] = scale.read_average(4);
// cache[right] = get_weight();
// // cache_time[right] = millis();
// if(cache[right] >= start_weight) temp++;
// if(temp > 10) break;
// if(right > 160){
// right = 0;
// }
// else right++;
// }
// if(!config.open((Name + ".csv").c_str(),FILE_WRITE)){
// ERROR(2);
// }
// if(right > 160){
// right = 0;
// }
// else right++;
// config.println(F("Time (s),Weight (g)"));
// // time_start = cache_time[right];
// for(int i=0;i<160;i++){
// // time_now = (cache_time[right] - time_start) / 1000.000;
// config.print(time_now,3);
// config.print(",");
// config.print(cache[right]);
// config.println();
// Serial.print(time_now,3);
// Serial.print(",");
// Serial.println(cache[right]);
// if(right > 160){
// right = 0;
// }
// else right++;
// }
}
//FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC
void loop() {
if(Output_mode == 1){
while(1){
Now_data = scale.get_units();
//Now_data = get_weight();
time_now = (millis() - time_start) / 1000.000;
data++;
Serial.print(time_now,3);
Serial.print(",");
Serial.print(Now_data);
Serial.print(",");
Serial.println(data);
config.print(time_now,3);
config.print(",");
config.print(Now_data);
config.println();
if (millis() - before >= 1000){
before = millis();
led = !led;
LED(led);
if(stop >= 2){
config.close();
while(1){
for (int i = 0; i < sizeof(bmd); i++) {
char c = pgm_read_byte(&bmd[i]);
if (c == '\0') break;
Serial.print(c);
}
Serial.print("\n");
delay(1000);
}
}
if(Now_data < stop_weight){
stop++;
}
}
}
}
else if(Output_mode == 2){
Serial.print(F("Output_mode: TF"));
while(1){
Now_data = scale.get_units();
time_now = (millis() - time_start) / 1000.000;
data++;
config.print(time_now,3);
config.print(",");
config.print(Now_data);
config.println();
if (millis() - before >= 1000){
before = millis();
led = !led;
LED(led);
if(stop >= 2){
config.close();
while(1){
for (int i = 0; i < sizeof(bmd); i++) {
char c = pgm_read_byte(&bmd[i]);
if (c == '\0') break;
Serial.print(c);
}
Serial.print("\n");
delay(1000);
}
}
if(Now_data < stop_weight){
stop++;
}
}
}
}
else {
while(1){
Now_data = scale.get_units();
time_now = (millis() - time_start) / 1000.000;
data++;
Serial.print(time_now,3);
Serial.print(",");
Serial.print(Now_data);
Serial.print(",");
Serial.println(data);
if (millis() - before >= 1000){
before = millis();
led = !led;
LED(led);
if(stop >= 5){
config.close();
while(1){
for (int i = 0; i < sizeof(bmd); i++) {
char c = pgm_read_byte(&bmd[i]);
if (c == '\0') break;
Serial.print(c);
}
Serial.print("\n");
delay(1000);
}
}
if(Now_data < stop_weight){
stop++;
}
else{
stop = 0;
}
}
}
}
}
[修改于 6天14时前 - 2025/07/03 21:55:25]
忘记放配置文件了 配置文件放在TF卡里面 通过修改XXXXXXXXXi文件来设置
校准长按板载按键(b站有校准使用教程 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
200字以内,仅用于支线交流,主线讨论请采用回复功能。