Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Ade7763 spi register read issue

Status
Not open for further replies.

user19

Junior Member level 2
Joined
Apr 7, 2017
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
209
Hi ,

I am new to microcontroller programming and currently working on SPI communication between PIC24FJ64GA006 and ADE7763. Tried to read the register values of ADE7763 from PIC but not getting the values . Is there any special initialisation needed for ADE7763 ? Could anyone please help me with the missing initialisation issue of DE7763 >


Thanks and Regards
 

Hi ,

We are using MPLAB XIDE v3.5 with XC16 compiler.

Please find the attached code.


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;
}



Thanks
 
Last edited by a moderator:

Is there any special initialisation needed for ADE7763?
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.

How are you operating the PIC24 SPI interface (Compiler, libraries)?

- - - Updated - - -

Two issues:
- Wrong SPI mode. According to datasheet AD7763 expects mode 1 (CKP=0, CKE=0).
- Don't know what's the result of writing 24 bits to 16-bit register 9. Data might be right justified or even ignored.
 
  • Like
Reactions: user19

    user19

    Points: 2
    Helpful Answer Positive Rating
Hi FvM,

I was able to read the registers with CKP=0 and CKE=0 settings now.

But when tried to write a value to one of the registers and read back the value , not getting any output.

Thanks
 

Hi,

not getting any output.
If you do a READ with SPI it is not possible to get no output. The output value will always be 0...255.

Klaus
 

Hi Klaus,

Tired to write into a register with a value and reading back , getting only 0 as output.

Thanks
 

Hi ,

Asso there is 8 bit mode and 16 bit mode in PIC settings .But could anyone please suggest a way to access 24 bit registers?

Thanks
 

It's correct to use 8-bit mode to access 24-bit registers as in the post #2 code. The number of byte read or writes must comply with the register size, which is not the case in post #2.

- - - Updated - - -

Tired to write into a register with a value and reading back , getting only 0 as output.
You don't show the latest code, we can only guess what's wrong there.
 

    V

    Points: 2
    Helpful Answer Positive Rating
A couple of general comments about the code you posted:
- you only need to include the 'xc.h" file and not the 'pic24f' file - the 'xc.h' file will select the correct include file based on the device you selected in the IDE.
- you should write to the LAT register and not the PORT - this applies to the SPI-SS_PORT define
- I would suggest that you set up the pins for SPI2 (and at least SDI2) correctly for input or output.
If you can read register values then you know the SPI interface is working correctly. Which register are you writing to and then trying to read back? Can you show us the code you use to do this?
Susan
 
  • Like
Reactions: user19

    user19

    Points: 2
    Helpful Answer Positive Rating
Thanks for all the support .SPI read write is working now.
 

Hi ,

I am trying to read 24 bit register by setting the PIC in 8 bit mode in the SPI code and sending 3 dummy variable to get 24 clock cycles .But I am getting different values everytime at the output. Could anyone suggest the correct way to read 24 bit registers ?

Thanks
 

Show us the code that does the 24-bit exchange.
What values do you get and what do you expect?
Susan
 

Hi Susan,

The default value of the register is 00 .We are getting values 00 05 2c and 05 and 2c are changing everytime.

Please find the code below.

Code:
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;
}



Thanks
 

Hi,

In your main loop I can't find a "read". All I see is "write".
And you only drive SS LOW, but never HIGH.

Klaus
 

Hi ,

The write function returns the value . SS pin is changed to drive high at the end. But still getting the same result.


Thanks
 

Hi,

WRITE: Ok, this is possible with SPI.
You are reading IRMS. Show us your schematic. Especially current path, reference path and power supplies.
Best if you post a picture of the PCB layout, too.

In what range are the results: min to max?

It´s not unusal that they change a little.

Klaus
 
  • Like
Reactions: user19

    user19

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top