玩玩LED点阵屏(arduino nano)

Posted aarond

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了玩玩LED点阵屏(arduino nano)相关的知识,希望对你有一定的参考价值。

做些记录,特别是led显示左移效果的代码,二进制位的特效函数

 

技术图片

 

unsigned char LedArray1[1][32]=
{
0xff,0xd7,0x83,0xd6,0xc6,0xd4,0xc6,0x82,0xd6,0xba,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x97,0x57,0x81,0x95,0x55,0xd5,0xdf,0xdd,0xe1,0xff,0xff,0xff,0xff,0xff,0xff,
};

const unsigned char LedArray1_hidden[1][32]=
{
0xff,0xd7,0x83,0xd6,0xc6,0xd4,0xc6,0x82,0xd6,0xba,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x97,0x57,0x81,0x95,0x55,0xd5,0xdf,0xdd,0xe1,0xff,0xff,0xff,0xff,0xff,0xff,
};

#define LEDARRAY_D 2
#define LEDARRAY_C 3
#define LEDARRAY_B 4
#define LEDARRAY_A 5
#define LEDARRAY_G 6
#define LEDARRAY_DI 7
#define LEDARRAY_CLK 8
#define LEDARRAY_LAT 9

void led_init(){
        pinMode(LEDARRAY_D, OUTPUT);
        pinMode(LEDARRAY_C, OUTPUT);
        pinMode(LEDARRAY_B, OUTPUT);
        pinMode(LEDARRAY_A, OUTPUT);
        pinMode(LEDARRAY_G, OUTPUT);
        pinMode(LEDARRAY_DI, OUTPUT);
        pinMode(LEDARRAY_CLK, OUTPUT);
        pinMode(LEDARRAY_LAT, OUTPUT);
}

unsigned char led_Display_Buffer[2];

void led_display(const unsigned char dat[][32])
{
    unsigned char i;

    for( i = 0 ; i < 16 ; i++ )
    {
        digitalWrite(LEDARRAY_G, HIGH);        //更新数据时候关闭显示。等更新完数据,打开138显示行。防止重影。

        led_Display_Buffer[0] = dat[0][i];
        led_Display_Buffer[1] = dat[0][i+16];

        led_send(led_Display_Buffer[1]);
        led_send(led_Display_Buffer[0]);

        digitalWrite(LEDARRAY_LAT, HIGH);                    //锁存数据
        delayMicroseconds(1);

        digitalWrite(LEDARRAY_LAT, LOW);
        delayMicroseconds(1);

        led_scan_line(i);                            //选择第i行

        digitalWrite(LEDARRAY_G, LOW);

        delayMicroseconds(100);;            //延时一段时间,让LED亮起来。
    }
}

//****************************************************
//扫描行
//****************************************************
void led_scan_line( unsigned char m)
{
    switch(m)
    {
        case 0:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 1:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 2:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 3:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 4:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 5:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 6:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 7:
            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 8:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 9:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 10:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 11:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 12:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 13:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
            break;
        case 14:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
            break;
        case 15:
            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
            break;
        default : break;
    }
}

