Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

another SPI-problem


  • Please log in to reply
15 replies to this topic

#1 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 05 July 2011 - 09:29 AM

Hi community,

I've a problem with connecting an ADE7758 PolyPhaseMeter via SPI to my NetDuino.

My task is to build a smart meter, so getting data (Voltage and Current) from the 7758 and save it on SD-Card of my NetDuino.

My code is:
public static string Connect()
        {
            
            try
            {
                SPIConfig = new SPI.Configuration(Pins.GPIO_PIN_D4,         // CS port
                                                    false,                  // CS active when LOW
                                                    1,                      // CS Setup time in ms
                                                    1,                      // CS Hold time in ms
                                                    false,                  // Clock idle is low
                                                    false,                   // Data is sampled on falling edge of clock
                                                    10,                     // Clock rate in KHz
                                                    SPI_Devices.SPI1);
                SPIConnection = new SPI(SPIConfig);
                
                Thread.Sleep(1000);

                Get(0x7F, 8); //for testing if something is coming back
            }
            catch (Exception ex) { return ex.ToString(); }
            return "ok";

        }

public static string Get(byte register, ushort bit)
        {
            
                byte[] readBuff;
                if (bit <= 8) readBuff = new byte[] { 0x00 };
                else if (bit <= 16) readBuff = new byte[] { 0x00, 0x00 };
                else if (bit <= 24) readBuff = new byte[] { 0x00, 0x00, 0x00 };
                else return "bit-Fehler";

                
                SPIConnection.WriteRead(new byte[] { register }, readBuff);

                long result = 0x00;


                if (readBuff.Length == 1) result = readBuff[0];
                else if (readBuff.Length == 2) result = (readBuff[0] << 8) + readBuff[1];
                else if (readBuff.Length == 3) result = (readBuff[0] << 16) + (readBuff[1] << 8) + readBuff[2];

                return result.ToString();
        }

The data, I should get back from ADE, is 1 to 3 bytes with the register value of the onboard ADCs,
but geting back only 255 (or 255 255 or 255 255 255)

All cable connections are ok, I've checked twice.
In the SPIConfig I've changed every parameter, but nothing changes on the result.

Sitting here for 3 days looking for the failure.

An idea?

Thanks for help.

#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 05 July 2011 - 09:48 AM

Hi jackoneil, Welcome to the boards :) You get 255 when there is no value at all. Try changing the bools (set chip select to true, clock idle to true, just check what it does). I can't find a datasheet that quickly to see what the values should be, but it's boolean so there aren't many options ;) Ps. the name; a stargate reference?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 05 July 2011 - 10:20 AM

Hi Stefan, thanks for fast answering. I've changed all parameters (also the bools), but nothing changes. That was my first idea for the problem, because I don't really understand what the data sheet means. It says for reading data: "All remaining bits of register data are shifted out on subsequent SCLK rising edges. The serial interface enters communications mode again as soon as the read is completed. The DOUT logic output enters a high impedance state on the falling edge of the last SCLK pulse." and for writing data: "The ADE7758 starts shifting in the register data on the next falling edge of SCLK. All remaining bits of register data are shifted in on the falling edge of the subsequent SCLK pulses" So what I've to choose falling or rising edge? I've linked the data sheet at the beginning of my first post. Is it possible that the IC is damaged? I forgot to say, it's my first time using SPI, so no experiences with it. P.S. of course, stargate fan for many years *g*

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 05 July 2011 - 10:30 AM

A damaged IC is always one of the possibilities, but lets not jump to conclusions yet ;) I always have a hard time understanding datasheets as well, too much information and I only need a portion of it. I found the datasheet here; http://www.analog.co...ets/ADE7758.pdf As far as I understand it clock idle should be low (false). Ohhh but I see it now! You're using pin 4 as Chip Select, there's a timing bug with that one. Could you try another chip select pin? The bug is already confirmed and working on a solution. See also: http://forums.netdui...t-a-high-speed/ I also posted a work-around in that thread that will make pin 4 work, but somewhat slower. Ps. me and my wife are stargate geeks as well :) She often compares me to Rodney McKay. Not sure if it's a positive thing though :D
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 05 July 2011 - 10:49 AM

I have read the issue about the timing problem (also all other posts on SPI in this forum), so I used Pin3, and reduced the speed from 1000 to 10kHz; but it's almost the same. P.S. hm, Rodney is a very ... hm... interesting person^^ but I'm more a fan of good old commando sg1

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 05 July 2011 - 10:57 AM

Just to be sure, Netduino pin 3 -> ADE7758 pin 21 Netduino pin 11 -> ADE7758 pin 24 Netduino pin 12 -> ADE7758 pin 22 Netduino pin 13 -> ADE7758 pin 23 am I correct?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#7 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 05 July 2011 - 11:07 AM

hm, I think it should be: Netduino pin 3 -> ADE7758 pin 21 Netduino pin 11 -> ADE7758 pin 22 Netduino pin 12 -> ADE7758 pin 24 Netduino pin 13 -> ADE7758 pin 23

#8 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 05 July 2011 - 11:36 AM

Hmm lets look at both specs...

Netduino specs are very clear imo:
digital pins 11-13: SPI MOSI, MISO, SPCK [source]
Pin 11 MOSI is Master Out, Slave In
Pin 12 MISO is Master In, Slave Out
Pin 13 SPCK is SPI Clock
The Netduino is the master, the IC is the slave.

Page 10 of the datasheet of the IC says:
Pin 22 Data Input for the Serial Interface. Data is shifted in at this pin on the falling edge of SCLK (see the Serial Interface section).
Pin 23 Serial Clock Input for the Synchronous Serial Interface. All serial data transfers are synchronized to this clock (see the Serial Interface section). The SCLK has a Schmidt-trigger input for use with a clock source that has a slow edge transition time, for example, opto-isolator outputs.
Pin 24 Data Output for the Serial Interface. Data is shifted out at this pin on the rising edge of SCLK. This logic output is normally in a high impedance state, unless it is driving data onto the serial data bus (see the Serial Interface section).

I read that as:
Pin 22: MOSI
Pin 24: MISO

So you're right :)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#9 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 05 July 2011 - 11:44 AM

Hi "jackoneill" (sorry, but I don't know stargate). The pinout wiring is right. Also, I believe easier in Santa Claus instead of the damaged chips. Damaging a chip is often an effort. Firstly, I'd use two equally-sized buffers in the WriteRead method. Not sure will solve it, but I'd like much more. Secondly, I admit there's a strange description on the datasheet: the data is shifted in on the falling edge of the clock, and is shifted out on the rising edge...never seen! However, the SPI config looks okay. Do you own a scope to monitor the SPI lines? Cheers PS: that chip looks *VERY* interesting. Where did you find it? How much did you pay for it?
Biggest fault of Netduino? It runs by electricity.

#10 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 06 July 2011 - 05:23 AM

Hi Mario, thanks for your answer, I'll scope it this morning, so see whats going on. I've got two samples of the chip by Analog Devices directly, took 3 weeks of shipping. There is also an distributor in Poland which sells it for about 15€. Don't know the name at the moment, but I'll look for it.

#11 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 06 July 2011 - 09:38 AM

hm, the NetDuino works correct, sending what I want and giving out what it has received

But it sends (MOSI/SCLK) only with 0,5V in stead of full 3V and gets (MISO) with about 0,3V.

I'll change the Chip and if it doesn't help, I'll try a level converter.

Posted Image

Chan1: SClk
Chan2: MOSI

#12 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 06 July 2011 - 09:44 AM

So pin 13 on the netduino only gives 0,5V? That's not right at all. It should give 3.3V as far as my knowledge goes. What kind of power source you use and how is it connected to the Netduino? How did you measure the voltages? A regular voltage meter won't do, since it can't handle the speed of the signal.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#13 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 06 July 2011 - 09:52 AM

hm, at the moment it's connected via USB directly to my computer (without USB-hubs in between) I added a picture in the post before.

#14 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 06 July 2011 - 03:48 PM

But it sends (MOSI/SCLK) only with 0,5V in stead of full 3V and gets (MISO) with about 0,3V.

Ah interesting.

MOSI and SCLK are outgoing, where the Netduino defines the voltage.
MISO is incoming, where the chip defines the voltage.

Perhaps the chip itself doesn't get enough power. If I read the datasheet correctly, pins 3 and 4 on the IC should get +5V. Do you have a power source connected to the IC itself?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#15 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 07 July 2011 - 03:59 AM

The levels on the picture are apparently wrong because probably the probes are set on x10, thus the 1/10 attenuation. The chip is allowed to work either with +3.3V levels, or +5V levels. So there's no need of a level converter. The source code (Get function) above has an error. The "WriteRead" method transfers simultaneously the outgoing and the incoming bytes. The first byte is always a command (so toward the ADE), then there are one or more data from the ADE. When you compose the "result" value, you should skip the first byte read, because it is shifted in during the command transfer. It has no reliability. The picture you attached shows the clock and MOSI, I suppose. If so, the outgoing command is not 0x7F as the code indicates. I'd like to know whether the first byte (the command) matches the bit pattern of the MOSI. I'd like to see another picture showing both the clock and the MISO lines. In this case, please specify which is the command sent. Be sure to issue a command to the ADE which is NOT depending to the chip config. I mean to read something like the status or whatever else, always readable without any previous command sequence. Last question: the ADE is fully powered and connected as in the eval circuit? I mean, it should give some correct (and reliable) data, or it is left floating thus its readings are random values? Cheers
Biggest fault of Netduino? It runs by electricity.

#16 jackoneil

jackoneil

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationGermany, Karlsruhe

Posted 08 July 2011 - 06:13 AM

Thanks for your help, Mario.

The problem was a damaged chip and to skip the first back coming byte.

If there is interesst, I found the chip on ebay -> http://cgi.ebay.com/...e=STRK:MEWAX:IT




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.