码迷,mamicode.com
首页 > 其他好文 > 详细

玩玩LED点阵屏(arduino nano)

时间:2019-02-19 01:20:18      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:oba   一段   lang   clear   rom   ros   str   pwm   ide   

做些记录,特别是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>\n";
//
//    var defines="#define SERVOMIN 200\n";
//    defines+="#define SERVOMAX 540\n";
//    defines+="#define SERVOMIN_Angle "+Blockly.McKay_ServoDriveBoard_MinAngle+"\n";
//    defines+="#define SERVOMAX_Angle "+Blockly.McKay_ServoDriveBoard_MaxAngle+"\n";
//
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_Defines"] = defines;
//
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_pwmObj"] = "Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();\n";
//
//
//    var gotoCode="void servo_goto_angle(int pinId, int angle)\n";
//    gotoCode+="{\n";
//    gotoCode+="     int anglePwmValue=map(angle, SERVOMIN_Angle, SERVOMAX_Angle, SERVOMIN, SERVOMAX);\n";
//    gotoCode+="     pwm.setPWM(pinId,0, anglePwmValue);\n";
//    gotoCode+="}\n";
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_goto_angle"] = gotoCode;
//
//    var initCode="void servo_init()\n";
//    initCode+="{\n";
//    initCode+="     Serial.begin(9600);\n";
//    initCode+="     pwm.begin();\n";
//    initCode+="     pwm.setPWMFreq(60);\n";
//    initCode+="     delay(200);\n";
//    initCode+="}\n";
//    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_initCode"] = initCode;
//
//    return "servo_init();\n";
//};
//
//
//
//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+");\n";
//  return code;
//};




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

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

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


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

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

    return "led_init();\n";
};



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]=\n" + code;
    Blockly.Arduino.definitions_[varName+‘_hidden‘] = "const unsigned char " + varName + "_hidden[1][32]=\n" + code;

    var functionCode="";
    functionCode+="for(int led_display_delay=0;led_display_delay<"+delay+";led_display_delay++)\n";
    functionCode+="{\n";
    functionCode+="     led_display("+varName+");\n";
    functionCode+="}\n";
    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]=\n" + code;

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


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

    var functionCode="    led_move_left("+reference+");\n";
    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+="{\n";
    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+="\n";
    code+="};\n";
    return code;
}

 技术图片

 

 

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

 

玩玩LED点阵屏(arduino nano)

标签:oba   一段   lang   clear   rom   ros   str   pwm   ide   

原文地址:https://www.cnblogs.com/aarond/p/10398658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!