/* ******************************************************** * * * Simple program with avr-gcc and ATtiny13 * -------------------------------------------------------- * * Created on: 07.06.2016 * Author: Paolo Iocco * ******************************************************** */ /* ******************************************************** * ATtiny 13/45/85 Pin map +--\/--+ /Reset ADC0 PB5 -|1° 8|- Vcc ADC3 PB3 -|2 7|- PB2 ADC1 SCK ADC2 PB4 -|3 6|- PB1 OC0B MISO AIN1 GND -|4 5|- PB0 OC0A MOSI AIN0 +------+ * ********************************************************* */ #include "main.h" void adc_setup (void) { // Set the ADC input to PB2/ADC1 ADMUX |= (1 << MUX0); ADMUX |= (1 << ADLAR); // Set the prescaler to clock/128 & enable ADC ADCSRA |= (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN); } int adc_read (void) { // Start the conversion ADCSRA |= (1 << ADSC); // Wait for it to finish while (ADCSRA & (1 << ADSC)); return ADCH; } void pwm_setup (void) { // Set Timer 0 prescaler to clock/8. // At 9.6 MHz this is 1.2 MHz. TCCR0B |= (1 << CS01) | (1 << CS00); // Set to 'Fast PWM' mode TCCR0A |= (1 << WGM01) | (1 << WGM00); // Clear OC0B output on compare match, upwards counting. TCCR0A |= (1 << COM0B1); } void pwm_write (int val) { OCR0B = val; } int main (void) { int adc_in; // LED is an output. DDRB |= (1 << LED); adc_setup(); pwm_setup(); while (1) { // Get the ADC value adc_in = adc_read(); // Now write it to the PWM counter pwm_write(adc_in); } }