Skip to content

Instantly share code, notes, and snippets.

@JoshAshby
Created February 14, 2010 20:19
Show Gist options
  • Save JoshAshby/304232 to your computer and use it in GitHub Desktop.
Save JoshAshby/304232 to your computer and use it in GitHub Desktop.
#include "adc.h"
#include "pwm.h"
//-------------------------------------------
//-------------------------------------------
int main(void)
{
pwm_setup();
for(;;){
pwm(65535, 0, 1);
}
return 0; // never reached
}
//-------------------------------------------
/*
PWM function
2010 - Josh Ashby
[email protected]
http://joshashby.com
http://github.com/JoshAshby
freenode - JoshAshby
*/
//-------------------------------------------
#include "pwm.h"
//setup timer1 for
int pwm_setup(void)
{
OCR1AH = 0;
DDRB |= (1<<1);
DDRB |= (1<<2);
TCCR1A = (1<<COM1A1)|(1<<WGM10);
TCCR1B = (1<<WGM12);
pwm_value = 0;
TIMSK1 |= (1<<TOIE1);
sei();
return 1;
}
int pwm(unsigned int value, int pin, unsigned int speed)
{
pwm_value = value;
pwm_speed = speed;
TCCR1B = (1<<CS10);
return 1;
}
enum { UP, DOWN };
ISR (TIMER1_OVF_vect)
{
unsigned int pwm;
int direction = UP;
switch (direction)
{
case UP:
for (pwm = 0; pwm <= pwm_value; pwm += pwm_speed){
OCR1AL = pwm;
pwm_value_old = pwm;
}
break;
case DOWN:
for (pwm = pwm_value_old; pwm >= pwm_value; pwm += pwm_speed){
OCR1AL = pwm;
pwm_value_old = pwm;
}
break;
}
}
//-------------------------------------------
/*
PWM function
2010 - Josh Ashby
[email protected]
http://joshashby.com
http://github.com/JoshAshby
freenode - JoshAshby
*/
//-------------------------------------------
#ifndef PWM_H
#define PWM_H
#include <avr/io.h>
#include <util/delay.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
int pwm_start(void);
int pwm(unsigned int value, int pin, unsigned int speed);
int pwm_ramp(unsigned int value, int pin, unsigned int speed);
unsigned int pwm_value;
unsigned int pwm_speed;
unsigned int pwm_value_old;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment