-
I've got a piece of code from a Arduino Uno that sets a clock output to run at 1MHz. This is user configurable to run at 1,2,4,8MHz
int ocr1aval = 7; //Default to 1MHz.
// 0 = 8MHz; 1 = 4MHz ;3 = 2MHZ ;7 = 1MHz
TCCR1A = ( (1 << COM1A0));
TCCR1B = ((1 << WGM12) | (1 << CS10));
TIMSK1 = 0;
OCR1A = ocr1aval;
In a new project I would like to use a ATiny85 as a clock generator. Thought I could use the same code as above but getting compile errors so obviously its not as simple as that.
Anyone ideas?
-
1. Please provide a copy of the compile errors you're seeing. This is asking-for-help-on-the-internet 101.
2. https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf is the datasheet you can use to figure out how to configure the timer. It's conspicuous that there is no TCCR1B mentioned in the datasheet. So either look at the documentation for Timer/Counter 1 and figure out how the register bits should be configured (i.e., without using the non-existent TCCR1B). Or, maybe use Timer/Counter 0 instead (TCCR0B does exist, so maaaybe your config would work if you just changed the "1"s to "0"s?)
-
TC1 is a 16 bit counter on most megas but it's a high speed 8 bit counter on this MCU so it's not surprising that registers are different.
That being said, you don't need 16 bits for this so you could use TC0. Here's what I once wrote to get 8MHz from OC0A and 4MHz from OC0B (or vice versa?) but I don't remember what all those bits do so I can't adapt it for your needs without looking in the datasheet.
OCR0A = 1;
OCR0B = 0;
TCCR0A = BV(COM0A0)|BV(COM0B1)|BV(WGM01)|BV(WGM00);
TCCR0B = BV(WGM02)|BV(CS00);
-
It's one of the reasons why I have come to dislike the AVR controllers.
Far too many versions of even simple peripherals which result in incompatibilities and problems with code re-use or porting code to a different variant.
Best method is probably to have the datasheets side to side to compare them.
-
Well, that's a different mcu family. It's pretty normal that such things happens, lower end devices normally have more simple peripherals or lack features which higher families have.
Don't ever expect seamless code migration between different families! Usually the work is not that much, only the lower hardware interface layer.
-
This might be helpfull
https://embeddedthoughts.com/2016/06/06/attiny85-introduction-to-pin-change-and-timer-interrupts/
If using Arduino , watch out not to change the timer used for millis etc ....
/Bingo
-
Thanks for the pointers. Managed to get it working by setting the CLKO fuse and then using prescalers to set the frequency.
// Change to 2 MHz by changing clock prescaler to 4
cli(); // Disable interrupts
CLKPR = (1<<CLKPCE); // Prescaler enable
CLKPR = (1<<CLKPS1); // Clock division factor 4 (0010)
sei(); // Enable interrupts
// Change back to 1 MHz by changing clock prescaler to 8
cli(); // Disable interrupts
CLKPR = (1<<CLKPCE); // Prescaler enable
CLKPR = ((1<<CLKPS1) | (1<<CLKPS0)); // Clock division factor 8 (0011)
sei(); // Enable interrupts