//****************************************************
//发送数据
//****************************************************
void led_send( unsigned char dat)
{
    unsigned char i;
    digitalWrite(LEDARRAY_CLK, LOW);
    delayMicroseconds(1);
    digitalWrite(LEDARRAY_LAT, LOW);
    delayMicroseconds(1);

    for( i = 0 ; i < 8 ; i++ )
    {
        if( dat&0x01 )
        {
            digitalWrite(LEDARRAY_DI, HIGH);
        }
        else
        {
            digitalWrite(LEDARRAY_DI, LOW);
        }

        delayMicroseconds(1);
        digitalWrite(LEDARRAY_CLK, HIGH);                //上升沿发送数据
            delayMicroseconds(1);
        digitalWrite(LEDARRAY_CLK, LOW);
            delayMicroseconds(1);
        dat >>= 1;

    }
}
int get_int_bit(unsigned char integer, int position)
{
  switch (position)
  {
    case 1:
      return integer & 0x01;
      break;
    case 2:
      return (integer & 0x02) >> 1;
    case 3:
      return (integer & 0x04) >> 2;
    case 4:
      return (integer & 0x08) >> 3;
    case 5:
      return (integer & 0x10) >> 4;
   case 6:
      return (integer & 0x20) >> 5;
    case 7:
      return (integer & 0x40) >> 6;
    case 8:
      return (integer & 0x80) >> 7;
    default:
      break;
  }
  return 0;
}
int remove_bit_value(unsigned char  value, int position)
{
  int bit = 1 << position-1;
  int nMark = 0;
  nMark = (~nMark) ^ bit;
  value &= nMark;
  return value;
}
int set_bit_value(unsigned char  value, int position)
{
  int pos = 1 << position-1;
  value |= pos;
  return value;
}
int set_int_bit(unsigned char integer, int position, int newValue)
{
  if (newValue > 0)
    return set_bit_value(integer, position);
  else
    return remove_bit_value(integer, position);
}
int led_move_header1[16];
int led_move_header2[16];
void led_move_left(unsigned char word2Move[][32])
{
    //RESET
    for(int i = 0;i < 16;i++)
    {
        led_move_header1[i]=1;
        led_move_header2[i]=1;
    }
    //记录左面LED点阵最左面一排的BIT位
    for(int i = 0;i < 16;i++)
    {
        if(get_int_bit(word2Move[0][i], 8)>0)
          led_move_header1[i]=1;
        else
          led_move_header1[i]=0;
    }
    //记录右面LED点阵最左面一排的BIT位
    for(int i = 16;i < 32;i++)
    {
        if(get_int_bit(word2Move[0][i], 8)>0)
          led_move_header2[i-16]=1;
        else
          led_move_header2[i-16]=0;
    }
    //左面LED点阵BIT位左移
    for(int i = 0;i < 16;i++)
        word2Move[0][i]=word2Move[0][i]<<1;
    //右面LED点阵BIT位左移
    for(int i = 16;i < 32;i++)
        word2Move[0][i]=word2Move[0][i]<<1;
    //将LED点阵最左面一排的BIT位SET到最右面一排BIT位
    for(int i = 0;i < 16;i++)
        word2Move[0][i]=set_int_bit(word2Move[0][i], 1, led_move_header2[i]);
    //将LED点阵最左面一排的BIT位SET到最右面一排BIT位
    for(int i = 16;i < 32;i++)
        word2Move[0][i]=set_int_bit(word2Move[0][i], 1, led_move_header1[i-16]);
}

void setup(){
  led_init();
}

void loop(){
  for(int led_display_delay=0;led_display_delay<100;led_display_delay++)
  {
       led_display(LedArray1);
  }
      led_move_left(LedArray1);

}

 LED屏:https://item.taobao.com/item.htm?id=16401785016&_u=iq9aqj2141

技术图片

 arduino连接图

技术图片

 

 

mixly代码:

~/MixlyControls.xml

<!--
type="company"
block="block/MixlyControls.js"
generator="generator/MixlyControls.js"
media="media/MixlyControls"
language="language/MixlyControls"
-->
<script type="text/javascript" src="../../blocks/company/MixlyControls.js"></script>
<script type="text/javascript" src="../../generators/arduino/company/MixlyControls.js"></script>
<category id="McKay_LED_LCD" name="McKay-LED点阵屏" colour="20">
    <block type="LED_Init">
    </block>
    <block type="LED_Display">
        <value name="content"></value>
        <value name="delay"></value>
    </block>
    <block type="LED_LeftMove">
        <value name="varName"></value>
    </block>
    <block type="LED_ClearDisplay">
    </block>
    <block type="LED_ContentArrayRef">
        <value name="reference"></value>
    </block>
    <block type="LED_Content">
        <value name="a1-1"></value>
        <value name="a1-2"></value>
        <value name="a1-3"></value>
        <value name="a1-4"></value>
        <value name="a1-5"></value>
        <value name="a1-6"></value>
        <value name="a1-7"></value>
        <value name="a1-8"></value>
        <value name="a1-9"></value>
        <value name="a1-10"></value>
        <value name="a1-11"></value>
        <value name="a1-12"></value>
        <value name="a1-13"></value>
        <value name="a1-14"></value>
        <value name="a1-15"></value>
        <value name="a1-16"></value>

        <value name="a2-1"></value>
        <value name="a2-2"></value>
        <value name="a2-3"></value>
        <value name="a2-4"></value>
        <value name="a2-5"></value>
        <value name="a2-6"></value>
        <value name="a2-7"></value>
        <value name="a2-8"></value>
        <value name="a2-9"></value>
        <value name="a2-10"></value>
        <value name="a2-11"></value>
        <value name="a2-12"></value>
        <value name="a2-13"></value>
        <value name="a2-14"></value>
        <value name="a2-15"></value>
        <value name="a2-16"></value>

        <value name="a3-1"></value>
        <value name="a3-2"></value>
        <value name="a3-3"></value>
        <value name="a3-4"></value>
        <value name="a3-5"></value>
        <value name="a3-6"></value>
        <value name="a3-7"></value>
        <value name="a3-8"></value>
        <value name="a3-9"></value>
        <value name="a3-10"></value>
        <value name="a3-11"></value>
        <value name="a3-12"></value>
        <value name="a3-13"></value>
        <value name="a3-14"></value>
        <value name="a3-15"></value>
        <value name="a3-16"></value>

        <value name="a4-1"></value>
        <value name="a4-2"></value>
        <value name="a4-3"></value>
        <value name="a4-4"></value>
        <value name="a4-5"></value>
        <value name="a4-6"></value>
        <value name="a4-7"></value>
        <value name="a4-8"></value>
        <value name="a4-9"></value>
        <value name="a4-10"></value>
        <value name="a4-11"></value>
        <value name="a4-12"></value>
        <value name="a4-13"></value>
        <value name="a4-14"></value>
        <value name="a4-15"></value>
        <value name="a4-16"></value>

        <value name="a5-1"></value>
        <value name="a5-2"></value>
        <value name="a5-3"></value>
        <value name="a5-4"></value>
        <value name="a5-5"></value>
        <value name="a5-6"></value>
        <value name="a5-7"></value>
        <value name="a5-8"></value>
        <value name="a5-9"></value>
        <value name="a5-10"></value>
        <value name="a5-11"></value>
        <value name="a5-12"></value>
        <value name="a5-13"></value>
        <value name="a5-14"></value>
        <value name="a5-15"></value>
        <value name="a5-16"></value>

        <value name="a6-1"></value>
        <value name="a6-2"></value>
        <value name="a6-3"></value>
        <value name="a6-4"></value>
        <value name="a6-5"></value>
        <value name="a6-6"></value>
        <value name="a6-7"></value>
        <value name="a6-8"></value>
        <value name="a6-9"></value>
        <value name="a6-10"></value>
        <value name="a6-11"></value>
        <value name="a6-12"></value>
        <value name="a6-13"></value>
        <value name="a6-14"></value>
        <value name="a6-15"></value>
        <value name="a6-16"></value>

        <value name="a7-1"></value>
        <value name="a7-2"></value>
        <value name="a7-3"></value>
        <value name="a7-4"></value>
        <value name="a7-5"></value>
        <value name="a7-6"></value>
        <value name="a7-7"></value>
        <value name="a7-8"></value>
        <value name="a7-9"></value>
        <value name="a7-10"></value>
        <value name="a7-11"></value>
        <value name="a7-12"></value>
        <value name="a7-13"></value>
        <value name="a7-14"></value>
        <value name="a7-15"></value>
        <value name="a7-16"></value>

        <value name="a8-1"></value>
        <value name="a8-2"></value>
        <value name="a8-3"></value>
        <value name="a8-4"></value>
        <value name="a8-5"></value>
        <value name="a8-6"></value>
        <value name="a8-7"></value>
        <value name="a8-8"></value>
        <value name="a8-9"></value>
        <value name="a8-10"></value>
        <value name="a8-11"></value>
        <value name="a8-12"></value>
        <value name="a8-13"></value>
        <value name="a8-14"></value>
        <value name="a8-15"></value>
        <value name="a8-16"></value>

        <value name="a9-1"></value>
        <value name="a9-2"></value>
        <value name="a9-3"></value>
        <value name="a9-4"></value>
        <value name="a9-5"></value>
        <value name="a9-6"></value>
        <value name="a9-7"></value>
        <value name="a9-8"></value>
        <value name="a9-9"></value>
        <value name="a9-10"></value>
        <value name="a9-11"></value>
        <value name="a9-12"></value>
        <value name="a9-13"></value>
        <value name="a9-14"></value>
        <value name="a9-15"></value>
        <value name="a9-16"></value>

        <value name="a10-1"></value>
        <value name="a10-2"></value>
        <value name="a10-3"></value>
        <value name="a10-4"></value>
        <value name="a10-5"></value>
        <value name="a10-6"></value>
        <value name="a10-7"></value>
        <value name="a10-8"></value>
        <value name="a10-9"></value>
        <value name="a10-10"></value>
        <value name="a10-11"></value>
        <value name="a10-12"></value>
        <value name="a10-13"></value>
        <value name="a10-14"></value>
        <value name="a10-15"></value>
        <value name="a10-16"></value>

        <value name="a11-1"></value>
        <value name="a11-2"></value>
        <value name="a11-3"></value>
        <value name="a11-4"></value>
        <value name="a11-5"></value>
        <value name="a11-6"></value>
        <value name="a11-7"></value>
        <value name="a11-8"></value>
        <value name="a11-9"></value>
        <value name="a11-10"></value>
        <value name="a11-11"></value>
        <value name="a11-12"></value>
        <value name="a11-13"></value>
        <value name="a11-14"></value>
        <value name="a11-15"></value>
        <value name="a11-16"></value>

        <value name="a12-1"></value>
        <value name="a12-2"></value>
        <value name="a12-3"></value>
        <value name="a12-4"></value>
        <value name="a12-5"></value>
        <value name="a12-6"></value>
        <value name="a12-7"></value>
        <value name="a12-8"></value>
        <value name="a12-9"></value>
        <value name="a12-10"></value>
        <value name="a12-11"></value>
        <value name="a12-12"></value>
        <value name="a12-13"></value>
        <value name="a12-14"></value>
        <value name="a12-15"></value>
        <value name="a12-16"></value>

        <value name="a13-1"></value>
        <value name="a13-2"></value>
        <value name="a13-3"></value>
        <value name="a13-4"></value>
        <value name="a13-5"></value>
        <value name="a13-6"></value>
        <value name="a13-7"></value>
        <value name="a13-8"></value>
        <value name="a13-9"></value>
        <value name="a13-10"></value>
        <value name="a13-11"></value>
        <value name="a13-12"></value>
        <value name="a13-13"></value>
        <value name="a13-14"></value>
        <value name="a13-15"></value>
        <value name="a13-16"></value>

        <value name="a14-1"></value>
        <value name="a14-2"></value>
        <value name="a14-3"></value>
        <value name="a14-4"></value>
        <value name="a14-5"></value>
        <value name="a14-6"></value>
        <value name="a14-7"></value>
        <value name="a14-8"></value>
        <value name="a14-9"></value>
        <value name="a14-10"></value>
        <value name="a14-11"></value>
        <value name="a14-12"></value>
        <value name="a14-13"></value>
        <value name="a14-14"></value>
        <value name="a14-15"></value>
        <value name="a14-16"></value>

        <value name="a15-1"></value>
        <value name="a15-2"></value>
        <value name="a15-3"></value>
        <value name="a15-4"></value>
        <value name="a15-5"></value>
        <value name="a15-6"></value>
        <value name="a15-7"></value>
        <value name="a15-8"></value>
        <value name="a15-9"></value>
        <value name="a15-10"></value>
        <value name="a15-11"></value>
        <value name="a15-12"></value>
        <value name="a15-13"></value>
        <value name="a15-14"></value>
        <value name="a15-15"></value>
        <value name="a15-16"></value>

        <value name="a16-1"></value>
        <value name="a16-2"></value>
        <value name="a16-3"></value>
        <value name="a16-4"></value>
        <value name="a16-5"></value>
        <value name="a16-6"></value>
        <value name="a16-7"></value>
        <value name="a16-8"></value>
        <value name="a16-9"></value>
        <value name="a16-10"></value>
        <value name="a16-11"></value>
        <value name="a16-12"></value>
        <value name="a16-13"></value>
        <value name="a16-14"></value>
        <value name="a16-15"></value>
        <value name="a16-16"></value>
    </block>
</category>

 

 ~/block/MixlyControls.js

‘use strict‘;

goog.provide(‘Blockly.Blocks.MixlyControls‘);

goog.require(‘Blockly.Blocks‘);

//
//Blockly.Blocks.ServoDriveBoard_Init={
//init:function(){
//    this.appendDummyInput("")
//          .appendField("初始化舵机");
//        this.appendValueInput("MinAngle").setCheck(Number).appendField("最小角度");
//        this.appendValueInput("MaxAngle").setCheck(Number).appendField("最大角度");
//
//        this.setColour(20);
//        this.setPreviousStatement(true);
//        this.setNextStatement(true);
//        this.setInputsInline(false);
//        this.setTooltip("初始化舵机");
//  }
//};




Blockly.Blocks.LED_Init={
    init:function(){
            this.setColour(Blockly.Blocks.display.HUE);
            this.appendDummyInput("").appendField("LED初始化");

            this.setPreviousStatement(true);
            this.setNextStatement(true);
            this.setInputsInline(false);
            this.setTooltip("LED初始化");
    }
};



Blockly.Blocks.LED_Display={
    init:function(){
            this.setColour(Blockly.Blocks.display.HUE);
            this.appendValueInput(‘content‘).setAlign(Blockly.ALIGN_RIGHT).appendField("LED显示");
            this.appendValueInput("delay").setCheck(Number).appendField("延时");

            this.setPreviousStatement(true);
            this.setNextStatement(true);
            this.setInputsInline(true);
            this.setTooltip("LED显示");
    }
};



Blockly.Blocks.LED_LeftMove={
    init:function(){
            this.setColour(Blockly.Blocks.display.HUE);
            this.appendDummyInput("").appendField("LED左移");
            this.appendValueInput("varName").appendField("变量名");

            this.setPreviousStatement(true);
            this.setNextStatement(true);
            this.setInputsInline(true);
            this.setTooltip("LED左移");
    }
};

