基于ArduinoNano的LED点阵时钟探索(1)四合一MAX7219+DS3231

基于ArduinoNano的LED点阵时钟探索(1)四合一MAX7219+DS3231

基于ArduinoNano的LED点阵时钟探索(1)四合一MAX7219+DS3231_第1张图片

这篇文章是自己打算做一个LED点阵时钟记录学习进度的,刚完成第一次点亮MAX7219四合一模块,所以先做好学习记录。

  1. 硬件连线
Arduino nano DS3231
3.3v vcc
GND GND
A3 SCL
A4 SCL
Arduino nano Max7219
5V vcc
GND GND
11 DIN
9 CS
13 CLK
  1. 模块介绍
    DS3231是一款高精度I2C实时时钟器件,具有集成的温度补偿晶体振荡器。该器件包含电池输入端,断开主电源时仍可保持精确计时。集成的晶体振荡器可提高器件的长期精确度。DS3231的寄存器能保存秒、分、时、星期、日期、月、年和闹钟设置等信息。少于31天的月份,可自动调整月末日期,包括闰年补偿。时钟的工作格式为24小时或带AM/PM指示的12小时格式。DS3231提供两个可编程日历闹钟和一路可编程方波输出……
    MAX7219……原理什么的需要的自己搜一下就可以啦,在这里就不赘述了(我单片机就懂一点皮毛,这些原理大部分我也看不懂,写多了就显得装杯了),直接上代码吧
  2. 实验代码

代码部分参考的mixly,Mixly官网基于ArduinoNano的LED点阵时钟探索(1)四合一MAX7219+DS3231_第2张图片
所需要的库文件在文章末尾有链接,有需要的自己下载就好



#include 
#include 
#include 
#include 
#include 

SoftwareWire myWire(A3,A4);//DS3231引脚sda-a3 scl-a4

RtcDS3231<SoftwareWire> Rtc(myWire);

Max72xxPanel myMatrix = Max72xxPanel(9,4,1);//max7219引脚din-11 cs-9 clk-13
uint8_t  LEDArray[8];
unsigned char const LedArray0[8] PROGMEM ={
     0x7e,0x66,0x66,0x66,0x66,0x66,0x66,0x7e};
unsigned char const LedArray1[8] PROGMEM ={
     0x7e,0x18,0x18,0x18,0x18,0x18,0x1c,0x18};
unsigned char const LedArray2[8] PROGMEM ={
     0x7e,0x06,0x06,0x7e,0x60,0x60,0x60,0x7e};
unsigned char const LedArray3[8] PROGMEM ={
     0x7e,0x60,0x60,0x7e,0x60,0x60,0x60,0x7e};
unsigned char const LedArray4[8] PROGMEM ={
     0x60,0x60,0x60,0x7e,0x66,0x66,0x66,0x66};
unsigned char const LedArray5[8] PROGMEM ={
     0x7e,0x60,0x60,0x7e,0x06,0x06,0x06,0x7e};
unsigned char const LedArray6[8] PROGMEM ={
     0x7e,0x66,0x66,0x7e,0x06,0x06,0x06,0x7e};
unsigned char const LedArray7[8] PROGMEM ={
     0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x7e};
unsigned char const LedArray8[8] PROGMEM ={
     0x7e,0x66,0x66,0x7e,0x66,0x66,0x66,0x7e};
unsigned char const LedArray9[8] PROGMEM ={
     0x7e,0x60,0x60,0x7e,0x66,0x66,0x66,0x7e};

void setup(){
     
  Rtc.Begin();
  Rtc.SetIsRunning(true);
  //Rtc.SetDateTime(RtcDateTime(__DATE__, __TIME__));//获取系统时间
}

void loop(){
     
  //Rtc.SetDateTime(RtcDateTime("Jan/01/2020", "08:00:00"));
  myMatrix.setIntensity(0);//点阵亮度调节数值0-5
  myMatrix.setRotation(0,1);//点阵显示方向数值1(270度)
  myMatrix.setRotation(1,1);
  myMatrix.setRotation(2,1);
  myMatrix.setRotation(3,1);
  int year = Rtc.GetDateTime().Year();
  int mon = Rtc.GetDateTime().Month();
  int day = Rtc.GetDateTime().Day();
  int hour = Rtc.GetDateTime().Hour();
  int min = Rtc.GetDateTime().Minute();
  int a=hour/10;
  int b=hour%10;
 if(a==0){
     
  memcpy_P(&LEDArray, &LedArray0, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=0*8; index_j<0*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
  if(a==1){
     
  memcpy_P(&LEDArray, &LedArray1, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=0*8; index_j<0*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(a==2){
     
  memcpy_P(&LEDArray, &LedArray2, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=0*8; index_j<0*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==0){
     
  memcpy_P(&LEDArray, &LedArray0, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==1){
     
  memcpy_P(&LEDArray, &LedArray1, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==2){
     
  memcpy_P(&LEDArray, &LedArray2, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==3){
     
  memcpy_P(&LEDArray, &LedArray3, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==4){
     
  memcpy_P(&LEDArray, &LedArray4, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==5){
     
  memcpy_P(&LEDArray, &LedArray5, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==6){
     
  memcpy_P(&LEDArray, &LedArray6, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==7){
     
  memcpy_P(&LEDArray, &LedArray7, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==8){
     
  memcpy_P(&LEDArray, &LedArray8, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(b==9){
     
  memcpy_P(&LEDArray, &LedArray9, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=1*8; index_j<1*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 //*********************************************************************
  int c=min/10;
  int d=min%10;
 if(c==0){
     
  memcpy_P(&LEDArray, &LedArray0, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=2*8; index_j<2*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
  if(c==1){
     
  memcpy_P(&LEDArray, &LedArray1, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=2*8; index_j<2*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(c==2){
     
  memcpy_P(&LEDArray, &LedArray2, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=2*8; index_j<2*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
  if(c==3){
     
  memcpy_P(&LEDArray, &LedArray3, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=2*8; index_j<2*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
  if(c==4){
     
  memcpy_P(&LEDArray, &LedArray4, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=2*8; index_j<2*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
  if(c==5){
     
  memcpy_P(&LEDArray, &LedArray5, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=2*8; index_j<2*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 //*****************************************************************
 if(d==0){
     
  memcpy_P(&LEDArray, &LedArray0, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
  if(d==1){
     
  memcpy_P(&LEDArray, &LedArray1, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==2){
     
  memcpy_P(&LEDArray, &LedArray2, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==3){
     
  memcpy_P(&LEDArray, &LedArray3, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==4){
     
  memcpy_P(&LEDArray, &LedArray4, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==5){
     
  memcpy_P(&LEDArray, &LedArray5, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==6){
     
  memcpy_P(&LEDArray, &LedArray6, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==7){
     
  memcpy_P(&LEDArray, &LedArray7, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==8){
     
  memcpy_P(&LEDArray, &LedArray8, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
 if(d==9){
     
  memcpy_P(&LEDArray, &LedArray9, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
     
    for(int index_j=3*8; index_j<3*8+8; index_j++)
    {
     
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
 }
}
  1. 总结
    程序现在仅仅能够显示时和分,而且用的方式十分笨拙,只是为了实现功能没有考虑优化,后面有时间会慢慢实现,主要是网上能够参考的资料太少,自己基础又很差,慢慢努力吧。
    Max72xxPanel库
    Adafruit_GFX库

你可能感兴趣的:(学习笔记,arduino)