Simple PWM

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Am I being stupid, what have I missed out?
I have no problems getting it to work if I put the stuff inside the ISR inside my while(1) loop:

/*----------------------------------------------------------------------*/
/* FAT file system sample project for FatFs            (C)ChaN, 2010    */
/*----------------------------------------------------------------------*/

#include 
#include "stdio.h"
#include "util/delay.h"
#include 
#include 

/*-----------------------------------------------------------------------*/
/* Main                                                                  */
int main(void) {
	TCCR0A |= (1 << COM0A1) | (1 << WGM01) | (1 << WGM00);
	TCCR0B |= (1 << CS00); // 1024 prescale
	OCR0A = 0x00;
	TIMSK0 |= (1 << TOIE0);
	//set OCR0 as output here:
	DDRB |= 0b10000000;
	while (1) {
	}
}

enum {UP, DOWN};
ISR (TIMER0_OVF_vect) {
	static uint16_t pwm = 0;
	static uint8_t direction = UP;
	static uint8_t divider = 0;
	if (++divider == 10) {
		divider = 0;
		switch (direction) {
		case UP:
			if (++pwm == 0xFF) //timer overflows at 255
				direction = DOWN;
			break;

		case DOWN:
			if (--pwm == 0)
				direction = UP;
			break;
		}
		OCR0A = pwm;
	}
}

I just didnt want to dedicate while(1) time to the pwm, so I thought I could just slap it in an ISR?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No sei()

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

woops.
Thanks