I am new in using a Raspberry Pi (3 Model B) and I am trying to use a ShiftIn Register.
I tryed the circuit with my Arduino Uno and it worked.
My code for the Arduino:
Code: Select all
int latchPin = 8;
int clockPin = 9;
int dataPin = 10;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(latchPin, HIGH);
delay(20);
digitalWrite(latchPin, LOW);
int buttons[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i < 8; i++){
digitalWrite(clockPin, LOW);
delay(20);
buttons[i] = digitalRead(dataPin);
digitalWrite(clockPin, HIGH);
}
for(int j = 0; j < 8; j++){
Serial.print(buttons[j]);
}
Serial.println("------------------------------------");
}
and transformed the code from above into a (theoretically) working code for the Pi, but it won't work like it should.
Here the code for the PI:
Code: Select all
#include <bcm2835.h>
#include <stdio.h>
#define DATA_PIN RPI_BPLUS_GPIO_J8_37 //Pin 37 - BLACK WIRE
#define CLOCK_PIN RPI_BPLUS_GPIO_J8_38 //Pin 38 - ORANGE WIRE
#define LATCH_PIN RPI_BPLUS_GPIO_J8_40 //Pin 40 - YELLOW WIRE
int main(int argc, char **argv)
{
// Init
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(CLOCK_PIN, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(LATCH_PIN, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(DATA_PIN, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(DATA_PIN, BCM2835_GPIO_PUD_DOWN);
bcm2835_gpio_write(LATCH_PIN, HIGH);
bcm2835_delay(20);
bcm2835_gpio_write(LATCH_PIN, LOW);
uint8_t buttons[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i;
for (i = 0; i < 8; i++){
bcm2835_gpio_write(CLOCK_PIN, LOW); // Clock Low
bcm2835_delay(20); // Delay
buttons[i] = bcm2835_gpio_lev(DATA_PIN); // Read and save
bcm2835_gpio_write(CLOCK_PIN, HIGH); // Clock High
}
uint8_t v;
for(v = 0; v < 8; v++){
printf("%d", buttons[v]);
}
bcm2835_close();
return 0;
}
Thanks for your help.
Einzigartigername