/******************************************************************************* * * Rotation.c * * A rotation controller for the target motor drive. Uses a PIC12F675. * * 3/9/05 by Dave Peterson *******************************************************************************/ /***************************************************************** * * Update information * * Date Change * -------- ----------------------------------------------------- * 3/9/05 Initial form. Adapted from Valve.C. * 3/11/05 Added most of the features. * 3/14/05 Turned off comparators. * 3/18/05 Removed motor commands if there is lack of trigger. * 8/ 4/06 Shortened minimum motor time. ******************************************************************/ /*** Defines ***/ // +5 volts on pin 1, Gnd on pin 8. #define Analog_In PIN_A0 // pin 7 #define Yellow_LED PIN_A5 // pin 2 #define Green_LED PIN_A4 // pin 3 #define Trigger_In PIN_A2 // pin 5 #define Motor_Drive PIN_A1 // pin 6 #define MOVING 1 #define NOT_MOVING 0 /*** Include Files ***/ #include <12F675.H> #fuses INTRC_IO, NOWDT, NOMCLR, PUT #device ADC=16 /*** Clock Speed ***/ #use Delay(Clock=4000000) /*** Function Prototypes ***/ /*** Global Variables ***/ #byte _ADRESH = 0x1e #byte _ADRESL = 0x9e #byte _OSCCAL = 0x90 #byte cmcon = 0x19 /*** Routines ***/ // External Interrupt routine int trig_count; #int_ext ext_isr() { trig_count++; } // Real Time Interrupt routine #define INTS_PER_TENTH 49 // (4000000/(4*256*8)) int int_count; // Number of interrupts left before a tenth has elapsed int tenths_count; int sec_count; #int_rtcc // This function is called every time clock_isr() // the RTCC (timer0) overflows (255->0). { if(--int_count==0) { int_count=INTS_PER_TENTH; // Do things once per tenth here... if(--tenths_count==0) { tenths_count=10; // Do things once per second here if (sec_count>0) { --sec_count; } } } } // End of Clock ISR /*** Main Program ***/ main() { unsigned long volts; int mode, i; // Set up ADC. setup_adc_ports(AN0_ANALOG); setup_adc(ADC_CLOCK_INTERNAL ); set_adc_channel( 0 ); cmcon = 0x07; // Comparators off. _OSCCAL = 0x9C; // Set up interrupts int_count=INTS_PER_TENTH; tenths_count=10; set_rtcc(0); setup_counters( RTCC_INTERNAL, RTCC_DIV_8); enable_interrupts(INT_RTCC); enable_interrupts(INT_EXT); enable_interrupts(GLOBAL); // Initialize System. trig_count=0; sec_count=10; output_high(Motor_Drive); // Blink the LEDs. for(i=0;i<8;++i) { output_high(Yellow_LED); output_low(Green_LED); delay_ms(100); output_low(Yellow_LED); output_high(Green_LED); delay_ms(100); } output_low(Green_LED); while(1) { volts = read_adc(); // Read junky value. volts = make16(_ADRESH,_ADRESL); // Read full value. volts = volts>>8; // Scale to 255 counts. This is for 5.00 volts full scale. // Check for trigger. if(trig_count>0) { trig_count=0; sec_count=10; output_low(Yellow_LED); output_low(Motor_Drive); output_high(Green_LED); delay_ms(100); output_low(Green_LED); for(i=0;i<10;++i) { delay_ms(volts); } output_high(Motor_Drive); } // Check for long lack of trigger. if (sec_count==0) { output_high(Yellow_LED); } }// End of loop } // End of Main