Re: LED матрица max7219
//Вот этот скетч использую.
////////// |PORT |pin | Arduino
// MAX7219
#define CLK PD2 // | 4 | D2
#define CS PD3 // | 5 | D3
#define DIN PD4 // | 6 | D4
// DS1307
#define SQW PB4 // | 18 | D12
// DHT11
#define OUT PB0 // | 14 | D8
// BUTTON
#define SET PC0 // | 23 | A0
#define UP PC1 // | 24 | A1
#define DW PC2 // | 25 | A2
// I2C
#define ADDR 0b1101000
#define CPU_F 16000000
#define SCL_F 100000
int beg,obr,br,tik;
byte data_dht[5],www;
byte d0,d1,d2,d3,i1,bb,i3,f1,sett,z;
byte hh,mm,dh_t,dh_h;
byte dat[18][8] = {{B00111100,B01100110,B01101110,B01111110,B01110110,B01100110,B00111100,B00000000},// 0
{B00011000,B00111000,B00011000,B00011000,B00011000,B00011000,B00011000,B00000000},// 1
{B00111100,B01100110,B00000110,B00011100,B00110000,B01100000,B01111110,B00000000},// 2
{B00111100,B01100110,B00000110,B00011100,B00000110,B01100110,B00111100,B00000000},// 3
{B00001110,B00011110,B00110110,B01100110,B01111110,B00000110,B00000110,B00000000},// 4
{B01111110,B01100000,B01111100,B00000110,B00000110,B01100110,B00111100,B00000000},// 5
{B00011100,B00110000,B01100000,B01111100,B01100110,B01100110,B00111100,B00000000},// 6
{B01111110,B00000110,B00000110,B00001100,B00011000,B00011000,B00011000,B00000000},// 7
{B00111100,B01100110,B01100110,B00111100,B01100110,B01100110,B00111100,B00000000},// 8
{B00111100,B01100110,B01100110,B00111110,B00000110,B00001100,B00111000,B00000000},// 9
{B00110000,B00110000,B01111100,B00110000,B00110000,B00110100,B00011000,B00000000},// t = 10
{B01100110,B01100110,B01100110,B01111110,B01100110,B01100110,B01100110,B00000000},// H = 11
{B00000000,B00111000,B01101100,B01101100,B00111000,B00000000,B00000000,B00000000},// градус = 12
{B00000000,B11000110,B11001100,B00011000,B00110000,B01100110,B11000110,B00000000},// % = 13
{B11000110,B11101110,B11111110,B11111110,B11010110,B11000110,B11000110,B00000000},// M = 14
{B01111000,B01101100,B01100110,B01100110,B01100110,B01101100,B01111000,B00000000},// D = 15
{B01111100,B01100110,B01100110,B01111100,B01100110,B01100110,B01111100,B00000000},// B = 16
{B00000000,B00000000,B01011100,B01110110,B01100000,B01100000,B01100000,B00000000} // r = 17
};
void setup() {
// Serial.begin(9600);
DDRB &= ~(1 << SQW);PORTB |= (1 << SQW);
TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
TWSR = 0;
set_time(21,2,5,3,0,4,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
i2c_write(ADDR,0x0E, 0b00010000); // выход SQW, частота 1 Гц
delay(2);
br = EEPROM_read(0);
if(br==255){br=0;EEPROM_write(0,br);}
DDRD |= (1 << DIN) | (1 << CS) | (1 << CLK);
PORTC |= (1 << SET) | (1 << UP) | (1 << DW);
max7219_L(0x0F, 0);// тест выкл.
max7219_L(0x0C, 0x01010101);// вкл. индик.
max7219_L(0x0A, 0x01010101*br);// яркость
max7219_L(0x09, 0);// дешифраторы выкл.
max7219_L(0x0B, 0x07070707);// кол-во разрядов
}
void loop() {
byte sec = (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
byte min = (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
byte hour = ((i2c_read(ADDR,2) & 0x0F) + ((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
byte day = (i2c_read(ADDR,3) & 0x0F);
byte datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
byte mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
byte year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10);
if(((PINC >> SET) & 1) == 0){sett++;www=1;if(sett>5){sett=0;}delay(300);}
if(sett==0 && www==1){set_time(255,255,255,255,255,255,0);www=0;}
if(((PINC >> UP) & 1) == 0 && sett == 1){hour++;if(hour>23){hour=0;}set_time(255,255,255,255,hour,255,255);delay(300);}
if(((PINC >> DW) & 1) == 0 && sett == 1){hour--;if(hour<0){hour=0;}set_time(255,255,255,255,hour,255,255);delay(300);}
if(((PINC >> UP) & 1) == 0 && sett == 2){min++;if(min>59){min=0;}set_time(255,255,255,255,255,min,255);delay(300);}
if(((PINC >> DW) & 1) == 0 && sett == 2){min--;if(min<0){min=0;}set_time(255,255,255,255,255,min,255);delay(300);}
if(((PINC >> UP) & 1) == 0 && sett == 3){datat++;if(datat>31){datat=1;}set_time(255,255,255,datat,255,255,255);delay(300);}
if(((PINC >> DW) & 1) == 0 && sett == 3){datat--;if(datat<1){datat=1;}set_time(255,255,255,datat,255,255,255);delay(300);}
if(((PINC >> UP) & 1) == 0 && sett == 4){mont++;if(mont>12){mont=1;}set_time(255,255,mont,255,255,255,255);delay(300);}
if(((PINC >> DW) & 1) == 0 && sett == 4){mont--;if(mont<1){mont=1;}set_time(255,255,mont,255,255,255,255);delay(300);}
if(((PINC >> UP) & 1) == 0 && sett == 5){br++;if(br>15){br=15;}EEPROM_write(0,br);max7219_L(0x0A, 0x01010101*br);delay(300);}
if(((PINC >> DW) & 1) == 0 && sett == 5){br--;if(br<0){br=0;}EEPROM_write(0,br);max7219_L(0x0A, 0x01010101*br);delay(300);}
if(sett==0){
if(((PINB >> SQW) & 1)==1){bb = 0;}else{bb = 3;}
if(obr==2 &&f1==0){f1=1; dht_read();delay(1000);}
if(obr!=2){f1=0;}
switch(tik){
case 0 : obr=0;break;
case 1 : obr=3;break;
case 2 : obr=2;break;
case 3 : obr=1;break;
case 4 : obr=0;break;
case 5 : obr=7;break;
case 6 : obr=6;break;
case 7 : obr=1;break;
case 8 : obr=0;break;
case 9 : obr=5;break;
case 10 : obr=4;break;
case 11 : obr=1;break;
case 12 : tik=0;break;
}
// Serial.println(data_dht[0]);
beg++;
///// time /////////////////////
if(obr==0){
d0=hour/10;d1=hour%10;d2=min/10;d3=min%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 17, 7, 0)<< beg);
if(z==1||z==2||z==4||z==5){
max7219_L(z+1, (ind(24, 17, 7, 0)|(unsigned long)bb << 15) << beg);}}
if(beg>32){beg=0;tik++;}}
if(obr==1){
d0=hour/10;d1=hour%10;d2=min/10;d3=min%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 17, 7, 0) >> 32 - beg);
if(z==1||z==2||z==4||z==5){
max7219_L(z+1, (ind(24, 17, 7, 0)|(unsigned long)bb << 15) >> 32 - beg);}}
if(beg>32){beg=-120;tik++;}}
/////// data /////////////////////////////////////////////////
if(obr==2){
d0=datat/10;d1=datat%10;d2=mont/10;d3=mont%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 17, 7, 0) << beg);
if(z==6||z==7){
max7219_L(z+1, (ind(24, 17, 7, 0)|(unsigned long)3 << 15) << beg);}}
if(beg>32){beg=0;tik++;}}
if(obr==3){
d0=datat/10;d1=datat%10;d2=mont/10;d3=mont%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 17, 7, 0) >> 32 - beg);
if(z==6||z==7){
max7219_L(z+1, (ind(24, 17, 7, 0)|(unsigned long)3 << 15) >> 32 - beg);}}
if(beg>32){beg=-50;tik++;}}
////// hhhh ////////////////////////////////////////////////////////////////////////
if(obr==4){
d0=11;d1=data_dht[0]/10;d2=data_dht[0]%10;d3=13;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 15, 8, 0) << beg);}
if(beg>32){beg=0;tik++;}}
if(obr==5){
d0=11;d1=data_dht[0]/10;d2=data_dht[0]%10;d3=13;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 15, 8, 0) >> 32 - beg);}
if(beg>32){beg=-50;tik++;}}
////// temp ////////////////////////////////////////////////////////////////////////
if(obr==6){
d0=10;d1=data_dht[2]/10;d2=data_dht[2]%10;d3=12;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 15, 8, 0) << beg);}
if(beg>32){beg=0;tik++;}}
if(obr==7){
d0=10;d1=data_dht[2]/10;d2=data_dht[2]%10;d3=12;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 15, 8, 0) >> 32 - beg);}
if(beg>32){beg=-50;tik++;}}
}
if(sett==1){
d0=11;d1=11;d2=hour/10;d3=hour%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 16, 8, 0));}}
if(sett==2){
d0=14;d1=14;d2=min/10;d3=min%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 16, 8, 0));}}
if(sett==3){
d0=15;d1=15;d2=datat/10;d3=datat%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 16, 8, 0));}}
if(sett==4){
d0=1;d1=14;d2=mont/10;d3=mont%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 16, 8, 0));}}
if(sett==5){
d0=16;d1=17;d2=br/10;d3=br%10;
for(z=0;z<8;z++){
max7219_L(z+1, ind(24, 16, 8, 0));}}
delay(25);
}//loop
void max7219_L(byte reg_n, unsigned long h){
byte h1 = h >> 24,h2 = h >> 16,h3 = h >> 8,h4 = h;
PORTD &=~(1 << CS);WriteBit16(reg_n,h1);WriteBit16(reg_n,h2);WriteBit16(reg_n,h3);WriteBit16(reg_n,h4);PORTD |=(1 << CS);
}
void WriteBit16(byte reg, byte data){
for(char i = 7; i >= 0; i--){
PORTD &= ~(1 << CLK);
if(((reg >> i) & 1) == 1){PORTD |= (1 << DIN);}else{PORTD &= ~(1 << DIN);}
PORTD |=(1 << CLK);}
for(char i = 7; i >= 0; i--){
PORTD &= ~(1 << CLK);
if(((data >> i) & 1) == 1){PORTD |= (1 << DIN);}else{PORTD &= ~(1 << DIN);}
PORTD |=(1 << CLK);}
PORTD &= ~(1 << CLK);PORTD |= (1 << DIN);
}
byte i2c_read(byte i2c_addr, byte i2c_reg){
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // START
while (!(TWCR & (1<<TWINT)));
TWDR = i2c_addr << 1;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR = i2c_reg;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // START
while (!(TWCR & (1<<TWINT)));
TWDR = (i2c_addr << 1) | 1;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
byte i2c_data = TWDR;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
return i2c_data;
}
void i2c_write(byte i2c_addr, byte i2c_reg, byte i2c_dat){
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // START
while (!(TWCR & (1<<TWINT)));
TWDR = i2c_addr << 1;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR = i2c_reg;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR = i2c_dat;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
}
void set_time(byte years, byte days, byte monts, byte datas, byte hours ,byte minute, byte second){
if(second < 255){i2c_write(ADDR,0x00,(second/10<<4)+second%10);}
if(minute < 255){i2c_write(ADDR,0x01,(minute/10<<4)+minute%10);}
if(hours < 255){i2c_write(ADDR,0x02,(hours/10<<4)+hours%10);}
if(days < 255){i2c_write(ADDR,0x03,days);}
if(datas < 255){i2c_write(ADDR,0x04,(datas/10<<4)+datas%10);}
if(monts < 255){i2c_write(ADDR,0x05,(monts/10<<4)+monts%10);}
if(years < 255){i2c_write(ADDR,0x06,(years/10<<4)+years%10);}
}
int dht_read(){
byte id = 0,id1 = 0;
for(id = 0;id < 5;id++){data_dht[id] = 0;}
DDRB |=(1 << OUT);
PORTB &= ~(1 << OUT);
delay(18);
PORTB |= (1 << OUT);
delayMicroseconds(40);
DDRB &= ~(1 << OUT);
delayMicroseconds(80);
while(PINB & (1 << OUT));
for (id = 0; id < 5; id++){
data_dht[id]=0;
for (id1=0; id1<8; id1++){
while(!(PINB & (1 << OUT)));
delayMicroseconds(30);
if (PINB & (1 << OUT)){data_dht[id] |= 1 << (7-id1);}
while(PINB & (1 << OUT));
}}return 1;}
unsigned char EEPROM_read(unsigned int uiAddress){
while(EECR & (1<<EEPE)); // проверка готовности EEPROM
EEAR = uiAddress; // регистр адреса
EECR |= (1<<EERE);// чтение EEPROM
return EEDR; // вывод значения
}
void EEPROM_write(unsigned int uiAddress, unsigned char ucData){
while(EECR & (1<<EEPE)); // проверка готовности EEPROM
EEAR = uiAddress; // регистр адреса
EEDR = ucData; // регистр данных
EECR |= (1<<EEMPE);// Разрешение записи в EEPROM
EECR |= (1<<EEPE); // Запись в EEPROM
}
unsigned long ind(int sd0, int sd1, int sd2, int sd3){
unsigned long long_data = (unsigned long)dat[d0][z]<<sd0 | (unsigned long)dat[d1][z]<<sd1 | (unsigned int)dat[d2][z]<<sd2 | dat[d3][z] << sd3;
return long_data;
}