简体   繁体   English

Arduino 8x8 LED矩阵字母

[英]Arduino 8x8 led matrix letters

I have an Arduino 8x8 led attached to a shield on the Arduino. 我有一个Arduino 8x8 led连接到Arduino的屏蔽层上。 Now I want it to display some text. 现在,我希望它显示一些文本。 I've discovered how to make the correct byte for each letter. 我发现了如何为每个字母设置正确的字节。 But I have no clue how to light it on my display. 但是我不知道如何在显示器上点亮它。 The tutorials I see on the internet are not really understandable. 我在互联网上看到的教程并不是很容易理解。 I would like to use a function where I can fill in the letter I want to use and how long it should be on the display. 我想使用一个函数,在其中可以填写要使用的字母以及它在显示屏上应该显示多长时间。 Like this: displayLetter(byte letter[], int timeOnDisplay) Any advice is nice. 像这样: displayLetter(byte letter[], int timeOnDisplay)任何建议都很好。

I don't the know model, it was included in my Elektor Arduino kit. 我不知道型号,它包含在我的Elektor Arduino套件中。 It can be seen at link. 可以在链接上看到。 Currently I have declared all my bytes like this 目前我已经声明了我所有的字节

byte A[] = {     
    B00000000,
    B00111100,
    B01100110,
    B01100110,
    B01111110,
    B01100110,
    B01100110,
    B01100110}; 

as well as 以及

void setup() { 
    Serial.begin(9600); 
    for (byte i = 2; i <= 13; i++) 
        pinMode(i, OUTPUT); 
    pinMode(A0, OUTPUT); 
    pinMode(A1, OUTPUT); 
    pinMode(A2, OUTPUT); 
    pinMode(A3, OUTPUT); 
}

Which I got from an online tutorial. 我从在线教程中获得的。

I use an Arduino Uno and my 8x8 led is connected via a shield. 我使用Arduino Uno,并且我的8x8 led通过屏蔽连接。

Greetings 问候

Without knowing the input type of the matrix, I'm going to assume it's controlled via column and row inputs and not SPI based on the sample code you provided. 在不知道矩阵的输入类型的情况下,我将假定它是通过列和行输入而不是基于您提供的示例代码的SPI控制的。

Below is a sample I put together real quick to demonstrate looping through the alphabet and displaying a letter for a specified amount of time. 下面是我快速汇总的示例,以演示在字母中循环显示字母并显示指定时间。 This code does work, you must connect the 8x8 led matrix inputs to the corresponding pin callouts for the rows and columns in the code provided (or change the code pins to match your connection configuration). 该代码确实有效,您必须将8x8 led矩阵输入连接到所提供代码中行和列的相应引脚标注(或更改代码引脚以匹配您的连接配置)。

These work by multiplexing which means the signals are shared between leds, each row input pin connects to 8 leds and each column is also connected to 8 leds. 这些通过多路复用来工作,这意味着信号在LED之间共享,每行输入引脚连接到8个LED,每列也连接到8个LED。 To turn on a particular led, it needs the correct signal from both the row pin it's connected to and also the column pin it's connected it. 要打开特定的LED,它需要来自与其连接的行引脚和与其连接的列引脚的正确信号。 So for example, to turn on the led at the bottom right corner, a signal needs to be sent to the last row pin and also the last column pin; 因此,例如,要打开右下角的LED,需要将信号发送到最后一行的引脚和最后一列的引脚。 if you wanted to turn on all the leds in the last column, a signal needs to be sent to all row pins and just the last column pin. 如果要打开最后一列中的所有LED,则需要将信号发送到所有行引脚,而仅发送到最后一列引脚。

To display shapes/letters requires a little more processing power as you cannot just turn on a few columns and rows and leave them on. 显示形状/字母需要更多的处理能力,因为您不能只打开几列和几行然后将它们保持打开状态。 To accomplish this, you need to loop through each row and each column, turning on the needed leds for a brief period of time and then turn them off again; 为此,您需要遍历每一行和每一列,短暂打开所需的LED,然后再次将其关闭。 doing this fast enough makes it appear as if all of the leds are on at the same time. 这样做的速度足够快,就好像所有指示灯都在同时亮起一样。

