Max value that is read is 30 V. The red section is to get max 30 V from max adc value which will be 255.
I have modified the code so that it displays floating point values but there is a bug which I was not able to find. The display flickers too much. See if you can fix the bug.
Keil uVision 4 Code v1.0.0
Code C - [expand] |
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
| #include <reg51.h>
#include <intrins.h>
sbit CS = P3^5;
sbit Clk = P3^6;
sbit DATI = P3^7;
sbit DATO = P3^7;
float dat = 0x00;
unsigned char count = 0x00;
unsigned char CH;
unsigned int dis[] = {0x00, 0x00, 0x00, 0x00};
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char code tablewe[]={ 0xfd,0xfb,0xf7,0xef,0xdf,0xfe };
//Function Prototypes
void floattoint(float FP_NUM);
//Sub Function
void floattoint(float FP_NUM) {
float fpnumber;
int befdec, aftdec;
fpnumber = FP_NUM;
befdec = fpnumber; // Fractional part is truncated
// 12.163456 becomes 12
aftdec = fpnumber * 100; // 12.163456 becomes 1216
aftdec = aftdec - (befdec * 100); // 1216 - 1200 = 16
if ((fpnumber >= 1) && (fpnumber < 10)) {
dis[0] = (befdec/1)%10 ;
dis[1] = (aftdec/10);
dis[2] = (aftdec/1)%10;
}
else if ((fpnumber >= 10) && (fpnumber < 40)) {
dis[0] = (befdec/10);
dis[1] = (befdec)%10;
dis[2] = (aftdec/10);
dis[3] = (aftdec)%10;
}
}
unsigned char adc0832(unsigned char CH)
{
unsigned char i,test,adval;
adval = 0x00;
test = 0x00;
Clk = 0;
CS=1;
DATI = 1;
CS = 0;
_nop_();
Clk = 1;
_nop_();
if ( CH == 0x00 )
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 0;
_nop_();
Clk = 1;
_nop_();
}
else
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
}
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
for( i = 0;i < 8;i++ )
{
_nop_();
adval <<= 1;
Clk = 1;
_nop_();
Clk = 0;
if (DATO)
adval |= 0x01;
//else
// adval |= 0x00;
}
for (i = 0; i < 8; i++)
{
test >>= 1;
if (DATO)
test |= 0x80;
//else
//test |= 0x00;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
}
if (adval == test)
dat = adval;
_nop_();
CS = 1;
DATO = 1;
Clk = 1;
return dat;
}
void delay(void)
{
unsigned int k;
for(k=10;k<100;k++);
}
void display(void)
{
if ((dat >= 1) && (dat < 10)) {
P2=0xf2;
P0= tab[dis[0]]& 0x7f;
delay();
P2=0xf4;
P0=tab[dis[1]];
delay();
P2=0xf8;
P0=tab[dis[2]];
delay();
}
else if ((dat >= 10) && (dat < 40)) {
P2=0xf1;
P0= tab[dis[0]];
delay();
P2=0xf2;
P0=tab[dis[1]]& 0x7f;
delay();
P2=0xf4;
P0=tab[dis[2]];
delay();
P2=0xf8;
P0=tab[dis[3]];
delay();
}
}
void main(void)
{
P1=0xff;
P0=0xff;
P2=0x00;
delay();
CH = 0x00;
TMOD = 0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
IE = 0x82;
TR0 = 1;
while(1)
{
dat = dat*500/255;
dat = dat / 16.66666666666667;
floattoint(dat);
display();
}
}
void timer0(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if (count == 0x01)
{
count = 0x00;
dat = adc0832(CH);
}
} |
Edit:
Fixed code. Works beautifully.
Keil uVision 4 C51 C Code v1.0.1
Code C - [expand] |
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
| #include <reg51.h>
#include <intrins.h>
#include <math.h>
sbit CS = P3^5;
sbit Clk = P3^6;
sbit DATI = P3^7;
sbit DATO = P3^7;
float dat = 0x00, oldval = 0;
unsigned char count = 0x00;
unsigned char CH;
unsigned int dis[] = {0x00, 0x00, 0x00, 0x00};
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char code tablewe[]={0xfd,0xfb,0xf7,0xef,0xdf,0xfe};
//Function Prototypes
unsigned char adc0832(unsigned char CH);
void floattoint(float FP_NUM);
void delay(void);
void display(void);
//Sub Function
void floattoint(float FP_NUM) {
float fpnumber;
int befdec, aftdec;
fpnumber = FP_NUM;
befdec = fpnumber; // Fractional part is truncated
// 12.163456 becomes 12
aftdec = fpnumber * 100; // 12.163456 becomes 1216
aftdec = aftdec - (befdec * 100); // 1216 - 1200 = 16
if ((fpnumber >= 1) && (fpnumber < 10)) {
dis[0] = (befdec/1)%10 ;
dis[1] = (aftdec/10);
dis[2] = (aftdec/1)%10;
}
else if ((fpnumber >= 10) && (fpnumber < 40)) {
dis[0] = (befdec/10);
dis[1] = (befdec)%10;
dis[2] = (aftdec/10);
dis[3] = (aftdec)%10;
}
}
unsigned char adc0832(unsigned char CH)
{
unsigned char i,test,adval;
adval = 0x00;
test = 0x00;
Clk = 0;
CS=1;
DATI = 1;
CS = 0;
_nop_();
Clk = 1;
_nop_();
if ( CH == 0x00 )
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 0;
_nop_();
Clk = 1;
_nop_();
}
else
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
}
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
for( i = 0;i < 8;i++ )
{
_nop_();
adval <<= 1;
Clk = 1;
_nop_();
Clk = 0;
if (DATO)
adval |= 0x01;
//else
// adval |= 0x00;
}
for (i = 0; i < 8; i++)
{
test >>= 1;
if (DATO)
test |= 0x80;
//else
//test |= 0x00;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
}
if (adval == test)
dat = adval;
_nop_();
CS = 1;
DATO = 1;
Clk = 1;
return dat;
}
void delay(void)
{
unsigned int k;
for(k=10;k<100;k++);
}
void display(void)
{
if ((dat >= 1) && (dat < 10)) {
P2=0xf2;
P0= tab[dis[0]]& 0x7f;
delay();
P2=0xf4;
P0=tab[dis[1]];
delay();
P2=0xf8;
P0=tab[dis[2]];
delay();
}
else if ((dat >= 10) && (dat < 40)) {
P2=0xf1;
P0= tab[dis[0]];
delay();
P2=0xf2;
P0=tab[dis[1]]& 0x7f;
delay();
P2=0xf4;
P0=tab[dis[2]];
delay();
P2=0xf8;
P0=tab[dis[3]];
delay();
}
}
void main(void)
{
P1=0xff;
P0=0xff;
P2=0x00;
delay();
CH = 0x00;
TMOD = 0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
IE = 0x82;
TR0 = 1;
while(1)
{
floattoint(dat);
display();
}
}
void timer0(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if (count == 0x01)
{
count = 0x00;
dat = adc0832(CH);
dat = dat*500/255;
dat = dat / 16.667;
}
} |
A bug found.
Edit 2:
Bug fixed. Final Code.
Keil uVision 4 C51 C Code v1.0.3
Code C - [expand] |
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
| #include <reg51.h>
#include <intrins.h>
#include <math.h>
sbit CS = P3^5;
sbit Clk = P3^6;
sbit DATI = P3^7;
sbit DATO = P3^7;
float dat = 0x00, oldval = 0;
unsigned char count = 0x00;
unsigned char CH;
unsigned int dis[] = {0x00, 0x00, 0x00, 0x00};
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char code tablewe[]={0xfd,0xfb,0xf7,0xef,0xdf,0xfe};
//Function Prototypes
unsigned char adc0832(unsigned char CH);
void floattoint(float FP_NUM);
void delay(void);
void display(void);
//Sub Function
void floattoint(float FP_NUM) {
float fpnumber;
int befdec, aftdec;
fpnumber = FP_NUM;
befdec = fpnumber; // Fractional part is truncated
// 12.163456 becomes 12
aftdec = fpnumber * 100; // 12.163456 becomes 1216
aftdec = aftdec - (befdec * 100); // 1216 - 1200 = 16
if ((fpnumber >= 0) && (fpnumber < 10)) {
dis[0] = (befdec/1)%10 ;
dis[1] = (aftdec/10);
dis[2] = (aftdec/1)%10;
}
else if ((fpnumber >= 10) && (fpnumber < 40)) {
dis[0] = (befdec/10);
dis[1] = (befdec)%10;
dis[2] = (aftdec/10);
dis[3] = (aftdec)%10;
}
}
unsigned char adc0832(unsigned char CH)
{
unsigned char i,test,adval;
adval = 0x00;
test = 0x00;
Clk = 0;
CS=1;
DATI = 1;
CS = 0;
_nop_();
Clk = 1;
_nop_();
if ( CH == 0x00 )
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 0;
_nop_();
Clk = 1;
_nop_();
}
else
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
}
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
for( i = 0;i < 8;i++ )
{
_nop_();
adval <<= 1;
Clk = 1;
_nop_();
Clk = 0;
if (DATO)
adval |= 0x01;
//else
// adval |= 0x00;
}
for (i = 0; i < 8; i++)
{
test >>= 1;
if (DATO)
test |= 0x80;
//else
//test |= 0x00;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
}
if (adval == test)
dat = adval;
_nop_();
CS = 1;
DATO = 1;
Clk = 1;
return dat;
}
void delay(void)
{
unsigned int k;
for(k=10;k<100;k++);
}
void display(void)
{
if ((dat >= 0) && (dat < 10)) {
P2=0xf2;
P0= tab[dis[0]]& 0x7f;
delay();
P2=0xf4;
P0=tab[dis[1]];
delay();
P2=0xf8;
P0=tab[dis[2]];
delay();
}
else if ((dat >= 10) && (dat < 40)) {
P2=0xf1;
P0= tab[dis[0]];
delay();
P2=0xf2;
P0=tab[dis[1]]& 0x7f;
delay();
P2=0xf4;
P0=tab[dis[2]];
delay();
P2=0xf8;
P0=tab[dis[3]];
delay();
}
}
void main(void)
{
P1=0xff;
P0=0xff;
P2=0x00;
delay();
CH = 0x00;
TMOD = 0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
IE = 0x82;
TR0 = 1;
while(1)
{
floattoint(dat);
display();
}
}
void timer0(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if (count == 0x01)
{
count = 0x00;
dat = adc0832(CH);
dat = dat*500/255;
dat = dat / 16.667;
}
} |