本帖最后由 PUA 于 2018-3-12 22:20 编辑
我想用Arduino,GPS,max7219制作一个车用HUD显示,但有个问题解决不了。
无法实时调整MAX7219亮度,有没有大神指导一下,谢谢!
还是MAX7219只能开机调整1次?
#include <LedControl.h>
byte algarism[11][5] = {B0111110, B1000001, B1000001, B1000001, B0111110, // 0
B0000000, B0000001, B1111111, B0100001, B0000000, // 1
B0110001, B1001001, B1000101, B1000011, B0100001, // 2
B0110110, B1001001, B1001001, B1000001, B0100010, // 3
B0000100, B1111111, B0100100, B0010100, B0001100, // 4
B1001110, B1010001, B1010001, B1010001, B1110010, // 5
B0100110, B1001001, B1001001, B1001001, B0111110, // 6
B1100000, B1010000, B1001111, B1000000, B1000000, // 7
B0110110, B1001001, B1001001, B1001001, B0110110, // 8
B0111110, B1001001, B1001001, B1001001, B0110010, // 9
B0000000, B0000000, B0000000, B0000000, B0000000
};//空
LedControl mx = LedControl(5, 7, 6, 2); //DIN、CLK、CS、数量
int val = 0;
int i = 0;
int y = 0;
void mxConfig(void) {
int devices = mx.getDeviceCount(); //查询连接MAX7219设备数量
for (int matrix = 0; matrix < devices; matrix++) {
mx.shutdown(matrix, false);
mx.setIntensity(matrix, y); //设置MAX7219亮度
mx.clearDisplay(matrix); //清屏MAX7219
}
}
void writeNumber(byte pos, byte number) {
switch (pos) {
case 1:
mx.setColumn(1, 0, algarism[number][0]);
mx.setColumn(1, 1, algarism[number][1]);
mx.setColumn(1, 2, algarism[number][2]);
mx.setColumn(1, 3, algarism[number][3]);
mx.setColumn(1, 4, algarism[number][4]);
break;
case 2:
mx.setColumn(1, 6, algarism[number][0]);
mx.setColumn(1, 7, algarism[number][1]);
mx.setColumn(0, 0, algarism[number][2]);
mx.setColumn(0, 1, algarism[number][3]);
mx.setColumn(0, 2, algarism[number][4]);
break;
case 3:
mx.setColumn(0, 5, algarism[number][1]);
mx.setColumn(0, 6, algarism[number][2]);
mx.setColumn(0, 7, algarism[number][3]);
break;
}
}
void displayTemp(void) {
//int i = random(0, 200);
writeNumber(1, (i % 10));
if ( i / 100 % 10 == 0 && i / 10 % 10 == 0) {
writeNumber(2, 10);
}
else
{
writeNumber(2, (i / 10 % 10));
}
if (i / 100 % 10 == 1) {
writeNumber(3, (i / 100 % 10));
}
else
{
writeNumber(3, 10);
}
}
void setup() {
Serial.begin(9600);
mxConfig();
}
void loop() {
displayTemp();
val = analogRead(A0);
i = map(val, 0, 1023, 0, 199);
y = map(val, 0, 1023, 0, 15);
Serial.println(val, DEC);
delay(100);
}
|