Mam programik przykladowy demo.c w WinAVR. Dzieki PWM zapala on diode z rosnaca i nastepnie malejaca intensywnoscia na porcie PB1. Potrzebuje sterowania trzema diodami w taki sam sposob. Czy ktos wie jak to zrobic np. na PB2 i PB3 ?
Oto kod:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/sleep.h>
#define OC1 PB1
#define DDROC DDRB
#define OCR OCR1A
#define PWM10 WGM10
#define PWM11 WGM11
#if defined(COM11)
# define XCOM11 COM11
#elif defined(COM1A1)
# define XCOM11 COM1A1
#else
# error "need either COM1A1 or COM11"
#endif
enum { UP, DOWN };
volatile uint16_t pwm; /* Note [1] */
volatile uint8_t direction;
SIGNAL (SIG_OVERFLOW1) /* Note [2] */
{
switch (direction) /* Note [3] */
{
case UP:
if (++pwm == 1023)
direction = DOWN;
break;
case DOWN:
if (--pwm == 0)
direction = UP;
break;
}
OCR = pwm; /* Note [4] */
}
void
ioinit (void) /* Note [5] */
{
/* tmr1 is 10-bit PWM */
TCCR1A = _BV (PWM10) | _BV (PWM11) | _BV (XCOM11);
/* tmr1 running on full MCU clock */
TCCR1B = _BV (CS10);
/* set PWM value to 0 */
OCR = 0;
/* enable OC1 and PB2 as output */
DDROC = _BV (OC1);
timer_enable_int (_BV (TOIE1));
/* enable interrupts */
sei ();
}
int
main (void)
{
ioinit ();
/* loop forever, the interrupts are doing the rest */
for (;
/* Note [6] */
;
return (0);
}
Oto kod:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/sleep.h>
#define OC1 PB1
#define DDROC DDRB
#define OCR OCR1A
#define PWM10 WGM10
#define PWM11 WGM11
#if defined(COM11)
# define XCOM11 COM11
#elif defined(COM1A1)
# define XCOM11 COM1A1
#else
# error "need either COM1A1 or COM11"
#endif
enum { UP, DOWN };
volatile uint16_t pwm; /* Note [1] */
volatile uint8_t direction;
SIGNAL (SIG_OVERFLOW1) /* Note [2] */
{
switch (direction) /* Note [3] */
{
case UP:
if (++pwm == 1023)
direction = DOWN;
break;
case DOWN:
if (--pwm == 0)
direction = UP;
break;
}
OCR = pwm; /* Note [4] */
}
void
ioinit (void) /* Note [5] */
{
/* tmr1 is 10-bit PWM */
TCCR1A = _BV (PWM10) | _BV (PWM11) | _BV (XCOM11);
/* tmr1 running on full MCU clock */
TCCR1B = _BV (CS10);
/* set PWM value to 0 */
OCR = 0;
/* enable OC1 and PB2 as output */
DDROC = _BV (OC1);
timer_enable_int (_BV (TOIE1));
/* enable interrupts */
sei ();
}
int
main (void)
{
ioinit ();
/* loop forever, the interrupts are doing the rest */
for (;
;
return (0);
}