Go Down

Topic: How to implement SPC(short-pwm-code) protocol with arduino (Read 226 times) previous topic - next topic

gaoyaqi411

Hello, I'm a fresh man in using arduino and there is some problems in my project. I hope someone can help me,because there isn't any reference about my project.
I don't know if you have heard of the SPC(short-pwm-code) protocol, which based on SENT( Single Edge Nibble Transmission) protocol.The difference between the SPC and SENT protocol is  the SPC protocol transmits data only after receiving a specific trigger pulse from the microcontroller.  So I try to use the I/O on arduino and talk to sensor with SPC  protocol. The problem is that I can't get any signal from sensor.Here is the code I write to communicate. (the key code is in the end, the front part is for the led and button,which can give signs and interact with arduino.)

const int buttonPin = 2;
const int outplusPin = 7;
volatile int opState = HIGH;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
unsigned char Re_buf[10],counter=0;
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(outplusPin,OUTPUT);
  digitalWrite(ledPin, ledState);
  digitalWrite(outplusPin, opState);
}

void loop() {
  // get the key state
  int reading = digitalRead(buttonPin);
  // if the key's state changed.
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // if key changed
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
  if(ledState==LOW){   
    opState=LOW; 
    digitalWrite(outplusPin,opState);  // output trigger to  activate sensor
    delayMicroseconds(117);
    opState=HIGH;
    digitalWrite(outplusPin,opState);
    delayMicroseconds(153);
    digitalWrite(outplusPin,LOW);
    pinMode(outplusPin,INPUT);   //waiting the signal from sensor
    while(counter<9){
    Re_buf[counter] = pulseIn(outplusPin,HIGH,10000);
    counter++;}
   if(counter==9){
      counter=0;
      for(int i=0;i<9;i=i+1){
        Serial.println(Re_buf);
      }
   }
    pinMode(outplusPin,OUTPUT);  //the initial recovery
    digitalWrite(outplusPin,opState);
     Serial.println("finish");
    delay(1000);
    ledState=HIGH;
    }
    }
 


JuergenUno

Usually SPC needs a pullup R. If you configure the pin to input without setting it LOW the Internat pullup might do the job.
Pulsein will be the next problem, as you need the time between all edges to low and no pulswidth.

Go Up