[SOLVED] Help checking my code, hex file only 1kb after compiling - MikroC for PIC

Status
Not open for further replies.

ghead

Member level 2
Joined
Nov 19, 2010
Messages
43
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,384
HI, So im new to using MikroC for Pic and programming in c in general, I have attempted to take the code found here:
https://nerdclub-uk.blogspot.co.nz/2015/01/temperature-controller-for-k-type.html
No idea what it was compiled with and pretty sure its incomplete but I've attempted to re-write it for a PIC16F877A and use MAX6675 thermocouple in Mikroc for Pic.
My first issue is (code not finished yet) when I compile program it results in a 1kb hex file and when simulated it get stuck on the first line which in assembler is goto L0 which as far as i can see is the same address so it does nothing.

Im sure there are many issues that i need to address so any help would be much appreciated.


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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
//Output Pins
sbit led_pin at RA0_bit;
sbit led_heater at RA1_bit;
sbit heater_relay at RA2_bit;
//Button Input Pins
sbit button_up at RB0_bit;
sbit button_down at RB1_bit;
sbit button_ok at RB2_bit;
sbit button_swop at RB3_bit;
//SPI pins
sbit spi_sck at RC0_bit;
sbit spi_cs at RC1_bit;
sbit spi_si at RC2_bit;
 
//Variables
int button_press_count, return_state, state, case1, b, current_temp, state_, spi_out;
long target_temp, overshoot, tmp_temp, last_temp;
unsigned short temp_direction;
unsigned char buffer;
 
 
void get_current_temp(){
     //;read the value from the thermistor
     //;in some libraries, they suggest performing a "dummy read"
     //;to force the device to re-sample the temperature
     spi_cs = 0;
     Delay_ms(2);
     spi_cs = 1;
     Delay_ms(220);  //;the device should now have a new temperature in its buffer
     spi_cs = 0;  //;enable CS so we can read data back from the device
     Delay_ms(50); //;give the device time to settle
     current_temp = spi1_read(buffer) <<8; // upper 8 bit from MAX6675
     current_temp|= Spi1_Read(buffer);     // lower 8 bit
 
     if (current_temp & 0x04);{        //;bit 2 is normally low, and goes high when the thermistor is not connected
         current_temp = 999;
     }
     current_temp = current_temp >> 3; //;bit-shift three places to the right
     if (current_temp = 0); {          //;something has gone wrong here
         current_temp = 999;
     }
     current_temp*=0.25; //;(float)??????the temperature reported has a resolution of 0.25 deg C so convert to actual degrees
}
 
void show_overshoot(){
     Lcd_cmd(_LCD_CLEAR); //Clear display
     Lcd_out (1,1,"Overshoot:");
     Lcd_out (1,11,overshoot);
}
 
void show_target_temp(){
     Lcd_cmd(_LCD_CLEAR); //Clear display
     Lcd_out (1,1,"Target:");
     Lcd_out (1,8,target_temp);
}
 
