在坛子里学习了很久,最近做了一个电桥传感器的数据采集记录仪,留个纪念。
引用帖,在此对作者表示感谢:
基于24位AD转换模块HX711的重量称量实验(已补充皮重存储,线性温度漂移修正)
http://www.geek-workshop.com/thread-2315-1-1.html
arduino学习笔记18 - SD卡读写实验
http://www.geek-workshop.com/thread-104-1-1.html
硬件含nano mini板、SD模块、hx711模块以及13块钱2个还包邮的2G miniSD卡
详细过程我就不再累述,大部分在代码里有注释
- #include <SPI.h>
- #include <SD.h>
- #include <HX711.h>
- const int chipSelect = 4;
- void(*resetFunc)(void) = 0;
- Sd2Card card;
- SdVolume volume;
- SdFile root;
- //--------------------记录模式
- void logger()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // see if the card is present and can be initialized:
- while (!SD.begin(chipSelect)) {
- Serial.println("Card failed, or not present");
- // don't do anything more:
- resetFunc();//异常重启
- }
- while (!card.init(SPI_HALF_SPEED, chipSelect)) {
- Serial.println("initialization failed.");
- resetFunc();//异常重启
- }
- // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
- while (!volume.init(card)) {
- Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
- resetFunc();//异常重启
- }
- Serial.println("ready to log...");
- delay(1000);
- Serial.println("3...");
- delay(1000);
- Serial.println("2...");
- delay(1000);
- Serial.println("1...");
- delay(1000);
- HX711 hx_A(5, 6, 128, 1);//传感器A,D5,D6,最后一位为修正系数
- HX711 hx_B(7, 8, 128, 1);//传感器B,D7,D8
- hx_A.set_offset(169600);//传感器A偏移值
- hx_B.set_offset(169600);//传感器B偏移值
- //案例用的9,10好像mini板不支持
- long sq = 0;
- for (int n = 0; n < 99999999; n++) {
- sq = millis() / 100 - 40;//序号以ms为单位,减去前边4s延时
- long t_A = 0, t_B = 0;
- t_A = hx_A.read();//直接读取原始数值
- t_B = hx_B.read();
-
- // t_A = hx_A.bias_read();//直接读取修正后数值
- // t_B = hx_B.bias_read();
- String rec = String(sq) + "," + String(t_A) + "," + String(t_B);
- Serial.println(rec);
- //---------------------------------------往SD里存储
- File dataFile = SD.open("datalog.txt", FILE_WRITE);
- //
- if (dataFile) {
- dataFile.println(rec);
- dataFile.close();
- }
- else {
- Serial.println("error opening datalog.txt");
- resetFunc();//异常重启
- }
- delay(77);//延时,使得采集频率为10HZ
- }
- }
- //--------------------------------------读取数据模式
- void reader()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // see if the card is present and can be initialized:
- while (!SD.begin(chipSelect)) {
- Serial.println("Card failed, or not present");
- // don't do anything more:
- resetFunc();//异常重启
- }
- while (!card.init(SPI_HALF_SPEED, chipSelect)) {
- Serial.println("initialization failed.");
- resetFunc();//异常重启
- }
- // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
- while (!volume.init(card)) {
- Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
- resetFunc();//异常重启
- }
- // print the type and size of the first FAT-type volume
- uint32_t volumesize;
- volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
- volumesize *= volume.clusterCount(); // we'll have a lot of clusters
- volumesize *= 512; // SD card blocks are always 512 bytes
- volumesize /= 1024;
- Serial.print("Volume size (Mbytes): ");
- volumesize /= 1024;
- Serial.println(volumesize);
- Serial.println("\nFiles found on the card (name, date and size in bytes): ");
- root.openRoot(volume);
- // list all files in the card with date and size
- root.ls(LS_R | LS_DATE | LS_SIZE);
- Serial.println("ready to read...");
- delay(1000);
- Serial.println("3...");
- delay(1000);
- Serial.println("2...");
- delay(1000);
- Serial.println("1...");
- delay(1000);
- File dataFile = SD.open("DATALOG.TXT");
- if (dataFile) {
- while (dataFile.available()) {
- Serial.write(dataFile.read());
- }
- dataFile.close();
- Serial.println("download finished!");
- //-------------------------------------------------------------------------
- int buttonPin = 3; // D3串口高电平,删除数据
- pinMode(buttonPin, INPUT);
- int buttonState = 0;
- buttonState = digitalRead(buttonPin);
- Serial.println(buttonState);
- while (buttonState == 1) {
- Serial.println("Clean data in 5 sec.");
- delay(5000);
- SD.remove("datalog.txt");
- Serial.println("Clean finished!");
- return;
- }
- }
- else {
- Serial.println("error opening datalog.txt");
- resetFunc();//异常重启
- }
- }
- //---------------------------------------------------------------------------------
- void setup() {
- int buttonPin = 2; // D2高电平--logger模式,D2低电平--reader模式
- int buttonState = 0;
- pinMode(9, OUTPUT);
- pinMode(10, OUTPUT);
- digitalWrite(9, LOW);
- digitalWrite(10, HIGH); //VCC与GND不够用,把D9,10口当电源
- pinMode(14, OUTPUT);
- pinMode(15, OUTPUT);
- analogWrite(14, 0); //A0-5就是P14-19,模拟口可以直接当数字口用,涨姿势了~
- analogWrite(15, 255); //VCC与GND不够用,把A0,1口当电源
- pinMode(buttonPin, INPUT);
- buttonState = digitalRead(buttonPin);
- if (buttonState == HIGH) {
- logger();
- }
- else {
- reader();
- }
- }
- //----------------
- void loop()
- {
- }
复制代码
|