【物联网智能网关-05】扫描键盘编程设计

.NET Micro Framework模拟器提供了5个模拟按键(上、下、左、右和确认按键),因此通常.NET MF开发板也只须要提供5个按键就能够了,而这5个键,也是直接和CPU的pin脚相连,用GPIO的输入相关的函数就能够操做了,使用很是简单。html

可是对一些特殊的应用,如一些.NET Micro Framework教育箱或一些工业实际用的系统,5个按键显然太少了点。可是若是须要十几个按键,若是直连芯片pin脚,显然占用的资源比较多了,也会致使其它的功能没法使用了,这时候最经常使用的就是扫描键盘了。web


上述扫描键盘的原理图应该是最简单的一种了,复杂一点的,在行或列上,经过一个上拉电阻接VCC。这样,咱们只须要8个pin脚,就能够获取16个按键的信息了。编程

通常实现的思路也比较简单:就是把行(或列)接芯片输出pin脚,把列(或行)接芯片输入pin脚,输出pin脚依次输出低(或高,须要看电路中接的上拉仍是下拉电阻)电平,而后检查输入pin脚的电平变化。若是有变化,那么就说明,该列和该行的按键被按下了。windows

每每这个判断就放在while循环或线程里,不断的去运行。对一些单片而言,若是实现的功能单一,这样作也无可厚非,可是对一个系统平台来讲,若是也这样作,显然对系统的资源占用仍是比较厉害的。ide

因此最好的办法仍是要采用中断的方式,平时的时候不去判断,靠中断触发,一旦中断触发了,而后再启动一轮判断,肯定是哪个按键被按下了。函数

一、扫描方式实现按键获取ui

public class ScanKeypadthis

    {spa

        public event NativeEventHandlerOnInterrupt;.net

 

        OutputPort[]rows = null;

        InputPort[]cols = null;

        publicScanKeypad(Cpu.Pin[]Output_Pins, Cpu.Pin[]Input_Pins)

        {

            rows = newOutputPort[] { newOutputPort(Output_Pins[0], false), new OutputPort(Output_Pins[1], false), new OutputPort(Output_Pins[2], false),new OutputPort(Output_Pins[3], false) };

            cols = newInputPort[] { newInputPort(Input_Pins[0], true, Port.ResistorMode.PullUp), newInputPort(Input_Pins[1], true,Port.ResistorMode.PullUp), newInputPort(Input_Pins[2], true, Port.ResistorMode.PullUp), newInputPort(Input_Pins[3], true, Port.ResistorMode.PullUp) };

 

            ThreadthreadKeypad = new Thread(new ThreadStart(KeypadScan));

            threadKeypad.Start();

        }

 

        voidKeypadScan()

        {

            intkey = -1, oldKey = -1;

            while(true)

            {

                key = -1;

                for(int i = 0; i < rows.Length; i++)

                {

                    rows[i].Write(false);

                    for(int j = 0; j < cols.Length; j++)

                    {

                        if (!cols[j].Read())

                        {

                            key = i *rows.Length + j;

                            break;

                        }

                    }

                    rows[i].Write(true);

                    if(key > -1) break;

                }

                if(key > -1 && key != oldKey)

                {

                    if(OnInterrupt != null) OnInterrupt((uint)key, 1, DateTime.Now);

                    oldKey = key;

                }

                elseif (oldKey > -1 && key == -1)

                {

                    if(OnInterrupt != null) OnInterrupt((uint)oldKey, 0, DateTime.Now);

                    oldKey = -1;

                }

                Thread.Sleep(100);

            }

        }

    }

二、中断方式实现按键获取

public class InterruptKeypad

    {

        public event NativeEventHandlerOnInterrupt;

 

        OutputPort[]rows = null;

        InterruptPort[]cols = null;

        Cpu.Pin[] Pins = null;

        uintkey = 0;

        publicInterruptKeypad(Cpu.Pin[]Output_Pins, Cpu.Pin[]Input_Pins)

        {

            rows = newOutputPort[] { newOutputPort(Output_Pins[0], false), new OutputPort(Output_Pins[1], false), new OutputPort(Output_Pins[2], false),new OutputPort(Output_Pins[3], false) };

            cols = newInterruptPort[Input_Pins.Length];

            Pins = Input_Pins;

            for(int i = 0; i < Input_Pins.Length; i++)

            {

                cols[i] = new InterruptPort(Input_Pins[i],true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);

                cols[i].OnInterrupt += new NativeEventHandler(InterruptKeypad_OnInterrupt);

            }

        }

 

        privateuint GetPinIndex(uintpin)

        {

            for(uint i = 0; i < Pins.Length; i++)

            {

                if(pin == (uint)Pins[i]) returni;

            }

            return0;

        }

 

        voidInterruptKeypad_OnInterrupt(uint data1, uint data2, DateTimetime)

        {

            if(data2 == 1)

            {

                for(int i = 0; i < cols.Length; i++)

                {

                    cols[i].OnInterrupt -= new NativeEventHandler(InterruptKeypad_OnInterrupt);

                }

                //--

                uintcol = GetPinIndex(data1);

                for(int i = 0; i < rows.Length; i++)

                {

                    rows[i].Write(true);

                    if(cols[col].Read())

                    {

                        key = (uint)(i * rows.Length + col);

                        Thread threadKeypad = new Thread(new ThreadStart(KeypadRun));

                        threadKeypad.Start();

                        break;

                    }

                }

                //--

                for(int i = 0; i < rows.Length; i++)rows[i].Write(false);

                for(int i = 0; i < cols.Length; i++)

                {

                    cols[i].OnInterrupt += new NativeEventHandler(InterruptKeypad_OnInterrupt);

                }

            }

        }

 

        voidKeypadRun()

        {

            OnInterrupt(key, 1, DateTime.Now);

            OnInterrupt(key, 0, DateTime.Now);

        }

    }

