maxim10373
Advanced Member level 4
Hello
This is a Arduino Nano with Max7219 Eight digit display stop watch project;
I got this sketch from the net. There is a small problem in this that it works only after two or three resets after turning on the power.
Sometimes it doesn't work at all. Tried many displays of max7219 and it is still the same.
Other sketches are working fine with this Arduino and display.
Can anyone help?
This is a Arduino Nano with Max7219 Eight digit display stop watch project;
I got this sketch from the net. There is a small problem in this that it works only after two or three resets after turning on the power.
Sometimes it doesn't work at all. Tried many displays of max7219 and it is still the same.
Other sketches are working fine with this Arduino and display.
Can anyone help?
Code:
#define RB5 5 // CS
#define RB6 4 // DATA
#define RB7 6 // CLK
#define SW1 2 // white PB
#define SW2 3 // red PB
void setup() {
pinMode(RB5, OUTPUT);
pinMode(RB6, OUTPUT);
pinMode(RB7, OUTPUT);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
//pullups
digitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
digitalWrite(RB5, 0);
init_MAX7219();
}
int digit;
unsigned int count;
char array1[10];
void loop() {
if (!digitalRead(SW1)) {
count = 99999;
for (int i = 0; i <= count; i++) {
bcdOut(i);
delay(100);
if (!digitalRead(SW2)) break;
} // end for
} //end if
}
int bcdOut(int j) {
// convert to BCD send to MAX7219
// get 1st digit of j
digit = j % 10; // MOD J
writeMAX7219(1, digit);
// get 2nd digit of j
j = j / 10;
digit = j % 10; // MOD j
// if (j == 0) digit = 0x0f;
writeMAX7219(2, digit + 0x80);
// get 3rd digit of j
j = j / 10;
digit = j % 10; // MOD j
if (j == 0) digit = 0x0f;
writeMAX7219(3, digit);
// get 4th digit of j
j = j / 10;
digit = j % 10; // MOD j
if (j == 0) digit = 0x0f;
writeMAX7219(4, digit);
// get 5th digit of j
j = j / 10;
digit = j % 10; // MOD j
if (j == 0) digit = 0x0f;
writeMAX7219(5, digit);
}
// note used reference only
// location is place on LCD display.
void typeInt(int k) {
char array1[10];
itoa(k, array1, 10); // int to string
// typeln(array1, location);
}
// shift data to MAX7219
// RB7 -> CLK, RB6 -> DATA, RB5 -> CS not
void ssrOut(unsigned char val) {
int j;
for (j = 1; j <= 8; j++) { // shift out MSB first
unsigned char temp = val & 0x80; // MSB out first
if (temp == 0x80) digitalWrite(RB6, 1); // RB6 DATA
else digitalWrite(RB6, 0);
digitalWrite(RB7, 1);
// delay(1);
digitalWrite(RB7, 0);
val = val << 1; // shift one place left
} // next j
}
void pulseCS(void) {
digitalWrite(RB5, 1);
// delay(2);
digitalWrite(RB5, 0);
}
void init_MAX7219(void) {
digitalWrite(RB5, 0); // CS NOT
// set decode mode
ssrOut(0x09); // address
// ssrOut(0x00); // no decode
ssrOut(0xFF); // 4-bit BCD decode eight digits
pulseCS();
// set intensity 0x00 to 0x0f
ssrOut(0x0A); // address
ssrOut(0x08); // 7/32
pulseCS();
// set scan limit 0-7
ssrOut(0x0B); // address
// ssrOut(0x07); // 8 digits
ssrOut(0x07); // 4 digits
pulseCS();
// clear MAX7219
for (int i = 1; i <= 8; i++) {
ssrOut(i);
ssrOut(0x0F);
pulseCS();
}
// set for normal operation
ssrOut(0x0C); // address
// ssrOut(0x00); // Off
ssrOut(0x01); // On
pulseCS();
}
void writeMAX7219(char address, char data) {
if ((address < 1) || (address > 8)) return;
ssrOut(address); // valid numbers 1-8
ssrOut(data);
pulseCS();
}