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

SPI issue: LED flickering when being controlled via MCP4921


  • Please log in to reply
6 replies to this topic

#1 ajoakim

ajoakim

    New Member

  • Members
  • Pip
  • 7 posts

Posted 26 November 2012 - 05:25 AM

Hello All,

I am Having an issue with SPI on Netduino Plus 2, I wrote a small class that manages the 12-bit input for MCP4921 chipset. and for the most part it functions like a charm. I do however get weird flickers when I test the setup on a pair of LEDs (attached Breadboard Diagram.)

here is my code.

Main Program

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace SLAMainboard
{
    class program
    {
        static SPI.Configuration SPI0 = new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, true, false, 10000, SPI_Devices.SPI1);
        static SPI.Configuration SPI1 = new SPI.Configuration(Pins.GPIO_PIN_D9, false, 0, 0, true, false, 10000, SPI_Devices.SPI1);
        public static void Main()
        {
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            SPIFormater SPIform = new SPIFormater();
            SPI SPIbus0 = new SPI(SPI0);
            while (true)
            {
                led.Write(true);
                for (int i = 4095; i > 0; i--)
                {
                    SPIbus0.Config = SPI0;
                    byte[] tmp = new byte[2];
                    tmp = SPIform.SPIformater(i,false,true,false,false);
                    //Debug.Print(i + " - " + SPIform.toString() + " - [" + tmp.GetValue(1) + "][" + tmp.GetValue(0) + "]");
                    SPIbus0.Write(tmp);
                    //Thread.Sleep(10);
                    SPIbus0.Config = SPI1;
                    tmp = SPIform.SPIformater(Abs(4095 - i), false, true, false, false);
                    //Debug.Print(i + " - " + SPIform.toString() + " - [" + tmp.GetValue(1) + "][" + tmp.GetValue(0) + "]");
                    SPIbus0.Write(tmp);
                    //Thread.Sleep(10);
                }
                led.Write(false);
                Thread.Sleep(200);

            }
        }
        public static int Abs(int i)
        {
            if (i < 0)
            {
                return i * -1;
            }
            else
            {
                return i;
            }
        }
    }
}

SPIFormater Class

using System;
using Microsoft.SPOT;


/*****************************************
 * SPI 12-bit value Formatter
 * 
 * Version: 1.0.0.0
 * Created: 11/23/2012
 * Updated: 11/24/2012 
 * 
 * Description: This Class was created to deal with MicroCHIP MCP492X Chip-Sets
 * I have not found a library to help with the input, so I decided to
 * write my own. 
 * 
 * The Code take the an input of integers from 1 - 4095 (12-bits unsigned) and 
 * returns a byte array of two, you do have the option of assigning your own
 * control bits, or just using the default I use. 
 * 
 * 15th bit | AB   -> 1- DACb, 0 DACa
 * 14th bit | BUF  -> 1 - Buffered, 0 - Unbuffered
 * 13th bit | GA   -> 1 - 1x ref Voltage, 0 - 2x Ref Voltage
 * 12th bit | SHDN -> 1 - Output Power Down Control Bit, 0 - Output Buffer Disabled, Out High Impedance
 * 
 * ***************************************/

namespace SLAMainboard
{
    class SPIFormater
    {
        Boolean[] bitArray;
        public byte[] SPIformater(int valueInt, Boolean SHDN, Boolean GA,Boolean BUF,Boolean AB) // Formatter for MicroCHIP MCP492X chip set
        {
            int cntVal = 0; //counter value
            bitArray = new Boolean[] { false, false, false, false, false, false, false, false, false, false, false, false, SHDN, GA, BUF, AB}; //0,0,0,0,0,0,0,0,0,0,0,0,|SHDN,GA,BUF,AB| <-- control bits
            Boolean[] tmpArray = new Boolean[16];
            if (valueInt > 4095 || valueInt < 0) // value has to in the range of 0 - 4095
            {
                return null; // Fail case
            }
            while (valueInt > 0)
            {
                if (valueInt % 2 != 0)
                {
                    bitArray[cntVal] = true; // If remainder of division by 2 is not zero that bit is 1 
                }
                else
                {
                    bitArray[cntVal] = false; // If remainder of division by 2 is zero that bit is 0
                }
                valueInt /= 2; // divide by two, a binary left shift.
                cntVal++; // next array value.
            }
            cntVal = 15;
            for (int i = 0; i < 16; i++) // inverse the array so it would match the MicoCHIP Docs.
            {
                tmpArray[i] = bitArray[cntVal];
                cntVal--;
            }
            bitArray = tmpArray;
            return new byte[] { boolToByte(bitArray, 0), boolToByte(bitArray, 8) }; // return an array of two bytes from array of 16 bits.
        }
        public byte[] SPIformater(int valueInt) //defaults that I used for my project
        {
           return SPIformater(valueInt, true, true, true, false);
        }
        private byte boolToByte(Boolean[] inBool, int index) // takes array Boolean, and generates a byte from its subset.  
        {
            if (inBool.Length - index - 8 < 0)
            {
                return 0;
            }
            byte b = 0;
            for (int i = index; i < index + 8; i++)
            {
                b = (byte)(b << 1);
                if (inBool[i])
                {
                    b |= 1;
                }
            }
            return b;
        }
        public string toString()
        {
            string s = "";
            for (int i = 0; i < bitArray.Length; i++)
            {
                if (bitArray[i] == true)
                {
                    s += 1;
                }
                else
                {
                    s += 0;
                }
            }
            return s;
        }
    }
}

Attached Files



#2 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 11 December 2012 - 01:23 PM

Having the same problem, I think. http://forums.netdui...etduino-plus-2/

#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 January 2013 - 07:39 AM

Hi ajoakim,

I am Having an issue with SPI on Netduino Plus 2, I wrote a small class that manages the 12-bit input for MCP4921 chipset. and for the most part it functions like a charm. I do however get weird flickers when I test the setup on a pair of LEDs (attached Breadboard Diagram.)

Quick follow-up: has this been resolved? The new 4.2.2.1 firmware has several SPI bugfixes in place and all of our testing shows that SPI is working correctly in all 4 modes now. Chris

#4 ajoakim

ajoakim

    New Member

  • Members
  • Pip
  • 7 posts

Posted 17 February 2013 - 12:42 AM

as of 4.2.2.2 SPI is still broken, 



#5 ajoakim

ajoakim

    New Member

  • Members
  • Pip
  • 7 posts

Posted 17 February 2013 - 02:11 AM

Here is a video if you would like to see it in action. 

 



#6 elettrozero

elettrozero

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 07 March 2013 - 03:55 PM

I just bought a ND2+: Is SPI on current version of ND2+ working?

I have the code running well on ND+ but not on the new board it seems not to dialogite with SPI device....

 

 

----

 

Still some issues but it seems that the problem I had was about a bad wiring



#7 ajoakim

ajoakim

    New Member

  • Members
  • Pip
  • 7 posts

Posted 09 March 2013 - 08:56 PM

I don't have a ND+, but I have been running the same circuit on Arduino with no problem, the only reason I switched over to ND2+ was the processor power. 






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.