注意,中断方式中,触发事件必须放在线程里执行,不然会有问题(若是在Winform中使用,最好不用线程,而用winfrom提供的timer,不然就没法直接操做UI了,那就必须用委托方式了,和windows上的编程相似)。

问题点1:因为咱们采用的键盘并无加上拉(或下拉)电阻电路,在最初作这个程序的时候,InputPort(Input_Pins[1],true,Port.ResistorMode.PullUp),最后一个参数,底层并无实现内部上拉,下拉和悬空功能,因此设置是无效的。这就形成了,在按钮没有按下时,输入pin脚的状态是未知的,有时候是1,有时候是0,程序是没法正确运行的。

此外STM32F103和STM32F207的GPIO寄存器差异很大,内部实现上拉、下拉的设置也是不一样的。分别实现后,发现内部上拉正常,设置下拉效果不明显,pin脚的状态仍是未知的。因此咱们实现的程序都设置为上拉。

问题点2:在实现中断方式的扫描键盘的代码的时候,发现PB六、PC0和PB1三个pin脚触发中断异常,可是在NativeSample层面又正常。目前没有发现这三个pin脚有何特别之处,此问题之后待查。因此若是采用中断方式,这三个pin脚不能使用。

注:该问题已修正,须要更新固件(版本V1.6.10以上),另外示例须要参考最新的扫描键盘示例。

以上两种方式都是在应用层面实现的,其实若是扫描键盘的pin脚固定,更好的方式能够在底层用C++实现,而且还能够把8个物理pin脚,虚拟出16个pin脚来,用法和物理的pin脚彻底同样。

官方SimpleWPFApplication示例,是一个比较典型的WPF应用,可是须要5个按键才能操做,咱们的紫藤207系统仅提供了一个物理按钮,因此是没法操做的。接上扫描键盘后,咱们就有可能完整的演示这个示例了,不过因为咱们使用的是扫描键盘,因此原程序没法使用,必须作以下修改才能够。

    public sealed class GPIOButtonInputProvider

    {

        public readonly DispatcherDispatcher;

        privateDispatcherOperationCallback callback;

        privateInputProviderSite site;

        privatePresentationSource source;

 

        publicGPIOButtonInputProvider(PresentationSourcesource)

        {

            this.source= source;

            site = InputManager.CurrentInputManager.RegisterInputProvider(this);

            callback = newDispatcherOperationCallback(delegate(objectreport)

            {

                InputReportArgsargs = (InputReportArgs)report;

                returnsite.ReportInput(args.Device, args.Report);

            });

            Dispatcher = Dispatcher.CurrentDispatcher;

 

            Cpu.Pin[] Output_Pins = { (Cpu.Pin)GPIO_NAMES.PC8,(Cpu.Pin)GPIO_NAMES.PC9, (Cpu.Pin)GPIO_NAMES.PB7,(Cpu.Pin)GPIO_NAMES.PC2 };

            Cpu.Pin[] Input_Pins = { (Cpu.Pin)GPIO_NAMES.PC3,(Cpu.Pin)GPIO_NAMES.PA0, (Cpu.Pin)GPIO_NAMES.PA5,(Cpu.Pin)GPIO_NAMES.PA6 };

           

            InterruptKeypadkey = new InterruptKeypad(Output_Pins,Input_Pins);

            key.OnInterrupt += new NativeEventHandler(key_OnInterrupt);

        }

 

        voidkey_OnInterrupt(uint data1, uint data2, DateTimetime)

        {

            RawButtonActionsaction = (data2 != 0) ? RawButtonActions.ButtonUp: RawButtonActions.ButtonDown;

            RawButtonInputReportreport = new RawButtonInputReport(source,time, GetButton(data1), action);

            Dispatcher.BeginInvoke(callback, new InputReportArgs(InputManager.CurrentInputManager.ButtonDevice,report));

        }

 

        ButtonGetButton(uint data)

        {

            switch(data)

            {

                case2:

                    returnButton.VK_UP;

                case5:

                    returnButton.VK_LEFT;

                case6:

                    returnButton.VK_SELECT;

                case10:

                    returnButton.VK_DOWN;

                case7:

                    returnButton.VK_RIGHT;

            }

            returnButton.None;

        }

    }

把GpioButtonInputProvider.cs里面的程序这样修改后,就可使用了。

效果图以下:

实际运行视频连接以下:

 http://v.youku.com/v_show/id_XNDI3ODU4OTg4.html

从视频能够看出,STM32F207平台运行WPF程序仍是蛮流畅的。

------------------------------------------------------------------------------- 

下载地址:http://www.sky-walker.com.cn/MFRelease/Sample/ScanKey_WPFTest.rar     

MF简介:http://blog.csdn.net/yefanqiu/article/details/5711770

MF资料:http://www.sky-walker.com.cn/News.asp?Id=25

相关文章
相关标签/搜索