Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ADDR_BITS 4
- #define ADDR_SIZE 16
- #define DATA_BITS 4
- #define CE_LOW_DELAY_COUNT 1024
- volatile int ram[ADDR_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- const byte dataPins[DATA_BITS] = {4, 5, 6, 7};
- const byte addrPins[ADDR_BITS] = {10, 11, 12, 13};
- const byte cePin = 2;
- const byte wePin = 9;
- void setup(){
- for(short a = 0; a < DATA_BITS; a++){
- pinMode(dataPins[a], INPUT);
- }
- for(short a = 0; a < ADDR_BITS; a++){
- pinMode(addrPins[a], INPUT);
- }
- pinMode(3, OUTPUT);
- pinMode(cePin, INPUT);
- pinMode(wePin, INPUT);
- attachInterrupt(0, ceLow, FALLING);
- #ifdef DEBUG
- Serial.begin(115200);
- #endif
- }
- #ifdef DEBUG
- volatile int lastDataRead = -1;
- volatile int lastAddressRead = -1;
- volatile int lastDataWritten = -1;
- #endif
- void loop(){
- digitalWrite(3, LOW);
- #ifdef DEBUG
- if(lastDataRead != -1){
- Serial.print("Last Data Read: ");
- Serial.println(lastDataRead, HEX);
- lastDataRead = -1;
- }
- if(lastDataWritten != -1){
- Serial.print("Last Data Written: ");
- Serial.println(lastDataWritten, HEX);
- lastDataWritten = -1;
- }
- if(lastAddressRead != -1){
- Serial.print("Last Address Read: ");
- Serial.println(lastAddressRead, HEX);
- lastAddressRead = -1;
- }
- #endif
- if(digitalRead(cePin) == HIGH){
- setDataOutput(false);
- }
- }
- void setDataOutput(bool isOutputMode){
- for(short a = 0; a < DATA_BITS; a++){
- if(isOutputMode == true){
- pinMode(dataPins[a], OUTPUT);
- }else{
- pinMode(dataPins[a], INPUT);
- }
- }
- }
- void ceLow(){
- digitalWrite(3, HIGH);
- int address = 0;
- int data = 0;
- for(short counter = 0; counter < ADDR_BITS; counter++){
- if(digitalRead(addrPins[counter]) == HIGH){
- address |= (0x01 << counter);
- }
- }
- #ifdef DEBUG
- lastAddressRead = address;
- #endif
- if(digitalRead(wePin) == HIGH){
- setDataOutput(false);
- for(short counter = 0; counter < DATA_BITS; counter++){
- if(digitalRead(dataPins[counter]) == HIGH){
- data |= (0x01 << counter);
- }
- }
- #ifdef DEBUG
- lastDataRead = data;
- #endif
- ram[address] = data;
- }else{
- setDataOutput(true);
- #ifdef DEBUG
- lastDataWritten = ram[address];
- #endif
- for(short counter = 0; counter < DATA_BITS; counter++){
- if((ram[address] & (0x01 << counter)) != 0){
- digitalWrite(dataPins[counter], HIGH);
- }else{
- digitalWrite(dataPins[counter], LOW);
- }
- }
- }
- while(digitalRead(cePin) == LOW){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement