// temper.c - measure a thermistor (or any variable resistor)
//            without an A/D.
// Robert Poor - Jan 20000
// MIT Media Laboratory
//
// This PIC program expects two resistors and a capacitor wired
// on the iRX board as follows:
//
// REFERENCE_PORT (A0) ---/\/\/\---+    Rref
//                                 |
//      TEST_PORT (A1) ---/\/\/\---+    Rtest
//                                 |
// CAPACITOR_PORT (A4) ------------+
//                                 |
//                               -----  C
//                               -----
//                                 |
//                                GND
//
// The actual choice of Rref, Rtest and C will depend on
// the application.  In general, Rref should approximately
// equal Rtest's "default" value, C should be chosen for a
// time constant of at least 100 uSec.

#case
#include <16F84.H>
#fuses HS,NOWDT,NOPROTECT,PUT
#use DELAY(clock=10000000)
#use fast_io(A)
#use fast_io(B)

// ================================================================
// Standard definitions for the irx2_1 board
//
#define RS232_XMIT      PIN_B1  // (output) RS232 serial transmit
#define RED_LED         PIN_B2  // (output) Red LED (low true)
#define IR_LED          PIN_B3  // (output) Infrared LED (low true)
#define IR_SENSOR       PIN_B4  // (input) IR sensor (Sharp IS1U30)
#define RS232_RCV       PIN_B5  // (input) RS232 serial receive

// Macros to simplify I/O operations
//
#define RED_LED_ON      output_low(RED_LED)
#define RED_LED_OFF     output_high(RED_LED)
#define IR_LED_ON       output_low(IR_LED)
#define IR_LED_OFF      output_high(IR_LED)

// communication at 9600 baud
#use rs232(baud=9600, xmit=RS232_XMIT, rcv=RS232_RCV)

#define IRX_TRIS_B	0b00110000

// ================================================================
// application specific constants

#define REFERENCE_RESISTOR  	PIN_A0		// 
#define TEST_RESISTOR  		PIN_A1
#define CAPACITOR_PORT		PIN_A4		// schmitt trigger input

#define RESET_TRIS      0b00000011      // REF hi-z, TEST hi-z, CAP output
#define REFERENCE_TRIS  0b00010010      // REF out, TEST hi-Z, CAP input
#define TEST_TRIS       0b00010001      // REF hi-Z, TEST out, CAP input

// Measure the time to charge capacitor via REFERENCE_RESISTOR or 
// TEST_RESISTOR, depending on the tris argument
//
int measure(int tris) {
  int t;

  set_tris_a(RESET_TRIS);       // discharge cap
  output_low(CAPACITOR_PORT);
  delay_ms(5);                  // give it time to settle

  set_tris_a(tris);
#if 0
  // we could go the the trouble to choose which output to set high...
  if (tris == REFERENCE_TRIS) {
    output_high(REFERENCE_RESISTOR);
  } else {
    output_high(TEST_RESISTOR);
  }
#else
  // ...but since one of the two is configured as an input, it's
  // easier just to set them both high.
  output_high(TEST_RESISTOR);
  output_high(REFERENCE_RESISTOR);
#endif

  RED_LED_ON;                   // useful for triggering an oscilliscope
  set_rtcc(0);                  // start counting
  do				// count until the capacitor port reads high
     t = get_rtcc();
     while (!input(CAPACITOR_PORT));
  RED_LED_OFF;
  return t;
}

void main() {
  int ref, sens;

  set_tris_a(RESET_TRIS);
  set_tris_b(IRX_TRIS_B);

  // DIV_64: 25.6 uSec per tic, 6.5536 mSec to roll over
  setup_counters(RTCC_INTERNAL, RTCC_DIV_64);

  RED_LED_ON;
  delay_ms(200);
  RED_LED_OFF;

  printf("Temp 1.0\r\n");
    
  while (1) {
    ref = measure(REFERENCE_TRIS);
    sens = measure(TEST_TRIS);
    printf("ref=%u, sens=%u\r\n", ref, sens);
  }
}


