完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
您好,
我正在尝试使用以下代码从我的 mitutoyo 高度计读取数据: 代码: #include #include #include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) // The pins for I2C are defined by the Wire-library. // On an arduino UNO: A4(SDA), A5(SCL) // On an arduino MEGA 2560: 20(SDA), 21(SCL) // On an arduino LEONARDO: 2(SDA), 3(SCL), ... #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // When defined, will send SPC data as hexadecimal over serial. #define HEX_DATA_OUTPUT // When defined, will convert raw data to human readable string // and send over serial. #define HUMAN_READABLE_OUTPUT int req = 5; //Arduino pin for REQ line, drives transistor to ground SPC port pin 5 (~REQ) int dat = 2; //Arduino pin for Data line, connects directly to SPC port pin 2 (DATA) int clk = 3; //Arduino pin for Clock line, connects directly to SPC port pin 3 (~CLK) int i = 0; int j = 0; int k = 0; // Mitutoyo SPC data port transmits 13 4-bit values // Byte Meaning // 0 Header, always 0xF // 1 Header, always 0xF // 2 Header, always 0xF // 3 Header, always 0xF // 4 Sign. Zero is positive. 0x8 is negative. // 5 Most significant digit (MSD) of measurement // 6 Next digit of measurement // 7 Next digit of measurement // 8 Next digit of measurement // 9 Next digit of measurement // 10 Least significant digit (LSD) of measurement // 11 Digit to place decimal point // 12 Unit. Zero is mm. Nonzero (b1000 is 1?) is inch byte spcdata[13]; // The raw data sent by instrument #ifdef HUMAN_READABLE_OUTPUT float value; // The value calculated from raw data int decimal; // Number of digits that are after decimal point #endif void setup() { Serial.begin(9600); pinMode(req, OUTPUT); pinMode(clk, INPUT_PULLUP); pinMode(dat, INPUT_PULLUP); digitalWrite(req,LOW); // set request at high // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Clear the buffer display.clearDisplay(); display.setTextSize(3); // Draw 2X-scale text display.setTextColor(SSD1306_WHITE); } void loop() { getData(); #ifdef HEX_DATA_OUTPUT for( i = 0; i < 13; i++ ) { Serial.print(spcdata[i],HEX); } #ifdef HUMAN_READABLE_OUTPUT // Need a separator if we're printing human readable as well Serial.print(" "); #endif // HUMAN_READABLE_OUTPUT #endif //HEX_DATA_OUTPUT #ifdef HUMAN_READABLE_OUTPUT // Combine measurement digits into a number value = 0; for( i = 5; i <= 10; i++) { value *= 10; value += spcdata[i]; } // Adjust number for decimal point position decimal = spcdata[11]; value /= pow(10,decimal); // Adjust if number is negative if (spcdata[4] == 0x8) { value *= -1; } // Print resulting value to serial port, to specified // number of digits after decimal point. display.clearDisplay(); display.setCursor(0, 0); Serial.print(value,decimal); display.println(value,decimal); // Append units for value if (spcdata[12] == 0) { Serial.print(" mm"); display.print(" mm"); } else { Serial.print(" in"); display.print(" in"); } display.display(); // Show initial text #endif // HUMAN_READABLE_OUTPUT Serial.println(); } void getData(void){ digitalWrite(req, HIGH); // generate set request for( i = 0; i < 13; i++ ) { k = 0; // Timing diagram indicates data bit has been valid for about 120 // microseconds before the clock signal is raised, and remains // valid for about 120 microseconds afterwards. This code reads data // bit at the falling clock edge. for (j = 0; j < 4; j++) { while( digitalRead(clk) == LOW) { } // hold until clock is high while( digitalRead(clk) == HIGH) { } // hold until clock is low bitWrite(k, j, (digitalRead(dat) & 0x1)); } // After reading the first 4-bit value, we can drop REQ output. if (i == 0) { digitalWrite(req,LOW); } spcdata[i] = k; } } 但是每当我将代码上传到我的 NODEMCU 时,它每隔几秒就会崩溃并显示一条软 WDT 重置消息。谁能帮我这是为什么? |
|
相关推荐
1个回答
|
|
, 21(SCL)#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)#define SCREEN_ADDRESS 0x3C // Address of the OLED displaySSD1306Wire display(SCREEN_ADDRESS, D2, D1);// D2 = SDA, D1 = SCL
Adafruit_MCP4725 dac;// Select I2C addresses for Adafruit DAC MCP4725unsigned int addr_mitutoyo = 0x60;// Address of Mitutoyo ABS Digimatic Indicatorconst int ledPin = 2; // LED connected to digital pin 13const int timeoutDelay = 100; // Delay to prevent timeout in loop()void setup() { Serial.begin(9600); display.init(); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_LEFT); display.clear(); display.display(); // Initialising the DAC dac.begin(0x62); // Default address is 0x62 // Initialise Mitutoyo high resolution mode Wire.beginTransmission(addr_mitutoyo); Wire.write(0x30); Wire.write(0x13); Wire.endTransmission(); // Set LED pin as output pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); }void loop() { int receivedBytes = 0; char data[32]; // Send request for measurement to Mitutoyo Wire.beginTransmission(addr_mitutoyo); Wire.write(0xF5); Wire.write(0x10); Wire.endTransmission(); // Wait for response delay(5); Wire.requestFrom(addr_mitutoyo, 8); while(Wire.available()) { data[receivedBytes++] = Wire.read(); if (receivedBytes > 31) break; } // Check if response is valid if(receivedBytes == 8 && data[0] == 82 && data[2] == 1 && data[3] == 240) { // Parse measurement data int measurement = (data[4] << 8) + data[5]; float resolution = ((float)data[7])/16.0; // Convert measurement to voltage float voltage = (float)measurement * resolution / 1000.0; // Convert voltage to DAC value uint16_t dacValue = (uint16_t)(voltage * (((1 << 12) - 1) / 5.0)); // Write DAC value dac.setVoltage(dacValue, false); // Display measurement and DAC value on OLED screen display.clear(); display.drawString(0, 0, "Measurement:"); display.drawString(0, 12, String(voltage) + " V"); display.drawString(0, 24, "DAC value:"); display.drawString(0, 36, String(dacValue)); display.display(); } else { // Display error message display.clear(); display.drawString(0, 0, "Error:"); display.drawString(0, 12, "No response"); display.display(); } // Blink LED to indicate operation digitalWrite(ledPin, HIGH); delay(timeoutDelay); digitalWrite(ledPin, LOW); delay(timeoutDelay); }但是,当我编辑了并上传了这个代码时,我的 ESP8266 却崩溃了。这是怎么回事? |
|
|
|
只有小组成员才能发言,加入小组>>
523浏览 6评论
433浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
421浏览 5评论
413浏览 4评论
387浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-1 07:02 , Processed in 0.670337 second(s), Total 80, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号