Blockly.Blocks.LED_ClearDisplay={
    init:function(){
            this.setColour(Blockly.Blocks.display.HUE);
            this.appendDummyInput("").appendField("LED清屏");

            this.setPreviousStatement(true);
            this.setNextStatement(true);
            this.setInputsInline(true);
            this.setTooltip("LED清屏");
    }
};

Blockly.Blocks.LED_Content={
    init:function(){
            this.setColour(Blockly.Blocks.display.HUE);
            this.appendDummyInput("").appendField(Blockly.MIXLY_DISPLAY_MATRIX_ARRAYVAR).appendField(new Blockly.FieldTextInput("LedArray1"), "VAR");
            for(var i=1;i<=16;i++)
            {
                var start_block=this.appendDummyInput("");
                for(var j=1;j<=16;j++)
                    start_block.appendField(new Blockly.FieldCheckbox("FALSE"), "a"+i+"-"+j);
            }

            this.setOutput(true, String);
    }
};


Blockly.Blocks.LED_ContentArrayRef={
    init:function(){
            this.setColour(Blockly.Blocks.display.HUE);
            this.appendDummyInput("").appendField(Blockly.MIXLY_DISPLAY_MATRIX_ARRAYVAR).appendField(new Blockly.FieldTextInput("LedArray1"), "reference");

            this.setOutput(true, String);
    }
};

 

~/generator/MixlyControls.js

‘use strict‘;

goog.provide(‘Blockly.Arduino.MixlyControls‘);

goog.require(‘Blockly.Arduino‘);


//
//Blockly.McKay_ServoDriveBoard_MinAngle=0;
//Blockly.McKay_ServoDriveBoard_MaxAngle=0;
//Blockly.Arduino.ServoDriveBoard_Init=function(){
//    var MinAngle = Blockly.Arduino.valueToCode(this, ‘MinAngle‘, Blockly.Arduino.ORDER_ATOMIC) || ‘0‘;
//    var MaxAngle = Blockly.Arduino.valueToCode(this, ‘MaxAngle‘, Blockly.Arduino.ORDER_ATOMIC) || ‘112‘;
//
//    Blockly.McKay_ServoDriveBoard_MinAngle=parseInt(MinAngle);
//    Blockly.McKay_ServoDriveBoard_MaxAngle=parseInt(MaxAngle);
//
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_Include"] = "#include <Adafruit_PWMServoDriver.h>
";
//
//    var defines="#define SERVOMIN 200
";
//    defines+="#define SERVOMAX 540
";
//    defines+="#define SERVOMIN_Angle "+Blockly.McKay_ServoDriveBoard_MinAngle+"
";
//    defines+="#define SERVOMAX_Angle "+Blockly.McKay_ServoDriveBoard_MaxAngle+"
";
//
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_Defines"] = defines;
//
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_pwmObj"] = "Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
";
//
//
//    var gotoCode="void servo_goto_angle(int pinId, int angle)
";
//    gotoCode+="{
";
//    gotoCode+="     int anglePwmValue=map(angle, SERVOMIN_Angle, SERVOMAX_Angle, SERVOMIN, SERVOMAX);
";
//    gotoCode+="     pwm.setPWM(pinId,0, anglePwmValue);
";
//    gotoCode+="}
";
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_goto_angle"] = gotoCode;
//
//    var initCode="void servo_init()
";
//    initCode+="{
";
//    initCode+="     Serial.begin(9600);
";
//    initCode+="     pwm.begin();
";
//    initCode+="     pwm.setPWMFreq(60);
";
//    initCode+="     delay(200);
";
//    initCode+="}
";
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_initCode"] = initCode;
//
//    return "servo_init();
";
//};
//
//
//
//Blockly.Arduino.ServoDriveBoard_Do=function(){
//    var ServoNumber = Blockly.Arduino.valueToCode(this, ‘ServoNumber‘, Blockly.Arduino.ORDER_ATOMIC) || ‘0‘;
//    var Angle = Blockly.Arduino.valueToCode(this, ‘Angle‘, Blockly.Arduino.ORDER_ATOMIC) || ‘0‘;
//
//  var code="servo_goto_angle("+ServoNumber+", "+Angle+");
";
//  return code;
//};




Blockly.Arduino.LED_Init=function(){
    var includes="#define LEDARRAY_D 2
";
    includes+="#define LEDARRAY_C 3
";
    includes+="#define LEDARRAY_B 4
";
    includes+="#define LEDARRAY_A 5
";
    includes+="#define LEDARRAY_G 6
";
    includes+="#define LEDARRAY_DI 7
";
    includes+="#define LEDARRAY_CLK 8
";
    includes+="#define LEDARRAY_LAT 9
";
    Blockly.Arduino.definitions_["define_MixlyControls_Include"] = includes;

    var setupCode="void led_init(){
";
    setupCode+="        pinMode(LEDARRAY_D, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_C, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_B, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_A, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_G, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_DI, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_CLK, OUTPUT);
";
    setupCode+="        pinMode(LEDARRAY_LAT, OUTPUT);
";
    setupCode+="}
";
    Blockly.Arduino.definitions_["define_MixlyControls_led_init"] = setupCode;

    Blockly.Arduino.definitions_["define_MixlyControls_led_var_led_display_buffer"] = "unsigned char led_Display_Buffer[2];
";


    var functionsCode="";
    functionsCode+="void led_display(const unsigned char dat[][32])
";
    functionsCode+="{
";
    functionsCode+="    unsigned char i;
";
    functionsCode+="
";
    functionsCode+="    for( i = 0 ; i < 16 ; i++ )
";
    functionsCode+="    {
";
    functionsCode+="        digitalWrite(LEDARRAY_G, HIGH);        //更新数据时候关闭显示。等更新完数据,打开138显示行。防止重影。
";
    functionsCode+="
";
    functionsCode+="        led_Display_Buffer[0] = dat[0][i];
";
    functionsCode+="        led_Display_Buffer[1] = dat[0][i+16];
";
    functionsCode+="
";
    functionsCode+="        led_send(led_Display_Buffer[1]);
";
    functionsCode+="        led_send(led_Display_Buffer[0]);
";
    functionsCode+="
";
    functionsCode+="        digitalWrite(LEDARRAY_LAT, HIGH);                    //锁存数据
";
    functionsCode+="        delayMicroseconds(1);
";
    functionsCode+="
";
    functionsCode+="        digitalWrite(LEDARRAY_LAT, LOW);
";
    functionsCode+="        delayMicroseconds(1);
";
    functionsCode+="
";
    functionsCode+="        led_scan_line(i);                            //选择第i行
";
    functionsCode+="
";
    functionsCode+="        digitalWrite(LEDARRAY_G, LOW);
";
    functionsCode+="
";
    functionsCode+="        delayMicroseconds(100);;            //延时一段时间,让LED亮起来。
";
    functionsCode+="    }
";
    functionsCode+="}
";
    functionsCode+="
";
    functionsCode+="//****************************************************
";
    functionsCode+="//扫描行
";
    functionsCode+="//****************************************************
";
    functionsCode+="void led_scan_line( unsigned char m)
";
    functionsCode+="{
";
    functionsCode+="    switch(m)
";
    functionsCode+="    {
";
    functionsCode+="        case 0:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 1:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 2:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 3:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 4:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 5:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 6:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 7:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, LOW);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 8:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 9:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 10:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 11:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, LOW);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 12:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 13:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, LOW);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        case 14:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, LOW);
";
    functionsCode+="            break;
";
    functionsCode+="        case 15:
";
    functionsCode+="            digitalWrite(LEDARRAY_D, HIGH);digitalWrite(LEDARRAY_C, HIGH);digitalWrite(LEDARRAY_B, HIGH);digitalWrite(LEDARRAY_A, HIGH);
";
    functionsCode+="            break;
";
    functionsCode+="        default : break;
";
    functionsCode+="    }
";
    functionsCode+="}
";
    functionsCode+="
";
    functionsCode+="//****************************************************
";
    functionsCode+="//发送数据
";
    functionsCode+="//****************************************************
";
    functionsCode+="void led_send( unsigned char dat)
";
    functionsCode+="{
";
    functionsCode+="    unsigned char i;
";
    functionsCode+="    digitalWrite(LEDARRAY_CLK, LOW);
";
    functionsCode+="    delayMicroseconds(1);
";
    functionsCode+="    digitalWrite(LEDARRAY_LAT, LOW);
";
    functionsCode+="    delayMicroseconds(1);
";
    functionsCode+="
";
    functionsCode+="    for( i = 0 ; i < 8 ; i++ )
";
    functionsCode+="    {
";
    functionsCode+="        if( dat&0x01 )
";
    functionsCode+="        {
";
    functionsCode+="            digitalWrite(LEDARRAY_DI, HIGH);
";
    functionsCode+="        }
";
    functionsCode+="        else
";
    functionsCode+="        {
";
    functionsCode+="            digitalWrite(LEDARRAY_DI, LOW);
";
    functionsCode+="        }
";
    functionsCode+="
";
    functionsCode+="        delayMicroseconds(1);
";
    functionsCode+="        digitalWrite(LEDARRAY_CLK, HIGH);                //上升沿发送数据
";
    functionsCode+="            delayMicroseconds(1);
";
    functionsCode+="        digitalWrite(LEDARRAY_CLK, LOW);
";
    functionsCode+="            delayMicroseconds(1);
";
    functionsCode+="        dat >>= 1;
";
    functionsCode+="
";
    functionsCode+="    }
";
    functionsCode+="}
";
    functionsCode+="int get_int_bit(unsigned char integer, int position)
";
    functionsCode+="{
";
    functionsCode+="  switch (position)
";
    functionsCode+="  {
";
    functionsCode+="    case 1:
";
    functionsCode+="      return integer & 0x01;
";
    functionsCode+="      break;
";
    functionsCode+="    case 2:
";
    functionsCode+="      return (integer & 0x02) >> 1;
";
    functionsCode+="    case 3:
";
    functionsCode+="      return (integer & 0x04) >> 2;
";
    functionsCode+="    case 4:
";
    functionsCode+="      return (integer & 0x08) >> 3;
";
    functionsCode+="    case 5:
";
    functionsCode+="      return (integer & 0x10) >> 4;
";
    functionsCode+="   case 6:
";
    functionsCode+="      return (integer & 0x20) >> 5;
";
    functionsCode+="    case 7:
";
    functionsCode+="      return (integer & 0x40) >> 6;
";
    functionsCode+="    case 8:
";
    functionsCode+="      return (integer & 0x80) >> 7;
";
    functionsCode+="    default:
";
    functionsCode+="      break;
";
    functionsCode+="  }
";
    functionsCode+="  return 0;
";
    functionsCode+="}
";
    functionsCode+="int remove_bit_value(unsigned char  value, int position)
";
    functionsCode+="{
";
    functionsCode+="  int bit = 1 << position-1;
";
    functionsCode+="  int nMark = 0;
";
    functionsCode+="  nMark = (~nMark) ^ bit;
";
    functionsCode+="  value &= nMark;
";
    functionsCode+="  return value;
";
    functionsCode+="}
";
    functionsCode+="int set_bit_value(unsigned char  value, int position)
";
    functionsCode+="{
";
    functionsCode+="  int pos = 1 << position-1;
";
    functionsCode+="  value |= pos;
";
    functionsCode+="  return value;
";
    functionsCode+="}
";
    functionsCode+="int set_int_bit(unsigned char integer, int position, int newValue)
";
    functionsCode+="{
";
    functionsCode+="  if (newValue > 0)
";
    functionsCode+="    return set_bit_value(integer, position);
";
    functionsCode+="  else
";
    functionsCode+="    return remove_bit_value(integer, position);
";
    functionsCode+="}
";
    functionsCode+="int led_move_header1[16];
";
    functionsCode+="int led_move_header2[16];
";
    functionsCode+="void led_move_left(unsigned char word2Move[][32])
";
    functionsCode+="{
";
    functionsCode+="    //RESET
";
    functionsCode+="    for(int i = 0;i < 16;i++)
";
    functionsCode+="    {
";
    functionsCode+="        led_move_header1[i]=1;
";
    functionsCode+="        led_move_header2[i]=1;
";
    functionsCode+="    }      
";
    functionsCode+="    //记录左面LED点阵最左面一排的BIT位
";
    functionsCode+="    for(int i = 0;i < 16;i++)
";
    functionsCode+="    {
";
    functionsCode+="        if(get_int_bit(word2Move[0][i], 8)>0)
";
    functionsCode+="          led_move_header1[i]=1;
";
    functionsCode+="        else
";
    functionsCode+="          led_move_header1[i]=0;
";
    functionsCode+="    }
";
    functionsCode+="    //记录右面LED点阵最左面一排的BIT位
";
    functionsCode+="    for(int i = 16;i < 32;i++)
";
    functionsCode+="    {
";
    functionsCode+="        if(get_int_bit(word2Move[0][i], 8)>0)
";
    functionsCode+="          led_move_header2[i-16]=1;
";
    functionsCode+="        else
";
    functionsCode+="          led_move_header2[i-16]=0;
";
    functionsCode+="    }    
";
    functionsCode+="    //左面LED点阵BIT位左移
";
    functionsCode+="    for(int i = 0;i < 16;i++)
";
    functionsCode+="        word2Move[0][i]=word2Move[0][i]<<1;
";
    functionsCode+="    //右面LED点阵BIT位左移
";
    functionsCode+="    for(int i = 16;i < 32;i++)
";
    functionsCode+="        word2Move[0][i]=word2Move[0][i]<<1;
";
    functionsCode+="    //将LED点阵最左面一排的BIT位SET到最右面一排BIT位
";
    functionsCode+="    for(int i = 0;i < 16;i++)
";
    functionsCode+="        word2Move[0][i]=set_int_bit(word2Move[0][i], 1, led_move_header2[i]);
";
    functionsCode+="    //将LED点阵最左面一排的BIT位SET到最右面一排BIT位
";
    functionsCode+="    for(int i = 16;i < 32;i++)
";
    functionsCode+="        word2Move[0][i]=set_int_bit(word2Move[0][i], 1, led_move_header1[i-16]);
";
    functionsCode+="}
";

    Blockly.Arduino.definitions_["define_MixlyControls_functions"] = functionsCode;

    return "led_init();
";
};



Blockly.Arduino.LED_Display=function(block){
    var varName = this.getInputTargetBlock(‘content‘).getFieldValue(‘VAR‘);
    var delay = this.getInputTargetBlock(‘delay‘);

    var ledArray = new Array();
    for (var i = 1; i <= 16; i++) {
        ledArray[i-1] = new Array();
        for (var j = 1; j <= 16; j++) {
          ledArray[i-1][j-1] = (this.getInputTargetBlock(‘content‘).getFieldValue(‘a‘ + i + ‘-‘ +j) == "TRUE") ? 0 : 1;
        }
    }

    //生成16进制数组
    var code=generateLEDArrayCode(ledArray);
    Blockly.Arduino.definitions_[varName] = "unsigned char " + varName + "[1][32]=
" + code;
    Blockly.Arduino.definitions_[varName+‘_hidden‘] = "const unsigned char " + varName + "_hidden[1][32]=
" + code;

    var functionCode="";
    functionCode+="for(int led_display_delay=0;led_display_delay<"+delay+";led_display_delay++)
";
    functionCode+="{
";
    functionCode+="     led_display("+varName+");
";
    functionCode+="}
";
    return functionCode;
};


Blockly.Arduino.LED_ClearDisplay=function(block){

    var ledArray = new Array();
    for (var i = 1; i <= 16; i++) {
        ledArray[i-1] = new Array();
        for (var j = 1; j <= 16; j++) {
          ledArray[i-1][j-1] = 1;
        }
    }

    //生成16进制数组
    var code=generateLEDArrayCode(ledArray);
    Blockly.Arduino.definitions_[‘led_clear_define‘] = "const unsigned char led_clear_define[1][32]=
" + code;

    var functionCode="    led_display(led_clear_define);
";
    return functionCode;
};


