PICとRPI4Bのuartシリアル通信  2. spi-sram

[前回](https://qiita.com/retro_C18_4B/items/fa807b8a833c39c9fad5)は、PIC18とRPI4Bのuart通信で、PIC18にｷｬﾗｸﾀｰlcdを追加してRPI4Bより受信した文字列を表示しました。今回は、RPI4Bより大量に連続受信することを想定して、spi-sramをPIC18に追加しました。

![2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3970076/ce4bf90a-6e71-380a-3419-d70fda079b07.png)

配線図です。左下にspi-sramを追加しただけで、他は前回と同じです。下図の写真中の左下部分に小さな8pinチップが確認できます。spi-sramの23LC1024です。同じMicrochip社製です。また配線図には記入していませんでしたが、バイパスコンデンサーには、10μF、0.1μF、1000pFの3連にしています。PICロジックの電源ラインにコイルなどを入れると、PICkit３が拒否します。AC/DCアダプターとコイルの相性で発振しているのかもしれません（詳細不明）。仕方ないのでコンデンサーを増やしました。この3連のコンデンサーは、PICkit３が拒否しません。ただし3連の効果は不明。緑LED は次回のpwm用です。今回の配線図にはありません。

![photo50-80.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3970076/3dea6921-011b-b118-2c16-ed18d1834728.jpeg)


右写真は裏面です。左下部分にspi-sramがあります。PICソケットのピンから、sramソケットのピンまで、0.2sqの単線（撚り線ではなく）で、直接wiringしてハンダ付しています。今回は、9.216MHz×pull4の36.864MHzで動作させていますので、4分周で、spiは9.216Mbpsです。PIC18F26k22を16MHz×pull4の64MHzで動作させると、spiは16Mbpsまで余裕で通信可能です。PICの直ぐ横で信号線の長さが大変短いですから。spi-sramのプログラムは、Microchip社のライブラリーを参考に、no-pointerバージョンのエコノミー版にしています。pointerがあるとエラーが出て、原因がわからなかったので。

RPI4B側のPythonプログラムは、[初回](https://qiita.com/retro_C18_4B/items/e224f302d4185a82fcc5)と同じものを使います。

``` Python
# RPI-0-uart-tx.py
# Serial Console OFF, Serial ON
# RPI4B Buster(x32) Python 3.7.3 (x32) Thonny, and RUN  or 
# RPI4B bookworm(x64) Python 3.11.2 Thonny, and RUN
    
import serial
    
#Open SERIALttyS0
SERIALttyS0 = serial.Serial('/dev/ttyS0', 2304000, timeout=1)
    
#Set SendMessage
SendMessage = "Hello PIC Im Pi.Hello PIC Im Pi."
    
#Send Command
SERIALttyS0.write(SendMessage.encode())
    
#Receive ReturnMessage and print
binary_ReturnMessage = SERIALttyS0.readline()
ReturnMessage = binary_ReturnMessage.decode()
print(ReturnMessage)
    
#Close SERIALttyS0
SERIALttyS0.close()
```

PIC側のプログラムは、spi-sramが増えています。今回も、xc8とC18のプログラムです。
PIC18のXC8プログラムです。

```
//main.c    MPLAB  X IDE v6.00,  XC8 Compiler v2.4.0, (C90),  PICkit3
//pic18f2620 or 26k22
#include <xc.h>
#define _XTAL_FREQ 36864000
#define LED_A0		LATA0
#define lcd_B7		LATB7
#define lcd_B6		LATB6
#define lcd_B5		LATB5
#define lcd_B4		LATB4
#define lcd_stb_E	LATA1
#define lcd_rs		LATA2
#define SPIRAM1024_CS_TRIS		TRISC1
#define SPIRAM1024_CS_IO		LATC1
#define SPIRAM1024_SCK_TRIS		TRISC3
#define SPIRAM1024_SDI_TRIS		TRISC4
#define SPIRAM1024_SDO_TRIS		TRISC5
#define SPIRAM1024_SPI_IF 		SSPIF
#define SPIRAM1024_SSPBUF		SSPBUF
#define SPIRAM1024_SPICON1		SSPCON1
#define SPIRAM1024_SPICON1bits	SSPCON1bits
#define SPIRAM1024_SPICON2		SSPCON2
#define SPIRAM1024_SPISTAT		SSPSTAT
#define SPIRAM1024_SPISTATbits	SSPSTATbits 
#define FXMA_CS0_TRIS			TRISC0
#define FXMA_CS0_IO				LATC0

//2620
#pragma config OSC = HSPLL
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config BORV = 3
#pragma config WDT = OFF
#pragma config WDTPS = 32768
#pragma config CCP2MX = PORTBE
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = ON
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTC = OFF
#pragma config WRTB = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
////26k22
//#pragma config FOSC = HSHP
//#pragma config PLLCFG = ON
//#pragma config PRICLKEN = ON
//#pragma config FCMEN = OFF
//#pragma config IESO = OFF
//#pragma config PWRTEN = ON
//#pragma config BOREN = OFF
//#pragma config BORV = 190
//#pragma config WDTEN = OFF
//#pragma config WDTPS = 32768
//#pragma config CCP2MX = PORTC1
//#pragma config PBADEN = OFF
//#pragma config CCP3MX = PORTB5
//#pragma config HFOFST = OFF
//#pragma config T3CMX = PORTC0
//#pragma config P2BMX = PORTB5
//#pragma config MCLRE = EXTMCLR
//#pragma config STVREN = OFF
//#pragma config LVP = OFF
//#pragma config XINST = OFF
//#pragma config CP0 = OFF
//#pragma config CP1 = OFF
//#pragma config CP2 = OFF
//#pragma config CP3 = OFF
//#pragma config CPB = OFF
//#pragma config CPD = OFF
//#pragma config WRT0 = OFF
//#pragma config WRT1 = OFF
//#pragma config WRT2 = OFF
//#pragma config WRT3 = OFF
//#pragma config WRTC = OFF
//#pragma config WRTB = OFF
//#pragma config WRTD = OFF
//#pragma config EBTR0 = OFF
//#pragma config EBTR1 = OFF
//#pragma config EBTR2 = OFF
//#pragma config EBTR3 = OFF
//#pragma config EBTRB = OFF

static unsigned char vBANK01[256];
static unsigned char vBuffer[32]
near uint8_t i,j,k,p,q,r,s;
near unsigned char asci, cmd, data, data0, Temp1, Temp2;
near uint16_t Wi, Wi_last, num, Wj, Wk, Wp, Wq, Wr, Ws;
near uint32_t Di, Dj, Dk;
// SPIRAM1024
near uint32_t dAddress, dAddreNewHead, dLength, dTotalLengh;
near uint8_t Dummy, SPICON1Save;

//   lcd functions
void LCD__vBuffer32(void);
void LCD__Data(unsigned char data);
void LCD__Command(unsigned char cmd);
void LCD__Command_Upper(unsigned char cmd);
void LCD__Init(void);
void LCD__Clear(void);
void PUT_DATA(void);
void PUT_CMD(void);
//   SPIRAM1024 functions
void SPIRAM1024Init(void);
void SPIRAM1024GetArray(near uint32_t dAddress, near uint32_t dLength);
void SPIRAM1024PutArray(near uint32_t dAddress, near uint32_t dLength);

void main()
{
    //2620
    ADCON0 = 0x00;
    ADCON1 = 0x0F;
    ////26k22
    //ANSELA = 0b00100000 ;		// AN4(RA5)
    //ANSELB = 0b00000000 ;
    //ANSELC = 0b00000000 ;
    TRISA  = 0b00100000 ;		// RA5
    TRISB  = 0b00000000 ;
    TRISC  = 0b00000000 ;
    PORTA  = 0b00000000 ;
    PORTB  = 0b00000000 ;
    PORTC  = 0b00000000 ;

    TRISC = 0x90;
    TXSTA = 0x24;
    RCSTA = 0x90;
    BAUDCON = 0x00;	
    SPBRG = 0x00;

    // sram init
    #if defined(SPIRAM1024_CS_TRIS)                
        SPIRAM1024Init();                            
    #endif    

    // lcd init
    LCD__Init();
    LCD__Command(0x01);
    LCD__Command(0x0c);

    Temp1=0x30;    
    for(i = 0; i < 32; i++)        
    {
        vBANK01[i]=Temp1;
        Temp1++;
    }

    for(i = 0; i < 32; i++)
    {
        vBuffer[i]=vBANK01[i];    
    }
    LCD__vBuffer32();
    __delay_ms(250);
    __delay_ms(250);
    __delay_ms(250);
    __delay_ms(250);

    SPIRAM1024PutArray(0,  32);    //Write

    for(i = 0; i < 32; i++)        
    {
        vBuffer[i]=0x30;
        vBANK01[i]=0x30;
    }
    LCD__vBuffer32();
    __delay_ms(1000);

    SPIRAM1024GetArray(0, 32);    //Read    
    for(i = 0; i < 32; i++)        
    {
        vBuffer[i]=vBANK01[i];    
    }
    LCD__vBuffer32();
    __delay_ms(250);
    __delay_ms(250);
    __delay_ms(250);
    __delay_ms(250);

    FXMA_CS0_TRIS =0;
    FXMA_CS0_IO=0;

    Wi=0;
    Wi_last=31;
    for(; ; )
    {
        if (RCSTAbits.OERR)
        {
            RCSTAbits.CREN = 0;
            Temp1 = RCREG;
            RCSTAbits.CREN = 1;
        }
        if(RCIF==1) 
        {
            vBANK01[Wi]  = RCREG;
            Wi++;
        }
        if(Wi>Wi_last) {  break;  }
   }

    vBuffer[0] =0x48;  // H
    vBuffer[1] =0x45;  // E
    vBuffer[2] =0x4C;  // L
    vBuffer[3] =0x4C;  // L
    vBuffer[4] =0x4F;  // O
    vBuffer[5] =0x20;  // 
    vBuffer[6] =0x50;  // P
    vBuffer[7] =0x69;  // i
    vBuffer[8] =0x20;  // 
    vBuffer[9] =0x49;  // I
    vBuffer[10]=0x6D;  // m
    vBuffer[11]=0x20;  // 
    vBuffer[12]=0x50;  // P
    vBuffer[13]=0x49;  // I
    vBuffer[14]=0x43;  // C
    vBuffer[15]=0x40;  // @
    vBuffer[16]=0x0A;  // LF
    Wi=0;
    Wi_last=17;
    for(; ; )
    {
        //2620
        if(TRMT==1)
        ////26k22
        //if(TXSTAbits.TRMT==1)
        {
            TXREG  =  vBuffer[Wi];
            Wi++;
        }
        if(Wi>Wi_last) {  break;  }
    }

    FXMA_CS0_IO=1;

    for(i = 0; i < 32; i++)
    {
        vBuffer[i]=vBANK01[i]; 
    }

    LCD__vBuffer32();
    __delay_ms(250) ;
    __delay_ms(250) ;
    __delay_ms(250) ;
    __delay_ms(250) ;

    while(1) {

        LED_A0 = 0;
        __delay_ms(250) ;
        LED_A0 = 1;
        __delay_ms(250) ;

     }

}


//  SC1602BS lcd functios  //

void LCD__vBuffer32(void)
{
	uint8_t ii;
	LCD__Clear();
	for(ii = 0; ii < 16; ii++){
		data= vBuffer[ii];
		LCD__Data(data);
	}
	LCD__Command(0xC0);
	for(ii = 16; ii < 32; ii++){
		data= vBuffer[ii];
		LCD__Data(data);
	}
}

void LCD__Data(unsigned char data)
{
	uint8_t jj;
	for(jj=0; jj<=1; jj++)
	{
		if (jj==0)
			data0=data;
		else
			data0=data<<4;
		lcd_B7 = 0;
		lcd_B6 = 0;
		lcd_B5 = 0;
		lcd_B4 = 0;
		if((data0 & 0x80) != 0)
			lcd_B7 = 1;
		if((data0 & 0x40) != 0)
			lcd_B6 = 1;
		if((data0 & 0x20) != 0)
			lcd_B5 = 1;
		if((data0 & 0x10) != 0)
			lcd_B4 = 1;
		PUT_DATA();
	}
	__delay_us(50);
}

void LCD__Command(unsigned char cmd)
{
	uint8_t jj;
	for(jj=0; jj<=1; jj++)
	{
		if (jj==0)
			data0=cmd;
		else
			data0=cmd<<4;
		lcd_B7 = 0;
		lcd_B6 = 0;
		lcd_B5 = 0;
		lcd_B4 = 0;
		if((data0 & 0x80) != 0)
			lcd_B7 = 1;
		if((data0 & 0x40) != 0)
			lcd_B6 = 1;
		if((data0 & 0x20) != 0)
			lcd_B5 = 1;
		if((data0 & 0x10) != 0)
			lcd_B4 = 1;
		PUT_CMD();
	}
	__delay_us(50);
}

void LCD__Command_Upper(unsigned char cmd)
{
	lcd_B7 = 0;
	lcd_B6 = 0;
	lcd_B5 = 0;
	lcd_B4 = 0;
	if((data0 & 0x80) != 0)
		lcd_B7 = 1;
	if((data0 & 0x40) != 0)
		lcd_B6 = 1;
	if((data0 & 0x20) != 0)
		lcd_B5 = 1;
	if((data0 & 0x10) != 0)
		lcd_B4 = 1;
	PUT_CMD();
	__delay_ms(5);
}

void LCD__Clear(void)
{
	LCD__Command(0x01);
	__delay_ms(250);
}

void LCD__Init(void)
{
	__delay_ms(50);
	LCD__Command_Upper(0x30);
	LCD__Command_Upper(0x30);
	LCD__Command_Upper(0x30);
	LCD__Command_Upper(0x20);
	LCD__Command(0x2E);
	LCD__Command(0x08);
	LCD__Command(0x0D);
	LCD__Command(0x06);
	LCD__Clear();
}

void PUT_CMD(void)
{
	lcd_rs  = 0;
	_delay(1);
	lcd_stb_E = 1;
	_delay(4);
	lcd_stb_E = 0;
}

void PUT_DATA(void)
{
	lcd_rs  = 1;
	_delay(1);
	lcd_stb_E = 1;
	_delay(4);
	lcd_stb_E = 0;
}

//// no-pointer spi-sram 23LC1024 functions,  referred from Microchip
// opcodes
#define READ	0x03			// Read data from memory array beginning at selected address
#define WRITE	0x02			// Write data to memory array beginning at selected address
#define RDSR	0x05			// Read Status register
#define WRSR	0x01			// Write Status register
// define
#define PROPER_SPICON1	(0x20)		// SSPEN bit is set, SPI in master mode, FOSC/4, IDLE state is low level

void SPIRAM1024Init(void)
{
	SPIRAM1024_CS_IO = 1;
	SPIRAM1024_CS_TRIS = 0;
	SPIRAM1024_SCK_TRIS = 0;
	SPIRAM1024_SDI_TRIS = 1;	
	SPIRAM1024_SDO_TRIS = 0;
	// Save SPI state (clock speed)
	SPICON1Save = SPIRAM1024_SPICON1;
	SPIRAM1024_SPICON1 = PROPER_SPICON1;
	SPIRAM1024_SPI_IF = 0;
	SPIRAM1024_SPISTATbits.CKE = 1; 
	SPIRAM1024_SPISTATbits.SMP = 0;
	// Set Burst mode
	// Activate chip select
	SPIRAM1024_CS_IO = 0;
	SPIRAM1024_SPI_IF = 0;
	// Send Write Status Register opcode
	SPIRAM1024_SSPBUF = WRSR;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Set status register to 0b01000000 to enable burst mode
	SPIRAM1024_SSPBUF = 0x40;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Deactivate chip select
	SPIRAM1024_CS_IO = 1;
	// Restore SPI state
	SPIRAM1024_SPICON1 = SPICON1Save;
}

void SPIRAM1024GetArray(near uint32_t dAddress, near uint32_t dLength)
{
	if(dLength == 0)
		return;
	// Save SPI state (clock speed)
	SPICON1Save = SPIRAM1024_SPICON1;
	SPIRAM1024_SPICON1 = PROPER_SPICON1;
	// Activate chip select
	SPIRAM1024_CS_IO = 0;
	SPIRAM1024_SPI_IF = 0;
	// Send READ opcode
	SPIRAM1024_SSPBUF = READ;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Send address
	SPIRAM1024_SSPBUF = ((uint8_t*)&dAddress)[2];
	while(!SPIRAM1024_SPI_IF);	
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	//
	SPIRAM1024_SSPBUF = ((uint8_t*)&dAddress)[1];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	//
	SPIRAM1024_SSPBUF = ((uint8_t*)&dAddress)[0];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Read data
	for(Dk = 0; Dk < dLength; Dk++)
	{
		SPIRAM1024_SSPBUF = 0;
		while(!SPIRAM1024_SPI_IF);
		vBANK01[Dk] = SPIRAM1024_SSPBUF;
		SPIRAM1024_SPI_IF = 0;
	}
	// Deactivate chip select
	SPIRAM1024_CS_IO = 1;
	// Restore SPI state
	SPIRAM1024_SPICON1 = SPICON1Save;
}

void SPIRAM1024PutArray(near uint32_t dAddress, near uint32_t dLength)
{
	if(dLength == 0)
		return;
	// Save SPI state (clock speed)
	SPICON1Save = SPIRAM1024_SPICON1;
	SPIRAM1024_SPICON1 = PROPER_SPICON1;
	// Activate chip select
	SPIRAM1024_CS_IO = 0;
	SPIRAM1024_SPI_IF = 0;
	// Send WRITE opcode
	SPIRAM1024_SSPBUF = WRITE;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Send address
	SPIRAM1024_SSPBUF = ((uint8_t*)&dAddress)[2];
	while(!SPIRAM1024_SPI_IF);	
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	//
	SPIRAM1024_SSPBUF = ((uint8_t*)&dAddress)[1];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	//
	SPIRAM1024_SSPBUF = ((uint8_t*)&dAddress)[0];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Write data
	for(Dk = 0; Dk < dLength; Dk++)
	{
		SPIRAM1024_SSPBUF = vBANK01[Dk];
		while(!SPIRAM1024_SPI_IF);
		Dummy = SPIRAM1024_SSPBUF;
		SPIRAM1024_SPI_IF = 0;
	}
	// Deactivate chip select
	SPIRAM1024_CS_IO = 1;
	// Restore SPI state
	SPIRAM1024_SPICON1 = SPICON1Save;
}
```

PIC18のC18プログラムです。

```
// MainDemo.c     MPLAB_IDE_8_92,  MPLAB_C18_3.46,  PICkit3
#include <p18f2620.h>
//#include <p18f26k22.h>
#include <HardwareProfile.h>
#include <GenericTypeDefs.h>
#include <delays.h>

#pragma udata bank1  = 0x100
static unsigned char vBANK01[256];
#pragma udata bank2 = 0x200
static unsigned char vBuffer[32];
//
#pragma udata access my_access
near unsigned char Temp1, Temp2;
near BYTE i, j, k, ii, jj, kk;
near WORD Wi, Wj, Wk;
near DWORD Di, Dj, Dk;
near DWORD dAddress, dAddreNewHead, dLength, dTotalLengh;
near BYTE Dummy, SPICON1Save;

// LCD functions
void LCD__Data(void);
void LCD__vBuffer32(void);
void LCD__Command(void);
void LCD__Clear(void);
void LCD__Init(void);
void PUT_CMD(void);
void PUT_DATA(void);

// Time functions
void TIME50(void);
void TIME5M(void);

// SPI-SRAM1024 functions 
void SPIRAM1024Init(void);
void SPIRAM1024GetArray(near DWORD dAddress, near DWORD dLength);
void SPIRAM1024PutArray(near DWORD dAddress, near DWORD dLength);

#pragma code
void main(void){

	//2620
	ADCON0 = 0x00;
	ADCON1 = 0x0F;
	////26k22
	//ANSELA = 0b00100000 ;		// AN4(RA5)
	//ANSELB = 0b00000000 ;
	//ANSELC = 0b00000000 ;
	TRISA  = 0b00100000 ;		// RA5
	TRISB  = 0b00000000 ;
	TRISC  = 0b00000000 ;
	PORTA  = 0b00000000 ;
	PORTB  = 0b00000000 ;
	PORTC  = 0b00000000 ;

	TRISC = 0x90;
	TXSTA = 0x24;
	RCSTA = 0x90;
	BAUDCON = 0x00;	
	SPBRG = 0x00;

	LCD__Init();

	#if defined(SPIRAM1024_CS_TRIS)				
		SPIRAM1024Init();							
	#endif																		

	Temp1=0x41;	
	for(i = 0; i < 32; i++)		
	{
		vBANK01[i]=Temp1;	
		_asm  INCF  Temp1, 1, 0  _endasm
	}
	for(i = 0; i < 32; i++)
	{
		vBuffer[i]=vBANK01[i];	
	}
	LCD__vBuffer32();
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);

	SPIRAM1024PutArray(0,  32);		//SRAM Write

	for(i = 0; i < 32; i++)			// clear buffer
	{
		vBuffer[i]=0x30;
		vBANK01[i]=0x30;
	}
	LCD__vBuffer32();
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);

	SPIRAM1024GetArray(0, 32);		// SRAM Read	
	for(i = 0; i < 32; i++)		
	{
		vBuffer[i]=vBANK01[i];	
	}
	LCD__vBuffer32();
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);

	LED0_IO = 0;

	FXMA_CS0_TRIS = 0;
	FXMA_CS0_IO = 0;

	for(Wi = 0; Wi < 32; Wi++)
	{
		if (RCSTAbits.OERR) 
		{
			RCSTAbits.CREN = 0;
			Temp1 = RCREG;
			RCSTAbits.CREN = 1;
		}
		_asm BTFSS  PIR1, 5, 0 	_endasm      // RCIF in P1R1 = 5
		_asm BRA -2	       		_endasm
		vBANK01[Wi]  = RCREG;
	}

	vBuffer[0] =0x48;  // H
	vBuffer[1] =0x45;  // E
	vBuffer[2] =0x4C;  // L
	vBuffer[3] =0x4C;  // L
	vBuffer[4] =0x4F;  // O
	vBuffer[5] =0x20;  // 
	vBuffer[6] =0x50;  // P
	vBuffer[7] =0x69;  // i
	vBuffer[8] =0x20;  // 
	vBuffer[9] =0x49;  // I
	vBuffer[10]=0x6D;  // m
	vBuffer[11]=0x20;  // 
	vBuffer[12]=0x50;  // P
	vBuffer[13]=0x49;  // I
	vBuffer[14]=0x43;  // C
	vBuffer[15]=0x40;  // @
	vBuffer[16]=0x0A;  // LF

	for(i = 0; i < 18; i++)		
	{
		_asm BTFSS TXSTA, 1, 0 _endasm      // TRMT in TXSTA = 1
		_asm BRA   -2	       _endasm
		TXREG  =  vBuffer[i];
	}

	FXMA_CS0_IO = 1;

	SPIRAM1024PutArray(0, 256);       		// SRAM Write

	for(i = 0; i < 32; i++)		
	{
		vBuffer[i]=0x30;
		vBANK01[i]=0x30;
	}
	
	dAddreNewHead = 0;
	SPIRAM1024GetArray(dAddreNewHead, 32);	// SRAM Read	
	for(i = 0; i < 32; i++)		
	{
		vBuffer[i]=vBANK01[i];	
	}
	LCD__vBuffer32();
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);
	Delay10KTCYx(250);

	while(1)
	{
		LED0_IO = 0;
		Delay10KTCYx(250);

		LED0_IO = 1;
		Delay10KTCYx(250);
	}	
}



//// lcd functions
void LCD__Data(void)
{
	_asm MOVWF Temp1, 0 	_endasm
	_asm MOVLW 0x0F 		_endasm
	_asm ANDWF PORTB, 1, 0 	_endasm
	_asm MOVF  Temp1, 0, 0 	_endasm
	_asm ANDLW 0xF0 		_endasm
	_asm IORWF PORTB, 1, 0 	_endasm
	PUT_DATA();
	_asm MOVLW 0x0F 		_endasm
	_asm ANDWF PORTB, 1, 0 	_endasm
	_asm SWAPF Temp1, 0, 0  _endasm
	_asm ANDLW 0xF0 		_endasm
	_asm IORWF PORTB, 1, 0 	_endasm
	PUT_DATA();
	TIME50();
}

void LCD__vBuffer32(void)
{
	LCD__Clear();
	for(kk = 0; kk < 16; kk++)		
	{
		Temp1= vBuffer[kk];
		LCD__Data();
	}
	_asm MOVLW 0xC0  	_endasm	
	LCD__Command();			
	TIME50();	
	for(kk = 16; kk < 32; kk++)		
	{
		Temp1= vBuffer[kk];
		LCD__Data();
	}
}

void PUT_CMD(void)
{	
	lcd_rs  = 0;
	Nop();
	lcd_stb = 1;
	Nop();				
	Nop();
	Nop();				
	Nop();	
	lcd_stb = 0;
}

void PUT_DATA(void)
{
	lcd_rs  = 1;
	Nop();
	lcd_stb = 1;
	Nop();				
	Nop();
	Nop();				
	Nop();	
	lcd_stb = 0;
}

void LCD__Command(void)
{						
	//_asm MOVLB 	0  		_endasm		
	_asm MOVWF Temp1, 0 	_endasm		
	_asm MOVLW 0x0F 		_endasm		
	_asm ANDWF PORTB, 1, 0 	_endasm		
	_asm MOVF  Temp1, 0, 0 	_endasm		
	_asm ANDLW 0xF0 		_endasm		
	_asm IORWF PORTB, 1, 0 	_endasm		
	PUT_CMD();
	_asm MOVLW 0x0F 		_endasm
	_asm ANDWF PORTB, 1, 0 	_endasm		
	_asm SWAPF Temp1, 0, 0  _endasm		
	_asm ANDLW 0xF0 		_endasm
	_asm IORWF PORTB, 1, 0 	_endasm		
	PUT_CMD();
	TIME50();				
}

void LCD__Clear(void)
{
	_asm MOVLW 0x01  		_endasm
	LCD__Command();	
	Delay10KTCYx(125);
	Delay10KTCYx(125);
}

void LCD__Init(void)
{	
	TIME5M();
	TIME5M();
	TIME5M();
	TIME5M();
	TIME5M();
	TIME5M();
	TIME5M();
	_asm MOVLW	0x0F	 		_endasm
	_asm ANDWF	PORTB,    1, 0 	_endasm
	_asm MOVLW	0x30			_endasm
	_asm IORWF	PORTB,    1, 0 	_endasm
	PUT_CMD();
	TIME5M();
	PUT_CMD();
	TIME5M();
	PUT_CMD();
	TIME5M();
	_asm MOVLW	0x0F	 		_endasm
	_asm ANDWF	PORTB,    1, 0 	_endasm	
	_asm MOVLW	0x20	 		_endasm
	_asm IORWF	PORTB,    1, 0 	_endasm
	PUT_CMD();
	TIME5M();
	_asm MOVLW 0x2E  		_endasm	
	LCD__Command();	
	_asm MOVLW 0x08 		_endasm
	LCD__Command();	
	_asm MOVLW 0x0D  		_endasm
	LCD__Command();	
	_asm MOVLW 0x06  		_endasm
	LCD__Command();	
	LCD__Clear();
}

//  about 50usec at 40MHz
void TIME50(void)
{
	_asm MOVLW	0x63		_endasm
	_asm MOVWF	ii, 0		_endasm 
	_asm NOP				_endasm
	_asm NOP				_endasm
	_asm DECFSZ	ii, 1, 0	_endasm	
	_asm BRA	-4			_endasm
}

//  about 5msec at 40MHz
void TIME5M(void)
{
	_asm MOVLW	0x64		_endasm
	_asm MOVWF	jj, 0		_endasm
	TIME50();
	_asm DECFSZ	jj, 1, 0	_endasm
	_asm BRA	-3			_endasm
}

//// no-pointer spi-sram (23LC1024) functions : referred from Microchip
// opcodes
#define READ	0x03			// Read data from memory array beginning at selected address
#define WRITE	0x02			// Write data to memory array beginning at selected address
#define RDSR	0x05			// Read Status register
#define WRSR	0x01			// Write Status register
// define
#define PROPER_SPICON1	(0x20)	// SSPEN bit is set, SPI in master mode, FOSC/4, IDLE state is low level

void SPIRAM1024Init(void)
{
	SPIRAM1024_CS_IO = 1;
	SPIRAM1024_CS_TRIS = 0;
	SPIRAM1024_SCK_TRIS = 0;		// SCK pin    output
	SPIRAM1024_SDI_TRIS = 1;		// SDI pin     input
	SPIRAM1024_SDO_TRIS = 0;		// SDO pin    output
	// Save SPI state (clock speed)
	SPICON1Save = SPIRAM1024_SPICON1;
	SPIRAM1024_SPICON1 = PROPER_SPICON1;
	SPIRAM1024_SPI_IF = 0;
	SPIRAM1024_SPISTATbits.CKE = 1; 	// Transmit data on rising edge of clock
	SPIRAM1024_SPISTATbits.SMP = 0;		// Input sampled at middle of data output time
	// Set Burst mode
	// Activate chip select
	SPIRAM1024_CS_IO = 0;
	SPIRAM1024_SPI_IF = 0;
	// Send Write Status Register opcode
	SPIRAM1024_SSPBUF = WRSR;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Set status register to 0b01000000 to enable burst mode
	SPIRAM1024_SSPBUF = 0x40;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Deactivate chip select
	SPIRAM1024_CS_IO = 1;
	// Restore SPI state
	SPIRAM1024_SPICON1 = SPICON1Save;
}

#pragma tmpdata SPIRAM1024
void SPIRAM1024GetArray(near DWORD dAddress, near DWORD dLength)
{
	// Ignore operations when the destination is NULL or nothing to read
	if(vBANK01 == NULL)
		return;
	if(dLength == 0)
		return;
	// Save SPI state (clock speed)
	SPICON1Save = SPIRAM1024_SPICON1;
	SPIRAM1024_SPICON1 = PROPER_SPICON1;
	// Activate chip select
	SPIRAM1024_CS_IO = 0;
	SPIRAM1024_SPI_IF = 0;
	// Send READ opcode
	SPIRAM1024_SSPBUF = READ;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Send address
	SPIRAM1024_SSPBUF = ((BYTE*)&dAddress)[2];
	while(!SPIRAM1024_SPI_IF);	
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	//
	SPIRAM1024_SSPBUF = ((BYTE*)&dAddress)[1];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	//
	SPIRAM1024_SSPBUF = ((BYTE*)&dAddress)[0];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Read data
	for(Dk = 0; Dk < dLength; Dk++)
	{
		SPIRAM1024_SSPBUF = 0;
		while(!SPIRAM1024_SPI_IF);
		vBANK01[Dk] = SPIRAM1024_SSPBUF;
		SPIRAM1024_SPI_IF = 0;
	}
	// Deactivate chip select
	SPIRAM1024_CS_IO = 1;
	// Restore SPI state
	SPIRAM1024_SPICON1 = SPICON1Save;
}

void SPIRAM1024PutArray(near DWORD dAddress, near DWORD dLength)
{
	// Ignore operations when the source data is NULL
	if(vBANK01 == NULL)
		return;
	if(dLength == 0)
		return;
	// Save SPI state (clock speed)
	SPICON1Save = SPIRAM1024_SPICON1;
	SPIRAM1024_SPICON1 = PROPER_SPICON1;
	// Activate chip select
	SPIRAM1024_CS_IO = 0;
	SPIRAM1024_SPI_IF = 0;
	// Send WRITE opcode
	SPIRAM1024_SSPBUF = WRITE;
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Send address
	SPIRAM1024_SSPBUF = ((BYTE*)&dAddress)[2];
	while(!SPIRAM1024_SPI_IF);	
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	////
	SPIRAM1024_SSPBUF = ((BYTE*)&dAddress)[1];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	////
	SPIRAM1024_SSPBUF = ((BYTE*)&dAddress)[0];
	while(!SPIRAM1024_SPI_IF);
	Dummy = SPIRAM1024_SSPBUF;
	SPIRAM1024_SPI_IF = 0;
	// Write data
	for(Dk = 0; Dk < dLength; Dk++)
	{
		SPIRAM1024_SSPBUF = vBANK01[Dk];
		while(!SPIRAM1024_SPI_IF);
		Dummy = SPIRAM1024_SSPBUF;
		SPIRAM1024_SPI_IF = 0;
	}
	// Deactivate chip select
	SPIRAM1024_CS_IO = 1;
	// Restore SPI state
	SPIRAM1024_SPICON1 = SPICON1Save;
}
#pragma tmpdata
```




C18用のHardwareProfile.hです。
```
// HardwareProfile.h MPLAB_IDE_8_92, MPLAB_C18_3.46
#ifndef __HARDWARE_PROFILE_H
#define __HARDWARE_PROFILE_H
// Clock frequency value.  This value is used to calculate Tick Counter value
	#define GetSystemClock()		(36864000ul)		// 9.216MHz Crystal,  PLL x4
	#define GetInstructionClock()	(GetSystemClock()/4)
	#define GetPeripheralClock()	GetInstructionClock()
// pins
	// I/O pins
	#define		LED0_TRIS		(TRISAbits.TRISA0)
	#define 	LED0_IO			(LATAbits.LATA0)
	#define 	FXMA_CS0_TRIS	(TRISCbits.TRISC0)
	#define 	FXMA_CS0_IO		(LATCbits.LATC0)
// lcd
	#define		lcd_port		(LATB)
	#define 	lcd_stb 		(LATAbits.LATA1)	//E
	#define 	lcd_rs  		(LATAbits.LATA2)
// spi-sram 
    #define 	SPIRAM1024_CS_TRIS		(TRISCbits.TRISC1)
	#define 	SPIRAM1024_CS_IO		(LATCbits.LATC1)
	#define 	SPIRAM1024_SCK_TRIS		(TRISCbits.TRISC3)
	#define 	SPIRAM1024_SDI_TRIS		(TRISCbits.TRISC4)
	#define 	SPIRAM1024_SDO_TRIS		(TRISCbits.TRISC5)
	#define 	SPIRAM1024_SPI_IF		(PIR1bits.SSPIF)
	#define 	SPIRAM1024_SSPBUF		(SSPBUF)
	#define 	SPIRAM1024_SPICON1		(SSPCON1)
	#define 	SPIRAM1024_SPICON1bits	(SSPCON1bits)
	#define 	SPIRAM1024_SPICON2		(SSPCON2)
	#define 	SPIRAM1024_SPISTAT		(SSPSTAT)
	#define 	SPIRAM1024_SPISTATbits	(SSPSTATbits)
#endif
```


C18用のConfiguration Bitsは、[初回](https://qiita.com/retro_C18_4B/items/e224f302d4185a82fcc5)と同じです。

次回はPIC側で、LEDの明るさを、pwmを使って変えてみます。