Created
August 10, 2018 10:48
-
-
Save brianc118/ffabc5d9ff0d790347510702405e2457 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define LEDPIN 13 | |
#define LATCHPIN 12 | |
volatile uint32_t i = 0; | |
volatile uint32_t j = 0; | |
volatile uint32_t tablesize = 1000; | |
volatile uint32_t tablesize2 = 1000; | |
uint8_t table[1000], table2[1000]; | |
void setup() | |
{ | |
pinMode(LEDPIN, OUTPUT); // PORTB5 | |
pinMode(LATCHPIN, OUTPUT); // PORTB4 | |
noInterrupts(); // disable all interrupts | |
TCCR1A = 0; | |
TCCR1B = 0; | |
TCNT1 = 0; | |
OCR1A = 1; // compare match register. this divides by 2. | |
TCCR1B |= (1 << WGM12); // CTC mode (compare match) | |
TCCR1B |= (1 << CS10) | (1 << CS11); // 64 prescaler | |
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt | |
interrupts(); // enable all interrupts | |
} | |
// freq = 16MHz / 64 / 2 = 125 kHz (or 62.5 kHz on 8MHz) | |
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine | |
{ | |
i++; | |
j++; | |
// the following if statement is slower than j % tablesize | |
// if (j > tablesize) { | |
// j = 0; | |
// } | |
// write to output 1 then switch to output 2 then write to output 2 | |
// (this probably won't work in practice since we instantly write after we toggle latch | |
PORTD = table[j % tablesize]; | |
PORTB |= (1 << PORTB4); // latch on | |
PORTD = table2[j % tablesize2]; | |
PORTB &= ~(1 << PORTB4); // latch off | |
// toggle LED at 250 / 2 / 62.5 Hz = 2 Hz. i.e LED blinks at 1 Hz | |
if (i > 62500) { | |
PORTB ^= (1 << PORTB5); // toggle led | |
i = 0; | |
} | |
} | |
void loop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment