Mblock使用时钟中断显示4段数码管的值

本文介绍如何使用Mblock配合自定义扩展SEG4_LED,通过时钟中断显示4段共阳数码管的数值。该扩展包含初始化和显示数值两个功能,并详细解释了代码工作原理及注意事项,涉及全局变量、中断服务函数和数码管显示码表的转换。
摘要由CSDN通过智能技术生成
【1.背景介绍】
使用arduino控制4段数码管显示数值,常用方法是在一个循环中不停刷新各数码管的值,使用余晖效应,显示4位数值。但是这种在循环中不停刷新的方式使得主程序很难再做别的事情,一种解决方法是使用定时中断来刷新4段数码管显示的值,这样主程序可以空闲出来处理别的事情。

【2.硬件连接】
这里的4段数码管采用的是共阳数码管,型号:MSQC6412C,采用arduino uno控制板。数码管引脚定义以及和arduino主控帮的链接方式如图1所示。

                      图1
【3.自定义mblock扩展】
这里使用mblock图形环境来编写程序。需要开发相应的扩展模块。此外,本程序中使用MsTimer2时钟产生定时中断,因此需要在arduino IDE中加载MsTimer2标准库。(加载方法:打开mblock自带的arduino IDE,搜搜MsTimer2标准库,然后安装)

自定义的4段数码管扩展只支持arduino模式,不支持Scratch模式,扩展压缩包中包括Seg4_led.s2e文件和src文件夹,src文件夹中包括demo.cpp,demo.h,seg4_led.c,seg4_led.h,相应的文件内容如下:

Seg4_led.s2e:
 
  1. {     
  2.     "extensionName": "SEG4_LED",
  3.     "description": "SEG4_LED for common anode",
  4.     "version": "1.1",
  5.     "author": "yoyoba(stuyou@126.com)",
  6.     "homepage": "youhaidong.cn",
  7.     "sort":0,
  8.     "javascriptURL":"",
  9.       "firmware":"1.0",
  10.       "extensionPort":0,
  11.     "tags" : "makeblock,seg4_led",
  12.     "blockSpecs": [
  13.         [
  14.             "w",
  15.             "seg4_led_init( %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n )",
  16.             "seg4_led_init",
  17.             "2",
  18.             "3",
  19.             "4",
  20.             "5",
  21.             "6",
  22.             "7",
  23.             "8",
  24.             "14",
  25.             "15",
  26.             "16",
  27.             "17",
  28.             "18",
  29.             {
  30.                 "setup":"",
  31.                 "inc":"#include \"demo.h\" \n #include ", //添加<MsTimer2.h>头文件
  32.                 "def":"DemoClass demo; \n",
  33.                 "work":"demo.seg4_led_init({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}); \n",
  34.                 "loop":""
  35.             }
  36.         ],
  37.         [
  38.             "w",
  39.             "seg4_led_display( %n )",
  40.             "seg4_led_display",
  41.             "1234",
  42.             {
  43.                 "setup":"",
  44.                 "inc":"#include \"demo.h\"",
  45.                 "def":"DemoClass demo; \n",
  46.                 "work":"demo.seg4_led_display({0}); \n",
  47.                 "loop":""
  48.             }
  49.         ],
  50.     ],
  51.     "translators":{
  52.         "zh_CN":{
  53.                   "seg4_led_init( %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n )":"4段共阳数码管初始化( %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n )",
  54.                   "seg4_led_display( %n )":"4段共阳数码管显示数值( %n )"
  55.         }
  56.     }
  57. }

demo.h文件内容:
 
  1. #ifndef demo_h
  2. #define demo_h
  3.  
  4. #include <Arduino.h>
  5. ///@brief Class for DemoClass
  6. class DemoClass
  7. {
  8.     public:
  9.     
  10.         DemoClass();
  11.         ~DemoClass();
  12.  
  13.         // 4段LED共阳数码管引脚初始化函数
  14.         // con1~con4为数码管4个共阳引脚使用的arduino数字I/O口;
  15.         // a~dp为数码管a~dp引脚使用的arduino数字I/O口
  16.         void seg4_led_init(int con1,int con2,int con3,int con4,
  17.                             int a,int b,
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值