Commit 899ce952 authored by Jan Kasprzak's avatar Jan Kasprzak
Browse files

pwm.c: modified for ATtiny45/step-up

So far two PWM channels OC1A/OC1B with sub-LSB resolution possible
(but unused yet) using timer IRQ.

TODO: get rid of OCR1C register and let it count to 255.
parent 280768d8
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -4,7 +4,7 @@
#define TESTING_FW 1
#define TESTING_FW 1


#define N_LEDS 7
#define N_LEDS 7
#define N_PWMLEDS 3
#define N_PWMLEDS 2
#define N_PWMLED_MODES 4
#define N_PWMLED_MODES 4


#define N_BUTTONS 2
#define N_BUTTONS 2
@@ -36,7 +36,7 @@ void timer_start_slow_adcs();
 * A/D converter frequency (125 kHz). Note that this is not the Top
 * A/D converter frequency (125 kHz). Note that this is not the Top
 * value of T/C 1, it is shifted by PWM_STEP_SHIFT as described in pwm.c
 * value of T/C 1, it is shifted by PWM_STEP_SHIFT as described in pwm.c
 */
 */
#define PWM_MAX 0x780
#define PWM_MAX 0x340
void init_pwm();
void init_pwm();
void susp_pwm();
void susp_pwm();
void pwm_off(unsigned char n);
void pwm_off(unsigned char n);
+13 −36
Original line number Original line Diff line number Diff line
@@ -7,7 +7,7 @@


#define PWM_STEP_SHIFT 2 /* sub-LSB precision */
#define PWM_STEP_SHIFT 2 /* sub-LSB precision */
#define PWM_TOP (((PWM_MAX) + (4 << (PWM_STEP_SHIFT))) >> (PWM_STEP_SHIFT))
#define PWM_TOP (((PWM_MAX) + (4 << (PWM_STEP_SHIFT))) >> (PWM_STEP_SHIFT))
#if PWM_TOP > 0x3FF
#if PWM_TOP > 0x0FF
#error PWM_TOP too high
#error PWM_TOP too high
#endif
#endif


@@ -37,26 +37,17 @@ void init_pwm()


	enable_pll();
	enable_pll();


	// PWM channel D is inverted, ...
	TCCR1 = _BV(CTC1) | _BV(CS10);	// no clock prescaling
	TCCR1C = _BV(COM1D1) | _BV(COM1D0) | _BV(PWM1D);
	GTCCR = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
	// PWM channels A and B are not
	TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
	TCCR1D = 0;
	TCCR1B = _BV(CS10); 			// no clock prescaling


	TC1H = PWM_TOP >> 8;
	OCR1C = PWM_TOP;
	OCR1C = PWM_TOP & 0xFF;				// TOP value
	OCR1A = OCR1B = 0;		// initial stride is 0


	TC1H = PWM_TOP >> 8;		// PWM3 is inverted
	DDRB  &= ~(_BV( PB1 ) | _BV( PB4 )); // tristate it
	OCR1D = PWM_TOP & 0xFF;
	PORTB &= ~(_BV( PB1 ) | _BV( PB4 )); // set to zero

	TC1H = 0x00;
	OCR1B = OCR1A = 0;		// initial stride is 0

	DDRB  &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // tristate it
	PORTB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // set to zero
}
}


#if 0
void susp_pwm()
void susp_pwm()
{
{
	unsigned char i;
	unsigned char i;
@@ -71,6 +62,7 @@ void susp_pwm()


	PLLCSR &= ~(_BV(PLLE) | _BV(PCKE));
	PLLCSR &= ~(_BV(PLLE) | _BV(PCKE));
}
}
#endif


void pwm_off(unsigned char n)
void pwm_off(unsigned char n)
{
{
@@ -79,35 +71,21 @@ void pwm_off(unsigned char n)


		switch (n) {
		switch (n) {
		case 0: DDRB &= ~_BV(PB1); break;
		case 0: DDRB &= ~_BV(PB1); break;
		case 1: DDRB &= ~_BV(PB3); break;
		case 1: DDRB &= ~_BV(PB4); break;
		case 2: DDRB &= ~_BV(PB5); break;
		}
		}
	}
	}
}
}


static void pwm_update_hw(unsigned char n)
static void pwm_update_hw(unsigned char n)
{
{
	unsigned char hi, lo;
	uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;
	uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;


	if (n == 2)
		stride = PWM_TOP - stride;

	hi = stride >> 8;
	lo = stride & 0xFF;

	switch (n) {
	switch (n) {
	case 0:
	case 0:
		TC1H = hi;
		OCR1A = stride;
		OCR1A = lo;
		break;
		break;
	case 1:
	case 1:
		TC1H = hi;
		OCR1B = stride;
		OCR1B = lo;
		break;
	case 2:
		TC1H = hi;
		OCR1D = lo;
		break;
		break;
	}
	}
}
}
@@ -124,8 +102,7 @@ void pwm_set(unsigned char n, uint16_t stride)


		switch(n) {
		switch(n) {
		case 0: DDRB |= _BV(PB1); break;
		case 0: DDRB |= _BV(PB1); break;
		case 1: DDRB |= _BV(PB3); break;
		case 1: DDRB |= _BV(PB4); break;
		case 2: DDRB |= _BV(PB5); break;
		}
		}
	}
	}
}
}