Introduction


Virtually all PIC microcontrollers have some banking mechanism to extend addressing to additional memory space. But this external data memory is not directly addressable (except in some high versions of PIC18 devices, which include PIC18F8520, PIC18F6620, etc.). In this post we describe easy to implement external memory interface for PIC microcontrollers. Theoretically most of the PIC microcontrollers can use this setup to extend its RAM space. At the prototyping stage we test this system with PIC16F877, PIC16F887, PIC18F4550 and PIC18F4620 microcontrollers.

Schematic



memlib.png
Sample Schematic for Extended RAM system



With this given schematic user may be able to address RAM space up to 192KB. But this can be extended up to 448KB by adding more SRAM modules to the system.
In this given setup we use 24512 – 64K x 8 CMOS SRAMs as a memory modules. 74HC373 latch is used as 8bit to 16bit address extender and 74HC138 demultiplexer is used as memory bank selector.

Function to write data to the external memory bank


//==============================================================================
// Function to write data to the external memory bank
// Input parameters:
//   bank: Memory band number. Valid range is between 1 to 7. 0 for no bank.
//   address: Memory address to write.
//   value: Value to write
// Return:
//   none
//==============================================================================
void WriteExMem(char bank, unsigned int address, char value) {
  asm {
    clrf TRISD
    clrf TRISB
    movlw 0xe0
    andwf TRISC, 1
    movlw 0xc3
    andwf PORTC, 1
    nop
    nop
    movf FARG_WriteExMem_address+1, 0
    movwf PORTD
    movlw 0xfd
    andwf PORTC, 1
    nop
    nop
    bsf PORTC, 1
    movf FARG_WriteExMem_address, 0
    movwf PORTD
    movf FARG_WriteExMem_value, 0
    movwf PORTB
    nop
    nop
    movlw 0xfe
    andwf PORTC, 1
    movf FARG_WriteExMem_bank, 0
    movwf R0
    rlcf R0, 1
    bcf R0, 0
    rlcf R0, 1
    bcf R0, 0
    movf R0, 0
    iorwf PORTC, 1
    nop
    nop
    bsf PORTC, 0
    movlw 0xe3
    andwf PORTC, 1
  }
}

Function to read data from external memory bank


//==============================================================================
// Function to read data from external memory bank
// Input parameters:
//   bank: Memory band number. Valid range is between 1 to 7. 0 for no bank.
//   address: Memory address to read.
// Return:
//   Data value
//==============================================================================
char ReadExMem(char bank, unsigned int address) {
  asm {
    clrf TRISD
    movlw 0xff
    movwf TRISB
    movlw 0xe0
    andwf TRISC, 1
    movlw 0xc3
    andwf PORTC, 1
    nop
    nop
    movf FARG_ReadExMem_address+1, 0
    movwf PORTD
    movlw 0xfd
    andwf PORTC, 1
    nop
    nop
    bsf PORTC, 1
    movf FARG_ReadExMem_address, 0
    movwf PORTD
    movf FARG_ReadExMem_bank, 0
    movwf R0
    rlcf R0, 1
    bcf R0, 0
    rlcf R0, 1
    bcf R0, 0
    movf R0, 0
    iorwf PORTC, 1
    nop
    nop
    movf PORTB, 0
    movwf R1
    movlw 0xe3
    andwf PORTC, 1
    movf R1, 0
    movwf R0
  }
}

Sample Code to test SRAM system


void main() {
  unsigned int address_;
  char bank = 1;
  char txt_buffer[20];
 
  ADCON1 = 15;
  ADCON0 = 0;
  SSPCON1 = 0;
  CCP1CON = 0;
 
  UART1_Init(4800);
  Delay_ms(100);
  UART_Write_Text("Start Testing...\n");
 
  while(bank < 4)
  {
    sprintf(txt_buffer, "Testing bank %c\n", (bank+48));
    UART_Write_Text(txt_buffer);
    for(address_ = 0; address_ < 65535; address_++)
      WriteExMem(bank, address_, 170);           //170 is a magic number to test
    for(address_ = 0; address_ < 65535; address_++)
      if(ReadExMem(bank, address_)!=170)
      {
        UART_Write_Text(" Test FAIL ");
        sprintf(txt_buffer, "address: %u\n", address_);
        UART_Write_Text(txt_buffer);
        bank = 5;
        break;
      }
    if(bank < 5)
      UART_Write_Text(" Test PASS \n");
    bank++;
  }
  UART_Write_Text("SRAM test completed\n");
  while(1);
 
}

The above listed source code is compatible with MikroC Pro for PIC compiler and it can use with other compilers with some slight modifications.

Project Resources


Project source codes and schematic files

References


PIC18F4620 - 40-Pin Enhanced Flash Microcontroller with 10-Bit A/D and nanoWatt Technology
W24512 - 64K x 8 - High Speed CMOS Static RAM
MAX232 - Dual EIA-232 Driver/Receiver
74HC373 - Octal D-Type Transparent Latches and Edge-Triggered Flip-Flops
74HC138 - 1-of-8 Decoder/Demultiplexer
Microchip's Application Note AN869 - External Memory Interfacing Techniques for the PIC18F8XXX