Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

comparing voltage with giving look up table value in microcontroller

Status
Not open for further replies.
See the attached file.


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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * MCU      PIC18F4580
 * Fosc     16 MHz
 * Author   Jayanth Devarayanadurga  
 */
 
#include <p18f4580.h>
#include <delays.h>
#include <math.h>
 
#pragma config WDT = OFF
#pragma config OSC = HS
#pragma config PWRT = ON
#pragma config LVP = OFF
 
#define ldata PORTD
#define rs PORTBbits.RB0 //define reset bit
#define rw PORTBbits.RB1 //define read/write bit
#define en PORTBbits.RB2 //define enable bit
 
//TCY = 0.25 us
 
//Function prototypes
void float2string(float fp_num);
void adc_read();
void Delay10TCYx(unsigned char unit);
void Delay100TCYx(unsigned char unit);
void Delay1KTCYx(unsigned char unit);
void Delay10KTCYx(unsigned char unit);
void lcdinit();
void lcdcmd (char value);
void lcddata(char value);
void lcdstring(unsigned char *lcd_data);
void MSDelay(int itime);
 
float V[9] = {3.31, 3.32, 3.33, 3.34, 3.35, 3.36, 3.37, 3.38, 3.39};
float I[8] = {0,5,6,16.69,3.94,3,3, 32.44};
float R[9] = {0, 1.1, 1.21, 1.37 ,1.32, 1.50, 1.35, 1.6, 1};
unsigned char disp[] = "ADC Value = ", ctr = 0;
unsigned int adc_val = 0;
unsigned char myString[8];
unsigned int iladc_val = 0, i = 0;
float dladc_val = 0.0, old_val, myflt;
 
//Function Definition
//Sub Function
void float2string(float fp_num){
        long int befdec, aftdec;
        
        fp_num = ceil(fp_num * 100) / 100;
        myflt = fp_num;
        
        befdec = fp_num;                      // Fractional part is truncated
                                                // 12.163456 becomes 12
        aftdec = fp_num * 100;            // 12.163456 becomes 1216
        aftdec = aftdec - (befdec * 100);   // 1216 - 1200 = 16
 
 
        if (fp_num < 1) {
                myString[0] = '0';
                myString[1] = '.';
                myString[2] = (aftdec/10) + 48;
                myString[3] = (aftdec/1)%10 + 48;
                myString[4] = '\0';
                myString[5] = ' ';
                myString[6] = ' ';
                myString[7] = '\0';
 
        }
 
        else if ((fp_num >= 1) && (fp_num < 10)) {
                myString[0] = (befdec/1)%10 + 48;
                myString[1] = '.';
                myString[2] = (aftdec/10) + 48;
                myString[3] = (aftdec/1)%10 + 48;
                myString[4] = '\0';
                myString[5] = '\0';
                myString[6] = '\0';
                myString[7] = '\0';
 
        }
 
        else if ((fp_num >= 10) && (fp_num < 100)) {
                myString[0] = (befdec/10) + 48;
                myString[1] = (befdec/1)%10 + 48;
                myString[2] = '.';
                myString[3] = (aftdec/10) + 48;
                myString[4] = (aftdec/1)%10 + 48;
                myString[5] = '\0';
                myString[6] = ' ';
                myString[7] = '\0';
 
        }
 
        else if ((fp_num >= 100) && (fp_num < 1000)) {
                myString[0] = (befdec/100) + 48;
                myString[1] = (befdec/10)%10 + 48;
                myString[2] = (befdec/1)%10 + 48;
                myString[3] = '.';
                myString[4] = (aftdec/10) + 48;
                myString[5] = '\0';
                myString[6] = ' ';
                myString[7] = '\0';
 
        }
 
        else if ((fp_num >= 1000) && (fp_num < 10000)) {
                myString[0] = (befdec/1000) + 48;
                myString[1] = (befdec/100)%10 + 48;
                myString[2] = (befdec/10)%10 + 48;
                myString[3] = (befdec/1)%10 + 48;
                myString[4] = '.';
                myString[5] = (aftdec/10) + 48;
                myString[6] = ' ';
                myString[7] = '\0';
 
        }
 
        else if ((fp_num >= 10000) && (fp_num < 100000)) {
                myString[0] = (befdec/10000) + 48;
                myString[1] = (befdec/1000)%10 + 48;
                myString[2] = (befdec/100)%10 + 48;
                myString[3] = (befdec/10)%10 + 48;
                myString[4] = (befdec/1)%10 + 48;
                myString[5] = '.';
                myString[6] = (aftdec/10) + 48;
                myString[7] = '\0';
 
        }
 
 
}
 
