Created
September 3, 2018 18:38
-
-
Save valerionew/3b7f3be883065c6469940819f6ff9fe0 to your computer and use it in GitHub Desktop.
Blinking a led at 38kHz on an attiny10. Gaining extra precision by swithching from the default :8 prescaler to the :1 prescaler (8MHz clock)
This file contains hidden or 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
#include <avr/interrupt.h> // cli | |
int main (void) { | |
char oldsreg = SREG; // save the interrupt setting register to oldsreg | |
cli(); // disable all the interrupts (SREG = 0) | |
CCP = 0xD8; // signature to CCP | |
CLKMSR = 0; // use clock 00: Calibrated Internal 8 MHzOscillator | |
CCP = 0xD8; // signature | |
CLKPSR = 0; // set prescaler to :1 (0x00) | |
SREG = oldsreg; // restore the sreg, enabling the interrupts | |
DDRB = 1; // set DDRB.PB0 to output | |
OCR0A = 105; // set OCR0A | |
TCCR0A = 1<<COM0A0; // set timer to toggle PB0 on compare match | |
TCCR0B = 1<<WGM02 | 1<<CS00; // set CTC mode | enable and set prescaler :1 | |
while(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's a dangerously lightweight program. I might be able to optimize it further into AVR assembly, especially if the halt instruction leaves the clock running.
Although, to be honest, I'm not really sure it would be useful. I'm going under the assumption that OCR0A contains the number of clock cycles needed to toggle PB0. I imagine it would be relatively easy to drop one of the writes to CCP, like so:
could replace four lines in your C++ program: