adc_setup, adc_read, pwm_setup, pwm_write. The ADC is set up, the PWM is ready to go and to get one talk to the other is as simple as just copying a value across: #define LED PB1 Remember to set the LED pin, PB1 as an output. I'll leave that up to you as we move on to combining the ADC and PWM. Now would be a good time to package all the PWM setup code into its own method. Set the ADC input to PB2/ADC1, left adjust result Selecting the ADC input is easy - look at Table 14.3 on page 92 of the datasheet for the correct mux settings. There are spare pins left over which could be utilised elsewhere should the need arise - in the microscope board I've broken these out into an extra header.įrom the schematic we can see that the PWM output to the LED will be on pin 6 (PB1) and the ADC input we are using is on pin 7 (ADC1, PB2). It's very simple, and it's certainly not going to win any awards for style or efficiency. This example is very simple, but will hopefully serve as a jumping off point for people new to the ADC and PWM systems in AVR microcontrollers.įirst let's take a look at a basic overview of the circuit we are using: In every PWM period counter must count 256 steps, so frequency of signal is 256 times lower than counter clock from prescaler. In not inverting mode duty cycle OCR2/256100 and it’s 50 if OCR2 is 0x80(middle between 0x00 0xFF). I want to use an ATtiny13 to control the brightness of an LED light source ( as a controller for this) and therefore need to control both the ADC input and the PWM output. From this all turns out that PWM duty cycle depends on OCR2 register.
0 Comments
Leave a Reply. |