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.

Can somebody find the bug in this code?

Status
Not open for further replies.
Advanced Member level 6
Joined
Dec 4, 2012
Messages
4,280
Helped
822
Reputation
1,654
Reaction score
791
Trophy points
1,393
Location
Bangalore, India
Visit site
Activity points
0
Here is my code and I have attached my Project files.

MCU PIC18F4580
Fosc 16 MHz

myflt contains the adc value and it is compared with values in V[]? If any value matches then ctr is incremented and if ctr > 0 i.e, if any value matches then LED turns ON. Upto here it is working fine but when I change the adc value then LED doesn't turn OFF even if myflt doesn't match with any value of V[].


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
#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(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.rar
    65.1 KB · Views: 73

You're not clearing ctr prior to your for-next loop. Once the first match is found, it will always be greater then 0 (for at least 254 more iterations).
 

After coming out of for loop I am clearing ctr and if adc value changes then only the for loop is executed again. for loop compares adc value (myflt) with all the elements of V[] and only if there is a match then ctr becomes positive and LED turns ON. Let us assume that adc value is 3.35 and a match has occured and LED is ON. Now if value is changed then for loop executes and compares again. If there is match again then ctr previously cleared becomes positive.

@spudboy488

See after for loop I have use ctr = 0;
 

Hi every on in forum...

I want small information from u,i am doing my work on STM32 controller,So i am storing some important data(parameters) of 44 bytes in internal flash, my stm32 consists of 128kbytes of memory ,So my doubt is that
1. How many times(no.of cycles) can i write data on stm32 flash memory?
2. I am using internal flash right,is it safe(against corruption in data) to write data?

Thanks in advance,
 

Excellent answer FvM. Thank you very much. I have another question. Please check my Simulation and tell why an underscore character is displayed after the adc value on LCD. I traced ever line of code but not able to find what is causing the underscore to be printed.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top