void main() {
 
     TRISA = 0;
     TRISB = 0x0f; //Portb set button input pins
     TRISC = 0x04; //PortC set SPI pins I/O
     PORTA = 0;
     SSPCON<5>=1;       //Turn on SPI
     OPTION_REG<7>=0;  //enable pullups on portb (RBPU)
     
     Lcd_init();  //Initialise LCD
     Lcd_Cmd(_LCD_CLEAR); // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF); //no cursor on the lcd display
     led_heater = 0; //make sure we boot up with the heater off
     heater_relay = 0; //heater indicator is off
 
     last_temp =0;
     state = 0;
     overshoot = 0;
     temp_direction = 0;
 
     //SPIPrepare       OSC / 16 = 1.25mhz clk  Sample data at end    Clk idle Low      SPI receive on clk edge High to Low
     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_END, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW);
     spi_cs = 1; //Set cs pin high  SPI device wait(sample)
     spi_sck = 0; //Set sck pin low SPI CLK idle
 
     switch (state) {
           
           case 0:
           Lcd_Cmd(_LCD_CLEAR); //Clear display
           Lcd_Out(1,1,"Temperature");
           Lcd_Out(2,1,"Controller");
 
           //Read 4, b;   //;read the last set overshoot value
           //overshootl = b;
           //Read 5, b;
           //overshooth = b;
 
           if (overshoot == 0x0ff) {overshoot = 10;}
           if (target_temp == 0x0ff) {target_temp = 50;}
           else Delay_ms(2000);
           state = 1;
           break;
 
 
           case 1: //show the target and current temperature(s)
           get_current_temp();
           Lcd_cmd(_LCD_CLEAR); //Clear display
           Lcd_out (1,1,"Target:");
           Lcd_out(1,8,target_temp);
           Lcd_out (2,1,"Actual:");
           Lcd_out (2,8,current_temp);
           state = 2;
           break;
 
 
           case 2: //;monitor the current temp and the target temp
           get_current_temp();
 
           tmp_temp = target_temp - overshoot;
 
           if (current_temp = 999); {
                 heater_relay = 0;          //;something has gone wrong
                 led_heater = 0;
                 Lcd_out (2,1,"**** ERROR! ****");
           }
           
           if (current_temp > last_temp); (current_temp < last_temp); {
                 Lcd_out (2,1,"Actual:");
                 Lcd_out (2,8,current_temp);
           }
 
           if (last_temp <= current_temp) {temp_direction = 1;}   //temperature is rising
           else  temp_direction = 0; //temperature is falling
 
           if (current_temp > target_temp); {
                 heater_relay = 0; //we're over the target temperature so shut the heaters off
                 led_heater = 0;
           }
 
           if (current_temp > tmp_temp) {
                 led_heater = 0;     //;threshold exceeded, turn off the heater(s)
                 heater_relay = 0;
           }                         //temperature is rising
           else  {
                 led_heater = 1;
                 heater_relay = 1;
           }                         //;still not quite there, keep the heaters on
 
           if (current_temp < tmp_temp) {
                 heater_relay = 1;   //;weve cooled down too much, turn the heaters on again
                 led_heater = 1;
           }
 
           else {
                 heater_relay = 0;   //;were cooling down, but are still pretty close to the target
                 led_heater = 0;     //;temperature, so allow a little more cooling
                 last_temp = current_temp;
           }
 
 
           //;now while the temperature is being monitored, check for button presses
           //;(on a button press, turn off the heater(s) just for good measure, and
           //;go to a new state just for handling changing the settings
           if (button_up = 0); (button_down = 0); (button_ok = 0); {
                 heater_relay = 0;
                 led_heater  = 0;
                 Delay_ms(50);
                 while (button_up = 0);  (button_down = 0);  (button_ok = 0); {
                       Delay_ms(50); //;wait f the button to be released
                 }
           state = 3;
           }
           Delay_ms(1000);
           break;
 
           case 3: //;press buttons to change menu option
           Lcd_cmd(_LCD_CLEAR); //Clear display
           Lcd_out (1,1,"> Change Target");
           Lcd_out (2,3,"Edit Overshoot");
           state = 4;
           break;
 
           case 4:
           if (button_up = 0);  (button_down = 0); {
                 Delay_ms(50);
                 while (button_up = 0);  (button_down = 0); {
                       Delay_ms (50); //;wait f the button to be released
                 }
           state = 5;
           }
 
           if (button_ok = 0); {
                 Delay_ms(50);
                 while (button_ok = 0); {
                       Delay_ms(50); //;wait f the button to be released
                 }
           state = 10;
           }
           break;
 
 
           case1 =5; //;press buttons to change menu option
           Lcd_cmd(_LCD_CLEAR); //Clear display
           Lcd_out (1,3,"Change Target");
           Lcd_out (2,1,"> Edit Overshoot");
           state = 6;
           break;
 
 
           case1 =6;
           if (button_up = 0); (button_down = 0); {
                 Delay_ms(50);
                 while (button_up = 0); (button_down = 0);{
                       Delay_ms(50); //;wait for the button to be released
                 }
           state = 3;
           }
           if (button_ok = 0); {
                 Delay_ms(50); //;debounce the button press
                while (button_ok = 0); {
                       Delay_ms(50);
                 }
           state = 11;
           }
           break;
 
 
           case 10: //;change the target temperature value
           show_target_temp();
           return_state = 10;
           state = 12;
           break;
 
 
           case 11: //;change the overshoot value
           show_overshoot();
           return_state = 11;
           state = 12;
           break;
 
           case 12: //;button presses to alter target temperature/overshoot value
 
           if (button_up = 0); {
                 Delay_ms(50);
                 button_press_count = 0;
                 while (button_up = 0 ); {   //;wait f the button to be released
                       button_press_count = button_press_count + 1;
                       Delay_ms(100);
                       if (button_press_count >= 5); {
                             //;this is a "long press" so increase the value by 10 every loop
 
                             if (return_state = 10); {
                             target_temp = target_temp + 10;
                                   if (target_temp > 500); {
                                   target_temp = 500; }
                                   target_temp = target_temp / 10;
                                   target_temp = target_temp * 10;;
                                   show_target_temp();
                             }
                             if (return_state = 11); {
                                   overshoot = overshoot + 10;
                                   if (overshoot > 500); { overshoot = 500; }
                                   overshoot = overshoot / 10;
                                   overshoot = overshoot * 10;
                                   if (overshoot >= target_temp); {overshoot = target_temp - 1;}
                                   show_overshoot();
                             }
 
                             Delay_ms(200);
                             //;stop the counter from rolling over
                             button_press_count = 5;
                       }
                 }
           }
 
           if (button_press_count < 5); {
                 if (return_state = 10); { target_temp = target_temp + 1; }
                 if (return_state = 11); { overshoot = overshoot + 1; }
           } //;this is a "short" press so increase the value by just one
           
           if (target_temp > 500); { target_temp = 500; }
           if (overshoot > 500); { overshoot = 500; }
           if (overshoot >= target_temp); { overshoot = target_temp - 1; }
           state = return_state;
 
           if (button_down = 0); {
                 Delay_ms(50);
                 button_press_count = 0;
                 while (button_down = 0); {
                       //;wait for the button to be released
                       button_press_count = button_press_count + 1;
                       Delay_ms(100);
 
                       if (button_press_count >= 5); {
                             //;this is a "long press" so decrease the value by 10 every loop
                             if (return_state = 10); {
                                   b = target_temp - 10;
                                   if (b > 0) { target_temp = target_temp - 10; }
                                   else target_temp = 0;
                             }
                             target_temp = target_temp / 10;
                             target_temp = target_temp * 10;
                             show_target_temp();
                             if (return_state = 11) {b = overshoot - 10;}
                             if (overshoot > 10) {overshoot = overshoot - 10;}
                             else overshoot = 0;
                             overshoot = overshoot / 10;
                             overshoot = overshoot * 10;
                             show_overshoot();
                       }
                             Delay_ms(200);
                             //;stop the counter from rolling over
                             button_press_count = 5;
                 }
           }
           if (button_press_count < 5); {
                       //;this is a "short" press so increase the value by just one
                 if (return_state = 10); {
                       if (target_temp > 1) {Target_temp = target_temp - 1;}
                       else target_temp = 0;
                 }
                 if (return_state = 11); {
                       if (overshoot > 1) {overshoot = overshoot - 1;}
                       else overshoot = 0;
                 }
           }
           state = return_state;
 
           if (button_ok = 0); {
                 Delay_ms(50); //;simple debounce
                 while (button_ok = 0); {Delay_ms(50);} //;wait for button to be released
                 if (return_state = 10) {
                       b = target_temp;     // Load target temp into write file
                       eeprom_write (2,b); //;write the new target temp to eeprom
                 }
                 else {
                       b = overshoot;      // Load target temp into write file
                       eeprom_write (4,b); //;write the new overshoot value to eeprom
                 }
           }
           state = 1;
           break;
     }
}



