Metaboard/2010 activities/6-pin auto-detecting programming: Unterschied zwischen den Versionen
Chrysn (Diskussion | Beiträge) plan for programming the sensors |
Chrysn (Diskussion | Beiträge) it works now |
||
Zeile 33: | Zeile 33: | ||
When idling, RESET is actively pulled low, and POWER is configured as input with the builtin pullup resistor. Thus, connecting a sensor regularly or flipped pulls POWER down and a device is recognized. | When idling, RESET is actively pulled low, and POWER is configured as input with the builtin pullup resistor. Thus, connecting a sensor regularly or flipped pulls POWER down and a device is recognized. | ||
After the presence of a plug is detected, it can be determined whether the direction is ok. | After the presence of a plug is detected, it can be determined whether the direction is ok by setting RESET as input with pullup. If POWER goes up again, the connector is plugged in the wrong way, otherwise the system can be powered. | ||
Here is an example of how to poll for the connector: | |||
#define RESET 9 | |||
#define POWER 10 | |||
#define MISO 12 | |||
#define MOSI 11 | |||
#define SCK 13 | |||
void reset() { | |||
digitalWrite(RESET, LOW); | |||
pinMode(RESET, OUTPUT); | |||
pinMode(POWER, INPUT); | |||
digitalWrite(POWER, HIGH); | |||
} | |||
void setup() { | |||
reset(); | |||
pinMode(MISO, INPUT); | |||
pinMode(MOSI, INPUT); | |||
pinMode(SCK, INPUT); | |||
digitalWrite(MISO, HIGH); | |||
digitalWrite(MOSI, HIGH); | |||
digitalWrite(SCK, HIGH); | |||
Serial.begin(19200); | |||
} | |||
void loop() { | |||
if(digitalRead(POWER) != LOW) { | |||
Serial.write("Nothing detected on power line, allowing other SPI communication\r\n"); | |||
while(digitalRead(POWER) != LOW); // blocking in the poll is nothing we normally want; using it here to supress duplicate messages | |||
} | |||
Serial.write("Detected something\r\n"); | |||
pinMode(RESET, INPUT); | |||
digitalWrite(RESET, HIGH); | |||
if(digitalRead(POWER) == HIGH) { | |||
Serial.write("Match was bad\r\n"); | |||
} else { | |||
Serial.write("Match was GOOD!\r\n"); | |||
Serial.write("Waiting for confirmation.\r\n"); | |||
int bad = 0; | |||
for(int i=0; i < 20; ++i) { | |||
delay(1); | |||
if(digitalRead(POWER) == HIGH) | |||
{ | |||
bad = 1; | |||
break; | |||
} | |||
} | |||
if(bad == 0) { | |||
Serial.write("Confirmed, taking up the chip.\r\n"); | |||
pinMode(RESET, OUTPUT); | |||
digitalWrite(RESET, HIGH); | |||
pinMode(POWER, OUTPUT); | |||
digitalWrite(POWER, HIGH); | |||
delay(10000); | |||
Serial.write("enough\r\n"); | |||
} else | |||
Serial.write("Unconfirmed, returning\r\n"); | |||
} | |||
delay(200); | |||
reset(); | |||
} | |||
Note that for polling to work in practice, the digitalWrite({MOSI, SCK}, HIGH) might have to be set before polling. |