在此输入图像描述

    /*
 Name:      Arduino8x8MatrixStackOverflow.ino
 Created:   3/10/2019 5:21:02 PM
 Author:    jjman
*/
//Connect these pins to the corresponding row input of the 8x8 matrix.
const unsigned char RowPins[] = {2,3,4,5,6,7,8,9};
//Connect these pins to the corresponding column input of the 8x8 matrix.
const unsigned char ColumnPins[] = {10,11,12,13,A0,A1,A2,A3};

unsigned char A[] = {B00000000,B00111100,B01100110,B01100110,B01111110,B01100110,B01100110,B01100110};
unsigned char B[] = {B01111000,B01001000,B01001000,B01110000,B01001000,B01000100,B01000100,B01111100};
unsigned char C[] = {B00000000,B00011110,B00100000,B01000000,B01000000,B01000000,B00100000,B00011110};
unsigned char D[] = {B00000000,B00111000,B00100100,B00100010,B00100010,B00100100,B00111000,B00000000};
unsigned char E[] = {B00000000,B00111100,B00100000,B00111000,B00100000,B00100000,B00111100,B00000000};
unsigned char F[] = {B00000000,B00111100,B00100000,B00111000,B00100000,B00100000,B00100000,B00000000};
unsigned char G[] = {B00000000,B00111110,B00100000,B00100000,B00101110,B00100010,B00111110,B00000000};
unsigned char H[] = {B00000000,B00100100,B00100100,B00111100,B00100100,B00100100,B00100100,B00000000};
unsigned char I[] = {B00000000,B00111000,B00010000,B00010000,B00010000,B00010000,B00111000,B00000000};
unsigned char J[] = {B00000000,B00011100,B00001000,B00001000,B00001000,B00101000,B00111000,B00000000};
unsigned char K[] = {B00000000,B00100100,B00101000,B00110000,B00101000,B00100100,B00100100,B00000000};
unsigned char L[] = {B00000000,B00100000,B00100000,B00100000,B00100000,B00100000,B00111100,B00000000};
unsigned char M[] = {B00000000,B00000000,B01000100,B10101010,B10010010,B10000010,B10000010,B00000000};
unsigned char N[] = {B00000000,B00100010,B00110010,B00101010,B00100110,B00100010,B00000000,B00000000};
unsigned char O[] = {B00000000,B00111100,B01000010,B01000010,B01000010,B01000010,B00111100,B00000000};
unsigned char P[] = {B00000000,B00111000,B00100100,B00100100,B00111000,B00100000,B00100000,B00000000};
unsigned char Q[] = {B00000000,B00111100,B01000010,B01000010,B01000010,B01000110,B00111110,B00000001};
unsigned char R[] = {B00000000,B00111000,B00100100,B00100100,B00111000,B00100100,B00100100,B00000000};
unsigned char S[] = {B00000000,B00111100,B00100000,B00111100,B00000100,B00000100,B00111100,B00000000};
unsigned char T[] = {B00000000,B01111100,B00010000,B00010000,B00010000,B00010000,B00010000,B00000000};
unsigned char U[] = {B00000000,B01000010,B01000010,B01000010,B01000010,B00100100,B00011000,B00000000};
unsigned char V[] = {B00000000,B00100010,B00100010,B00100010,B00010100,B00010100,B00001000,B00000000};
unsigned char W[] = {B00000000,B10000010,B10010010,B01010100,B01010100,B00101000,B00000000,B00000000};
unsigned char X[] = {B00000000,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B00000000};
unsigned char Y[] = {B00000000,B01000100,B00101000,B00010000,B00010000,B00010000,B00010000,B00000000};
unsigned char Z[] = {B00000000,B00111100,B00000100,B00001000,B00010000,B00100000,B00111100,B00000000};