void adc_read()
{
    
    ADRESL = 0x00;
    ADRESH = 0x00;
    ADCON0bits.ADON = 1;
    Delay100TCYx(200);
    ADCON0bits.GO = 1;
    while(ADCON0bits.GO);
    ADCON0bits.ADON = 0;
    iladc_val = ADRESH;        // copy high byte to result
    iladc_val <<= 8;           // shift low byte to 2nd byte
    iladc_val |= ADRESL;       // OR result with ADRESL to get both the bytes into one var.
    Delay100TCYx(200);
    dladc_val = (float)iladc_val;
    dladc_val = dladc_val * 0.0048875855327468;
    float2string(dladc_val);
    
}
 
void lcdinit(){
    lcdcmd(0x0E); // display and cursor ON.Writing 0x0F makes the cursor blink
    MSDelay(15); //delay
    lcdcmd(0x01); // this is to clear LCD
    MSDelay(15);
    lcdcmd(0x06); // shift the cursor to right
    MSDelay(15);
    lcdcmd(0x0C);
    MSDelay(15);
    lcdcmd(0x80); //line 1, position 0
    MSDelay(1);
}
 
void lcdcmd(char value)
{
    ldata = value;  // put the value on the pins of PORTD
    rs = 0;
    rw = 0;
    en = 1;         // strobe enable pin
    MSDelay(1);
    en = 0;
}
 
void lcddata (char value)
{
    ldata = value;  //put the value on the pins of PORTD
    rs = 1;
    rw = 0;
    en = 1;         // strobe enable pin
    MSDelay(1);
    en = 0;
}
 
void lcdstring(unsigned char *lcd_data){
    unsigned char cllcd_data;
 
    while(*lcd_data){
        cllcd_data = *lcd_data;
        lcddata(cllcd_data);
        lcd_data++;
    }
}
 
 
void MSDelay(int itime)
{
    unsigned int i,j;
 
    for(i=0;i<itime;i++)
        for(j=0;j<135;j++);
}
 
 
 
void main(){
    TRISA = 0x01;
    PORTA = 0x00;
    LATA = 0x00;
    TRISB = 0x00;
    PORTB = 0x00;
    LATB = 0x00;
    TRISD = 0x00;
    PORTD = 0x00;
    LATD = 0x00;
    ADCON0 = 0x00;
    ADCON1 = 0x0E;
    ADCON2 = 0xB6;
    CMCON = 0x00;
 
    lcdinit();
 
    while(1){
 
        adc_read();
        if(dladc_val != old_val){
              lcdcmd(0x01);
              Delay100TCYx(50);
              lcdcmd(0x80);
              Delay100TCYx(50);
              lcdstring(&myString);
 
              for(i = 0; i < 9; i++){
                    if(myflt == V[i]){
                        ctr++;
                    }
                    if(ctr > 0){
                          PORTBbits.RB7 = 1;
                    }
                    else{
                          PORTBbits.RB7 = 0;
                    }
              }
              ctr = 0;
              myflt = 0.0;
              old_val = dladc_val;
        }
        
    }
}

 

Attachments

  • ADC_PIC18F4580 v3.rar
    65.4 KB · Views: 44
Last edited:

I have posted the code. Your code is wrong.

i will try it and i will let u know ........thank u so much
please can u explain me wat is going on here
fp_num = ceil(fp_num * 100) / 100;
myflt = fp_num;

befdec = fp_num; // Fractional part is truncated
// 12.163456 becomes 12
aftdec = fp_num * 100; // 12.163456 becomes 1216
aftdec = aftdec - (befdec * 100); // 1216 - 1200 = 16
 

dladc_val will have some value like 4.3456

It will be passed to float2string function and so fp_num will be equal to 4.3456

we need only 2 digits after decimal. So,

fp_num = ceil(fp_num * 100) / 100;

(fp_num * 100) = 434.56

ceil(434.56) = 434

434/100 = 4.34

Now fp_num and myflt will be 4.34

befdec is before decimal

befdec is integer and if float like 4.34 is assigned to it it becomes 4

aftdec is after decimal

