Arduino code-writing/reading static ram

Status
Not open for further replies.

Thread Starter

Zeus_1550923062

Joined Feb 23, 2019
3
Hi . Please someone assist me, thanks


C:
int da[8] = {22, 23, 24,25,26,27,28,29};
int ad[8] = {36,37,38,39,40,41,42,43};

int we = 2;
int OE = 4;

void set(int a) {
  for(int k = -1; k++<7;  )
  pinMode(ad[k], OUTPUT);

  for(int k = -1; k++<7;  ) {
  digitalWrite(ad[k], (a&1));
  a = a>>1;
  }
}

void wr(int a, byte b) {
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  set(a);

  for(int k = -1; k++<7;  )
  pinMode(da[k], OUTPUT);

  for(int k = -1; k++<7;  ) {
  digitalWrite(da[k], (b&1));
  b = b>>1;
  }
  digitalWrite(2, HIGH);
  digitalWrite(2, LOW);
  delayMicroseconds(1);
  digitalWrite(2, HIGH);
  delay(2);
}

byte re(int a) {
  set(a);
  byte v = 0;
  digitalWrite(2, HIGH);
  digitalWrite(4, LOW);
  for(int k = 8; k-->0;  )
  pinMode(da[k], INPUT);

  for(int k = 8; k-->0;  )
  v = (v<<1) + digitalRead(da[k]);
 

  return v;

 
}

void setup() {
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);

  for(int i = -1; i++<255;  ) {

  wr(i, 0xf6);
  delay(2);

  }
 
  for(int f = -1; f++<21;  )
  Serial.println(re(f), HEX);

}

void loop() {

}


if I write f6 or ff and read maybe 6 lines. This is the output - 00, 00, f6, f6, 00, 00
Alternates in that pattern

Please what's wrong? Thank you

Moderators note : applied code tags for C
Please use code tags for pieces of code
 

Ian Rogers

Joined Dec 12, 2012
1,136
You need to enumerate the pins so we can follow your code..

When you read the static RAM you are enabling CE / RD before you set the databus to inputs..I don't know the impact of this but I would set the pins to inputs and then set the RD pin or you'll have a conflict..

I take it you cannot use a bus directly? If you are using a mega there is an 8 bit port on port L pins 42 ~ 49..

Question? why do you start at RAM location -1 ?

If you re-wire your code using TEXT rather than just the pin number (ie.. #define READPIN 2 )
You will get far more help..

*** Note** one other thing... What are you doing with the spare address pins on the SRAM
 

Thread Starter

Zeus_1550923062

Joined Feb 23, 2019
3
You need to enumerate the pins so we can follow your code..

When you read the static RAM you are enabling CE / RD before you set the databus to inputs..I don't know the impact of this but I would set the pins to inputs and then set the RD pin or you'll have a conflict..

I take it you cannot use a bus directly? If you are using a mega there is an 8 bit port on port L pins 42 ~ 49..

Question? why do you start at RAM location -1 ?

If you re-wire your code using TEXT rather than just the pin number (ie.. #define READPIN 2 )
You will get far more help..

*** Note** one other thing... What are you doing with the spare address pins on the SRAM
Hi. I don't understand "
If you are using a mega there is an 8 bit port on port L pins 42 ~ 49.."

I tied the other address pins low. The location starts at 0

Please do you know where the code tag is? can't find
 

Ian Rogers

Joined Dec 12, 2012
1,136
You can read / write the whole port in one

v = PORTA; ( pin 22 ~ pin 29 ) you don't need to set or unset each pin...

You have the address bus on PORTC.. same here...
 

spinnaker

Joined Oct 29, 2009
7,830
I don't understand the question. Subject line is about reading static RAM yet the question and the code has nothing to do with that.
You need to be a lot more clear in stating your problem if you want help.
 

spinnaker

Joined Oct 29, 2009
7,830
Wow is your code confusing. Comments would help. Have you tried using the debugger and step through the code? That is hat any competent developer would do.
 

Ian Rogers

Joined Dec 12, 2012
1,136
hmmm...I still do not understand :(
Firstly.. I am assuming you are using a Mega as the Portc and Porta.. you are setting each data pin and address pin separately..
You have connected
PORTA pin 0 ~ PORTA pin 7 to the data pins..
PORTC pin 0 ~ PORTC pin 7 to the address pins..

You can just write to the port.... PORTC = addr; PORTA = data;
 

LesJones

Joined Jan 8, 2017
4,174
To give you an idea what is required here is some code that does basically what you are trying to do. It was written to read and write a DS1225NV (NV ram chip) It should function the same with your SRAM.

Code:
; DS1225Y MVRam programmer
;
; Uses PIC16F877 with 20 Mhz crystal


  list p=16f877A, st=OFF, x=OFF, n=0
  errorlevel -302
  errorlevel -306

  #include <p16f877A.inc>

   __CONFIG   _HS_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _CP_OFF

;Port use
;   PORTB   Used to read and write data to DS1225Y NVRAM chip being programmed
;   PORTD   Used for address lines 0 to 7 of DS1225Y NVRAM chip being programmed
;   PORTA   Used for address lines 8 to 12 of DS1225Y NVRAM chip being programmed


; Define constants
;BAUD_CONSTANT  EQU 40  ;Constant for baud generator for 19200 baud  (Fosc is 20MHz)
BAUD_CONSTANT  EQU 81  ;Constant for baud generator for 9600 baud  (Fosc is 20MHz)

#DEFINE   Not_CE   PORTE,0
#DEFINE   Not_OE   PORTE,1
#DEFINE   Not_WE   PORTE,2

;  ******** RAM Locations ***********
;Bank 0
   CBLOCK   0x20   ;GPR' starts at 0x20
D_Count01
D_Count02
ADD_L
ADD_H

   ENDC       ;Must end at 07F


;   ******* Flag bit definitions ********

;Flags
   


   
  ORG  0

RESET:
  GOTO  Main

  ORG  4
   
INTERRUPT:
   NOP
   RETFIE


Main:
  clrf  STATUS  ; Select bank 0
  clrf  INTCON  ; No interrupts
  clrf  PCLATH  ; Code is in first bank



   CALL   Init
;Test for function  read or write  (High for read.)
   BTFSC   PORTC,0
   GOTO   READ



WRITE:
   BSF   PORTC,5     ;Turn Write LED on
  BSF STATUS, 0x5    ;Select bank 1     
  CLRF TRISD       ;Clear TRISD  - All bits as outputs   
  BCF STATUS, 0x5    ;Select bank 0   
   CLRF   PORTD
   BSF   Not_WE
   BSF   Not_OE
   BCF   Not_CE
   GOTO   Write_LOOP


READ:
  BSF STATUS, 0x5    ;Select bank 1   
   MOVLW   0x0FF
   MOVWF   TRISB     ;Set all bits to "1"  All bits as inputs
  BCF STATUS, 0x5    ;Select bank 0   

   BSF   Not_WE
   BCF   Not_OE
   BCF   Not_CE
   GOTO   Read_LOOP



Write_LOOP:
   Call   RXByte     ;Get data byte from host computer
   MOVWF   PORTB     ;Set data lines
; Put code hear to pulse write line low
   BCF   Not_WE
   NOP
   NOP
   BSF   Not_WE

;Increment address
   INCF   ADD_L,F
   BTFSC    STATUS, Z
   INCF   ADD_H,F
   MOVFW   ADD_L
  MOVWF    PORTD     ;Lower 8 bits of address
   MOVFW   ADD_H
   MOVWF   PORTA     ;Top 5 bits of address
   GOTO   Write_LOOP


Read_LOOP:
   Call   RXByte     ;Just for syncronisation.  Value of byte received does not matter.
; Put code hear to pulse outout enable line low

   MOVFW   PORTB   ;Read data lines
   CALL   TXByte   ;Send to host computer
;Increment address
   INCF   ADD_L,F
   BTFSC    STATUS, Z
   INCF   ADD_H,F
   MOVFW   ADD_L
  MOVWF    PORTD     ;Lower 8 bits of address
   MOVFW   ADD_H
   MOVWF   PORTA     ;Top 5 bits of address





   GOTO Read_LOOP





Init:
;Init I/O ports

   
  BCF STATUS, 0x6   
  BSF STATUS, 0x5    ;Select bank 1   
  CLRF TRISA       ;Clear TRISA  - All bits as outputs   
  CLRF TRISB       ;Clear TRISB  - All bits as outputs   
  CLRF TRISD       ;Clear TRISD  - All bits as outputs   
  CLRF TRISE       ;Clear TRISB  - All bits as outputs   
   MOVLW   0x0C7
   MOVWF   TRISC     ;Bits 0,1,2,6,7 inputs bits 3,4,5 outputs

;Enable PORTE as digital I/O
   MOVLW   0x06
   MOVWF   ADCON1   
;   Init USART
;
     movlw  BAUD_CONSTANT  ;set baud rate
     movwf  SPBRG
     bsf  TXSTA,BRGH  ;baud rate high speed option
     bsf  TXSTA,TXEN  ;enable transmission
     bcf  STATUS,RP0   
     bsf  RCSTA,CREN  ;enable reception
     bsf  RCSTA,SPEN  ;enable serial port


;Clear I/O ports
  BCF STATUS, 0x6  
  BCF STATUS, 0x5    ;Select bank 0   

     movf  RCREG,W    ;Read read buffer to clear DAV flag

   CLRF   PORTA
   CLRF   PORTB
   CLRF   PORTC
   CLRF   PORTD
   MOVLW   0x07
   MOVWF   PORTE     ;Set Not_CE, Not_OE & NOT_WE  high as these are active low signals
   CLRF   ADD_L
   CLRF   ADD_H   
   RETURN


   


;Subroutines

;*****************************************************************************   
;
;  Function :  SerialReceive
;  This function extracts a byte from the receiver hardware.
;  It will block until a byte is available
;
;  Input:  None.
;
;  Output:  byte in W
;
;*****************************************************************************   
RXByte:
  btfss  PIR1,RCIF  ;check if data received
  goto  RXByte  ;wait until new data
  movf  RCREG,W  ;get received data into W
  return



;*****************************************************************************   
;
;  Function :  SerialTransmit
;  This function sends the byte in W over the RS232 port. The
;  function will wait until previous data has been sent
;
;  Input:  Byte in W
;
;  Output:   
;
;*****************************************************************************   
TXByte:
  btfss  PIR1,TXIF  ;check that buffer is empty
  goto  TXByte
  movwf  TXREG  ;transmit byte
  return


S_delay:     ; 200 nS per instruction cycle So delay is about 256 * 600 nS = 154 uS
   DECFSZ   D_Count01,F   ;(200 nS)
   GOTO   S_delay     ;(400 nS)
   RETURN

L_delay:     ;Delay about 256 * 154 uS = 39.4 mS
   CALL   S_delay
   DECFSZ   D_Count02,F
   GOTO   L_delay
   RETURN


   end
You should be able to work out what it is doing from the comments without understanding the actual code.
For reading a byte you send a dummy byte to the serial port from the host computer. The program then reads a byte from the RAM chip ant outputs it to the serial port. It then increments the address counter. For writing the byte to be written is sent from the host and it written to the RAM chip and the address counter incremented. From memory I think it resets the address counter to zero if there is no data sent from the host for a certain amount of time. I can post the schematic if it is any interest to you.

Les.
 
Status
Not open for further replies.
Top