#include #include /* * main.c */ #define DRIVE_CNT 2000 #define PULSE_OUTPUT_PIN BIT2 // #define STATE_TIMER0 0x0010 #define STATE_NULL 0x0000 //..... uint16_t state; int16_t drive_out= 0; //returns error code uint16_t measure_adc10( int16_t* p_val_store ); #define ADC_CTL1_PARAMS 0 #define ERR_ADC_TIMOUT 1 #define SAFTY_MAXADC_COUNT 32 #define ETEMP_SENSE BIT3 #define ETEMP_INCH INCH_3 #define POT_A_SENSE BIT4 #define POT_A_INCH INCH_4 #define POT_B_SENSE BIT5 #define POT_B_INCH INCH_5 #define SAMPLES 4 uint16_t error; int16_t temp_sense; int16_t pot_a_sense; int16_t pot_b_sense; // ......... int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P1DIR|= PULSE_OUTPUT_PIN; P2OUT|= PULSE_OUTPUT_PIN; //counter for PWM output TACCR0= DRIVE_CNT; // + limit_set; P1SEL|= PULSE_OUTPUT_PIN; //SMCLK, Up Mode, Clear TACTL= TASSEL_2 | MC_1 | TACLR | ID_0; //Interrupt TACCTL0= CCIE; //control_1 set/reset TACCTL1= OUTMOD_3; //the adc... ///Vref ~ Vss to ref; ref on; SHT * 16 clock ADC10CTL0= SREF_1 | REF2_5V | REFON | ADC10SHT_1; _enable_interrupt( ); for( ; ; ) { if( ! state ) continue; //for now, state will only be STATE_TIMER0; //measure temperature of engine ADC10AE0= POT_A_SENSE; ADC10CTL1= POT_A_INCH | ADC_CTL1_PARAMS; error= measure_adc10( &pot_a_sense ); ADC10AE0= POT_B_SENSE; ADC10CTL1= POT_B_INCH | ADC_CTL1_PARAMS; error+= measure_adc10( &pot_b_sense ); ADC10AE0= ETEMP_SENSE; ADC10CTL1= ETEMP_INCH | ADC_CTL1_PARAMS; error+= measure_adc10( &temp_sense ); /* * temp_sense= 0; int16_t temp; uint8_t c= 0; for( ; c < SAMPLES; ++c ) { error+= measure_adc10( &temp ); temp_sense+= temp; } temp_sense/= SAMPLES; */ //.... pot_a_sense= 1024 - pot_a_sense; int16_t temp; if( pot_a_sense > temp_sense ) { temp= ( pot_a_sense - temp_sense ) * ( pot_b_sense / 30 ); if( temp > DRIVE_CNT ) temp= DRIVE_CNT; temp+=1; temp-=1; if( temp > 70 ) drive_out= temp; } else drive_out= 0; state= STATE_NULL; } // return 0; } //returns error code uint16_t measure_adc10( int16_t* p_val_store ) { ADC10CTL0|= ADC10ON | ENC | ADC10SC; uint16_t serror= 0; uint16_t temp_16= 0; for( ; ADC10CTL1 & ADC10BUSY; ++temp_16 ) { if( temp_16 > SAFTY_MAXADC_COUNT ) { ADC10CTL0&= ~( ENC | ADC10ON ); serror= ERR_ADC_TIMOUT; } } *p_val_store= ADC10MEM; ADC10CTL0&= ~( ENC | ADC10ON ); return serror; } #define SAMPLES 1 #pragma vector= TIMER0_A0_VECTOR __interrupt void timer0_interrupt( ) { TACCR1= drive_out; state|= STATE_TIMER0; }