Here is hex file that is generated:
Code:
:020000000028D6
:020C48000800A2
:100B2400543465346D3470346534723461347434DF
:080B340075347234653400349D
:100B3C0043346F346E34743472346F346C346C34BC
:060B4C0065347234003430
:100B52005434613472346734653474343A34003452
:100B62004F34763465347234733468346F346F348E
:060B720074343A34003433
:100B78005434613472346734653474343A3400342C
:100B88004334683461346E34673465342034543403
:0C0B980061347234673465347434003406
:100BA4003E342034453464346934743420344F344E
:100BB400763465347234733468346F346F34743417
:020BC4000034FB
:100BC600453464346934743420344F3476346534AF
:0E0BD6007234733468346F346F347434003406
:100BE4002A342A342A342A342034453452345234B0
:100BF4004F345234213420342A342A342A342A34C7
:020C04000034BA
:100C0600413463347434753461346C343A340034AA
:100C16003E3420344334683461346E34673465348A
:100C26002034543461347234673465347434003497
:100C3600413463347434753461346C343A3400347A
:02400E004A2F37
:00000001FF
 

My suggestion is you need not look into the hex file. You can run in the debug mode and see that the program is not halting and it is executing all sections of the code.
 
Reactions: ghead

    ghead

    Points: 2
    Helpful Answer Positive Rating
