arduino MAX7219 ds3231 点阵时钟
本帖最后由 zipcord 于 2018-6-27 16:22 编辑潜水好久了,出来冒个泡 。
正在学习中 ,只能先玩玩表了。只有一块小屏,显示效果就这么回事了, 效果如下
接线图
代码是网上找到的一部分,还有论坛里面的, 串口修改数据
具体看代码吧。
有个问题, 就是代码本事是有个scrollmessage,但是参数是const unsigned char * ,而写入的参数string 的话,不好转换,
基础知识不够扎实啊,希望高手改进改进!
//connet diam
// ds3231
// uno:SDA ---->SDA nano: Sda -->a4
// SCL ---->SCL Scl -->a5
//max7219
//Din -- 12
//Clk -- 11
//Cs --10
#include <avr\pgmspace.h>
#include <LedControl.h>
#include <DS3231.h>
#include <Wire.h>
//for serial read
String comdata = "";
int numdata = {0};
int mark = 0;
//ds3231 need
bool Century=false,h12,PM;
int year, month, date, DoW,week , hour, minute, second,temperature;
const int numDevices = 1; // number of MAX7219s used
const long scrollDelay = 75; // adjust scrolling speed
unsigned long bufferLong = {0};
const unsigned char scrollText[] PROGMEM ={"Scroll Clock , prog by zipcord\0"};
LedControl lc=LedControl(12,11,10,numDevices);
DS3231 Clock;
void setup(){
Wire.begin();
Serial.begin(9600);
for (int x=0; x<numDevices; x++){
lc.shutdown(x,false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(x,12); // Set the brightness to default value
lc.clearDisplay(x); // and clear the display
}
// scrollMessage(scrollText);
Serial.println("Set_time :");
Serial.println("week : 1:Sun; 2:Mon; 3:Tue....7:Sat");
Serial.println();
Serial.println("For example :2018-6-19 Tue 13:30:30");
Serial.println("input: 18 6 19 3 13 30 30");
Serial.println();
}
void loop(){
int j = 0;
while (Serial.available() > 0) //检测串口是否有数据
{
comdata += char(Serial.read());
delay(2);
mark = 1;
// display();
}
if(mark == 1)
{
Serial.println(comdata); //串口打印检测到的数据
for(int i = 0; i < comdata.length() ; i++)
{
if(comdata == ' ')
{
j++;
}
else
{
numdata = numdata * 10 + (comdata - '0');
}
}
comdata = "";
set_time();
Serial.print("set time is");
Serial.println(" OK ");
for(int i = 0; i < 7; i++)
{
numdata = 0;
}
mark = 0;
}
display() ;
}
//自己写的 ,
voidLCD_Write_String(String str1)
{
// trans string to led disp.
for (int x=0;x<str1.length();x++)
{
if (str1=='0')
loadBufferLong(48+0);
else if ( str1=='1')
loadBufferLong(48+1);
else if (str1=='2')
loadBufferLong(48+2);
else if (str1=='3')
loadBufferLong(48+3);
else if (str1=='4')
loadBufferLong(48+4);
else if (str1=='5')
loadBufferLong(48+5);
else if (str1=='6')
loadBufferLong(48+6);
else if (str1=='7')
loadBufferLong(48+7);
else if (str1=='8')
loadBufferLong(48+8);
else if (str1=='9')
loadBufferLong(48+9);
else if (str1=='-')
loadBufferLong(45);
else if (str1==':')
loadBufferLong(58);
else if (str1==' ')
loadBufferLong(32);
else if (str1=='#')
loadBufferLong(35);
else if (str1=='S')
loadBufferLong(83);
else if (str1=='u')
loadBufferLong(117);
else if (str1=='n')
loadBufferLong(110);
else if (str1=='T')
loadBufferLong(84);
else if (str1=='M')
loadBufferLong(77);
else if (str1=='W')
loadBufferLong(87);
else if (str1=='F')
loadBufferLong(70);
else if (str1=='o')
loadBufferLong(111);
else if (str1=='e')
loadBufferLong(101);
else if (str1=='d')
loadBufferLong(100);
else if (str1=='h')
loadBufferLong(104);
else if (str1=='r')
loadBufferLong(114);
else if (str1=='i')
loadBufferLong(105);
else if (str1=='a')
loadBufferLong(97);
else if (str1=='t')
loadBufferLong(116);
else if (str1=='C')
loadBufferLong(67);
else
return;
}
}
void set_time() //DS3231设置时间
{
Clock.setSecond(numdata); //秒
Clock.setMinute(numdata); //分
Clock.setHour(numdata); //时
Clock.setDoW(numdata); //周
Clock.setDate(numdata); //日
Clock.setMonth(numdata);//月
Clock.setYear(numdata); //年
}
void ReadDS3231() //读取DS3231 参数
{
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12,PM);
week=Clock.getDoW();
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
temperature=Clock.getTemperature();
}
StringchgWeekToString(int week)
{
if (week==1)
return "Sun";
else if(week==2)
return "Mon";
else if(week==3)
return "Tue";
else if(week==4)
return "Wed";
else if(week==5)
return "Thu";
else if(week==6)
return "Fri";
else if(week==7)
return "Sat";
}
void display()
{
ReadDS3231();
String disp_str;
String str_minute;
//dimm the ledwhendusk is coming
if (hour>18 && hour<24 || hour>=0 && hour<=9 )
lc.setIntensity(0,1);
else
lc.setIntensity(0,8);
//Format minute
if (minute<10)
str_minute="0"+String(minute);
else
str_minute=String(minute);
//show time in "6-20Wed 10:11 "
disp_str=String(month)+"-"+String(date)+chgWeekToString(week)+" "+String(hour)+":"+str_minute+" "+String(temperature)+"C"+" ";
//Serial.println(disp_str);
LCD_Write_String(disp_str);
}
/////////////////////////////////
//ASCII Table
/////////////////////////////////
const unsigned char font5x7 [] PROGMEM = { //Numeric Font Matrix (Arranged as 7x font data + 1x kerning data)
B00000000,//Space (Char 0x20)
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
6,
B10000000,//!
B10000000,
B10000000,
B10000000,
B00000000,
B00000000,
B10000000,
2,
B10100000,//"
B10100000,
B10100000,
B00000000,
B00000000,
B00000000,
B00000000,
4,
B01010000,//#
B01010000,
B11111000,
B01010000,
B11111000,
B01010000,
B01010000,
6,
B00100000,//$
B01111000,
B10100000,
B01110000,
B00101000,
B11110000,
B00100000,
6,
B11000000,//%
B11001000,
B00010000,
B00100000,
B01000000,
B10011000,
B00011000,
6,
B01100000,//&
B10010000,
B10100000,
B01000000,
B10101000,
B10010000,
B01101000,
6,
B11000000,//'
B01000000,
B10000000,
B00000000,
B00000000,
B00000000,
B00000000,
3,
B00100000,//(
B01000000,
B10000000,
B10000000,
B10000000,
B01000000,
B00100000,
4,
B10000000,//)
B01000000,
B00100000,
B00100000,
B00100000,
B01000000,
B10000000,
4,
B00000000,//*
B00100000,
B10101000,
B01110000,
B10101000,
B00100000,
B00000000,
6,
B00000000,//+
B00100000,
B00100000,
B11111000,
B00100000,
B00100000,
B00000000,
6,
B00000000,//,
B00000000,
B00000000,
B00000000,
B11000000,
B01000000,
B10000000,
3,
B00000000,//-
B00000000,
B11111000,
B00000000,
B00000000,
B00000000,
B00000000,
6,
B00000000,//.
B00000000,
B00000000,
B00000000,
B00000000,
B11000000,
B11000000,
3,
B00000000,///
B00001000,
B00010000,
B00100000,
B01000000,
B10000000,
B00000000,
6,
B01110000,//0
B10001000,
B10011000,
B10101000,
B11001000,
B10001000,
B01110000,
6,
B01000000,//1
B11000000,
B01000000,
B01000000,
B01000000,
B01000000,
B11100000,
4,
B01110000,//2
B10001000,
B00001000,
B00010000,
B00100000,
B01000000,
B11111000,
6,
B11111000,//3
B00010000,
B00100000,
B00010000,
B00001000,
B10001000,
B01110000,
6,
B00010000,//4
B00110000,
B01010000,
B10010000,
B11111000,
B00010000,
B00010000,
6,
B11111000,//5
B10000000,
B11110000,
B00001000,
B00001000,
B10001000,
B01110000,
6,
B00110000,//6
B01000000,
B10000000,
B11110000,
B10001000,
B10001000,
B01110000,
6,
B11111000,//7
B10001000,
B00001000,
B00010000,
B00100000,
B00100000,
B00100000,
6,
B01110000,//8
B10001000,
B10001000,
B01110000,
B10001000,
B10001000,
B01110000,
6,
B01110000,//9
B10001000,
B10001000,
B01111000,
B00001000,
B00010000,
B01100000,
6,
B00000000,//:
B11000000,
B11000000,
B00000000,
B11000000,
B11000000,
B00000000,
3,
B00000000,//;
B11000000,
B11000000,
B00000000,
B11000000,
B01000000,
B10000000,
3,
B00010000,//<
B00100000,
B01000000,
B10000000,
B01000000,
B00100000,
B00010000,
5,
B00000000,//=
B00000000,
B11111000,
B00000000,
B11111000,
B00000000,
B00000000,
6,
B10000000,//>
B01000000,
B00100000,
B00010000,
B00100000,
B01000000,
B10000000,
5,
B01110000,//?
B10001000,
B00001000,
B00010000,
B00100000,
B00000000,
B00100000,
6,
B01110000,//@
B10001000,
B00001000,
B01101000,
B10101000,
B10101000,
B01110000,
6,
B01110000,//A
B10001000,
B10001000,
B10001000,
B11111000,
B10001000,
B10001000,
6,
B11110000,//B
B10001000,
B10001000,
B11110000,
B10001000,
B10001000,
B11110000,
6,
B01110000,//C
B10001000,
B10000000,
B10000000,
B10000000,
B10001000,
B01110000,
6,
B11100000,//D
B10010000,
B10001000,
B10001000,
B10001000,
B10010000,
B11100000,
6,
B11111000,//E
B10000000,
B10000000,
B11110000,
B10000000,
B10000000,
B11111000,
6,
B11111000,//F
B10000000,
B10000000,
B11110000,
B10000000,
B10000000,
B10000000,
6,
B01110000,//G
B10001000,
B10000000,
B10111000,
B10001000,
B10001000,
B01111000,
6,
B10001000,//H
B10001000,
B10001000,
B11111000,
B10001000,
B10001000,
B10001000,
6,
B11100000,//I
B01000000,
B01000000,
B01000000,
B01000000,
B01000000,
B11100000,
4,
B00111000,//J
B00010000,
B00010000,
B00010000,
B00010000,
B10010000,
B01100000,
6,
B10001000,//K
B10010000,
B10100000,
B11000000,
B10100000,
B10010000,
B10001000,
6,
B10000000,//L
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
B11111000,
6,
B10001000,//M
B11011000,
B10101000,
B10101000,
B10001000,
B10001000,
B10001000,
6,
B10001000,//N
B10001000,
B11001000,
B10101000,
B10011000,
B10001000,
B10001000,
6,
B01110000,//O
B10001000,
B10001000,
B10001000,
B10001000,
B10001000,
B01110000,
6,
B11110000,//P
B10001000,
B10001000,
B11110000,
B10000000,
B10000000,
B10000000,
6,
B01110000,//Q
B10001000,
B10001000,
B10001000,
B10101000,
B10010000,
B01101000,
6,
B11110000,//R
B10001000,
B10001000,
B11110000,
B10100000,
B10010000,
B10001000,
6,
B01111000,//S
B10000000,
B10000000,
B01110000,
B00001000,
B00001000,
B11110000,
6,
B11111000,//T
B00100000,
B00100000,
B00100000,
B00100000,
B00100000,
B00100000,
6,
B10001000,//U
B10001000,
B10001000,
B10001000,
B10001000,
B10001000,
B01110000,
6,
B10001000,//V
B10001000,
B10001000,
B10001000,
B10001000,
B01010000,
B00100000,
6,
B10001000,//W
B10001000,
B10001000,
B10101000,
B10101000,
B10101000,
B01010000,
6,
B10001000,//X
B10001000,
B01010000,
B00100000,
B01010000,
B10001000,
B10001000,
6,
B10001000,//Y
B10001000,
B10001000,
B01010000,
B00100000,
B00100000,
B00100000,
6,
B11111000,//Z
B00001000,
B00010000,
B00100000,
B01000000,
B10000000,
B11111000,
6,
B11100000,//[
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
B11100000,
4,
B00000000,//(Backward Slash)
B10000000,
B01000000,
B00100000,
B00010000,
B00001000,
B00000000,
6,
B11100000,//]
B00100000,
B00100000,
B00100000,
B00100000,
B00100000,
B11100000,
4,
B00100000,//^
B01010000,
B10001000,
B00000000,
B00000000,
B00000000,
B00000000,
6,
B00000000,//_
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B11111000,
6,
B10000000,//`
B01000000,
B00100000,
B00000000,
B00000000,
B00000000,
B00000000,
4,
B00000000,//a
B00000000,
B01110000,
B00001000,
B01111000,
B10001000,
B01111000,
6,
B10000000,//b
B10000000,
B10110000,
B11001000,
B10001000,
B10001000,
B11110000,
6,
B00000000,//c
B00000000,
B01110000,
B10001000,
B10000000,
B10001000,
B01110000,
6,
B00001000,//d
B00001000,
B01101000,
B10011000,
B10001000,
B10001000,
B01111000,
6,
B00000000,//e
B00000000,
B01110000,
B10001000,
B11111000,
B10000000,
B01110000,
6,
B00110000,//f
B01001000,
B01000000,
B11100000,
B01000000,
B01000000,
B01000000,
6,
B00000000,//g
B01111000,
B10001000,
B10001000,
B01111000,
B00001000,
B01110000,
6,
B10000000,//h
B10000000,
B10110000,
B11001000,
B10001000,
B10001000,
B10001000,
6,
B01000000,//i
B00000000,
B11000000,
B01000000,
B01000000,
B01000000,
B11100000,
4,
B00010000,//j
B00000000,
B00110000,
B00010000,
B00010000,
B10010000,
B01100000,
5,
B10000000,//k
B10000000,
B10010000,
B10100000,
B11000000,
B10100000,
B10010000,
5,
B11000000,//l
B01000000,
B01000000,
B01000000,
B01000000,
B01000000,
B11100000,
4,
B00000000,//m
B00000000,
B11010000,
B10101000,
B10101000,
B10001000,
B10001000,
6,
B00000000,//n
B00000000,
B10110000,
B11001000,
B10001000,
B10001000,
B10001000,
6,
B00000000,//o
B00000000,
B01110000,
B10001000,
B10001000,
B10001000,
B01110000,
6,
B00000000,//p
B00000000,
B11110000,
B10001000,
B11110000,
B10000000,
B10000000,
6,
B00000000,//q
B00000000,
B01101000,
B10011000,
B01111000,
B00001000,
B00001000,
6,
B00000000,//r
B00000000,
B10110000,
B11001000,
B10000000,
B10000000,
B10000000,
6,
B00000000,//s
B00000000,
B01110000,
B10000000,
B01110000,
B00001000,
B11110000,
6,
B01000000,//t
B01000000,
B11100000,
B01000000,
B01000000,
B01001000,
B00110000,
6,
B00000000,//u
B00000000,
B10001000,
B10001000,
B10001000,
B10011000,
B01101000,
6,
B00000000,//v
B00000000,
B10001000,
B10001000,
B10001000,
B01010000,
B00100000,
6,
B00000000,//w
B00000000,
B10001000,
B10101000,
B10101000,
B10101000,
B01010000,
6,
B00000000,//x
B00000000,
B10001000,
B01010000,
B00100000,
B01010000,
B10001000,
6,
B00000000,//y
B00000000,
B10001000,
B10001000,
B01111000,
B00001000,
B01110000,
6,
B00000000,//z
B00000000,
B11111000,
B00010000,
B00100000,
B01000000,
B11111000,
6,
B00100000,//{
B01000000,
B01000000,
B10000000,
B01000000,
B01000000,
B00100000,
4,
B10000000,//|
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
2,
B10000000,//}
B01000000,
B01000000,
B00100000,
B01000000,
B01000000,
B10000000,
4,
B00000000,//~
B00000000,
B00000000,
B01101000,
B10010000,
B00000000,
B00000000,
6,
B01100000,// (Char 0x7F)
B10010000,
B10010000,
B01100000,
B00000000,
B00000000,
B00000000,
5
};
// Scroll Message
void scrollMessage(const unsigned char * messageString) {
int counter = 0;
int myChar=0;
do {
// read back a char
myChar =pgm_read_byte_near(messageString + counter);
if (myChar != 0){
loadBufferLong(myChar);
}
counter++;
}
while (myChar != 0);
}
// Load character into scroll buffer
void loadBufferLong(int ascii){
if (ascii >= 0x20 && ascii <=0x7f){
for (int a=0;a<7;a++){ // Loop 7 times for a 5x7 font
unsigned long c = pgm_read_byte_near(font5x7 + ((ascii - 0x20) * 8) + a); // Index into character table to get row data
unsigned long x = bufferLong ; // Load current scroll buffer
x = x | c; // OR the new character onto end of current
bufferLong = x; // Store in buffer
}
byte count = pgm_read_byte_near(font5x7 +((ascii - 0x20) * 8) + 7); // Index into character table for kerning data
for (byte x=0; x<count;x++){
rotateBufferLong();
printBufferLong();
delay(scrollDelay);
}
}
}
// Rotate the buffer
void rotateBufferLong(){
for (int a=0;a<7;a++){ // Loop 7 times for a 5x7 font
unsigned long x = bufferLong ; // Get low buffer entry
byte b = bitRead(x,31); // Copy high order bit that gets lost in rotation
x = x<<1; // Rotate left one bit
bufferLong = x; // Store new low buffer
x = bufferLong ; // Get high buffer entry
x = x<<1; // Rotate left one bit
bitWrite(x,0,b); // Store saved bit
bufferLong = x; // Store new high buffer
}
}
// Display Buffer on LED matrix
void printBufferLong(){
for (int a=0;a<7;a++){ // Loop 7 times for a 5x7 font
unsigned long x = bufferLong ; // Get high buffer entry
byte y = x; // Mask off first character
lc.setRow(3,a,y); // Send row to relevent MAX7219 chip
x = bufferLong ; // Get low buffer entry
y = (x>>24); // Mask off second character
lc.setRow(2,a,y); // Send row to relevent MAX7219 chip
y = (x>>16); // Mask off third character
lc.setRow(1,a,y); // Send row to relevent MAX7219 chip
y = (x>>8); // Mask off forth character
lc.setRow(0,a,y); // Send row to relevent MAX7219 chip
}
}
先收藏为敬。
页:
[1]