summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/extruder-v2/Software/src/extruder.c
blob: 88947151e32460179d75219c163e91d250dac944 (plain)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*

  RepRap
  ------

  The Replicating Rapid Prototyper Project


  Copyright (C) 2005
  Adrian Bowyer & The RepRap Researchers

  http://reprap.org

  Principal author:

     Adrian Bowyer
     Department of Mechanical Engineering
     Faculty of Engineering and Design
     University of Bath
     Bath BA2 7AY
     U.K.

     e-mail: A.Bowyer@bath.ac.uk

  RepRap is free; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  Licence as published by the Free Software Foundation; either
  version 2 of the Licence, or (at your option) any later version.

  RepRap is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public Licence for more details.

  For this purpose the words "software" and "library" in the GNU Library
  General Public Licence are taken to mean any and all computer programs
  computer files data results documents and other copyright information
  available from the RepRap project.

  You should have received a copy of the GNU Library General Public
  Licence along with RepRap; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA,
  or see

      http://www.gnu.org/

==================================================================================

This is the C program for the PIC that controls the polymer extruder head.

Version 1.1  18 September 2005

*/

#include "extruder.h"

// PIC configuration bits

typedef unsigned int config;
config at 0x2007 __CONFIG = _CP_OFF &
 _WDT_OFF &
 _BODEN_OFF &
 _PWRTE_ON &
 _INTRC_OSC_CLKOUT &
 _MCLRE_OFF &
 _LVP_OFF;

// Global variables

byte set_temp;   // The temperature required
byte low_ref;    // Use low or high voltage reference
byte motor;      // 0 for stop, or FORWARD, or REVERSE
byte speed;      // Motor speed
byte f_count1;   // LED flash...
byte f_count2;   // ...counters
byte f_val;      // LED on (1) or off (0)

byte a_to_d();  // Function prototype

// ***********************************************************************

// Save and load from EEPROM

// This function saves the current set speed, 
// temperature, and voltage reference into the EEPROM.  See the PIC
// data sheet for details of how it works.

void eeSave()
{
  // while(GIE) GIE = 0;

  while(WR);
  EEADR = SPEED_ADDR;
  EEDATA = speed;
  WREN = 1;
  EECON2 = 0x55;
  EECON2 = 0xaa;
  WR = 1;

  while(WR);
  EEADR = TEMP_ADDR;
  EEDATA = set_temp;
  WREN = 1;
  EECON2 = 0x55;
  EECON2 = 0xaa;
  WR = 1;

  while(WR);
  EEADR = HL_ADDR;
  EEDATA = low_ref;
  WREN = 1;
  EECON2 = 0x55;
  EECON2 = 0xaa;
  WR = 1;

  // GIE = 1;
}


// This function loads the saved speed, 
// temperature and voltage reference from the EEPROM.  See the PIC
// data sheet for details of how it works.

void eeLoad()
{
  while(RD); 
  EEADR = SPEED_ADDR;
  RD = 1;
  speed = EEDATA;

  while(RD); 
  EEADR = TEMP_ADDR;
  RD = 1;
  set_temp = EEDATA;

  while(RD); 
  EEADR = HL_ADDR;
  RD = 1;
  low_ref = EEDATA;
}

// ************************************************************************

// COMMUNICATIONS



// Send a byte out of the PIC's USART.

void sendByte(byte c)
{
  while(!TRMT); 
  TXREG = c;
}

// Send a string
// This provokes some compiler bug that I
// Can't be bothered to find...

void sendString(char* s)
{
  byte b;
  b = 0;
  while(s[b]) sendByte(s[b++]);
}


// If a byte is available from the USART 
// return and echo it, otherwise return 0.  
// NB - this means it can't get a 0 as input; 
// a serious limitation that ought to be corrected...

byte rcvByte()
{
  byte b;
  if(!RCIF) return 0;
  b = RCREG;
  sendByte(b);   
  return b;
}


