Arduino Mega 2560 board and SN74HC595 shift register(s). I'm working on a school assignment and having issues getting the shift register(s) working without using Arduino libraries.
I know with the libraries you can use functions such as digitalWrite(....);
and shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
.
For my project, I've set up my individual pins as inputs and outputs, but I am having trouble converting the library code into basic code.
IE:
digitalWrite(latchPin, LOW);
can be rewritten as
DDRB &= ~(1 << DDB1);
where latchPin
is the SCK
pin.
At this stage in the project, we're just trying to get 8 LEDs lit up using 1 shift register, then 16 using 2 shift registers. Any help in understanding what I need to do or direction in which to look would be appreciated.
Thank you,
Nic
Edit:
This is what I have converted from the void setup from https://www.arduino.cc/en/Tutorial/ShftOut12 :
void setup() {
//Clear the shift registers prev data, Need a low input to reset registers
//PORTL0 to first SReg SRCLR, PORTL2 to second SReg SRCLR
//* See data sheet for sn74hc595 shift registers
DDRL &= ~((1 << PORTL0) | (1 << PORTL2)); //Pg 100 - 21.2.3
//Use port L0/L2 for SRCLR pin on SReg
PORTL |= (1 << PORTL0) | (1 << PORTL2); //Pg 100 - 21.2.3
//Set OE (Enable) to 0 until after last bit is sent
DDRL &= ~(1 << PORTL4); //Pg 100 - 21.2.3
//TODO SPI DELAY HERE USING SPDR pg 199
//Enable SCK as output to drive RCLK and SRCLK on SR
DDRB &= ~(1 << DDB1); //Pg 96 - 13.4.6
PORTB |= (1 << PORTB1); //Pg 96 - 13.4.5
//Enable MOSI as output to drive SER Input on SR
DDRB &= ~(1 << DDB0); //Pg 96 - 13.4.6
PORTB |= (1 << PORTB0); //Pg 96 - 13.4.5
Serial.begin(9600);
Serial.println("reset");
}