17/01/31 10:20:43.94 ZIqHsoBv.net
>>309
配線はそのままです。結果は次レスにします。
#include <pic18f46k22.h>
#include <stdio.h>
#include <xc.h>
#define _XTAL_FREQ 64000000L
#pragma config FOSC = INTIO67
#pragma config PLLCFG = OFF
#pragma config PRICLKEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = ON
unsigned char SPI1RxData, SPI2RxData;
volatile int SPI1flag = 0, SPI2flag = 0;
volatile long TimerTickCount = 0L;
void putch(unsigned char data) { while ((PIR3 & 0x10) == 0) ; TXREG2 = data; }
void interrupt low_priority ISR_L(void) {
if (PIR1bits.SSP1IF == 1) {__delay_us(2); SPI1RxData = SSP1BUF; PIR1bits.SSP1IF = 0; SPI1flag = 1; }
if (PIR3bits.SSP2IF == 1) {__delay_us(2); SPI2RxData = SSP2BUF; PIR3bits.SSP2IF = 0; SPI2flag = 1; }
if (INTCONbits.TMR0IF) {TMR0L = 131; TimerTickCount++; INTCONbits.TMR0IF = 0; }
}
void main(void) {
char data;
/* MSSP1=SLAVE, MSSP2=MASTER */
TRISA = TRISB = TRISC = TRISD = TRISE = 0;
OSCCON = 0x70; OSCTUNEbits.PLLEN = 1;
RCONbits.IPEN = INTCONbits.GIEH = INTCONbits.GIEL = 1;
PORTDbits.RD6 = 1;
TXSTA2 = 0x04 | 0x20; RCSTA2 = 0x80 | 0x10; BAUDCON2 = 0x08;
SPBRG2 = (unsigned char)(((64000000L / 4L) / 115200L) - 1L);
SPBRGH2 = (unsigned char)((((64000000L / 4L) / 115200L) - 1L) >> 8);
PIE3bits.RC2IE = PIE3bits.TX2IE = 0;
TRISCbits.RC3 = 1; ANSELCbits.ANSC3 = 0; TRISCbits.RC4 = 1;
ANSELCbits.ANSC4 = TRISCbits.RC5 = SSP1STATbits.SMP = 0;
SSP1STATbits.CKE = 1; SSP1CON1bits.WCOL = SSP1CON1bits.SSPOV = 0;
SSP1CON1bits.SSPEN = SSP1CON1bits.CKP = 1; SSP1CON1bits.SSPM = 0b0101;
PIR1bits.SSP1IF = IPR1bits.SSP1IP = 0; PIE1bits.SSP1IE = 1; data = SSP1BUF;
TRISDbits.RD0 = 0; TRISDbits.RD1 = 1;
ANSELDbits.ANSD1 = TRISDbits.RD4 = SSP2STATbits.SMP = 0;
SSP2STATbits.CKE = 1; SSP2CON1bits.WCOL = SSP2CON1bits.SSPOV = 0;
SSP2CON1bits.SSPEN = SSP2CON1bits.CKP = 1; SSP2CON1bits.SSPM = 0b0010; // FOSC/64 = 1MHz
PIR3bits.SSP2IF = IPR3bits.SSP2IP = 0; PIE3bits.SSP2IE = 1; data = SSP2BUF;
INTCON2bits.TMR0IP = 0; T0CONbits.T08BIT = 1; T0CONbits.PSA = 0;
T0CONbits.T0PS = 6; T0CONbits.T0CS = 0; TMR0L = 131; INTCONbits.TMR0IE = T0CONbits.TMR0ON = 1;
while (TimerTickCount < 1000L) ; printf("START!\r\n");
for (int i = 0 ; i < 10 ; i++) {
SPI1flag = SPI2flag = 0;
SSP1BUF = 0x12; // スレーブデータセット
SSP2BUF = 0x34; // マスターデータセット
while (1) {
if (SPI1flag == 1) {printf("From Master Data = %02Xh\r\n", SPI1RxData); SPI1flag = 0; break; }
}
while (1) {
if (SPI2flag == 1) {printf("From Slave Data = %02Xh\r\n", SPI2RxData); SPI2flag = 0; break; }
}
}
while (TimerTickCount != 0L);
}