Skip to content

Instantly share code, notes, and snippets.

@dranger003
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dranger003/4ffb85f76187c5610cd6 to your computer and use it in GitHub Desktop.
Save dranger003/4ffb85f76187c5610cd6 to your computer and use it in GitHub Desktop.
const double _F = F_CPU / 38000.0 + 0.5 - 1;
const double _D = _F * 0.33 + 0.5 - 1;
const uint16_t _cv[] = { 40085, 36.39716198 };
const uint16_t _cp[][2] =
{
{ 97, 24 },
{ 24, 24 },
{ 48, 24 },
{ 24, 1032 },
{ 48, 1032 },
};
const uint8_t _cs[][64] =
{
{ 39,
0, 2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 3,
0, 2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 3,
0, 2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1 },
{ 39,
0, 2, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 3,
0, 2, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 3,
0, 2, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1 },
};
const uint16_t *_cw = _cv;
const uint16_t (*_cu)[2] = _cp;
const uint8_t *_ct = _cs[1];
volatile uint8_t _cr = 0;
uint16_t _cc = 0;
uint16_t _ci = 0;
uint16_t _cj = 0;
ISR(TIMER1_COMPB_vect)
{
if (!_cr)
return;
if (!(TCCR1A & _BV(COM1A1)) && _cc == 0 && _ci == 0)
{
TCCR1A |= _BV(COM1A1);
++_ci;
return;
}
if (_cc >= _cu[_ct[_ci]][_cj % 2] - 1)
{
if (_ci > _ct[0] - 1)
{
TCCR1A &= ~_BV(COM1A1);
_cr = 0;
_ci = 0;
_cj = 0;
}
else
{
TCCR1A ^= _BV(COM1A1);
if (_cj % 2)
++_ci;
++_cj;
}
_cc = 0;
}
else
++_cc;
}
int main()
{
sei();
Serial.begin(115200);
Serial.println("main()");
DDRB = _BV(PORTB1) | _BV(PORTB2);
TIMSK1 = _BV(OCIE1B);
ICR1 = (uint16_t)_F;
OCR1A = (uint16_t)_D;
OCR1B = (uint16_t)_D;
TCCR1A = _BV(COM1B1) | _BV(WGM11);
TCCR1B = _BV(WGM12) | _BV(WGM13) | _BV(CS00);
for (;;)
{
if (Serial.available())
{
uint8_t k = Serial.read();
switch (k)
{
case 's':
{
uint16_t f = F_CPU * 1.0 / _cw[0] + 0.5 - 1;
uint16_t d = f * (_cw[1] / 100.0) + 0.5 - 1;
char buf[64] = { 0 };
sprintf(buf, "f=%u, d=%u", f, d);
Serial.println(buf);
ICR1 = f;
OCR1A = d;
OCR1B = d;
}
break;
case 't':
_cr = 1;
break;
default:
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment