简体   繁体   中英

Read on/off signal from high speed 5v single wire bus

I am trying to read from a single wire 5v bus that should be either ~0v or ~5v and change at most every 25 microseconds.

On the hardware side: I am just connecting the single wire to a pin on my arduino (atmel328p). I have tried pins 3, 8 and A0. I will need to do port manipulation to achieve the speed I need. Since the atmel328p runs 16 million operations/second this should be well within the capabilities of an arduino.

Here is my code:

void setup() {
  pinMode(3, INPUT);
  Serial.begin(115200);
  for(;;) {
    uint8_t a = PIND;//analogRead(A0);
    if (a==0)
      // normally I won't be writing to USB serial this often, I'm
      // just simplifying as much as possible and I just want to know if this
      // actually works
      Serial.println(a);
  }
}
void loop() {}

What am I doing wrong on the hardware or software side?

Several other projects, such as SoftwareSerial, NewSoftSerial or AltSerial or I2C or OneWire all do the same thing, but I can't figure out what I'm doing wrong.

Your loop is stateless. In other words; if A0==1 then nothing happens. But if A0==0 then it jam's the Serial.print filling it faster than it can actually send out forever in a tight loop. Blowing up. You need to put some edge conditions in, to send only on the state of a transition.

You could also think about using the PinChangeInt feature and supporting library to use interrupts to look for the edges. Library can be found at here

#include <PinChangeInt.h>

void pinfunc() {
  Serial.println();
  Serial.print("Pin =");
  Serial.print(A0, DEC); 
  Serial.println("!");
}

void setup() {
  PCintPort::attachInterrupt(A0, &pinfunc, CHANGE);
  Serial.begin(115200);
}

uint8_t i;
void loop() {
  Serial.print(".");
  delay(1000); // important to slow up preceding print.
  // do other things. 
  // possibly look for a flag set in pinfunc()
}

There are more in-depth examples in the library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM