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