aftdec = fp_num * 100; // 4.34 becomes 434
aftdec = aftdec - (befdec * 100); // 434 - 400 = 34


ceil function was used because V[] values had 2 digits after decimal and so we had to make our adc value like 3.4567 to 3.45 to compare with V[] values.
 
I am not getting any output at LCD
 

no...
i am using c compiler and mplab to dumb the program in Pic and checking in LCD..
I have to display on LCD

- - - Updated - - -

no...
i am using c compiler and mplab to dumb the program in Pic and checking in LCD..
I have to display on LCD
on hardware
 

for(i = 0; i < 9; i++){
if(myflt == V){
ctr++;
}
if(ctr > 0){
PORTBbits.RB7 = 1;
}
else{
PORTBbits.RB7 = 0;
}
}
ctr = 0;
myflt = 0.0;
old_val = dladc_val;

please explain me wat is going on here because i dont have any connection on RB7
 

Remove that code. You don't need that code. I just used that piece of code to check if values of V[] is matching with adc value. If adc value matches with your lookup table values then LED turns ON else turns OFF.

I have not used I[], and R[] in the code as I don't know how you will use it. Don't delete the last line of code you posted.
 

if(myflt == V)
then R == V
that means i can see R value corresponding to Vi
which line u say not to delete it
 

i change the value of
Delay100TCYx(100);
but i am nt getting it on LCD

- - - Updated - - -

i change the value of
Delay100TCYx(100);
but i am nt getting it on LCD
why u choose
ADCON2 = 0xB6;
and please explain the
while(1){

adc_read();
if(dladc_val != old_val){
lcdcmd(0x01);
Delay100TCYx(100);
lcdcmd(0x80);
Delay100TCYx(100);
lcdstring(&myString);



old_val = dladc_val;
}
 

PHP:
Fosc = 16 MHz

Tosc = 0.0000000625 sec = 0.0625 us

Tad = Tosc * 64 (Tosc * 32 and below yeild 2 us Tad which is equal to min Tad. So, Tosc * 64 is choosen)
Tad = 0.0625 us * 64 = 4 us
min Tad required = 2 us
Fosc/64 or Tosc * 16 = 110	[pages 249 and 253]

The A/D conversion time per bit is defined as TAD. The
A/D conversion requires 11 TAD per 10-bit conversion.
The source of the A/D conversion clock is software
selectable. There are seven possible options for TAD:
• 2 TOSC
• 4 TOSC
• 8 TOSC
• 16 TOSC
• 32 TOSC
• 64 TOSC
• Internal RC Oscillator

For correct A/D conversions, the A/D conversion clock
(TAD) must be as short as possible, but greater than the
minimum TAD (approximately 2us, see parameter 130
for more information, page 450 it is given min Tad is 0.7 us).

After the A/D conversion is completed or aborted, a
2 TAD wait is required before the next acquisition can
be started.

11 Tad is needed for each conversion and 2 Tad between conversions

11 + 2 = 13 Tad

12 Tad is less

16 Tad is choosen

16 Tad = 110	[page 249]

ADCON0 = 0x00;
ADCON1 = 0b00001111;
ADCON2 = 0b10110110;
CMCON = 0x00;

Try calculating the value for ADCON2 using min Tad as 0.7 us and use it and see if it gives correct value. If you get correct value then use it.

That piece of code prints data on LCD only if adc value changes. adc value is copied into old_val and if old value is not equal to current adc value then data is printed else data is not printed. I had to write that piece of code because your lcd library was not good and it was printing data continuosly on LCD.

See if ADC works ok if ADCON2 is 0xB5.
 
Last edited:

in which function......where comparison take place with table ?
 

Do you know C? Can't you read the code and understand it.

This code shows that it compares myflt (adc value) with all the values in V[].


Code C - [expand]
1
2
for(i = 0; i < 9; i++){
                    if(myflt == V[i]){

 

Sorry sir, i didnt see it
but i cant get any output on LCD sir.........
 

I have not tested it on hardware. Are you using PCB board or breadboard? What MCU are you using? All I can say is low LCD delays cause problems. Post your circuit.
 

I am using Breadboard.........
MCU is 18f4580

how i will attached the photos of circuit

- - - Updated - - -

I am using Breadboard.........
MCU is 18f4580

how i will attached the photos of circuit

- - - Updated - - -

give ur email id so that i can send to u
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top