Module atmega32u4_hal::timer [−][src]
Timers
PWM
atmega32u4_hal
currently only implements timers for PWM. Different uses
might get added later on. To configure a timer for PWM, create a new corresponding
Timer#Pwm
object:
let dp = atmega32u4::Peripherals::take().unwrap(); let mut pwm4 = atmega32u4_hal::timer::Timer4Pwm::new(dp.TIMER4);
Next up, convert your pin into a PWM output. You can only configure PWM for pins already configured as outputs:
let mut pin = portc.pc7.into_output(&mut portc.ddr).into_pwm(&mut pwm4);
Pins supporting PWM
Only the following pins support PWM:
Timer | Channel | Port | Pin |
---|---|---|---|
atmega32u4::TIMER0 | OC0A | atmega32u4::PORTB | PB7 |
atmega32u4::TIMER0 | OC0B | atmega32u4::PORTD | PD0 |
atmega32u4::TIMER1 | OC1A | atmega32u4::PORTB | PB5 |
atmega32u4::TIMER1 | OC1B | atmega32u4::PORTB | PB6 |
atmega32u4::TIMER1 | OC1C | atmega32u4::PORTB | PB7 |
atmega32u4::TIMER3 | OC3A | atmega32u4::PORTC | PC6 |
atmega32u4::TIMER4 | OC4A | atmega32u4::PORTC | PC7 |
atmega32u4::TIMER4 | OC4B | atmega32u4::PORTC | PB6 |
atmega32u4::TIMER4 | OC4D | atmega32u4::PORTD | PD7 |
Example
let dp = atmega32u4::Peripherals::take().unwrap(); // According to the manual, PC7(D13) is connected to Timer/Counter4 let mut pwm4 = atmega32u4_hal::timer::Timer4Pwm::new(dp.TIMER4); // Split portc into 8 pins let mut portc = dp.PORTC.split(); // First make the pin an output, then enable the PWM timer let mut pin = portc.pc7.into_output(&mut portc.ddr).into_pwm(&mut pwm4); // Set a duty cycle pin.set_duty(pin.get_max_duty() / 2);
Structs
Timer0Pwm |
PWM Timer |
Timer1Pwm |
PWM Timer |
Timer3Pwm |
PWM Timer |
Timer4Pwm |
PWM Timer |