At a glance, there are syntax errors in your code, which seems to have even never been compiled. The below construction using "<>" I have never seen being used in C language, unless its usage is specific of the MikroC compiler.

OPTION_REG<7>=0
 
Reactions: ghead

    ghead

    Points: 2
    Helpful Answer Positive Rating
Also you need a 'while(1)' loop, probably containing the switch statement.
Embedded code is not the same as code running on a PC (etc.) in that it is intended to run forever and without any operating system assistance. Therefore you will always initialise your system and then enter an infinite loop (often called the 'while' or 'main' loop and often written as 'while(1){...}' ) where all of the work gets done.
Code lines such as 'if(button_up = 0);' will certainly not be doing what I suspect you want. It will be *assigning* 'button_up' with the value 0 and the 'if' part will effectively be ignored as the ';' straight afterwards will terminate the whole statement. There are similar errors throughout.
Susan
 
Reactions: ghead

    ghead

    Points: 2
    Helpful Answer Positive Rating
Thanks for input but I can't get debug mode to work with current hardware i have. I'm using pickit2v2 to program mcu in gui mode as i cant get it working via cmdline through mikroc, Hence why I tried software simulation of hex.
 

It compiled without errors, not saying there aren't as there obviously are many . That line is supposed to enable portb pullups and was from another compilers language and I was yet to figure out what it should be in mikroc. Any pointers to fix syntax?

- - - Updated - - -

Thanks I will attempt to fix now, One question should I have 'if(button_up ==0); ' or am I misunderstanding the operators?

- - - Updated - - -

Edit, Thanks I will attempt to fix now, One question should I have 'if(button_up ==0) ' or am I misunderstanding the operators?
 

I feel that the discussion about possible program errors is missing the point, because it's obvious that the shown main() code hasn't been compiled at all. Respectively there's nothing to debug, you have to read the compiler messages to understand why the code hasn't been compiled. I don't believe that it's not indicated somewhere.
 

So I think Ive fixed the issues pointed out,

Changed all if statements and removed the ";" and added the extra "=" so they become viable statements hopefully.

Changed "SSPCON<5> =1;" and "OPTION_REG<7> = 0;" to "SPCON.B5 =1;" and "OPTION_REG.B7 = 0;"

