Required downloads :

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. 

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 :

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 :

 

Then declare our Bargraph object :

 

In the main() method, we will now add the initialisation sequence and working code :

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 :

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 :

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.

Screen Shot 071

This step needs to be done only once at the beginning of the debugging session.

You are now ready to : Compile and run !

If you want to add other Click boards, then you simply have to “Add existing item” to your project and select the corresponding driver’s source code. Yes, it’s that easy !

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 :

 

Coding drivers for MBN is (almost) as easy as coding NETMF applications !

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” :

ScreenShot753

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 :

As you can see, the template also comes with XML comments ready. This will be useful for users as VS auto-completion will use this information. At the beginning of the class, we declare the DemoEventFired event that will be raised in the MethodWithEvent() method. If you don’t need events, you can delete this line and the code in the method.

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 :

Also, since it’s using PWM, we have to declare some (private) variables : PWM needs that a reference to Microsoft.SPOT.Hardware.PWM be added to the project. The PWMLevel variable will hold the current PWM value as we will use a property to get/set the leds brightness.

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 :

The CS pin and the SPI module are attached to the socket passed to the constructor, so you don’t need to know the exact pin number/name on the chip and simply add “Socket.Cs” (for the CS pin).

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 :

 

Now, the method to light on/off some leds :

The parameter NbBars is the number of bar to light on. Fill will indicate if the preceding leds will also be lit or not.  

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 😉

Help us