wiki:wade/embedded/arduino/example/example_3-2

Version 4 (modified by wade, 15 years ago) (diff)

--

/*
 * 由 12 個 digital pins 控制 5*7 矩陣式 LED Matrix,col:掃描,row:data。載入 5x7 字形 
 */

#define lpin0 7             // 定義 LED 負極腳位
#define lpin1 8             // 定義 LED 負極腳位
#define lpin2 9             // 定義 LED 負極腳位
#define lpin3 10             // 定義 LED 負極腳位
#define lpin4 11             // 定義 LED 負極腳位
#define lpin5 12             // 定義 LED 負極腳位
#define lpin6 13             // 定義 LED 負極腳位
#define hpin0 2             // 定義 LED 正極腳位
#define hpin1 3             // 定義 LED 正極腳位
#define hpin2 4            // 定義 LED 正極腳位
#define hpin3 5            // 定義 LED 正極腳位
#define hpin4 6            // 定義 LED 正極腳位
#define row_size 7          // 定義 Matrix row 大小
#define col_size 5          // 定義 Matrix colum 小
#define delay_time 500      // 延遲時間

#define data_A {1, 0, 0, 0, 0,\
                0, 1, 0, 0, 0,\
                0, 0, 1, 0, 0,\
                0, 0, 1, 0, 0,\
                0, 0, 1, 0, 0,\
                0, 0, 0, 1, 0,\
                0, 0, 0, 0, 1}
#define data_ascii_null {0x00, 0x00, 0x00, 0x00, 0x00} // 全暗
#define data_ascii_0 {0x3E, 0x51, 0x49, 0x45, 0x3E}; // 0 48
#define data_ascii_1 {0x00, 0x42, 0x7F, 0x40, 0x00}; // 1 49
#define data_ascii_2 {0x72, 0x49, 0x49, 0x49, 0x46}; // 2 50
#define data_ascii_3 {0x21, 0x41, 0x49, 0x4D, 0x33}; // 3 51
#define data_ascii_4 {0x18, 0x14, 0x12, 0x7F, 0x10}; // 4 52
#define data_ascii_5 {0x27, 0x45, 0x45, 0x45, 0x39}; // 5 53
#define data_ascii_6 {0x3C, 0x4A, 0x49, 0x49, 0x31}; // 6 54
#define data_ascii_7 {0x41, 0x21, 0x11, 0x09, 0x07}; // 7 55
#define data_ascii_8 {0x36, 0x49, 0x49, 0x49, 0x36}; // 8 56
#define data_ascii_9 {0x46, 0x49, 0x49, 0x29, 0x1E}; // 9 57
#define data_ascii_: {0x00, 0x00, 0x14, 0x00, 0x00}; // : 58
#define data_ascii_; {0x00, 0x40, 0x34, 0x00, 0x00}; // ; 59
#define data_ascii_< {0x00, 0x08, 0x14, 0x22, 0x41}; // <
#define data_ascii_A {0x7C, 0x12, 0x11, 0x12, 0x7C} // A 65
#define data_ascii_B {0x7F, 0x49, 0x49, 0x49, 0x36} // B 66
#define data_ascii_C {0x3E, 0x41, 0x41, 0x41, 0x22} // C 67
#define data_ascii_D {0x7F, 0x41, 0x41, 0x41, 0x3E} // D 68
#define data_ascii_E {0x7F, 0x49, 0x49, 0x49, 0x41} // E 69
#define data_ascii_F {0x7F, 0x09, 0x09, 0x09, 0x01} // F 70
#define data_ascii_G {0x3E, 0x41, 0x41, 0x51, 0x73} // G 71
#define data_ascii_H {0x7F, 0x08, 0x08, 0x08, 0x7F} // H 72
#define data_ascii_I {0x00, 0x41, 0x7F, 0x41, 0x00} // I 73
#define data_ascii_J {0x20, 0x40, 0x41, 0x3F, 0x01} // J 74
#define data_ascii_K {0x7F, 0x08, 0x14, 0x22, 0x41} // K 75
#define data_ascii_L {0x7F, 0x40, 0x40, 0x40, 0x40} // L 76
#define data_ascii_M {0x7F, 0x02, 0x1C, 0x02, 0x7F} // M 77
#define data_ascii_N {0x7F, 0x04, 0x08, 0x10, 0x7F} // N 78
#define data_ascii_O {0x3E, 0x41, 0x41, 0x41, 0x3E} // O 79
#define data_ascii_P {0x7F, 0x09, 0x09, 0x09, 0x06} // P 80
#define data_ascii_Q {0x3E, 0x41, 0x51, 0x21, 0x5E} // Q 81
#define data_ascii_R {0x7F, 0x09, 0x19, 0x29, 0x46} // R 82
#define data_ascii_S {0x26, 0x49, 0x49, 0x49, 0x32} // S 83
#define data_ascii_T {0x03, 0x01, 0x7F, 0x01, 0x03} // T 84
#define data_ascii_U {0x3F, 0x40, 0x40, 0x40, 0x3F} // U 85
#define data_ascii_V {0x1F, 0x20, 0x40, 0x20, 0x1F} // V 86
#define data_ascii_W {0x3F, 0x40, 0x38, 0x40, 0x3F} // W 87
#define data_ascii_X {0x63, 0x14, 0x08, 0x14, 0x63} // X 88
#define data_ascii_Y {0x03, 0x04, 0x78, 0x04, 0x03} // Y 89
#define data_ascii_Z {0x61, 0x59, 0x49, 0x4D, 0x43} // Z 90
//byte data_0[5]={0x03E, B00111110, 0x049, 0x045, 0x03E};
byte data_0[5]={0x03E, 0x051, 0x049, 0x045, 0x03E};
// ascii 5x7 dot font
byte data_ascii[][5] = {
                       data_ascii_H,
                       data_ascii_A,
                       data_ascii_P,
                       data_ascii_P,
                       data_ascii_Y,
                       data_ascii_null,
                       data_ascii_N,
                       data_ascii_E,
                       data_ascii_W,
                       data_ascii_null,
                       data_ascii_Y,
                       data_ascii_E,
                       data_ascii_A,
                       data_ascii_R
                       }; // 9
// 所有要顯示字的 array
boolean word_array[4][row_size][col_size] = {data_A};
 // LED Matrix
boolean led[row_size][col_size] ;
// 水平方向 led
byte row_pin[row_size]={lpin0, lpin1, lpin2, lpin3, lpin4, lpin5, lpin6};
// 垂直方向 led
byte col_pin[col_size]={hpin0, hpin1, hpin2, hpin3, hpin4};

// 初始設定
void setup()
{
  for (int i = 0; i <= 13; i++)
    pinMode(i , OUTPUT);
  clear_led();
 Serial.begin(9600);
 // for (int j = 0x01; j<=0x80; j <<= 1)
 //   if ((data_0[1] & j) == j)
 //     Serial.println(data_0[0], HEX);
 //     Serial.println(data_0[1], HEX);
      
}

// 主程式迴圈
void loop()
{
  
  // HEX 000
  // 將第一個 word 讀入 led
  for (int i = 0; i < 11; i++)
  {
  hex_word_to_led(i, 1000);
  delay(delay_time);
  clear_led();
  }
  
  /*
  word_to_led(1);
  display_led(led, 1000);
  */
}

void hex_word_to_led(int n, int continue_time)
{
  for(int l = 0; l < continue_time; l++)
  {
    for(int k = 0; k < col_size; k++)
    {
      int j = 0;
      digitalWrite(col_pin[k], HIGH);
      for (byte i = 1; i != 0; i <<= 1)
      {
        if ((data_ascii[n][k] & i) == i)
        {
          digitalWrite(row_pin[j++], LOW);
        }else
        {j++;}     
      }
      clear_led();
    } 
  }
}

// 將第 n 個 word 讀入 led
void word_to_led(int n){
  for (int i = 0; i < row_size; i++)
    for (int j = 0; j < col_size; j++)
      led[i][j] = word_array[n][i][j];
}


// 將 led 全關
void clear_led()
{
  for (int i = 0; i < row_size; i++)
    digitalWrite(row_pin[i], HIGH);
  for (int i = 0; i < col_size; i++)
    digitalWrite(col_pin[i], LOW);
}

// 顯示led
void display_led(byte led[row_size][col_size], int continue_time)
{
  for (int k = 0; k < continue_time; k++)
  {
    for (int i = 0; i < col_size; i++)
    {
      // 將 COL 打開,以 ROW 為資料
      digitalWrite(col_pin[i], HIGH);
      for (int j = 0; j < row_size; j++)
      {
        if (led[j][i] == 1)
        {
          digitalWrite(row_pin[j], LOW);
        }
      }
      clear_led();
    }
  }
}

Attachments (1)

Download all attachments as: .zip