Added "while (1)" loop to main program

Yet to test fixes but any thoughts or did I get something wrong.

Thanks for the feedback


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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
//Output Pins
sbit led_pin at RA0_bit;
sbit led_heater at RA1_bit;
sbit heater_relay at RA2_bit;
//Button Input Pins
sbit button_up at RB0_bit;
sbit button_down at RB1_bit;
sbit button_ok at RB2_bit;
sbit button_swop at RB3_bit;
//SPI pins
sbit spi_sck at RC0_bit;
sbit spi_cs at RC1_bit;
sbit spi_si at RC2_bit;
 
//Variables
int button_press_count, return_state, state, case1, b, current_temp, state_, spi_out;
long target_temp, overshoot, tmp_temp, last_temp;
unsigned short temp_direction;
unsigned char buffer;
 
 
void get_current_temp(){
     //;read the value from the thermistor
     //;in some libraries, they suggest performing a "dummy read"
     //;to force the device to re-sample the temperature
     spi_cs = 0;
     Delay_ms(2);
     spi_cs = 1;
     Delay_ms(220);  //;the device should now have a new temperature in its buffer
     spi_cs = 0;  //;enable CS so we can read data back from the device
     Delay_ms(50); //;give the device time to settle
     current_temp = spi1_read(buffer) <<8; // upper 8 bit from MAX6675
     current_temp|= Spi1_Read(buffer);     // lower 8 bit
 
     if (current_temp & 0x04);{        //;bit 2 is normally low, and goes high when the thermistor is not connected
         current_temp = 999;
     }
     current_temp = current_temp >> 3; //;bit-shift three places to the right
     if (current_temp = 0); {          //;something has gone wrong here
         current_temp = 999;
     }
     current_temp*=0.25; //;(float)??????the temperature reported has a resolution of 0.25 deg C so convert to actual degrees
}
 
void show_overshoot(){
     Lcd_cmd(_LCD_CLEAR); //Clear display
     Lcd_out (1,1,"Overshoot:");
     Lcd_out (1,11,overshoot);
}
 
void show_target_temp(){
     Lcd_cmd(_LCD_CLEAR); //Clear display
     Lcd_out (1,1,"Target:");
     Lcd_out (1,8,target_temp);
}
 
void main() {
 
     TRISA = 0;
     ADCON1 = 6;
     TRISB = 0x0f; //Portb set button input pins
     TRISC = 0x04; //PortC set SPI pins I/O
     PORTA = 0;
     SSPCON.B5 =1;       //Turn on SPI
     OPTION_REG.B7 = 0;  //enable pullups on portb (RBPU)
 
     Lcd_init();  //Initialise LCD
     Lcd_Cmd(_LCD_CLEAR); // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF); //no cursor on the lcd display
     led_heater = 0; //make sure we boot up with the heater off
     heater_relay = 0; //heater indicator is off
 
     last_temp =0;
     state = 0;
     overshoot = 0;
     temp_direction = 0;
 
     //SPIPrepare       OSC / 16 = 1.25mhz clk  Sample data at end    Clk idle Low      SPI receive on clk edge High to Low
     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_END, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW);
     spi_cs = 1; //Set cs pin high  SPI device wait(sample)
     spi_sck = 0; //Set sck pin low SPI CLK idle
 