Blockly.Arduino.LED_LeftMove=function(block){
    var reference = this.getInputTargetBlock(‘varName‘).getFieldValue(‘reference‘);

    var functionCode="    led_move_left("+reference+");
";
    return functionCode;
};


Blockly.Arduino.LED_ContentArrayRef=function(block){
    var varName = this.getInputTargetBlock(‘reference‘);

    return varName;
};


function get_led_2_string(array, from, to)
{
    var tmpStr2 = "";
    for (var j = from; j < to; j++) {
      tmpStr2 += array[j];
    }
    return tmpStr2;
}

function get_16_string(str_2)
{
    return (parseInt(str_2, 2)).toString(16);
}

function generateLEDArrayCode(ledArray)
{
    var code="";
    code+="{
";
    for (var i = 0; i < 16; i++)
    {
        var str1_2=get_led_2_string(ledArray[i], 0, 4);
        var str1_16=get_16_string(str1_2);

        var str2_2=get_led_2_string(ledArray[i], 4, 8);
        var str2_16=get_16_string(str2_2);

        code += ‘0x‘ + str1_16 + str2_16 + ‘,‘;
    }
    for (var i = 0; i < 16; i++)
    {
        var str1_2=get_led_2_string(ledArray[i], 8, 12);
        var str1_16=get_16_string(str1_2);

        var str2_2=get_led_2_string(ledArray[i], 12, 16);
        var str2_16=get_16_string(str2_2);

        code += ‘0x‘ + str1_16 + str2_16 + ‘,‘;
    }
    code+="
";
    code+="};
";
    return code;
}

 技术图片

 

 

最后,虽然是能显示,也能左移,但是由于没有自带文字库,所以都靠自己画点,麻烦,以后不干这事了,还是买入整体为好,这么太折腾了。

 

以上是关于玩玩LED点阵屏(arduino nano)的主要内容,如果未能解决你的问题,请参考以下文章

Arduino实现8*8LED点阵显示爱心显示和Proteus仿真

基于Proteus仿真的Arduino学习——LED点阵探究A(LED点阵基础)

Proteus仿真8×8LED点阵屏仿电梯数字滚动显示

51单片机8×8LED点阵屏动态显示取模汉字+ Proteus仿真

5.1 单片机-16x16LED点阵屏

Arduino基本人机接口:点阵LED汉字库键盘