Beispiel #1
0
// Set up PWM on the given pin
boolean pwmInitDeciHertz(const IOPin* pin, uint32_t deciHertz, PERCENTAGE duty, uint32_t* actualDeciHertz){
	boolean rtn = FALSE;
	const TimerCompare* channel = compareFromIOPin(pin);

	if(channel==null){
		setError(PWM_PIN_NOT_AVAILABLE);
	}else{
		// The pin is valid
		// The pin is valid and available
		TIMER_MODE mode;
		uint16_t icr;
		uint16_t prescaler;
		uint32_t actual;

		const Timer* timer = compareGetTimer(channel);

		// Find the best PWM setting
		boolean valid = timerCalcPwm(timer, deciHertz, 100, &mode, &icr, &prescaler, &actual);

		if(!valid){
			// There is no PWM setting that is valid
			setError( (timerIsInUse(timer)) ? PWM_TIMER_IN_USE : TIMER_HAS_NO_PWM );
		}else{
			// Lets set up the PWM
			timerSetMode(timer,mode);
			if(modeIsICR(mode)){
				// Set the ICR
				PORT icrPort = pgm_read_word(&timer->pgm_icr);
				_SFR_MEM16(icrPort)=icr;
			}

			// Mark the channel as in use
//			compareAttach(channel,&nullTimerCompareCallback,0,null);

			// Turn the pin into an output, low
			pin_make_output(pin, FALSE);

			// Turn on the PWM pin output
			compareSetOutputMode(channel, CHANNEL_MODE_NON_INVERTING);

			// Turn on the timer
			timerSetPrescaler(timer,prescaler);

			// Set the initial duty cycle
			pwmSetDutyCycle(pin,duty);

			// Set the return value
			if(actualDeciHertz){
				*actualDeciHertz = actual;
			}

			rtn = TRUE;
		}
	}
	return rtn;
}
static void soundOn(void){
	#if !defined(_WINDOWS_)
	pwmSetDutyCycle(pwmPin,50);	// Set 50% duty cycle
//	cli();
	#endif

	// initialise random number seed
	seed[0]=0xecu;
	seed[1]=7;
	seed[2]=0xcfu;

}
Beispiel #3
0
int main(void)
{
  #ifndef CFG_PWM
    #ERROR "CFG_PWM must be enabled in projectconfig.h for this example."
  #endif

  // Configure cpu and mandatory peripherals
  // PWM is initialised in systemInit and defaults to using P1.9
  systemInit();

  // Set duty cycle to 50% for square wave output
  pwmSetDutyCycle(50);

  // Frequency can be set anywhere from 2khz and 10khz (4khz is loudest)
  // Note: Since this is a 16-bit timer, make sure the delay is not
  //       greater than 0xFFFF (65535), though anything 2khz and higher
  //       is within an acceptable range
  // The piezo buzzer used for this example is the PS1240, available at
  // http://www.adafruit.com/index.php?main_page=product_info&cPath=35&products_id=160
  pwmSetFrequencyInTicks(CFG_CPU_CCLK / 2000);

  uint32_t currentSecond, lastSecond;
  currentSecond = lastSecond = 0;

  while (1)
  {
    // Beep the piezo buzzer very briefly once per second, toggling the LED at the same time
    currentSecond = systickGetSecondsActive();
    // Make sure that at least one second has passed
    if (currentSecond != lastSecond)
    {
      // Update the second tracker
      lastSecond = currentSecond;
      // Set the LED state and buzzer loudness depending on the current LED state
      if (gpioGetValue(CFG_LED_PORT, CFG_LED_PIN) == CFG_LED_OFF)
      {
        pwmSetFrequencyInTicks(CFG_CPU_CCLK / 4000);            // 4khz (louder)
        pwmStartFixed(200);                                     // 2x as long as @ 2khz since it's 2x faster
        gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON);   // Turn the LED on
      }
      else
      {
        pwmSetFrequencyInTicks(CFG_CPU_CCLK / 2000);            // 2khz (softer)
        pwmStartFixed(100);                                    
        gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);  // Turn the LED off
      }
    }
  }

  return 0;
}
static void soundOff(void){
	#if !defined(_WINDOWS_)
//	sei();
	pwmSetDutyCycle(pwmPin,50);	// Set 50% duty cycle
	#endif
}