while (1){
     switch (state) {
           
           case 0:
           Lcd_Cmd(_LCD_CLEAR); //Clear display
           Lcd_Out(1,1,"Temperature");
           Lcd_Out(2,1,"Controller");
 
           //Read 4, b;   //;read the last set overshoot value
           //overshootl = b;
           //Read 5, b;
           //overshooth = b;
 
           if (overshoot == 0x0ff) {overshoot = 10;}
           if (target_temp == 0x0ff) {target_temp = 50;}
           else Delay_ms(2000);
           state = 1;
           break;
 
 
           case 1: //show the target and current temperature(s)
           get_current_temp();
           Lcd_cmd(_LCD_CLEAR); //Clear display
           Lcd_out (1,1,"Target:");
           Lcd_out(1,8,target_temp);
           Lcd_out (2,1,"Actual:");
           Lcd_out (2,8,current_temp);
           state = 2;
           break;
 
 
           case 2: //;monitor the current temp and the target temp
           get_current_temp();
 
           tmp_temp = target_temp - overshoot;
 
           if (current_temp == 999) {
                 heater_relay = 0;          //;something has gone wrong
                 led_heater = 0;
                 Lcd_out (2,1,"**** ERROR! ****");
           }
           
           if (current_temp > last_temp, current_temp < last_temp) {
                 Lcd_out (2,1,"Actual:");
                 Lcd_out (2,8,current_temp);
           }
 
           if (last_temp <= current_temp) {temp_direction = 1;}   //temperature is rising
           else  temp_direction = 0; //temperature is falling
 
           if (current_temp > target_temp) {
                 heater_relay = 0; //we're over the target temperature so shut the heaters off
                 led_heater = 0;
           }
 
           if (current_temp > tmp_temp) {
                 led_heater = 0;     //;threshold exceeded, turn off the heater(s)
                 heater_relay = 0;
           }                         //temperature is rising
           else  {
                 led_heater = 1;
                 heater_relay = 1;
           }                         //;still not quite there, keep the heaters on
 
           if (current_temp < tmp_temp) {
                 heater_relay = 1;   //;weve cooled down too much, turn the heaters on again
                 led_heater = 1;
           }
 
           else {
                 heater_relay = 0;   //;were cooling down, but are still pretty close to the target
                 led_heater = 0;     //;temperature, so allow a little more cooling
                 last_temp = current_temp;
           }
 
 
           //;now while the temperature is being monitored, check for button presses
           //;(on a button press, turn off the heater(s) just for good measure, and
           //;go to a new state just for handling changing the settings
           if (button_up == 0, button_down == 0, button_ok == 0) {
                 heater_relay = 0;
                 led_heater  = 0;
                 Delay_ms(50);
                 while (button_up == 0, button_down == 0, button_ok == 0) {
                       Delay_ms(50); //;wait f the button to be released
                 }
           state = 3;
           }
           Delay_ms(1000);
           break;
 
           case 3: //;press buttons to change menu option
           Lcd_cmd(_LCD_CLEAR); //Clear display
           Lcd_out (1,1,"> Change Target");
           Lcd_out (2,3,"Edit Overshoot");
           state = 4;
           break;
 
           case 4:
           if (button_up == 0, button_down == 0) {
                 Delay_ms(50);
                 while (button_up == 0, button_down == 0) {
                       Delay_ms (50); //;wait f the button to be released
                 }
           state = 5;
           }
 
           if (button_ok == 0) {
                 Delay_ms(50);
                 while (button_ok == 0) {
                       Delay_ms(50); //;wait f the button to be released
                 }
           state = 10;
           }
           break;
 
 
           case1 =5; //;press buttons to change menu option
           Lcd_cmd(_LCD_CLEAR); //Clear display
           Lcd_out (1,3,"Change Target");
           Lcd_out (2,1,"> Edit Overshoot");
           state = 6;
           break;
 
 
           case1 =6;
           if (button_up == 0, button_down == 0) {
                 Delay_ms(50);
                 while (button_up == 0) (button_down == 0);{
                       Delay_ms(50); //;wait for the button to be released
                 }
           state = 3;
           }
           if (button_ok == 0) {
                 Delay_ms(50); //;debounce the button press
                while (button_ok == 0) {
                       Delay_ms(50);
                 }
           state = 11;
           }
           break;
 
 
           case 10: //;change the target temperature value
           show_target_temp();
           return_state = 10;
           state = 12;
           break;
 
 
           case 11: //;change the overshoot value
           show_overshoot();
           return_state = 11;
           state = 12;
           break;
 
           case 12: //;button presses to alter target temperature/overshoot value
 
           if (button_up == 0) {
                 Delay_ms(50);
                 button_press_count = 0;
                 while (button_up == 0 ) {   //;wait f the button to be released
                       button_press_count = button_press_count + 1;
                       Delay_ms(100);
                       if (button_press_count >= 5) {
                             //;this is a "long press" so increase the value by 10 every loop
 
                             if (return_state == 10) {
                             target_temp = target_temp + 10;
                                   if (target_temp > 500) {
                                   target_temp = 500; }
                                   target_temp = target_temp / 10;
                                   target_temp = target_temp * 10;;
                                   show_target_temp();
                             }
                             if (return_state == 11) {
                                   overshoot = overshoot + 10;
                                   if (overshoot > 500) { overshoot = 500; }
                             //      overshoot = overshoot / 10;
                             //      overshoot = overshoot * 10;
                                   if (overshoot >= target_temp) {overshoot = target_temp - 1;}
                                   show_overshoot();
                             }
 
                             Delay_ms(200);
                             //;stop the counter from rolling over
                             button_press_count = 5;
                       }
                 }
           }
 
           if (button_press_count < 5) {
                 if (return_state == 10) { target_temp = target_temp + 1; }
                 if (return_state == 11) { overshoot = overshoot + 1; }
           } //;this is a "short" press so increase the value by just one
           
           if (target_temp > 500) { target_temp = 500; }
           if (overshoot > 500) { overshoot = 500; }
           if (overshoot >= target_temp) { overshoot = target_temp - 1; }
           state = return_state;
 
           if (button_down == 0) {
                 Delay_ms(50);
                 button_press_count = 0;
                 while (button_down == 0) {
                       //;wait for the button to be released
                       button_press_count = button_press_count + 1;
                       Delay_ms(100);
 
                       if (button_press_count >= 5) {
                             //;this is a "long press" so decrease the value by 10 every loop
                             if (return_state == 10) {
                                   b = target_temp - 10;
                                   if (b > 0) { target_temp = target_temp - 10; }
                                   else target_temp = 0;
                             }
                          //   target_temp = target_temp / 10;
                          //   target_temp = target_temp * 10;
                             show_target_temp();
                             if (return_state == 11) {b = overshoot - 10;}
                             if (overshoot > 10) {overshoot = overshoot - 10;}
                             else overshoot = 0;
                             overshoot = overshoot / 10;
                             overshoot = overshoot * 10;
                             show_overshoot();
                       }
                             Delay_ms(200);
                             //;stop the counter from rolling over
                             button_press_count = 5;
                 }
           }
           if (button_press_count < 5) {
                       //;this is a "short" press so increase the value by just one
                 if (return_state == 10) {
                       if (target_temp > 1) {Target_temp = target_temp - 1;}
                       else target_temp = 0;
                 }
                 if (return_state == 11) {
                       if (overshoot > 1) {overshoot = overshoot - 1;}
                       else overshoot = 0;
                 }
           }
           state = return_state;
 
           if (button_ok == 0) {
                 Delay_ms(50); //;simple debounce
                 while (button_ok == 0) {Delay_ms(50);} //;wait for button to be released
                 if (return_state == 10) {
                       b = target_temp;     // Load target temp into write file
                       eeprom_write (2,b); //;write the new target temp to eeprom
                 }
                 else {
                       b = overshoot;      // Load target temp into write file
                       eeprom_write (4,b); //;write the new overshoot value to eeprom
                 }
           }
           state = 1;
           break;
      }
   }
}

 

