Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int aanstaanden = 0;
- bool firstLoop = true;
- long time = 0;
- long prevTime = 0;
- // pinout:
- // D6 through D13 -- data lines
- // A0 through A2 -- WE, OE, CE respectively
- // D2 up to D6 -- control pins for the shift registers
- // Main function
- void loop()
- {
- time = millis();
- if(firstLoop)
- {
- write_a_byte(1, 1);
- firstLoop = false;
- }
- else if(time - prevTime > 5000)
- {
- Serial.print("The following EEPROM value has been found: ");
- Serial.print(read_a_byte(1));
- prevTime = time;
- }
- }
- uint8_t read_a_byte(uint16_t address) {
- io_input();
- turn_on(OE);
- turn_off(CE);
- turn_on(WE);
- put_adresses(address);
- turn_off(OE);
- delayMicroseconds(10);
- byte data = read_io();
- turn_on(OE);
- turn_on(CE);
- }
- void write_a_byte(uint8_t data, uint16_t address) {
- turn_on(CE);
- turn_on(OE);
- turn_on(WE);
- delay(100);
- put_adresses(address);
- put_io(data);
- turn_off(CE);
- turn_off(WE);
- // Time to latch address
- delayMicroseconds(15);
- turn_on(WE);
- // Time to latch data
- delayMicroseconds(15);
- turn_on(CE);
- delay(1);
- // NOTE this bit is commented out in my code, as this will result in an infinite wait since the write function
- // isn't working, so it always reads back 255
- uint8_t done = 0;
- while(done != 1)
- {
- if(read_a_byte(address) == data)
- {
- done = 1;
- }
- else
- {
- delay(1);
- }
- }
- turn_adresses_off();
- }
- void turn_on(uint8_t port)
- {
- digitalWrite(port, HIGH);
- }
- void turn_off(uint8_t port)
- {
- digitalWrite(port, LOW);
- }
- void put_adresses(uint16_t address)
- {
- for(uint16_t i = 0; i < 16; i++)
- {
- if((1 << i) & address)
- {
- aanstaanden |= (1 << i);
- }
- else
- {
- aanstaanden &= ~(1 << i);
- }
- }
- shift_a_byte(aanstaanden);
- }
- void turn_addresses_off()
- {
- for(uint16_t i = 0; i < 16; i++)
- {
- aanstaanden &= ~(1 << i);
- }
- shift_a_byte(aanstaanden);
- }
- void put_io(int val)
- {
- for(int i = 0; i < 8; i++)
- {
- if((1 << i) & val)
- {
- // The io-lines start at digital pin 6 and run up to and including digital pin 13
- digitalWrite(6 + i, HIGH);
- }
- else
- {
- digitalWrite(6 + i, LOW);
- }
- }
- }
- byte read_io()
- {
- byte b = 0;
- for(int i = 0; i < 8; i++)
- {
- if(digitalRead(6 + i) == HIGH)
- {
- b |= (1 << i);
- }
- }
- return b;
- }
- void io_input()
- {
- pinMode(io_1, INPUT);
- pinMode(io_2, INPUT);
- pinMode(io_3, INPUT);
- pinMode(io_4, INPUT);
- pinMode(io_5, INPUT);
- pinMode(io_6, INPUT);
- pinMode(io_7, INPUT);
- pinMode(io_8, INPUT);
- }
- // Yes, these pins are all mapped correctly and tested with a volt meter!
- void shift_a_byte(int b)
- {
- digitalWrite(clk, LOW);
- digitalWrite(srclk, LOW);
- for(int i = 16; i >= 0; i--)
- {
- if((1 << i) & b)
- {
- digitalWrite(serial_out, HIGH);
- }
- else
- {
- digitalWrite(serial_out, LOW);
- }
- digitalWrite(srclk, HIGH);
- digitalWrite(srclk, LOW);
- }
- digitalWrite(clk, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment