我正在使用 PIC24FJ128GB202 我想与不需要响应的从设备通信。所以配置如下
我已将代码编写为
/*
* File: main.c
* Author: Nikhil
*
* Created on September 18, 2015, 2:59 PM
*/
#include <stdio.h>
#include <stdlib.h>
#define FCY 16000000UL
#include <xc.h>
#include <libpic30.h>
#include<GenericTypeDefs.h>
#include <p24FJ128GB202.h>
_CONFIG4(DSWDTPS_DSWDTPS5 & DSWDTOSC_LPRC & DSBOREN_OFF & DSWDTEN_OFF & DSSWEN_OFF & PLLDIV_PLL8X & I2C1SEL_DISABLE & IOL1WAY_OFF)
_CONFIG3(WPFP_WPFP63 & SOSCSEL_OFF & WDTWIN_PS75_0 & BOREN_OFF & PLLSS_PLL_FRC & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPSTARTMEM) //& PLLSS_PLL_FRC
_CONFIG2(POSCMD_NONE & WDTCLK_LPRC & OSCIOFCN_ON & FCKSM_CSDCMD & FNOSC_FRCPLL & ALTRB6_RETAIN & ALTCMPI_CxINC_RX & WDTCMX_WDTCLK & IESO_OFF)//& FNOSC_FRCPLL
_CONFIG1(WDTPS_PS1024 & FWPSA_PR128 & WINDIS_OFF & FWDTEN_OFF & ICS_PGx1 & LPCFG_ON & GCP_OFF & JTAGEN_OFF)
void __attribute__((__interrupt__,__auto_psv__)) _SPI1Interrupt(void);
void SPI_write()
{
PORTAbits.RA2 =0;
SPI1BUFL = 0xAAAA;
}
void SPI_delay()
{
short i;
for(i=0;i<8;i++)
;
}
void SPI_config(){
SPI1CON1Lbits.SPIEN = 0 ;
__builtin_write_OSCCONL(OSCCON & 0xbf); // Unlock PPS
RPOR1bits.RP2R = 7; //SPI SDO1
RPOR1bits.RP3R = 8; //SPI SCK1
SPI1STATLbits.SPIROV =0; //OVERflow Flag reset
SPI1CON1Lbits.MSTEN = 1; //SPI Master
SPI1CON1Lbits.MODE = 1; //16- bit Data transfer
SPI1CON1Lbits.CKE = 0;
SPI1CON1Lbits.CKP =0;
__builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS
SPI1CON1Lbits.SPIEN = 1 ;
}
static inline void
init_io(void)
{
/* Digital Mode */
ANSA = 0;
ANSB = 0;
/* Reset O/P */
LATB = 0;
LATA =0 ;
/* O/D Off */
ODCA = 0;
ODCB = 0;
/* Define Outpot port */
TRISB = 0 ;
TRISA =0 ;
}
int main (void)
{
CLKDIVbits.CPDIV = 0x01;
init_io();
SPI_config();
//////////////////////////////// SPI Interrupt ////////////////////////////////
_SPI1IP = 1;
_SPI1IF = 0;
SPI1IMSKLbits.SRMTEN = 0x01;
_SPI1IE =1;
//////////////////////////////// main code //////////////////////////////////////////
while(1)
{
SPI_write();
__delay_ms(1);
}
return 0;
}
void __attribute__((__interrupt__, __auto_psv__)) _SPI1Interrupt(void)
{
// Clear SPI1 1 interrupt flag
_SPI1IF = 0;
PORTAbits.RA2 =1;
}
我正在正确接收时钟和数据。但是端口 A(从选择)在 SPI 传输时变为高电平。
我什至尝试过 SPIwrite:
void SPI_write()
{
PORTAbits.RA2 =0;
short temp,temp1;
SPI1BUFL = 0xAAAA;
__delay_ms(1);
temp = SPI1BUFL;
temp1 =SPI1BUFH;
while(!SPI1STATLbits.SPIRBF);
}
并且还尝试在写
Like之前和之后启用禁用 pin
PORTAbits.RA2 =0;
SPI_write();
PORTAbits.RA2 =1;
还是一样的错误
求求求求!!!帮助我对这个错误感到沮丧