int main(void) { boardConfig(); tickConfig( 1, 0 ); digitalConfig( 0, ENABLE_DIGITAL_IO ); uint8_t dutyCycle = 0; /* 0 a 255 */ analogConfig( ENABLE_ANALOG_INPUTS ); pwmConfig( 0, PWM_TIMERS_ENABLE ); pwmConfig( PWM0, PWM_OUTPUT_ENABLE ); pwmConfig( PWM7, PWM_OUTPUT_ENABLE ); pwmWrite( PWM7, 0 ); pwmWrite( PWM0, 0 ); while(1) { dutyCycle = analogRead(AI1) / 4 ; pwmWrite( PWM7, dutyCycle ); pwmWrite( PWM0, dutyCycle ); } return 0 ; }
/* normally the tone frequency should be 31 to 4978, refer to piches.h */ void tone(uint8_t pin, uint16_t frequency, unsigned long duration) { static struct rt_timer timer2, timer3; rt_timer_t timer; RT_ASSERT(frequency * 2 < UINT16_MAX); if(pin < 2 || pin > 3) return; if (pin == 2) { timer = &timer2; } else if (pin == 3) { timer = &timer3; } rt_timer_stop(timer); rt_timer_init(timer, "pwmkill", pwm_shutdown, (void*) pin, rt_tick_from_millisecond(duration), RT_TIMER_FLAG_ONE_SHOT); TIM_config(pin, frequency); pwmConfig(pin, UINT8_MAX / 2); rt_timer_start(timer); }
void analogWrite(uint8_t pin, uint8_t value) { // We need to make sure the PWM output is enabled for those pins // that support it, as we turn it off when digitally reading or // writing with them. Also, make sure the pin is in output mode // for consistenty with Wiring, which doesn't require a pinMode // call for the analog output pins. if(value == 0) { pinMode(pin, OUTPUT); digitalWrite(pin, LOW); } else if(value == 255) { pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); } else { pwmConfig(pin, value); } }