Hi,

run the compiler and read it´s informations/warnings/errors.

Klaus
 

I have compiled it...Not sure if im misunderstanding what you're saying. I've attached my project folder in which is lst hex etc that is generated when i compile project. Which returns no warnings/errors or messages as far as i can see..
I have updated my code with fixes from above to.
View attachment Temp sensor.7z

- - - Updated - - -

Now looking at the .lst file the first command is a "GOTO 2048" an address that doesn't exist Unless i'm missing something?

Code C - [expand]
1
2
3
4
5
6
7
8
9
;  LST file generated by mikroListExporter - v.2.0 
; Date/Time: 29/05/2018 12:32:54 a.m.
;----------------------------------------------
 
;Address Opcode    ASM
0x0000   0x2800         GOTO       2048
; end of _Delay_50us
; end of _Delay_5500us
; end of _Delay_1us

 

Attachments

  • until.bmp
    355.2 KB · Views: 161

The GOTO address what ever you are mentioning should be part of your linker file. Please check the linker file you have given.
 

2048 decimal = 0x800 hexadecimal
 
Reactions: ghead

    ghead

    Points: 2
    Helpful Answer Positive Rating
What do you mean by linker file?

- - - Updated - - -

Thanks I understand now.
 

My question is why you are trying to decode the hex file. The hex file is only an output which comes out of your input. Please check the inputs properly then you will get proper output.
 

