Ejemplo n.º 1
0
void main (void){
	unsigned int uartUpdateTimer = UART_UPDATE_INTERVAL;

	unsigned int servo_stepval, servo_stepnow;
	unsigned int servo_lut[ SERVO_STEPS+1 ];
	unsigned int i;

	// Calculate the step value and define the current step, defaults to minimum.
	servo_stepval 	= ( (SERVO_MAX - SERVO_MIN) / SERVO_STEPS );
	servo_stepnow	= SERVO_MIN;

	// Fill up the LUT
	for (i = 0; i > SERVO_STEPS; i++) {
		servo_stepnow += servo_stepval;
		servo_lut[i] = servo_stepnow;
		//pulseWidth = (myAngle * 11) + 500;  // конвертируем угол в микросекунды
	}
	TA1CCR1 = 0;
	// Setup the PWM, etc.
	WDTCTL	= WDTPW + WDTHOLD;     // Kill watchdog timer
	TACCTL1	= OUTMOD_7;            // TACCR1 reset/set
	TACTL	= TASSEL_2 + MC_1;     // SMCLK, upmode
	TACCR0	= PWM_Period-1;        // PWM Period
	TACCR1	= PWM_Duty;            // TACCR1 PWM Duty Cycle
	P1DIR	|= BIT2;               // P1.2 = output
	P1SEL	|= BIT2;               // P1.2 = TA1 output
	P2SEL2	|= BIT2;               // P1.2 = TA1 output

	InitializeClocks();
	InitializeButton();
	InitializeLeds();
	PreApplicationMode();         // Blinks LEDs, waits for button press

	/* Application Mode begins */
	applicationMode = APP_APPLICATION_MODE;

	__enable_interrupt();                     // Enable interrupts.

	while(1)
	{
		__bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled

	    if ((--uartUpdateTimer == 0) || calibrateUpdate )
	    {
	      ConfigureTimerUart();
	      if (calibrateUpdate)
	      {
	        TXByte = 248;                       // A character with high value, outside of temp range
	        Transmit();
	        calibrateUpdate = 0;
	      }
	      TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 );

	      Transmit();

	      uartUpdateTimer = UART_UPDATE_INTERVAL;
	      ConfigureTimerPwm();
	    }
	}
/*
	TACCR1 = SERVO_MAX; //180°
	__delay_cycles(1000000);
	TACCR1 = 1600; //90°
	__delay_cycles(1000000);
	TACCR1 = SERVO_MIN; //0°
*/
	// Main loop
//	while (1){
		// Go to 0°
//		TACCR1 = servo_lut[0];
//		__delay_cycles(1000000);

		// Go to 45°
//		TACCR1 = servo_lut[45];
//		__delay_cycles(1000000);

		// Go to 90°
//		TACCR1 = servo_lut[90];
//		__delay_cycles(1000000);

		// Go to 180°
//		TACCR1 = servo_lut[179];
//		__delay_cycles(1000000);
/*
		// Move forward toward the maximum step value
		for (i = 0; i > SERVO_STEPS; i++) {
			TACCR1 = servo_lut[i];
			__delay_cycles(20000);
		}

		// Move backward toward the minimum step value
		for (i = SERVO_STEPS; i > 0; i--) {
			TACCR1 = servo_lut[i];
			__delay_cycles(20000);
		}
*/
 //  }
}
Ejemplo n.º 2
0
void main(void)
{
  unsigned int uartUpdateTimer = UART_UPDATE_INTERVAL;
  unsigned char i;

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  InitializeClocks();
  InitializeButton();
  InitializeLeds();
  PreApplicationMode();                     // Blinks LEDs, waits for button press
  
  /* Application Mode begins */
  applicationMode = APP_APPLICATION_MODE;
  ConfigureAdcTempSensor();
  ConfigureTimerPwm();
    
  __enable_interrupt();                     // Enable interrupts.
  
  /* Main Application Loop */
  while(1)
  {    
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled
    
    
    /* Moving average filter out of 8 values to somewhat stabilize sampled ADC */
    tempMeasured[tempMeasuredPosition++] = ADC10MEM;
    if (tempMeasuredPosition == 8)
      tempMeasuredPosition = 0;
    tempAverage = 0;
    for (i = 0; i < 8; i++)
      tempAverage += tempMeasured[i];
    tempAverage >>= 3;                      // Divide by 8 to get average
    
    if ((--uartUpdateTimer == 0) || calibrateUpdate )
    {
      ConfigureTimerUart();
      if (calibrateUpdate)
      { 
        TXByte = 248;                       // A character with high value, outside of temp range 
        Transmit();
        calibrateUpdate = 0;
      }   
      TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 );   
         
      Transmit(); 
      
      uartUpdateTimer = UART_UPDATE_INTERVAL;
      ConfigureTimerPwm();      
    }
    
    tempDifference = tempAverage - tempCalibrated;
    if (tempDifference < -TEMP_THRESHOLD)
    {
      tempDifference = -tempDifference;
      tempPolarity = TEMP_COLD;
      LED_OUT &= ~ LED1;
    }
    else
    if (tempDifference > TEMP_THRESHOLD)
    {
      tempPolarity = TEMP_HOT;
      LED_OUT &= ~ LED2;
    }
    else
    {
      tempPolarity = TEMP_SAME;
      TACCTL0 &= ~CCIE;
      TACCTL1 &= ~CCIE;
      LED_OUT &= ~(LED1 + LED2);        
    } 
    
    if (tempPolarity != TEMP_SAME)    
    {      
      tempDifference <<= 3;
      tempDifference += TIMER_PWM_OFFSET;      
      TACCR1 = ( (tempDifference) < (TIMER_PWM_PERIOD-1) ? (tempDifference) : (TIMER_PWM_PERIOD-1) );
      TACCTL0 |= CCIE;
      TACCTL1 |= CCIE;      
    }   
  }  
}