Christophe Gerbier
Published © Apache-2.0

HT16K33 with NanoFramework

This demonstrates the use of I2C with nanoFramework.

BeginnerProtip15 minutes995
HT16K33 with NanoFramework

Things used in this project

Hardware components

Adafruit HT16K33 7-Seg led display
×1

Software apps and online services

Visual Studio extension
.NET nanoFramework Visual Studio extension
Microsoft Visual Studio 2017
.NET nanoFramework nanoFramework I2C Package

Story

Read more

Code

I2C with nanoFramework

C#
Source code for the I2C demo.
using System;
using System.Threading;
using Windows.Devices.I2c;

namespace Hackster.io_02
{
    public class Program
    {
        #region Digits
        /// <summary>
        /// Byte values to send to the HT16K33 for displaying digits
        /// </summary>
        public static byte[] Digits =
        {
            0x3F, // '0'
            0x06, // '1'
            0x5B, // '2'
            0x4F, // '3'
            0x66, // '4'
            0x6D, // '5'
            0x7D, // '6'
            0x07, // '7'
            0x7F, // '8'
            0x6F  // '9'
        };
        /// <summary>
        /// Byte value for the minus sign
        /// </summary>
        public const Byte Minus = 0x40;
        #endregion

        private static Byte[] _buffer;
        private static I2cDevice _display;
        private static byte _brightness = 5;

        public static void Main()
        {
            Thread.Sleep(1000);
            var z = 0;

            try
            {
                _buffer = new Byte[11];
                _display = I2cDevice.FromId("I2C1", new I2cConnectionSettings(0x70) { BusSpeed = I2cBusSpeed.FastMode });

                _display.Write(new byte[] { 0x21 });
                Thread.Sleep(10);
                _display.Write(new byte[] { 0x81 });
                _display.Write(new[] { (byte)(0xDF + _brightness) });

                for (int i = 0; i < 2000; i++)
                {
                    Write(i, i > 999);
                    Thread.Sleep(10);
                }

                for (int i = 0; i > -1000; i--)
                {
                    Write(i);
                    Thread.Sleep(10);
                }
            }
            catch (Exception)
            {
                
            }
            finally
            {
                while (true)
                {
                    Thread.Sleep(100);
                }
            }
        }

        /// <summary>
        /// Displays an integer with the optionnal colon.
        /// </summary>
        /// <param name="number">The number to display.</param>
        /// <param name="colon">If set to <c>true</c>, then colon ':' will be displayed.</param>
        /// <example>
        /// <code language="C#">
        ///     // Displays 123
        ///     _disp.Write(123);
        /// 
        ///     // Displays 4567 with colon -> 45:67
        ///     _disp.Write(4567, true);
        /// </code>
        /// </example>
        private static void Write(Int32 number, Boolean colon = false)
        {
            if ((number > 9999) || (number < -999)) { return; }

            _buffer[5] = colon ? (Byte)0xFF : (Byte)0x00;
            if (number >= 0)
            {
                _buffer[9] = Digits[number % 10];
                if (number > 9) { _buffer[7] = Digits[(number / 10) % 10]; }
                if (number > 99) { _buffer[3] = Digits[(number / 100) % 10]; }
                if (number > 999) { _buffer[1] = Digits[(number / 1000) % 10]; }
            }
            else
            {
                _buffer[7] = Minus; // '-'
                _buffer[9] = Digits[-number % 10];
                if (number < -9) { _buffer[7] = Digits[(-number / 10) % 10]; _buffer[3] = Minus; }
                if (number < -99) { _buffer[3] = Digits[(-number / 100) % 10]; _buffer[1] = Minus; }
            }
            _display.Write(_buffer);
        }
    }
}

Credits

Christophe Gerbier

Christophe Gerbier

3 projects • 7 followers

Comments