Thanks, So would this work

Code C - [expand]
1
current_temp = current_temp * 0.25;


or is this how to do it by bit shifting?

Code C - [expand]
1
current_temp >> 2;


Also fixed other mistakes mentioned .
 

Code:
     if (current_temp = 0); {          //;something has gone wrong here
Something certainly has gone wrong. Try...
Code:
     if (current_temp == 0); {          //;something has gone wrong here

Code:
    current_temp*=0.25; //;(float)??????the temperature reported has a resolution of 0.25 deg C so convert to actual degrees
Don't think that is a valid way to divide an integer by 4. Try shifting by two instead.


EDIT: erroneous statement removed

- - - Updated - - -

Code:
           if (current_temp > last_temp, current_temp < last_temp) {
I don't think that using commas in an if statement works the way that you think it does. Use or/and between bracketed comparisons.

Same for other instances of IF using commas....
 
Last edited:

This is how I thought it would be done,

Code C - [expand]
1
if (button_up == 0) or  (button_down == 0) or (button_ok == 0)


But looking through mikroC PRO for PIC Language Reference if can't find anything on how to do a multi parameter IF statement.
I'm missing the obvious here i'm sure.
I'm trying to poll 3 switches at once and if any are true then complete the IF statement true expression.
 

But looking through mikroC PRO for PIC Language Reference if can't find anything on how to do a multi parameter IF statement.
Just enclose all evaluations within "()" parenthesis.

Code:
if ( (condition1)or(condition2)or(condition3) )

It is not a compiler specific functionality, but rather an ordinary feature from standard C language.
 

If you don't know multiple IF statements try like this
Code:
if(condition1 == TRUE) 
{
  if(condition2 == TRUE) 
   {
     if(condition3 == TRUE) 
     {
         ;
     }
   }
 }
 

I am no C expert, but this is a simple example of how I combine comparisons within if statements:
Code:
// simple if statement tests

// global variables
unsigned char var1;
unsigned char var2;
unsigned char var3;
unsigned char var4;


void main() {
    var1 = 2;                                                     // initialise variables to known values - adjust to make and/or tests true of false
    var2 = 0;                                                     //  adjust to make and/or tests true of false
    var3 = 0;
    var4 = 0;
    
    if((var1 == 2) || (var2 == 5)){                               // test of logical or function
        var3 = 1;
    }

    if((var1 == 2) && (var2 == 5)){                               // test of logical and function
        var4 = 1;
    }

                                                                  // make use of each variable to prevent it being optimised away
    PORTB = var1;
    PORTB = var2;
    PORTB = var3;
    PORTB = var4;

    while(1);                                                     // not strictly necessary for mikroC (which adds infinite loop anyway) but essential on other compilers to prevent program exit
    
}
 

    V

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…