unsigned char *Alphabet[] = {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
unsigned char AlphabetCharactersLower[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};


unsigned char* DisplayBuffer = nullptr;

unsigned char DisplayIndex = 0;
unsigned long DisplayLetterBeginMilliseconds = 0;
unsigned long DisplayLetterEndMilliseconds = 0;
bool DisplayActive = false;

void setup()
{
    // Open serial port
    Serial.begin(9600);
    //Sets up all of the row pins to be used as outputs.
    for(unsigned char i = 0; i < sizeof(RowPins); i++)
    {
        pinMode(RowPins[i], OUTPUT);
    }
    //Sets up all of the column pins to be used as outputs.
    for(unsigned char i = 0; i < sizeof(ColumnPins); i++)
    {
        pinMode(ColumnPins[i], OUTPUT);
    }
}


//Resets the values used to time the duration of display.
void SetDisplayTiming(const unsigned displayMilliseconds)
{
    DisplayLetterBeginMilliseconds = millis();
    DisplayLetterEndMilliseconds = DisplayLetterBeginMilliseconds + displayMilliseconds;
    DisplayActive = true;
}

void UpdateDisplay()
{
    //Loop through rows.
    for(unsigned char i = 0; i < sizeof(RowPins); i++)
    {
        //Loop through columns.
        for(unsigned char j = 0; j < sizeof(ColumnPins); j++)
        {
            //Turn column on for the corresponding bit.
            digitalWrite(ColumnPins[j], ~DisplayBuffer[i] >> j & 1);
        }
        //Turn on row to activate led.
        digitalWrite(RowPins[i], 1);

        //Uncomment the delay to increase brightness.  With 8 rows, any delay greater than 2 ms will cause flickering.  This is due to the refresh rate dropping below 60hz.
        //delayMicroseconds(2000);

        //Turn row back off.
        digitalWrite(RowPins[i], 0);
    }
}


//Updates the DisplayBuffer to the corresponding letter.  Letter can be either uppercase, 'A', or lowercase 'a'.
void SetDisplayLetter(char letter)
{
    //If letter supplied is an uppercase letter, subtract 65 to zero the letter so it can be used as an index. (The 'A' character has an integer value of 65)
    if(letter > 64 && letter < 91) letter -= 65;
    //If letter supplied is a lowercase letter, subtract 97 to zero the letter so it can be used as an index.  (The 'a' character has an integer value of 97)
    else if(letter > 96 && letter < 123) letter -= 97;
    //Letter supplied is not a lowercase or uppercase letter, print error to serial.
    else
    {
        Serial.println("Error - Function: 'DisplayLetter' Cause: Specified Letter character is not an alphabet character");
        return;
    }
    DisplayBuffer = Alphabet[letter];
}
void SetDisplayLetter(const char letter, const unsigned displayMilliseconds)
{
    SetDisplayLetter(letter);
    SetDisplayTiming(displayMilliseconds);
}

//If it's time to display the next letter, do so.
void TryToDisplayNextLetter()
{
    //Letter is currently displayed, now we can check to see if enough time has elapsed for us to turn it off.
    if(DisplayActive)
    {
        //If enough time has elapsed, turn off display to allow display of the next letter.
        if(millis() > DisplayLetterEndMilliseconds)
        {
            DisplayActive = false;
        }
        return;
    }
    SetDisplayLetter(AlphabetCharactersLower[DisplayIndex],200);
    //Counts up the index for the next letter.
    DisplayIndex++;
    //Reset the index to zero if we just displayed the last letter in the alphabet.
    if(DisplayIndex == 26) DisplayIndex = 0;
}

//This example does not use delays and is non-blocking.
void loop()
{
    while(true)
    {
        TryToDisplayNextLetter();
        UpdateDisplay();
    }

    //Add other code as necessary.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM