【STM32 .Net MF开发板学习-06】蜂鸣器和LED数码管显示

 

 

无论是蜂鸣器还是LED数码管显示,其实这二者对代码编写来说没有太大区别,都是GPIO的一个典型应用。红牛开发板有一个蜂鸣器,而EM-STM3210E有一个四位LED数码管,代码都相对简单,不值的为二者单独写一篇博文,所以二者合一以一篇文章来说明,不过两个示例代码是独立的。

先说一下蜂鸣器,查原理图,发现控制该蜂鸣器的管脚是PB2,此外值得一提的是和BOOT1插针的12脚共用,所以如果你把跳线连接12,上电后,蜂鸣器会响。相关代码如下:

   public class Buzzer

    {

        enum GPIO_NAMES {}

        OutputPort BuzzerPort;

        public Buzzer()

        {

            BuzzerPort = new OutputPort((Cpu.Pin)GPIO_NAMES.PB2, false);

        }

        public void Beep()

        {

            Beep(500);

        }

        public void Beep(int millisecond)

        {

            BuzzerPort.Write(true);

            Thread.Sleep(millisecond);

            BuzzerPort.Write(false);

            Thread.Sleep(millisecond);

        }

       //仅仅是模拟,效果较差,估计封装为C++代码,效果会更好些,此外是不是这样实现,存疑

        public void Sound(int hz, int millisecond)

        {

            int t = 1000 / hz;

            for (int i = 0; i < millisecond / t; i++)

            {

                BuzzerPort.Write(true);

                Thread.Sleep(t / 2);

                BuzzerPort.Write(false);

                Thread.Sleep(t / 2);

            }

        }  

     }

使用比较简单,这里代码就不罗列了,可以直接看源程序。

EM-STM3210E开发板有4位数码管,本来觉得挺好的,但是发现四个数码管只能同时显示相同的数字,而不能同时显示不同的数字,所以应用价值就不高了(是不是我用错了?还它提供的示例也是如此)。核心代码类如下:

 public class LED

    {

        enum GPIO_NAMES{……};

        Cpu.Pin[] LED_CS_Pins = new Cpu.Pin[] { (Cpu.Pin)GPIO_NAMES.PB10, (Cpu.Pin)GPIO_NAMES.PB11, (Cpu.Pin)GPIO_NAMES.PB12, (Cpu.Pin)GPIO_NAMES.PB13 };

        Cpu.Pin[] LED_Data_Pins = new Cpu.Pin[] { (Cpu.Pin)GPIO_NAMES.PC0, (Cpu.Pin)GPIO_NAMES.PC1, (Cpu.Pin)GPIO_NAMES.PC2, (Cpu.Pin)GPIO_NAMES.PC3,

                                                 (Cpu.Pin)GPIO_NAMES.PC4, (Cpu.Pin)GPIO_NAMES.PC5, (Cpu.Pin)GPIO_NAMES.PC6, (Cpu.Pin)GPIO_NAMES.PC7};

        OutputPort[] LED_CS;

        OutputPort[] LED_Data;

        byte[] DigitalFlag = new byte[]{0xBF,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};

        public LED()

        {

            LED_CS = new OutputPort[LED_CS_Pins.Length];

            LED_Data = new OutputPort[LED_Data_Pins.Length];

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

            {

                LED_CS[i] = new OutputPort(LED_CS_Pins[i], true);

            }

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

            {

                LED_Data[i] = new OutputPort(LED_Data_Pins[i], true);

            }

        }

        public void Display(int cs,int hex,bool decimalpoint)

        {

            if((cs<0 || cs>3) && cs != 0xFF) return;

            if(hex<0 || hex>15) return;

 

            for (int i = 0; i < 4; i++) LED_CS[i].Write(cs == 0xFF ? false : cs!=i);

            for (int i = 0; i < 7; i++)

            {

                LED_Data[i].Write(!((DigitalFlag[hex]>>i & 0x1)>0));

            }

            LED_Data[7].Write(!decimalpoint);

        }

        //无法同时在四个LED管中显示不同的数字

        public void Display(int num)

        {

            if (num < 0 || num > 9999) return;

            Display(0, num - (num / 10) * 10, false);

            Display(1, num / 10 - (num / 100) * 10, false);

            Display(2, num / 100 - (num / 1000) * 10, false);

            Display(3, num / 1000 - (num / 10000) * 10, false);

        }

    }

运行后的效果图如下:

【STM32 .Net MF开发板学习-06】蜂鸣器和LED数码管显示_第1张图片

 

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

    

 

【低价开发板】http://item.taobao.com/item.htm?id=7117999726 

 

源码下载:http://www.sky-walker.com.cn/yefan/MFV40/SourceCode/Buzzer-LEDTest.rar

 

文章参考: 《.Net Micro Framework 快速入门
中文讨论组:http://space.cnblogs.com/group/MFSoft/

你可能感兴趣的:(.net,.net,职场,framework,stm32,休闲,Micro)