Required downloads :
- Visual Studio 2012
- or Visual Studio Community 2013 (best choice)
- .NET Micro Framework V4.3 SDK (QFE2-RTM)
- Oberon/Mountaineer drivers
Download and install in this order, then plug your board on a USB port on your computer. Mountaineer drivers should be installed automatically, at this stage.
Now be sure to have the latest MBN Core assemblies installed. [wpdm_file id=81 title=”true” ]
Your environment is now ready for your first program (or driver) !
Please see the tutorials pages to :
For this tutorial, we will suppose that you have a BarGraph Click board and you want to code an application that is using it.
Furthermore it is assumed that you have completed all downloads and install on the First-time users page.
If it’s the fist time you’re using this board, then you may not have the corresponding driver. You should then go to the Downloads page and download it from the list of available drivers. The driver is zipped and contains complete commented out source code, an example application and a .chm help file.
Unzip this driver in a folder for future reference.
Open Visual Studio and create a new MicroFramework “Console application”.
Since the Bargraph driver is using PWM and SPI, you also have to add references to Microsoft.SPOT.Hardware and Microsoft.SPOT.Hardware.PWM :
You will of course need the MBN Core assembly, which is located in c:\Users\Public Documents\MBN (this was installed here by the setup program) :
Locate where you unzipped the BarGraph Click driver and “Add existing item” to your project :
Now, the solution should look like this :
So far, you have referenced the needed assemblies and added the driver’s source code to you project. So go to Program.cs source code. It should look like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using Microsoft.SPOT; namespace MFConsoleApplication3 { public class Program { public static void Main() { Debug.Print(Resources.GetString(Resources.StringResources.String1)); } } } |
You can remove the Debug.Print() statement as we don’t need it in our example.
At this stage, we need to add some Using statements :
1 2 3 |
using System.Threading; using MBN; using MBN.Modules; |
Then declare our Bargraph object :
1 |
private static BarGraphClick _bargraph; |
In the main() method, we will now add the initialisation sequence and working code :
1 2 3 |
_bargraph = new BarGraphClick(Hardware.SocketOne); _bargraph.Bars(6); Thread.Sleep(Timeout.Infinite); |
The first line creates the Bargraph object, which will be plugged on Socket #1 on the mainboard. You can of course choose another socket if you have other Click boards.
It’s now almost done… The Bargraph is ready and only needs to be told how many leds it will light on 😉 Here, we tell it to display 6 bars with the 5 preceding leds being lit.
Should you want only the sixth led lit and not the others, simply use false as second parameter :
1 |
_bargraph.Bars(6, false); |
Last line is the standard way to terminate a NETMF program, telling it to sleep forever.
You’re done !
Now the complete source code should look like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using Microsoft.SPOT; using System.Threading; using MBN; using MBN.Modules; namespace MFConsoleApplication3 { public class Program { private static BarGraphClick _bargraph; public static void Main() { _bargraph = new BarGraphClick(Hardware.SocketOne); _bargraph.Bars(6); Thread.Sleep(Timeout.Infinite); } } } |
Now you will have to tell Visual Studio that the deployment and debugging will be done on the MBN board. To do this, go in the Project properties, select the “.NET Micro Framework” tab and choose “Transport : USB”. Your MBN board should then appear in the device dropdown box below.
This step needs to be done only once at the beginning of the debugging session.
You are now ready to : Compile and run !
Tip :
If you want to easily use our drivers with modules connected on screw terminals, you may create a virtual MBN socket and use it in the driver constructor :
1 2 3 4 5 6 7 8 9 10 11 12 |
var vds = new Hardware.Socket { An = Pin.PC5, Rst = Pin.PB0, Cs = Pin.PE7, Pwm = Pin.PE8, Int = Pin.PE11, Rx = Pin.PC4, Tx = Pin.PE13, Name = "VirtualCharacterDisplaySocket" }; lcd = new CharacterDisplay(vds); |
Since there are some constraints with pins mappings and different checks involved, you should start with one of the two drivers templates, depending on which kind of communication it’s using (I²C or any other).
In the following example, we will use the Bargraph Click board, which is by default a SPI board. Coding for an I²C driver is absolutely identical except for the template, which will of course be the I2C template.
Open Visual Studio and create a new Micro Framework -> MikroBus.Net -> “MBN SPI driver” :
At this point, you can see that the template has generated two source files : Driver.cs and DriverEvents.cs. If you don’t need to generate events in your driver, then you may safely delete the DriverEvents.cs file and remove the reference to the event at the beginning of Driver.cs.
Select the Driver.cs file. The code should look like this :
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
/* * ExampleDriver driver skeleton generated on 11/10/2014 9:41:04 AM * * Initial revision coded by Christophe Gerbier * * References needed : (change according to your driver's needs) * Microsoft.SPOT.Hardware * Microsoft.SPOT.Native * MikroBusNet * mscorlib * * Copyright 2014 MBN * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language governing permissions and limitations under the License. * * Follow the TODO tasks to complete the driver code and remove this line */ //TODO: review the XML comments so that they are applicable to your driver using System; using System.Reflection; using MBN; using MBN.Enums; using MBN.Exceptions; using MBN.Extensions; using Microsoft.SPOT.Hardware; namespace MBN.Modules { //TODO: Add other "Pins used" if needed /// <summary> /// Main class for the ExampleDriver driver /// <para><b>Pins used :</b> Miso, Mosi, Cs, Sck</para> /// </summary> public partial class ExampleDriver : IDriver { // This is a demo event fired by the driver. Implementation is in the BaF.Events namespace below. /// <summary> /// Occurs when a demo event is detected. /// </summary> public event DemoEventEventHandler DemoEventFired = delegate { }; private readonly SPI.Configuration _spiConfig; // SPI configuration /// <summary> /// Initializes a new instance of the <see cref="ExampleDriver"/> class. /// </summary> /// <param name="socket">The socket on which the ExampleDriver module is plugged on MikroBus.Net board</param> public ExampleDriver(Hardware.Socket socket) { try { // Enumerate all pins of the socket used by your driver Hardware.CheckPins(socket, socket.Miso, socket.Mosi, socket.Cs, socket.Sck); // Change the SPI.Configuration parameters according to the chip datasheet _spiConfig = new SPI.Configuration(socket.Cs, false, 15, 10, false, true, 20000, socket.SpiModule); if (Hardware.SPIBus == null) { Hardware.SPIBus = new SPI(_spiConfig); } //TODO: implement the constructor here } // Catch only the PinInUse exception, so that program will halt on other exceptions // Send it directly to the caller catch (PinInUseException) { throw new PinInUseException(); } } /// <summary> /// This is a sample method used by the <see cref="ExampleDriver"/> module. It raises an event. /// </summary> public void MethodWithEvent() { //TODO: code your method here. WARNING : don't forget to use the SPIBus.Write() extension method to pass the SPI configuration !!! Hardware.SPIBus.Write(_spiConfig, new Byte[] { 0x00, 0x01 }); // A temporary event is used to avoid any race condition var tempEvent = DemoEventFired; tempEvent(this, new DemoEventEventArgs(1, false, true)); } /// <summary> /// Gets or sets the power mode. /// </summary> /// <example> This sample shows how to use the PowerMode property. /// <code language="C#"> /// _ExampleDriver.PowerMode = PowerModes.Off; /// </code> /// </example> /// <value> /// The current power mode of the module. /// </value> /// <exception cref="System.NotImplementedException">Thrown if the property is set and the module doesn't support power modes.</exception> public PowerModes PowerMode { get { return PowerModes.On; } set { //TODO: implement the different power modes here if needed. throw new NotImplementedException("PowerModes"); } } /// <summary> /// Gets the driver version. /// </summary> /// <example> This sample shows how to use the DriverVersion property. /// <code language="C#"> /// Debug.Print ("Current driver version : "+_ExampleDriver.DriverVersion); /// </code> /// </example> /// <value> /// The driver version. /// </value> public Version DriverVersion { get { return Assembly.GetExecutingAssembly().GetName().Version; } } /// <summary> /// Resets the module /// </summary> /// <param name="resetMode">The reset mode : /// <para>SOFT reset : generally by sending a software command to the chip</para><para>HARD reset : generally by activating a special chip's pin</para></param> /// <returns></returns> /// <exception cref="System.NotImplementedException">Thrown because this module has no reset feature.</exception> public bool Reset(ResetModes resetMode) { throw new NotImplementedException("Reset"); } } } |
In our example, since we won’t be using events, we will delete the related code and the SPIDriverEvents.cs file. Also, it’s now a good time to rename the SPIDriver.cs file to something more adapted to your driver. Here, we will call it BargraphClick.cs. This is not mandatory, of course, but using a generic name is not really useful.
Back to the driver itself…
You will notice that your driver needs to implement the IDriver interface. This interface is part of the MBN core and tells that the driver will expose some common methods and properties. The IDriver interface exposes the Reset() and PowerMode() methods as well as the DriverVersion property. Users can then check for the driver’s version to enable more advanced features, for example.
In the class constructor, the Hardware.CheckPins() method is used to see if there’s another driver that may already use the same pins as the Bargraph Click board. So it is very important to put the correct pins used, here.
The Bargraph Click board is using SPI pins by default, PWM pin to control brightness and the RST pin, so we have to modify the line accordingly :
1 |
Hardware.CheckPins(socket, socket.Miso, socket.Mosi, socket.Cs, socket.Sck, socket.Pwm, socket.Rst); |
1 2 |
private Double PWMLevel; // Brightness level private PWM Pwm; // Brightness control |
Just to show that the default constructor call can be modified, we will add a parameter for the initial brightness of the leds when the object is created (leds will have full brightness in the example). Now, let’s change the constructor to initialize SPI and PWMLevel as well :
1 2 3 4 5 6 7 8 9 |
public BarGraphClick(Hardware.Socket socket, Double InitialBrightness = 1.0) { SpiConfig = new SPI.Configuration(socket.Cs, false, 50, 0, false, true, 25000, socket.SpiModule); Spi = new SPI(SpiConfig); // Sets initial brightness Pwm = new PWM(socket.PwmChannel, 10000, InitialBrightness, false); Pwm.Start(); PWMLevel = InitialBrightness; } |
So far, we have created the driver’s constructor, which is fine, but useless without some properties/methods to control the bargraph leds ! Let’s then add a property to control brightness :
1 2 3 4 5 6 7 8 9 |
public Double Brightness { get { return PWMLevel; } set { PWMLevel = (value > 1.0) || (value < 0.0) ? 1.0 : value; Pwm.DutyCycle = PWMLevel; } } |
Now, the method to light on/off some leds :
1 2 3 4 |
public void Bars(UInt16 NbBars, Boolean Fill = true) { Spi.Write(new UInt16[1] { (UInt16)((1 << (Fill ? NbBars : NbBars - 1)) - (Fill ? 1 : 0)) }); } |
1 2 3 4 5 |
//Example 1, light first 8 leds : Bargraph.Leds(8); //Example 2, light only the 8th led : Bargraph.Leds(8, false); |
Here you are ! The driver for the Bargraph Click board is done ! At least for basic features. You can download the complete source code of this driver on the Download page of this site.
Final note :
At this point, you may want to add more methods or properties, so that you have a very complete driver. Although this is understandable, be aware that this is a driver, and as such it’s not meant to contain a full application ! Temptation is huge to code an application in a driver, but think at users that don’t need the ExtraFeature(Boolean MyNice Param) ; method. It will consume memory while being useless in most cases. So, our advice would be to keep the driver as small as possible and to allow users to access the internals of the chip if needed. This can be done for example by making public the methods used to access internal registers. Or you can create a separate assembly that contains advanced features, based on the basic driver.
Virtual Sockets ®
Discover how Virtual Sockets can make your life easier when you want to use modules from different manufacturers.
Storage class
Short text about Storage class and a link to the full article.
Available Click boards
- Drivers already coded 72%
We need you !
MikroElektronika is building Click boards faster than we can code the drivers…
If you think you can help us in coding drivers (very easy), then you are only one click away 😉