Things used in this project

Hardware components:
IngenuityMicro Electron
×1
Adafruit HT16K33 7-Segment LED Display
×1
GHI Electronics Potentiometer 1.2
×1
GHI Gadgeteer Breakout Module
×1
Software apps and online services:
Nano framework logo 500 trans a1otqnkp8q
nanoFramework Visual Studio extension
nanoFramework I2C Package
nanoFramework ADC Package
Microsoft Visual Studio 2017 (15.6.4)

Code

ADC with nanoFrameworkC#
Source code to demo the use of ADC and I2C
using System;
using System.Threading;
using System.Diagnostics;

using Windows.Devices.Adc;
using Windows.Devices.I2c;

namespace ADC
{
    public class Program
    {
        // The array containing the digital numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
        static readonly byte[] numbertable = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F };

        // The 4 digits 7-Segment display from Adafruit
        private static I2cDevice _ht16k33;

        public static void Main()
        {
            try
            {
                // Init the 4-Digits 7-Segment Display
                _ht16k33 = I2cDevice.FromId("I2C1", new I2cConnectionSettings(0x70) { BusSpeed = I2cBusSpeed.StandardMode });
                _ht16k33.Write(new byte[] { 0x21, 0x00 });
                _ht16k33.Write(new byte[] { 0x81, 0x00 });
                _ht16k33.Write(new byte[] { 0xE5, 0x00 });

                // Init the ADC
                AdcController adc = AdcController.GetDefault();
                AdcChannel ac4 = adc.OpenChannel(0);

                int val4, d1, d2, d3, d4 = 0;
                
                // Oh no, not again, man what a day I'm having
                for (; ; )
                {
                    // Get the value
                    val4 = ac4.ReadValue();

                    // Calc the individual digits
                    d1 = val4 / 1000;
                    d2 = (val4 - (d1 * 1000)) / 100;
                    d3 = (val4 - (d1 * 1000) - (d2 * 100)) / 10;
                    d4 = (val4 - (d1 * 1000) - (d2 * 100) - (d3 * 10));

                    // Show the value on display
                    _ht16k33.Write(new byte[] {
                                0x00, numbertable[d1],
                                0x00, numbertable[d2],
                                0x00, (byte)0x00,
                                0x00, numbertable[d3],
                                0x00, numbertable[d4]
                    });

                    // Very slow sampling rate 
                    Thread.Sleep(250);
                }
            }
            catch (Exception ex)
            {
                // Do whatever pleases you with the exception caught
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Credits

Nanoframework 256 hdm1m5cjfz
Peter Wessel
2 projects • 4 followers
Contributor for nanoFramework organisation, long time developer, started with a Motorola 6800 in machine code.
Contact

Comments

Add projectSign up / Login