Skip to content

Instantly share code, notes, and snippets.

@JoshAshby
Created February 15, 2010 18:36
Show Gist options
  • Save JoshAshby/304864 to your computer and use it in GitHub Desktop.
Save JoshAshby/304864 to your computer and use it in GitHub Desktop.
//-------------------------------------------
/*
Main.c
2010 - Josh Ashby
[email protected]
http://joshashby.com
http://github.com/JoshAshby
freenode - JoshAshby
Simple little program for the ATINY45 that will read the ADC on PB4 and write that value to PWM on PB0
*/
//-------------------------------------------
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void pwm_setup(void);
void pwm(unsigned int value);
int main(void)
{
pwm_setup();
adc_start();
for ( ; 1==1 ; )
{
pwm(ADCH); // read the ADCH bit, and write it to PWM
}
return 1;
}
void pwm_setup(void)
{
TCCR0B |= (1<<CS00); //get everything set up for PWM
TCCR0A |= (1<<WGM00);
DDRB |= (1<<0);
}
void pwm(unsigned int value)
{
TCCR0A |= (1<<COM0A1); // turn pwm on
OCR0A = value; //set pwm to said vale
}
ISR(ADC_vect)
{
//don't have to do anything as long as ADCH is updated
}
void adc_start(void)
{
ADCSRA |= (1 << ADPS2)
| (1 << ADPS1)
| (1 << ADPS0) // Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
| (1 << ADATE) //set auto trigger
| (1 << ADEN) // Enable ADC
| (1 << ADIE) // Enable ADC Interrupt
| (1 << ADSC); // Start A2D Conversions
ADMUX |= (1 << ADLAR)
| (1<<MUX1); // Left adjust ADC result to allow easy 8 bit reading and set the input pin as PB4
sei();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment