1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| /*
8 X 7-Segment MAX7219 Driver C Code
Author Jayanth Devarayanadurga
Data 02/03/2015
*/
sbit Data7219 at LATD0_bit;
sbit Load7219 at LATD1_bit ;
sbit Clk7219 at LATD2_bit;
sbit Data7219_Direction at LATD0_bit;
sbit Load7219_Direction at LATD1_bit;
sbit Clk7219_Direction at LATD2_bit;
const unsigned char Font_B[16] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,
0x7f,0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47
};
void Send7219 (char,char);
void Send7219byte(char);
void MAX7219init();
void Display(float);
unsigned int value = 0;
void MAX7219init() {
Data7219_Direction = 0;
Load7219_Direction = 0;
Clk7219_Direction = 0;
Data7219 = 0;
Load7219 = 0;
Clk7219 = 0;
Send7219(0x09, 0x00); //Decode Mode
Send7219(0x0A, 0x05); //Brightness
Send7219(0x0B, 0x07); //Scan limit
Send7219(0x0C, 0x01);
Send7219(0x0F, 0x00);
}
void Send7219(char Digit,char Data) {
Send7219byte(Digit);
Send7219byte(Data);
Data7219 = 0;
Load7219 = 1;
Delay_us(20);
Load7219 = 0;
}
void Send7219byte(char byte) {
unsigned char i;
for(i = 0; i < 8; i++)
{
if(byte & 0x80)
Data7219 = 1;
else
Data7219 = 0;
Clk7219 = 1;
Delay_us(20);
Clk7219 = 0;
byte <<= 1;
}
}
void Display(unsigned int val) {
unsigned char dispskip; // add 1 if "." found within string
unsigned char i, str[17];
IntToStr(val, str); //Convert float value to string
Ltrim(str); //remove spaces padded to left of string
dispskip = strlen(str) - 1;
if(dispskip >= 8)dispskip = 7;
i = 0;
while(i <= dispskip) {
if((str[dispskip-i] >= 0x30) && (str[dispskip - i] <= 0x39)) {
Send7219(i+1, (Font_B[str[dispskip-i] - 0x30])); //0-9
}
i++;
}
i++;
while(i <= 8) {
Send7219(i, 0);
i++;
}
}
void Blink_Digit(unsigned char digit) {
Delay_ms(500);
Send7219(digit, 0);
Delay_ms(500);
}
void main() {
TRISA = 0xFF;
TRISB = 0x00;
PORTB = 0x00;
LATB = 0x00;
TRISD = 0x00;
PORTD = 0x00;
LATD = 0x00;
ANSELA = 0x01;
ANSELB = 0x00;
ANSELC = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
CM1CON0 = 0x00;
CM2CON0 = 0x00;
MAX7219init();
while(1) {
unsigned char i;
value = 1023;
Display(value);
Blink_Digit(2);
}
} |