///////////////////////////////////////////////////////////////////////////////////////// // Module Name: main.c // // Project Number / Name: 2210, Gas sensor // // Latest Change: June 2014 // // Module Purpose: // main file. // Does all the work, this is the only user maintained file involved // // Dependancies // See #includes list // // Change History: // ///////////////////////////////////////////////////////////////////////////////////////// #pragma DATA_SEG DEFAULT #include #include "derivative.h" unsigned char ICSTRM_FLASH @0xFFAF; Bool Alarm = FALSE; Bool SpkrOn = FALSE; Bool Period = FALSE; int Counter, Vgas, Vref; //************************************************************************************************ // Timer 8 bit MTIM Interrupt Handler //************************************************************************************************ void interrupt 12 Timers_MtimHandler (void) // called at 4 kHz rate { PTAD_PTAD2 = TRUE; // diagnose MTIMSC_TOF = 0; // clr Timer Overflow Flag if (Alarm && Period) SpkrOn = !SpkrOn; // toggle speaker else { SpkrOn = FALSE; // no current through speaker Period = FALSE; } PTAD_PTAD3 = SpkrOn; // signal to speaker output if (++Counter > 1000) // make periods of 1/4 second { Counter = 0; Period = !Period; // once per 1/4 second } PTAD_PTAD2 = FALSE; // diagnose } //************************************************************************************************ // AdConvert10 // Performs 1 10-bit A/D conversion //************************************************************************************************ int AdConvert10 (byte channel) { int Temp; ADCCFG = 0x18; // clk div 1, long sampletime, 10 bit, busclk ADCSC1 = (channel & 0x1F); // no interrupts, this starts the conversion while (!ADCSC1_COCO); // wait until conversion complete Temp = (ADCRH << 8) + ADCRL; return Temp; } //************************************************************************************************ // Initialize Timer MTIM (fixed 4 kHz) //************************************************************************************************ void Timers_InitMtim (void) { // assume busclock of 8 MHz MTIMCLK = 0x05; // BUSCLK div 32 = 250 kHz MTIMMOD = 61; // divide by 62 for 4000 Hz MTIMSC_TSTP = 0; // clear STOP state MTIMSC_TOIE = 1; // enable interrupts } //************************************************************************************************ // MAIN Entry point // Initializes various registers and peripherals // Then goes into an infinite loop performing the A/D conversions and // comparisons to determine the Alarm State. //************************************************************************************************ void main (void) { // set clock mode ICSTRM = ICSTRM_FLASH; ICSC1 = 0x06; // internal reference ICSC2 = 0x00; // Busclock = Osc div 2 (8 Mhz) SOPT1_COPE = 0; // Disable watchdog Timers_InitMtim (); PTADD = 0x0C; // bit 0 and 1 are analog inputs, 2 and 3 are outputs APCTL1 &= 0x03; // disables digital I/O on pins 7 and 8 EnableInterrupts; for(;;) // Forever do { Vref = Vref - (Vref >> 4) + AdConvert10(1); // Leaking Bucket integration 1/16 Vgas = Vgas - (Vgas >> 4) + AdConvert10(0); Alarm = (Vgas > Vref); } }