Du kannst doch die Schleife über alle OCRs machen und jeder eine eigene Variable verpassen, die zählt, ob der Wert verändert werden soll.
Also, so könnte man das machen, indem man die inneren Schleife auflöst. Du kannst dir jetzt das Hirn zerrauchen und überlegen, was das alles soll und was es tut und das Zeug kommentieren. Denn wenn du was abgibst, das du nicht blickst, gibt's bestimmt auch ne 5 
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define NUM_OCRS 2
typedef struct
{
uint8_t countdown;
void volatile * const preg;
const uint8_t size;
uint16_t zielwert;
} ocr_count_t;
void loop();
void tick (ocr_count_t*);
ocr_count_t ocr_counters[NUM_OCRS] =
{
{ 0, &OCR2, sizeof (OCR2) },
{ 0, &OCR1A, sizeof (OCR1A) }
};
void tick (ocr_count_t * poc)
{
if (poc->countdown != 0)
{
poc->countdown--;
return;
}
poc->countdown = rand();
if (1 == poc->size)
{
uint8_t * pocr = (uint8_t*) poc->preg;
uint8_t ocr = * pocr;
uint8_t ziel = poc->zielwert;
if (ocr == ziel)
{
ziel = rand();
poc->zielwert = ziel;
}
if (ocr < ziel) ocr++;
if (ocr > ziel) ocr--;
*pocr = ocr;
}
if (2 == poc->size)
{
uint16_t volatile * pocr = (uint16_t volatile *) poc->preg;
uint16_t ocr = * pocr;
uint16_t ziel = poc->zielwert;
if (ocr == ziel)
{
ziel = rand();
poc->zielwert = ziel;
}
if (ocr < ziel) ocr++;
if (ocr > ziel) ocr--;
*pocr = ocr;
}
}
void loop()
{
uint8_t i;
while (1)
{
for (i=0; i < sizeof (ocr_counters) / sizeof (ocr_count_t); i++)
tick (& ocr_counters[i]);
_delay_loop_1 (42);
}
}
Lesezeichen