user19
Junior Member level 2
- Joined
- Apr 7, 2017
- Messages
- 22
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 209
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #include "xc.h" #include "p24fxxxx.h" #ifdef __PIC24FJ64GA006__ //Defined by MPLAB when using 24FJ256GB110 device // JTAG/Code Protect/Write Protect/Clip-on Emulation mode // Watchdog Timer/ICD pins select _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) // Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator, // Primary oscillator _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL) #endif // RB2 as SS for SPI slave MCU #define SPI_SS_TRIS TRISBbits.TRISB6 #define SPI_SS_PORT PORTBbits.RB6 unsigned short spiBufT[]={0x89,0x78,0xed,0}; // for demo only unsigned short spiBufR[]={0,0,0,0}; // SPI buffer for Receiving void SPI2Init(void) { //config SPI1 SPI2STATbits.SPIEN = 0; // disable SPI port SPI2STATbits.SPISIDL = 0; // Continue module operation in Idle mode SPI2STATbits.SPIROV = 0; SPI2BUF = 0; // clear SPI buffer IFS2bits.SPI2IF = 0; // clear interrupt flag IEC2bits.SPI2IE = 0; // disable interrupt SPI2CON1bits.DISSCK = 0; // Internal SPIx clock is enabled SPI2CON1bits.DISSDO = 0; // SDOx pin is controlled by the module SPI2CON1bits.MODE16 = 0; // set in 16-bit mode, clear in 8-bit mode SPI2CON1bits.SMP = 0; // Input data sampled at middle of data output time SPI2CON1bits.CKP = 1; // CKP and CKE is subject to change ... SPI2CON1bits.CKE = 0; // ... based on your communication mode. SPI2CON1bits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode SPI2CON1bits.SPRE = 4; // Secondary Prescaler = 4:1 SPI2CON1bits.PPRE = 2; // Primary Prescaler = 4:1 SPI2CON2 = 0; // non-framed mode SPI_SS_PORT = 1; // ALL SPI_SS_PORT PINS ARE CLEARED SPI_SS_TRIS = 0; // set SS as output -RB6 SPI2STATbits.SPIEN = 1; // enable SPI port, clear status } unsigned short writeSPI2( unsigned short data ) { SPI2BUF = data; // write to buffer for TX while(!SPI2STATbits.SPIRBF); // wait for transfer to complete return SPI2BUF; // read the received value }//writeSPI1 int main (void) { unsigned short i; // Disable Watch Dog Timer RCONbits.SWDTEN = 0; // for LED // ODCAbits.ODA6 = 0; TRISEbits.TRISE4 = 0; SPI2Init(); while (1) { for (i=0; i<0xffff; i++); // a simple delay SPI_SS_PORT = 0; spiBufR[0] = writeSPI2(spiBufT[0]); spiBufR[1] = writeSPI2(spiBufT[1]); spiBufR[2] = writeSPI2(spiBufT[2]); spiBufR[3] = writeSPI2(0); SPI_SS_PORT = 1; } return 0; }
No, standard SPI requirements. ADE7763 must be powered and out of reset, SPI must be correctly wired, SPI mode correctly setup on the PIC side.Is there any special initialisation needed for ADE7763?
If you do a READ with SPI it is not possible to get no output. The output value will always be 0...255.not getting any output.
You don't show the latest code, we can only guess what's wrong there.Tired to write into a register with a value and reading back , getting only 0 as output.
unsigned short spiBufT[]={0x16,0x3e,0x0d,0}; // for demo only
void SPI2Init(void)
{
//config SPI1
SPI2STATbits.SPIEN = 0; // disable SPI port
SPI2STATbits.SPISIDL = 0; // Continue module operation in Idle mode
SPI2BUF = 0; // clear SPI buffer
IFS2bits.SPI2IF = 0; // clear interrupt flag
IEC2bits.SPI2IE = 0; // disable interrupt
SPI2CON1bits.DISSCK = 0; // Internal SPIx clock is enabled
SPI2CON1bits.DISSDO = 0; // SDOx pin is controlled by the module
SPI2CON1bits.MODE16 = 0; // set in 16-bit mode, clear in 8-bit mode
SPI2CON1bits.SMP = 0; // Input data sampled at middle of data output time
SPI2CON1bits.CKP = 0; // CKP and CKE is subject to change ...
SPI2CON1bits.CKE = 0; // ... based on your communication mode.
SPI2CON1bits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode
SPI2CON1bits.SPRE = 4; // Secondary Prescaler = 4:1
SPI2CON1bits.PPRE = 2; // Primary Prescaler = 4:1
SPI2CON2 = 0; // non-framed mode
SPI_SS_PORT = 1; // ALL SPI_SS_PORT PINS ARE CLEARED
SPI_SS_TRIS = 0; // set SS as output -RB6
SPI2STATbits.SPIEN = 1; // enable SPI port, clear status
}
int main (void)
{
unsigned short i;
// Disable Watch Dog Timer
RCONbits.SWDTEN = 0;
SPI2Init();
while (1) {
for (i=0; i<0xffff; i++); // a simple delay
SPI_SS_PORT = 0;
spiBufR[0] = writeSPI2(spiBufT[0]);
spiBufR[1] = writeSPI2(0);
spiBufR[2] = writeSPI2(0);
spiBufR[3] = writeSPI2(0);
}
return 0;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?