Help converting timer code from Atmega to Attiny - Page 1
Forum section: electronics / beginners

author: gmc
author_rank: Regular Contributor
timestamp: October 28, 2021, 09:45:30 pm
content: 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,8MHzCode:int ocr1aval = 7; //Default to 1MHz.// 0 = 8MHz; 1 = 4MHz ;3 = 2MHZ ;7 = 1MHzTCCR1A = ( (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?

author: rs20
author_rank: Super Contributor
timestamp: October 28, 2021, 09:54:18 pm
content: 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.pdfis 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?)

author: magic
author_rank: Super Contributor
timestamp: October 29, 2021, 03:01:46 am
content: 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.Code:OCR0A = 1;OCR0B = 0;TCCR0A = BV(COM0A0)|BV(COM0B1)|BV(WGM01)|BV(WGM00);TCCR0B = BV(WGM02)|BV(CS00);

author: Doctorandus_P
author_rank: Super Contributor
timestamp: October 29, 2021, 07:17:15 am
content: 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 29, 2021, 07:54:29 am
content: 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.

author: bingo600
author_rank: Super Contributor
timestamp: October 30, 2021, 06:18:59 am
content: This might be helpfullhttps://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

author: gmc
author_rank: Regular Contributor
timestamp: November 01, 2021, 10:06:33 pm
content: Thanks for the pointers. Managed to get it working by setting the CLKO fuse and then using prescalers to set the frequency.Code:// Change to 2 MHz by changing clock prescaler to 4cli(); // Disable interruptsCLKPR = (1<<CLKPCE); // Prescaler enableCLKPR = (1<<CLKPS1); // Clock division factor 4 (0010)sei(); // Enable interrupts// Change back to 1 MHz by changing clock prescaler to 8cli(); // Disable interruptsCLKPR = (1<<CLKPCE); // Prescaler enableCLKPR = ((1<<CLKPS1) | (1<<CLKPS0)); // Clock division factor 8 (0011)sei(); // Enable interrupts