ghead
Member level 2
I've been having this same problem intermittently while writing/testing this code, Basically each routine/loop will run fine separately but when added all together code compiles with no warnings/errors but will not run. Currently when compiled and loaded into simulator this first instruction is "GOTO L0'' which is the location of the instruction so it can go no further. When enough or certain parts of the code are excluded then the remaining will run as expected but as soon as all parts are included it breaks. I've cut down RAM/ROM usage down to 19%RAM used and 35%ROM used. I've combed through and checked that i'm not causing an stack overflow as far as I can tell, I've tried not to call routines within any routines except within the main 'SWITCH' statements.
One thing i'm not to sure about is am I using 'SWITCH" in the wrong way causing this?
Am I not clearing variables that i need to?
Or is this a compiler thing?? (tried reinstalling a couple times and compiling other projects with no problems)
Basically i'm down to re configuring the whole program layout unless someone has some insight to where i'm going wrong?
Using PIC16F877A + LCD on PORTD
- - - Updated - - -
Example: Here I've commented out main loop and code will run as expected and hang when it gets to main while loop as expected and inversley if other parts are commented out then main etc loop runs fine.
One thing i'm not to sure about is am I using 'SWITCH" in the wrong way causing this?
Am I not clearing variables that i need to?
Or is this a compiler thing?? (tried reinstalling a couple times and compiling other projects with no problems)
Basically i'm down to re configuring the whole program layout unless someone has some insight to where i'm going wrong?
Using PIC16F877A + LCD on PORTD
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 // LCD module connections ////////////////////////////////////////////////////// sbit LCD_RS at RD1_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 TRISD1_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 relay_non at RA0_bit; sbit relay_inv at RA1_bit; sbit out_heat at RA2_bit; sbit out_cool at RA3_bit; // Button Input Pins /////////////////////////////////////////////////////////// sbit button_up at RB0_bit; sbit button_down at RB1_bit; sbit button_ok at RB2_bit; // SPI pins //////////////////////////////////////////////////////////////////// sbit spi_sck at RC3_bit; //CLK sbit spi_cs at RE2_bit; //CS sbit spi_si at RC4_bit; //SDI // Variables /////////////////////////////////////////////////////////////////// bit a, horc, temp_direction; unsigned int target_temp, act_target_temp, last_temp, overshoot; short b, bb, state, button_press_count; unsigned char buffer, buffer0, buffer1, arrayFlow[7]; unsigned int current_temp, current_tmp1, target_temp1; // Lcd Text //////////////////////////////////////////////////////////////////// const char txt1[] = "TARGET: "; const char txt2[] = "ACTUAL: "; const char txt3[] = "OVERSHOOT:"; const char txt4[] = "TEMP"; const char txt5[] = "CONTROL"; const char txt6[] = "ERROR"; const char txt7[] = ">"; const char txt8[] = "<"; const char txt9[] = "C"; const char txt10[] = "Are You..."; const char txt11[] = "HEATING"; const char txt12[] = "COOLING"; const char txt15[] = "EDIT"; const char txt16[] = "00"; char msg[17]; //declare array set 16 bytes of RAM aside for copying into // copy const to ram string char * CopyConst2Ram(char * dest, const char * src){ char * d ; d = dest; for(;*dest++ = *src++;) ; return d; } //////////////////////////////////////////////////////////////////////////////// union U16_ { unsigned int word; // For accessing the whole 16-bit unsigned int/////// unsigned char byte[2]; // For accessing 16-bits as individual bytes///////// }; //////////////////////////////////////////////////////////////////////////////// void getcurrent_temp(){ //////;read the value from MAX6675/////////////////// SSPCON.B5 = 1; //Initialise SPI DEVICE spi_cs = 0; //Start dummy read Delay_ms(2); spi_cs = 1; //Start re-sample Delay_ms(220); //should have new buffer spi_cs = 0; //Start actual read Delay_ms(1); //give the device time to settle // buffer1 = spi1_read(buffer); //fill buffer MSB's // buffer0 = spi1_read(buffer); //fill buffer LSB's buffer0 = 250; //Dummy read 1 buffer1 = 10; //Dummy read 2 buffer = 1; //Not sure why this is needed?? { //maybe needed for union?? union U16_ Bytes2Int; Bytes2Int.byte[1] = buffer1; //combine MSB's with LSB's Bytes2Int.byte[0] = buffer0; current_temp = Bytes2Int.word; //combined bytes as a 16-bit word } buffer = (buffer0 & 0x04); //Mask off all but bit 2 if (buffer == 0x04 ) {current_temp = 999;} //bit_2 = 1 if thermocouple is not connected else { current_temp = current_temp >>3; //remove flag values from read current_temp = current_temp *25; //convert read to avoid decimal place } } //////////////////////////////////////////////////////////////////////////////// void eepromread(){// Read Target_temp & overshoot from eeprom and convert to int buffer0 = eeprom_read (0x00); buffer1 = eeprom_read (0x01); if (buffer0 == 0xff) { if (buffer1 == 0x0ff) {target_temp = 50;} } else { union U16_ Bytes2Int; Bytes2Int.byte[1] = buffer1; // combine bytes Bytes2Int.byte[0] = buffer0; // target_temp = Bytes2Int.word; // return the combined bytes as a 16-bit word }; buffer0 = eeprom_read (0x10); buffer1 = eeprom_read (0x11); if (buffer0 == 0x0ff) { if (buffer1 == 0xff) {overshoot = 10;} } else { union U16_ Bytes2Int; Bytes2Int.byte[1] = buffer1; // combine bytes Bytes2Int.byte[0] = buffer0; // overshoot = Bytes2Int.word; // return the combined bytes as a 16-bit word } Delay_us(10); } //////////////////////////////////////////////////////////////////////////////// void show_overshoot(){ Lcd_cmd(_LCD_CLEAR); //Clear display///////////////////////// Lcd_out (1,1,CopyConst2Ram(msg,txt3)); //Overshoot////////// IntToStr(overshoot, arrayFlow); //convert overshoot value to display//// Lcd_out(1,10,arrayFlow); //display overshoot value/////////////// Lcd_out (1,16,CopyConst2Ram(msg,txt9)); //C////////////////// } //////////////////////////////////////////////////////////////////////////////// void show_target_temp(){ Lcd_cmd(_LCD_CLEAR); //Clear display///////////////////////// Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //Target Temp//////// IntToStr(target_temp, arrayFlow); //convert target value to display/////// Lcd_out(1,10,arrayFlow); //display target value////////////////// Lcd_out (1,16,CopyConst2Ram(msg,txt9)); //C////////////////// } //////////////////////////////////////////////////////////////////////////////// void setout(){ //Turn on/off outputs if (horc = 1){ relay_inv = 1; relay_non = 0; } else { relay_inv = 0; relay_non = 1; } } //////////////////////////////////////////////////////////////////////////////// void unsetout(){ //Turn off/on outputs if (horc = 1){ relay_inv = 0; relay_non = 1; } else { relay_inv = 1; relay_non = 0; } } //////////////////////////////////////////////////////////////////////////////// void menustart(){ //starup menu decide between heating or cooling Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out(1,4,CopyConst2Ram(msg,txt10)); //ARE YOU... Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); // Clear display state = 0; bb = 1; while (bb){ //menu loop leave here when finshed////////////////////////////// switch (state){ ////////////////////////////////////////////////////////////////////////// case 0: Lcd_Out (1,4,CopyConst2Ram(msg,txt7)); Lcd_Out (1,5,CopyConst2Ram(msg,txt11)); // >>HEATING<< Lcd_Out (1,12,CopyConst2Ram(msg,txt8)); Lcd_Out (2,5,CopyConst2Ram(msg,txt12)); // COOLING 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 } Lcd_Cmd(_LCD_CLEAR); state = 1; } if (button_ok == 0) { Delay_ms(50); //;debounce the button press while (button_ok == 0) {Delay_ms(50);} state = 2; } break; ////////////////////////////////////////////////////////////////////////// case 1: Lcd_Out (1,5,CopyConst2Ram(msg,txt11)); // HEATING Lcd_Out (2,4,CopyConst2Ram(msg,txt7)); Lcd_Out (2,5,CopyConst2Ram(msg,txt12)); // >>COOLING<< Lcd_Out (2,12,CopyConst2Ram(msg,txt8)); 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 } Lcd_Cmd(_LCD_CLEAR); // Clear display state = 0; } if (button_ok == 0) { Delay_ms(50); //;debounce the button press while (button_ok == 0) {Delay_ms(50);} state = 3; } break; ////////////////////////////////////////////////////////////////////////// case 2: Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out (1,3,CopyConst2Ram(msg,txt7)); Lcd_Out (1,4,CopyConst2Ram(msg,txt7)); Lcd_Out (1,5,CopyConst2Ram(msg,txt11)); // ****HEATING**** Lcd_Out (1,12,CopyConst2Ram(msg,txt8)); Lcd_Out (1,13,CopyConst2Ram(msg,txt8)); horc = 0; out_cool = 0; out_heat = 1; bb = 0; Delay_ms(2000); break; ////////////////////////////////////////////////////////////////////////// case 3: Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out (2,3,CopyConst2Ram(msg,txt7)); Lcd_Out (2,4,CopyConst2Ram(msg,txt7)); Lcd_Out (2,5,CopyConst2Ram(msg,txt12)); // ****COOLING**** Lcd_Out (2,12,CopyConst2Ram(msg,txt8)); Lcd_Out (2,13,CopyConst2Ram(msg,txt8)); horc = 1; out_heat = 0; out_cool = 1; bb = 0; Delay_ms(2000); break; } } Lcd_Cmd(_LCD_CLEAR); // Clear display unsetout(); } //////////////////////////////////////////////////////////////////////////////// void IntMain() { //Prepare ports turn a/d off comparators off TRISA = 240; //PORTA led and relay outputs rest inputs TRISB = 255; //PORTB set button input pins TRISC = 255; //PORTC is spare set as Intputs TRISD = 0; //PORTD LCD TRISE = 2; //SPI CS = RE2 OPTION_REG.B7 = 0; //enable pullups on portb (RBPU) CVRCON = 0; //vref off CMCON = 7; //compartors off ADCON0 = 0; //a/d converter off ADCON1 = 6; //All Digital //Turn on SPI SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW); SSPCON.B5 = 1; //SPIPrepare_OSC / 16 = 1.25mhz clk_Sample data at MIDDLE_Clk idle Low_SPI receive on clk edge High to Low spi_cs = 1; //Set cs pin high SPI device wait(sample) spi_sck = 0; //Set sck pin low SPI CLK idle } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// void main() { IntMain(); //Initialise PORTS and Periphirals Lcd_init(); //Initialise LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); //no cursor on the lcd display Lcd_Out(1,7,CopyConst2Ram(msg,txt4)); //TEMP Lcd_Out(2,6,CopyConst2Ram(msg,txt5)); //CONTROL Delay_ms(2000); //Delay for display menustart(); //select device attached heater or fan ?? eepromread(); //Load target and overshoot from eeprom last_temp = 0; state = 0; temp_direction = 0; while(1){ //Main loop - Never leave here switch(state){ //Sub routine loop / switch ////////////////////////////////////////////////////////////////////////// case 0: //show the target and current temperature(s) getcurrent_temp(); Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //TARGET: IntToStr(target_temp, arrayFlow); Lcd_Chr_Cp(arrayFlow[3]); //100.00 Lcd_Chr_Cp(arrayFlow[4]); //010.00 Lcd_Chr_Cp(arrayFlow[5]); //001.00 Lcd_Chr_Cp('.'); //000.00 Lcd_out(1,14,CopyConst2Ram(msg,txt16)); //000.11 Lcd_out(1,16,CopyConst2Ram(msg,txt9)); // C Lcd_out(2,1,CopyConst2Ram(msg,txt2)); //ACTUAL: IntToStr(current_temp, arrayFlow); Lcd_Chr_Cp(arrayFlow[1]); Lcd_Chr_Cp(arrayFlow[2]); Lcd_Chr_Cp(arrayFlow[3]); Lcd_Chr_Cp('.'); Lcd_Chr_Cp(arrayFlow[4]); Lcd_Chr_Cp(arrayFlow[5]); Lcd_out (2,16,CopyConst2Ram(msg,txt9)); // C Delay_ms(250); state = 1; break; ////////////////////////////////////////////////////////////////////////// case 1: //;monitor the current temp and the target temp state = 0; getcurrent_temp(); current_tmp1 = current_temp / 100; if (horc = 0) {act_target_temp = target_temp - overshoot;} else act_target_temp = target_temp + overshoot; if (current_temp == 999) { //Error code check unsetout(); Lcd_cmd(_LCD_CLEAR); //Clear display Lcd_out (2,3,CopyConst2Ram(msg,txt8)); // Display ***ERROR*** ///// Lcd_out (2,4,CopyConst2Ram(msg,txt8)); // Display ***ERROR*** ///// Lcd_out (2,5,CopyConst2Ram(msg,txt8)); // Display ***ERROR*** ///// Lcd_out (2,6,CopyConst2Ram(msg,txt6)); // Display ***ERROR*** ///// Lcd_out (2,11,CopyConst2Ram(msg,txt7)); // Display ***ERROR*** //// Lcd_out (2,12,CopyConst2Ram(msg,txt7)); // Display ***ERROR*** //// Lcd_out (2,13,CopyConst2Ram(msg,txt7)); // Display ***ERROR*** //// } if (last_temp <= current_tmp1) {temp_direction = 1;}//temperature is rising else temp_direction = 0; // else temperature is falling if (current_tmp1 > target_temp) {unsetout();} //we're over the target temperature so shut the heaters off if (current_tmp1 > act_target_temp) {unsetout();} //;threshold exceeded, turn off the heater(s) else setout(); //temperature is rising if (current_tmp1 < act_target_temp) {setout();} //;weve cooled down, turn the heaters on again else unsetout(); //;were cooling down, but are still pretty close to the target /////while the temperature is being monitored, check for button presses/// if ((button_up == 0)||(button_down == 0)||(button_ok == 0)) { unsetout(); Delay_ms(50); while ((button_up == 0)||(button_down == 0)||(button_ok == 0)) { Delay_ms(50); //;wait f the button to be released } state = 2; } break; ////////////////////////////////////////////////////////////////////////// case 2: //;press buttons to change menu option///////////////////////// Lcd_cmd(_LCD_CLEAR); //Clear display////////////////////////////////// Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //Target: ///////// Lcd_out (1,10,CopyConst2Ram(msg,txt7)); // > ////////// Lcd_out (1,12,CopyConst2Ram(msg,txt15)); // Edit ////////// Lcd_out (1,16,CopyConst2Ram(msg,txt8)); // <////////// Lcd_out (2,1,CopyConst2Ram(msg,txt3)); //OVERSHOOT: ////////// Lcd_out (2,12,CopyConst2Ram(msg,txt15)); // Edit ////////// state = 3; break; ////////////////////////////////////////////////////////////////////////// case 3: //Decide which button is pressed 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 = 4; } if (button_ok == 0) { Delay_ms(50); while (button_ok == 0) { Delay_ms(50); //;wait f the button to be released } a = 0; state = 6; } break; ////////////////////////////////////////////////////////////////////////// case 4: //;press buttons to change menu option///////////////////////// Lcd_cmd(_LCD_CLEAR); //Clear display Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //TARGET: //////////// Lcd_out (1,12,CopyConst2Ram(msg,txt8)); // EDIT //////////// Lcd_out (2,1,CopyConst2Ram(msg,txt3)); //Overshoot: //////////// Lcd_out (2,10,CopyConst2Ram(msg,txt7)); // > //////////// Lcd_out (2,12,CopyConst2Ram(msg,txt15)); // EDIT //////////// Lcd_out (2,16,CopyConst2Ram(msg,txt8)); // <//////////// state = 5; break; ////////////////////////////////////////////////////////////////////////// case 5: //Is button up or down 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 = 2; } if (button_ok == 0) { Delay_ms(50); //;debounce the button press while (button_ok == 0) {Delay_ms(50);} a = 1; state = 6; } break; ////////////////////////////////////////////////////////////////////////// case 6: //;change the target and overshoot temperature value/////////// if (a == 0) { show_target_temp(); state = 7; } if (a == 1) { show_overshoot(); state = 7; } break; ////////////////////////////////////////////////////////////////////////// case 7: //;button presses to alter target temperature/overshoot value// if (button_up == 0) { Delay_ms(50); button_press_count = 0; while (button_up == 0) { //;wait for the button to be released button_press_count = button_press_count + 1; Delay_ms(100); if (button_press_count >= 5) { if (a == 0) { if (target_temp < 500) { target_temp = target_temp + 10; } else target_temp = 500; show_target_temp(); } else { if (overshoot < 500) { overshoot = overshoot + 10; } else overshoot = 500; show_overshoot(); } Delay_ms(200); button_press_count = 5; //;stop the counter from rolling over } if (button_press_count < 5) { if (a==0) { if (target_temp < 500) {Target_temp = target_temp + 1;} else target_temp = 500; show_target_temp(); } if (a==1) { if (overshoot < 500) {overshoot = overshoot + 1;} else overshoot = 500; show_overshoot(); } } } state = 7; } 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) { if (a == 0) { if (target_temp > 10) { target_temp = target_temp - 10; } else target_temp = 0; show_target_temp(); } else { if (overshoot > 10) { overshoot = overshoot - 10; } else overshoot = 0; show_overshoot(); } Delay_ms(200); //;stop the counter from rolling over button_press_count = 5; } if (button_press_count < 5) { if (a==0) { if (target_temp > 1) {Target_temp = target_temp - 1;} else target_temp = 0; show_target_temp(); } if (a==1) { if (overshoot > 1) {overshoot = overshoot - 1;} else overshoot = 0; show_overshoot(); } } } state = 7; } if (button_ok == 0) { Delay_ms(50); //;simple debounce while (button_ok == 0) {Delay_ms(50);} //;wait for button to be released if (a == 0) { { union U16_ Int2Bytes; Int2Bytes.word = target_temp; eeprom_write (0x01,Int2Bytes.byte[1]); // write the hi byte eeprom_write (0x00,Int2Bytes.byte[0]); // write the lo byte to eeprom } } if (a == 1) { { union U16_ Int2Bytes; Int2Bytes.word = overshoot; eeprom_write (0x11,Int2Bytes.byte[1]); // write the hi byte eeprom_write (0x10,Int2Bytes.byte[0]); // write the lo byte to eeprom } } state = 0; } break; } } } ////////////////////////////////PROGRAM END///////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
- - - Updated - - -
Example: Here I've commented out main loop and code will run as expected and hang when it gets to main while loop as expected and inversley if other parts are commented out then main etc loop runs fine.
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 // LCD module connections ////////////////////////////////////////////////////// sbit LCD_RS at RD1_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 TRISD1_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 relay_non at RA0_bit; sbit relay_inv at RA1_bit; sbit out_heat at RA2_bit; sbit out_cool at RA3_bit; // Button Input Pins /////////////////////////////////////////////////////////// sbit button_up at RB0_bit; sbit button_down at RB1_bit; sbit button_ok at RB2_bit; // SPI pins //////////////////////////////////////////////////////////////////// sbit spi_sck at RC3_bit; //CLK sbit spi_cs at RE2_bit; //CS sbit spi_si at RC4_bit; //SDI // Variables /////////////////////////////////////////////////////////////////// bit a, horc, temp_direction; unsigned int target_temp, act_target_temp, last_temp, overshoot; short b, bb, state, button_press_count; unsigned char buffer, buffer0, buffer1, arrayFlow[7]; unsigned int current_temp, current_tmp1, target_temp1; // Lcd Text //////////////////////////////////////////////////////////////////// const char txt1[] = "TARGET: "; const char txt2[] = "ACTUAL: "; const char txt3[] = "OVERSHOOT:"; const char txt4[] = "TEMP"; const char txt5[] = "CONTROL"; const char txt6[] = "ERROR"; const char txt7[] = ">"; const char txt8[] = "<"; const char txt9[] = "C"; const char txt10[] = "Are You..."; const char txt11[] = "HEATING"; const char txt12[] = "COOLING"; const char txt15[] = "EDIT"; const char txt16[] = "00"; char msg[17]; //declare array set 16 bytes of RAM aside for copying into // copy const to ram string char * CopyConst2Ram(char * dest, const char * src){ char * d ; d = dest; for(;*dest++ = *src++;) ; return d; } //////////////////////////////////////////////////////////////////////////////// union U16_ { unsigned int word; // For accessing the whole 16-bit unsigned int/////// unsigned char byte[2]; // For accessing 16-bits as individual bytes///////// }; //////////////////////////////////////////////////////////////////////////////// void getcurrent_temp(){ //////;read the value from MAX6675/////////////////// SSPCON.B5 = 1; //Initialise SPI DEVICE spi_cs = 0; //Start dummy read Delay_ms(2); spi_cs = 1; //Start re-sample Delay_ms(220); //should have new buffer spi_cs = 0; //Start actual read Delay_ms(1); //give the device time to settle // buffer1 = spi1_read(buffer); //fill buffer MSB's // buffer0 = spi1_read(buffer); //fill buffer LSB's buffer0 = 250; //Dummy read 1 buffer1 = 10; //Dummy read 2 buffer = 1; //Not sure why this is needed?? { //maybe needed for union?? union U16_ Bytes2Int; Bytes2Int.byte[1] = buffer1; //combine MSB's with LSB's Bytes2Int.byte[0] = buffer0; current_temp = Bytes2Int.word; //combined bytes as a 16-bit word } buffer = (buffer0 & 0x04); //Mask off all but bit 2 if (buffer == 0x04 ) {current_temp = 999;} //bit_2 = 1 if thermocouple is not connected else { current_temp = current_temp >>3; //remove flag values from read current_temp = current_temp *25; //convert read to avoid decimal place } } //////////////////////////////////////////////////////////////////////////////// void eepromread(){// Read Target_temp & overshoot from eeprom and convert to int buffer0 = eeprom_read (0x00); buffer1 = eeprom_read (0x01); if (buffer0 == 0xff) { if (buffer1 == 0x0ff) {target_temp = 50;} } else { union U16_ Bytes2Int; Bytes2Int.byte[1] = buffer1; // combine bytes Bytes2Int.byte[0] = buffer0; // target_temp = Bytes2Int.word; // return the combined bytes as a 16-bit word }; buffer0 = eeprom_read (0x10); buffer1 = eeprom_read (0x11); if (buffer0 == 0x0ff) { if (buffer1 == 0xff) {overshoot = 10;} } else { union U16_ Bytes2Int; Bytes2Int.byte[1] = buffer1; // combine bytes Bytes2Int.byte[0] = buffer0; // overshoot = Bytes2Int.word; // return the combined bytes as a 16-bit word } Delay_us(10); } //////////////////////////////////////////////////////////////////////////////// void show_overshoot(){ Lcd_cmd(_LCD_CLEAR); //Clear display///////////////////////// Lcd_out (1,1,CopyConst2Ram(msg,txt3)); //Overshoot////////// IntToStr(overshoot, arrayFlow); //convert overshoot value to display//// Lcd_out(1,10,arrayFlow); //display overshoot value/////////////// Lcd_out (1,16,CopyConst2Ram(msg,txt9)); //C////////////////// } //////////////////////////////////////////////////////////////////////////////// void show_target_temp(){ Lcd_cmd(_LCD_CLEAR); //Clear display///////////////////////// Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //Target Temp//////// IntToStr(target_temp, arrayFlow); //convert target value to display/////// Lcd_out(1,10,arrayFlow); //display target value////////////////// Lcd_out (1,16,CopyConst2Ram(msg,txt9)); //C////////////////// } //////////////////////////////////////////////////////////////////////////////// void setout(){ //Turn on/off outputs if (horc = 1){ relay_inv = 1; relay_non = 0; } else { relay_inv = 0; relay_non = 1; } } //////////////////////////////////////////////////////////////////////////////// void unsetout(){ //Turn off/on outputs if (horc = 1){ relay_inv = 0; relay_non = 1; } else { relay_inv = 1; relay_non = 0; } } //////////////////////////////////////////////////////////////////////////////// void menustart(){ //starup menu decide between heating or cooling Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out(1,4,CopyConst2Ram(msg,txt10)); //ARE YOU... Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); // Clear display state = 0; bb = 1; while (bb){ //menu loop leave here when finshed////////////////////////////// switch (state){ ////////////////////////////////////////////////////////////////////////// case 0: Lcd_Out (1,4,CopyConst2Ram(msg,txt7)); Lcd_Out (1,5,CopyConst2Ram(msg,txt11)); // >>HEATING<< Lcd_Out (1,12,CopyConst2Ram(msg,txt8)); Lcd_Out (2,5,CopyConst2Ram(msg,txt12)); // COOLING 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 } Lcd_Cmd(_LCD_CLEAR); state = 1; } if (button_ok == 0) { Delay_ms(50); //;debounce the button press while (button_ok == 0) {Delay_ms(50);} state = 2; } break; ////////////////////////////////////////////////////////////////////////// case 1: Lcd_Out (1,5,CopyConst2Ram(msg,txt11)); // HEATING Lcd_Out (2,4,CopyConst2Ram(msg,txt7)); Lcd_Out (2,5,CopyConst2Ram(msg,txt12)); // >>COOLING<< Lcd_Out (2,12,CopyConst2Ram(msg,txt8)); 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 } Lcd_Cmd(_LCD_CLEAR); // Clear display state = 0; } if (button_ok == 0) { Delay_ms(50); //;debounce the button press while (button_ok == 0) {Delay_ms(50);} state = 3; } break; ////////////////////////////////////////////////////////////////////////// case 2: Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out (1,3,CopyConst2Ram(msg,txt7)); Lcd_Out (1,4,CopyConst2Ram(msg,txt7)); Lcd_Out (1,5,CopyConst2Ram(msg,txt11)); // ****HEATING**** Lcd_Out (1,12,CopyConst2Ram(msg,txt8)); Lcd_Out (1,13,CopyConst2Ram(msg,txt8)); horc = 0; out_cool = 0; out_heat = 1; bb = 0; Delay_ms(2000); break; ////////////////////////////////////////////////////////////////////////// case 3: Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out (2,3,CopyConst2Ram(msg,txt7)); Lcd_Out (2,4,CopyConst2Ram(msg,txt7)); Lcd_Out (2,5,CopyConst2Ram(msg,txt12)); // ****COOLING**** Lcd_Out (2,12,CopyConst2Ram(msg,txt8)); Lcd_Out (2,13,CopyConst2Ram(msg,txt8)); horc = 1; out_heat = 0; out_cool = 1; bb = 0; Delay_ms(2000); break; } } Lcd_Cmd(_LCD_CLEAR); // Clear display unsetout(); } //////////////////////////////////////////////////////////////////////////////// void IntMain() { //Prepare ports turn a/d off comparators off TRISA = 240; //PORTA led and relay outputs rest inputs TRISB = 255; //PORTB set button input pins TRISC = 255; //PORTC is spare set as Intputs TRISD = 0; //PORTD LCD TRISE = 2; //SPI CS = RE2 OPTION_REG.B7 = 0; //enable pullups on portb (RBPU) CVRCON = 0; //vref off CMCON = 7; //compartors off ADCON0 = 0; //a/d converter off ADCON1 = 6; //All Digital //Turn on SPI SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW); SSPCON.B5 = 1; //SPIPrepare_OSC / 16 = 1.25mhz clk_Sample data at MIDDLE_Clk idle Low_SPI receive on clk edge High to Low spi_cs = 1; //Set cs pin high SPI device wait(sample) spi_sck = 0; //Set sck pin low SPI CLK idle } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// void main() { IntMain(); //Initialise PORTS and Periphirals Lcd_init(); //Initialise LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); //no cursor on the lcd display Lcd_Out(1,7,CopyConst2Ram(msg,txt4)); //TEMP Lcd_Out(2,6,CopyConst2Ram(msg,txt5)); //CONTROL Delay_ms(2000); //Delay for display menustart(); //select device attached heater or fan ?? eepromread(); //Load target and overshoot from eeprom last_temp = 0; state = 0; temp_direction = 0; while(1){ //Main loop - Never leave here /* switch(state){ //Sub routine loop / switch ////////////////////////////////////////////////////////////////////////// case 0: //show the target and current temperature(s) getcurrent_temp(); Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //TARGET: IntToStr(target_temp, arrayFlow); Lcd_Chr_Cp(arrayFlow[3]); //100.00 Lcd_Chr_Cp(arrayFlow[4]); //010.00 Lcd_Chr_Cp(arrayFlow[5]); //001.00 Lcd_Chr_Cp('.'); //000.00 Lcd_out(1,14,CopyConst2Ram(msg,txt16)); //000.11 Lcd_out(1,16,CopyConst2Ram(msg,txt9)); // C Lcd_out(2,1,CopyConst2Ram(msg,txt2)); //ACTUAL: IntToStr(current_temp, arrayFlow); Lcd_Chr_Cp(arrayFlow[1]); Lcd_Chr_Cp(arrayFlow[2]); Lcd_Chr_Cp(arrayFlow[3]); Lcd_Chr_Cp('.'); Lcd_Chr_Cp(arrayFlow[4]); Lcd_Chr_Cp(arrayFlow[5]); Lcd_out (2,16,CopyConst2Ram(msg,txt9)); // C Delay_ms(250); state = 1; break; ////////////////////////////////////////////////////////////////////////// case 1: //;monitor the current temp and the target temp state = 0; getcurrent_temp(); current_tmp1 = current_temp / 100; if (horc = 0) {act_target_temp = target_temp - overshoot;} else act_target_temp = target_temp + overshoot; if (current_temp == 999) { //Error code check unsetout(); Lcd_cmd(_LCD_CLEAR); //Clear display Lcd_out (2,3,CopyConst2Ram(msg,txt8)); // Display ***ERROR*** ///// Lcd_out (2,4,CopyConst2Ram(msg,txt8)); // Display ***ERROR*** ///// Lcd_out (2,5,CopyConst2Ram(msg,txt8)); // Display ***ERROR*** ///// Lcd_out (2,6,CopyConst2Ram(msg,txt6)); // Display ***ERROR*** ///// Lcd_out (2,11,CopyConst2Ram(msg,txt7)); // Display ***ERROR*** //// Lcd_out (2,12,CopyConst2Ram(msg,txt7)); // Display ***ERROR*** //// Lcd_out (2,13,CopyConst2Ram(msg,txt7)); // Display ***ERROR*** //// } if (last_temp <= current_tmp1) {temp_direction = 1;}//temperature is rising else temp_direction = 0; // else temperature is falling if (current_tmp1 > target_temp) {unsetout();} //we're over the target temperature so shut the heaters off if (current_tmp1 > act_target_temp) {unsetout();} //;threshold exceeded, turn off the heater(s) else setout(); //temperature is rising if (current_tmp1 < act_target_temp) {setout();} //;weve cooled down, turn the heaters on again else unsetout(); //;were cooling down, but are still pretty close to the target /////while the temperature is being monitored, check for button presses/// if ((button_up == 0)||(button_down == 0)||(button_ok == 0)) { unsetout(); Delay_ms(50); while ((button_up == 0)||(button_down == 0)||(button_ok == 0)) { Delay_ms(50); //;wait f the button to be released } state = 2; } break; ////////////////////////////////////////////////////////////////////////// case 2: //;press buttons to change menu option///////////////////////// Lcd_cmd(_LCD_CLEAR); //Clear display////////////////////////////////// Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //Target: ///////// Lcd_out (1,10,CopyConst2Ram(msg,txt7)); // > ////////// Lcd_out (1,12,CopyConst2Ram(msg,txt15)); // Edit ////////// Lcd_out (1,16,CopyConst2Ram(msg,txt8)); // <////////// Lcd_out (2,1,CopyConst2Ram(msg,txt3)); //OVERSHOOT: ////////// Lcd_out (2,12,CopyConst2Ram(msg,txt15)); // Edit ////////// state = 3; break; ////////////////////////////////////////////////////////////////////////// case 3: //Decide which button is pressed 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 = 4; } if (button_ok == 0) { Delay_ms(50); while (button_ok == 0) { Delay_ms(50); //;wait f the button to be released } a = 0; state = 6; } break; ////////////////////////////////////////////////////////////////////////// case 4: //;press buttons to change menu option///////////////////////// Lcd_cmd(_LCD_CLEAR); //Clear display Lcd_out (1,1,CopyConst2Ram(msg,txt1)); //TARGET: //////////// Lcd_out (1,12,CopyConst2Ram(msg,txt8)); // EDIT //////////// Lcd_out (2,1,CopyConst2Ram(msg,txt3)); //Overshoot: //////////// Lcd_out (2,10,CopyConst2Ram(msg,txt7)); // > //////////// Lcd_out (2,12,CopyConst2Ram(msg,txt15)); // EDIT //////////// Lcd_out (2,16,CopyConst2Ram(msg,txt8)); // <//////////// state = 5; break; ////////////////////////////////////////////////////////////////////////// case 5: //Is button up or down 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 = 2; } if (button_ok == 0) { Delay_ms(50); //;debounce the button press while (button_ok == 0) {Delay_ms(50);} a = 1; state = 6; } break; ////////////////////////////////////////////////////////////////////////// case 6: //;change the target and overshoot temperature value/////////// if (a == 0) { show_target_temp(); state = 7; } if (a == 1) { show_overshoot(); state = 7; } break; ////////////////////////////////////////////////////////////////////////// case 7: //;button presses to alter target temperature/overshoot value// if (button_up == 0) { Delay_ms(50); button_press_count = 0; while (button_up == 0) { //;wait for the button to be released button_press_count = button_press_count + 1; Delay_ms(100); if (button_press_count >= 5) { if (a == 0) { if (target_temp < 500) { target_temp = target_temp + 10; } else target_temp = 500; show_target_temp(); } else { if (overshoot < 500) { overshoot = overshoot + 10; } else overshoot = 500; show_overshoot(); } Delay_ms(200); button_press_count = 5; //;stop the counter from rolling over } if (button_press_count < 5) { if (a==0) { if (target_temp < 500) {Target_temp = target_temp + 1;} else target_temp = 500; show_target_temp(); } if (a==1) { if (overshoot < 500) {overshoot = overshoot + 1;} else overshoot = 500; show_overshoot(); } } } state = 7; } 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) { if (a == 0) { if (target_temp > 10) { target_temp = target_temp - 10; } else target_temp = 0; show_target_temp(); } else { if (overshoot > 10) { overshoot = overshoot - 10; } else overshoot = 0; show_overshoot(); } Delay_ms(200); //;stop the counter from rolling over button_press_count = 5; } if (button_press_count < 5) { if (a==0) { if (target_temp > 1) {Target_temp = target_temp - 1;} else target_temp = 0; show_target_temp(); } if (a==1) { if (overshoot > 1) {overshoot = overshoot - 1;} else overshoot = 0; show_overshoot(); } } } state = 7; } if (button_ok == 0) { Delay_ms(50); //;simple debounce while (button_ok == 0) {Delay_ms(50);} //;wait for button to be released if (a == 0) { { union U16_ Int2Bytes; Int2Bytes.word = target_temp; eeprom_write (0x01,Int2Bytes.byte[1]); // write the hi byte eeprom_write (0x00,Int2Bytes.byte[0]); // write the lo byte to eeprom } } if (a == 1) { { union U16_ Int2Bytes; Int2Bytes.word = overshoot; eeprom_write (0x11,Int2Bytes.byte[1]); // write the hi byte eeprom_write (0x10,Int2Bytes.byte[0]); // write the lo byte to eeprom } } state = 0; } break; } */ } } ////////////////////////////////PROGRAM END///////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Last edited: