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
| // this array should hold the segment port values that light the correct segments: ssd_val[0] light segments to show 0, val[8] light segments to show 8 etc
ssd_val[] = {0x5f, 0x06, 0x9b, 0x8f, 0xc6, 0xcd, 0xdd, 0x07, 0xdf, 0xc7};
// I see 4 digits so you should have an array of four char
char display[4];
// these 4 chars will be shown to the four ssd,
display[0] // to the first
display[1] // to the second
display[2] // to the third
display[3] // to the fourth
// to shown a value like 1234, the array content should be 0x1 0x2 0x3 0x4 (values not ADCII character representation)
// then when you write ssd_port=ssd_val[display[0]]; the port will have the correct value to show number 1
// or ssd_port=ssd_val[display[2]]; the port will have the correct value to show number 3 (assuming that the array content is 1234 as above)
//You can also add
char enable ssd[4]; // array that holds the values to turn on/off the segments
// then assign the values so that
enable_ssd[0]; // turns on only the first digit, depends on the port pins you use, for example 0b00001000 if ssd0 on/off is in bit 3
enable_ssd[1]; // turns on only the second digit, depends on the port pins you use, for example 0b00000100 if ssd1 on/off is in bit 2
enable_ssd[2]; // turns on only the third digit, depends on the port pins you use, for example 0b00000010 if ssd2 on/off is in bit 1
enable_ssd[3]; // turns on only the fourth digit, depends on the port pins you use, for example 0b00000001 if ssd3 on/off is in bit 0
ADC interrupt{ // called when result is available
// write code to get ADC result to a variable
}
TIMER interrupt { // to refresh the display, use timer overflow interrupt called about 400 times/sec so that refresh rate is 100Hz
static char index; // a variable that holds the index value when you re-enter the interrupt
// lets assume that the four ssd are controlled from PORTA bits 3 2 1 0, they turn on with high(1) and off with low(0)
PORTA=PORTA & 0xf0; // clear bits 3 2 1 0 so that all ssd are off
ssd_port=ssd_val[display[index]]; //set the new value to the port that control the segments
PORTA=PORTA | enable_ssd[index]; // turn on ssd number "index"
index++ // increment the index for the next interrupt
if (index==4) index=0; // after the fourth digit go to the first again
}
void main (void){
while(1){
// use a variable to hold the previous result to avoid doing conversions for the same result
if (ADC_result != last_result)
{ last_result=ADC_result // store the new value
// do the conversion
res = div(ADC_result, 1000);
thousands = res.quot;
res = div(res.rem, 100);
hundreds = res.quot;
res = div(res.rem, 10);
tens = res.quot;
ones = res.rem;
display[0]=thousands; // you can obviously use this above instead of the thousands variable
display[1]=hundreds;
display[2]=tens;
display[3]=ones;
}
}
} |