12 years ago.

converting Arduino code to C++ help

I'm using a 74HC595N shift register to save some mbed pins while trying to display multiple LED.

I'm inexperience in Arduino language. I need help coverting this Arduino code into C++ langauge. Got it from this website - http://arduino.cc/en/Tutorial/ShftOut12

(Pin connected to latch pin (ST_CP) of 74HC595)
const int latchPin = 8;
(Pin connected to clock pin (SH_CP) of 74HC595)
const int clockPin = 12;
(Pin connected to Data in (DS) of 74HC595)
const int dataPin = 11;

void setup() {
  (set pins to output because they are addressed in the main loop)
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("reset");
}

void loop() {
  if (Serial.available() > 0) {
    (ASCII '0' through '9' characters are)
    (represented by the values 48 through 57.)
    (so if the user types a number from 0 through 9 in ASCII,)
    (you can subtract 48 to get the actual value:)
    int bitToSet = Serial.read() - 48;

  (write to the shift register with the correct bit set high:)
    registerWrite(bitToSet, HIGH);
  }
}

(This method sends bits to the shift register:)

void registerWrite(int whichPin, int whichState) {
(the bits you want to send)
  byte bitsToSend = 0;

  (turn off the output so the pins don't light up)
  (while you're shifting bits:)
  digitalWrite(latchPin, LOW);

  (turn on the next highest bit in bitsToSend:)
  bitWrite(bitsToSend, whichPin, whichState);

  (shift the bits out:)
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

  (turn on the output so the LEDs can light up:)
  digitalWrite(latchPin, HIGH);
}

http://mbed.org/users/paul80nd/code/7SegSRDriver/ This is a driver for the 74HC595N. It may give you some hints if you can't use it directly.

posted by Stephen Paulger 08 Nov 2012

1 Answer

12 years ago.

DigitalOut latchPin  (p??);
DigitalOut lockPin  (p??);
DigitalOut DataPin  (p??);


main 

pc.baud (9600);
pc.printf ("reset");

while (1)
{
  if (pc.avalable)
  {
    pc.scanf ("%i",Value);
    // convert ..
    registerWrite (i);
  }
}


// -----------

void registerWrite (int SendMe)
{
  latchPin = 0;

  // SPI code, 

  latchPin = 1;
}

This should be a starting piont ..

Ceri

registerWrite here does the same job as write_raw in the 7SegSRDriver code.

posted by Stephen Paulger 08 Nov 2012