// Wait for a byte from the USART

byte waitByte()
{
  byte b;
  while(!(b = rcvByte()));
  return b;
}


// Send a number in hex.

void sendNumber(byte n)
{
  byte b;
  sendByte('0');
  sendByte('x');
  b = n >> 4;
  if(b >= 10)
    sendByte(b + ('a' - 10));
  else
    sendByte(b + '0');
  b = n & 0x0f;
  if(b >= 10)
    sendByte(b + ('a' - 10));
  else
    sendByte(b + '0');
}


// Get a number in hex (no leading 0x needed).

byte waitNumber()
{
  byte b, n;
  b = waitByte();
  if(b > '9')
    b = b - 'a' + 10;
  else
    b = b - '0';
  n = waitByte();
  if(n > '9')
    n = n - 'a' + 10;
  else
    n = n - '0';
  n = n | (b << 4);
  return n;
}

// Send a new line.

void crlf()
{
  sendByte('\r');
  sendByte('\n');
}

// Send a "> " prompt.

void prompt()
{
  crlf();
  sendByte('>');
  sendByte(' ');
}

// Deal with user input (single letter command in b)

void menu(byte b)
{
    switch(b)
    {
    // Return the set temperature
    case 't':
      sendNumber(set_temp);
      break;

    // Set a new temperature
    case 'T':
      set_temp = waitNumber();
      break;

    // Return the actual temperature
    case 'v':
      sendNumber(a_to_d());
      break;

    // Turn the motor off
    case 'm':
      motor = 0;
      break;

    // Turn the motor on
    case 'F':
      motor = FORWARD;
      break;

    // Reverse the motor on
    case 'R':
      motor = REVERSE;
      break;

    // Return the speed
    case 's':
      sendNumber(speed);
      break;

    // Set a new speed
    case 'S':
      speed = waitNumber();
      break;

    // Set the A to D range
    case 'L':
      low_ref = 1;
      VRCON = V_REF_LO;
      break;
    case 'H':
      low_ref = 0;
      VRCON = V_REF_HI;
      break;

    // Report the A->D range
    case 'l':
      sendNumber(low_ref);
      break;

    // Uh?
    default:
      sendByte('?');
    }
    prompt();

    // Save any changes to the set speed or temperature

    eeSave();
}


//**************************************************************************

// CONTROL OF LEDS, HEATER, AND MOTOR


// Should the motor be on?

byte motor_on()
{
  if(motor) return motor;     // Request from the user via the menu or...
  if(!PORTB5) return FORWARD; // ... the control pin is grounded
  return 0;
}

// This flashes the blue LED when the motor is running, or
// puts it on continuously when it is not. 

void flash()
{
  if(motor_on())
  {
    f_count1++;
    if(!f_count1)
    {
      f_count2--;
      if(!f_count2)
      {
	f_count2 = F_DEF;
        f_val = 1 - f_val;
	if(f_val)
	  PORTB7 = 0;
	else
	  PORTB7 = 1;
       }
    }
  } else
  {
    PORTB7 = 1;
  }
}


// Crude 4-bit A to D converter
// According to the spec a delay > 10 us should
// be needed after each bit VRx is set.  But it 
// seems to work without, so let sleeping dogs lie...

byte a_to_d()
{
  byte b;

  // Low or high range?

  if(low_ref)
      VRCON = V_REF_LO;
  else
      VRCON = V_REF_HI;

  //Initialise the voltage returned value to 0

  VREN = 1;
  VRCON = VRCON & 0xf0;

  b = 0x0f;

  // 4-bit succesive approximation register

  VR3 = 1;
  if(C1OUT)
  {
    b = b & BIN(00000111);
    VR3 = 0;
  }
  VR2 = 1;
  if(C1OUT)
  {
    b = b & BIN(00001011);
    VR2 = 0;
  }
  VR1 = 1;
  if(C1OUT)
  {
    b = b & BIN(00001101);
    VR1 = 0;
  }
  VR0 = 1;
  if(C1OUT)
  {
    b = b & BIN(00001110);
    VR0 = 0;
  }

  VREN = 0;
  return b;
}

// The thermostat

void temperature()
{
  byte t = a_to_d();
  if(t < set_temp)
    PORTA1 = 1;    // Heater on
  else
    PORTA1 = 0;    // Heater off
}

// Controlling the motor

void stopMotor()
{
  CCP1CON = 0;
  CCPR1L = 0;
  PORTB3 = 0;
  PORTB4 = 0;
}

void motorControl()
{

  // Turn off the motor if we're not (almost) up to the set temp.

  if(set_temp - a_to_d() > 1) 
  { 
    stopMotor();
    return; 
  }

  // Otherwise do what motor_on() says

  switch(motor_on())
  {
  case FORWARD:
    CCPR1L = speed;
    CCP1CON = BIN(00001100);
    PORTB4 = 0;
    break;

  case REVERSE:
    CCPR1L = 255 - speed;
    CCP1CON = BIN(00001100);
    PORTB4 = 1;
    break;
  
  default:
    stopMotor();
  }
}

//******************************************************************************

// Set everything up

void init()
{
  OPTION_REG = BIN(01011111); // Disable TMR0 on RA4, 1:128 WDT
                              // Enable Port B pullups

  CMCON = BIN(00000010);      // Comparator module defaults: compare RA0
                              // With the internal reference

  TRISA = BIN(00110001);      // Port A outputs (except 0/4/5)
                              // RA0 is input from the temp. sensor
                              // RA1 drives the heater
                              // RA4 is used for clock out (debugging)
                              // RA5 is motor-demand input for autonomous running
                              // RA7 drives the activity/motor LED
  
  TRISB = BIN(00100110);      // Port B outputs (except 1/2 for serial and
                              // 5 - motor control)

  CCPR1L = 0;
  PR2 = 0xff;
  T2CON = BIN(00000111);
  CCP1CON = 0;

  PIE1 = BIN(00000000);       // All peripheral interrupts initially disabled
  INTCON = BIN(00000000);     // Interrupts disabled
  PIR1 = 0;                   // Clear peripheral interrupt flags
  SPBRG = 25;                 // 25 = 2400 baud @ 4MHz
  TXSTA = BIN(00000000);      // 8 bit low speed 
  RCSTA = BIN(10000000);      // Enable port for 8 bit receive

  TXEN = 1;              // Enable transmit

  CREN = 1;              // Start reception
  VRCON = V_REF_LO;      // V reference - low range

  WREN = 0;              // Disable EEPROM writes

  // Clear the output pins

  PORTA = 0;
  PORTB = 0;

  // Set the motor stopped and the blue flash values

  motor = 0;
  f_count2 = F_DEF;
  f_val = 0;

  // Load the saved speed and temperature from the EEPROM

  eeLoad();

  // Blow the trumpet...

  crlf();
  sendByte('R');
  sendByte('e');
  sendByte('p');
  sendByte('R');
  sendByte('a');
  sendByte('p');
  crlf();


  //sendString("\n\rRepRap\n\r");
}


// Main program initialises everything, then runs an infinite loop listening
// for commands and controlling the peripherals by repeated function calls -
// i.e. crude process timesharing without interrupts.  Note menu items
// that need further input stop the loop until that input is complete, so don't
// sit scratching your head thinking what to type when the heater's on, for example...

void main()
{
  byte b;

  init();

  prompt();

  while(1)
  {
    b = rcvByte();  // Incomming from serial port...
    if(b) menu(b);  // ...?  If yes use menu() to find what was said
    temperature();  // Heater on or off as need be
    motorControl(); // Motor on or off as need be
    flash();        // Blue LED on or off as need be...
  }
}