I am currently working on hacking an old snack vending machine.
The motor matrix for the dispense is controlled by a 5890 and a 5842, both regular shift registers.
(the 2 form a cascade)
I wanna keep using the existing vending machine's controller board with the motor drivers.
Now the first step is intercepting the signals sent to the shift registers by the original microcontroller in order to later replace it by an arduino and do a kind of "capture and replay".
When I connect a LogicAnalyzer(OLS/Arduino), i get nice results(see picture), but somehow my Arduino sketch, which should just
decode the sent signal.
The arduino console output for the same signal as on the picture is
Sniffing...
16
1000000000000000
Sniffing...
...which just can't be right...
Here's my code:
#define CLCK 2
#define DATA 3
#define OE 5
volatile int master_count = 0; // universal count
volatile uint16_t in_buffer =0;
void setup() {
pinMode(DATA, INPUT);
pinMode(OE, INPUT);
pinMode(CLCK,INPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("Sniffing...");
delay(10);
while(1)
if(digitalRead(OE)==LOW)break;
master_count = 0;
in_buffer = 0;
attachInterrupt(0, interrupt, RISING);
// interrupt 0 digital pin 2 positive edge trigger
while(1)
{
if(master_count>15)
{
detachInterrupt(0);
Serial.println(master_count,DEC);
printBIN(&in_buffer);
delay(6000);
*in_buffer =0;
master_count=0;
break;
}
}
}
void interrupt() {
boolean read = digitalRead(DATA);
noInterrupts();
if(read==true)bitSet(in_buffer,(15-master_count));
master_count++;
interrupts();
}
//function that also prints leading zeros
void printBIN(volatile uint16_t* binary)
{
for(int i=15;i!=-1;i--)
{
if(bitRead(*binary,i))Serial.print('1');
else Serial.print('0');
}
}
I was thinking that it might be a timing issue, but the shift register is being sent to with about 100khz, and the arduino is running at 16mhz, so between to edges on the clock signal there is space for about 160 instructions on the Arduino, which should be fine, rihgt?
Thanks, Robert