80Hz PWM with attiny85, looking for advice
Hi there
I'm fairly new to the attiny world and I am looking for some help regarding an 80 Hz PWM I would like to generate.
The goal is an Analog to PWM converter to drive some fans which accept a 72 - 90 Hz PWM. The datasheet specifies ideally 80 Hz. The circuit I used is already up and running in some other projects which aren't quite as picky with the frequency range.
The ADC part i already figured out and it is working well. With the PWM frequency however... by playing with the timer and scaler registers the closest I got was around 62Hz.
I'm using an Ardoino Uno as an ISP if that matters.
Has anyone a piece of code, an online source which he could direct me to or some advice on how this would be achievable with an attiny85? Or is a PWM frequency outside the predefined scalers just not possible here?
Posted Feb 2, 2018, 12:18 AM UTC
The code below is for an atmega328p but it might work "out of the box" for an attiny85. I originally wrote it to take an ADC signal and output it to serial while at the same time using that ADC value to change the pitch of a buzzer. I started with the atmega because it was easier to use serial output to figure out what was going on. I would be willing to modify the code for you if you would like. Edit: Sorry, I have not got a place to put the file. Thought I could paste it below but that's a mess. http://textuploader.com/dhtjh
Thank you, I tried a modified version of your script and it indeed works. The frequency changed accoring to TCNT1/ORC1A which would result in a higher/lower pitch on a speaker.
I'm looking to have the same frequency at a different duty cycle though. Do you have any idea how I would do that?
(I disabled some of the relevant lines here to continue working on it) http://textuploader.com/dhr0z
What duty cycle do you want? @80 Mhz? Edit: I assume you are programming a raw attiny 85. The base frequency will be 8 Mhz.
TCCR0A = (1 << WGM01); //CTC mode
TCCR0B = (1 << CS02) | (1<< CS00); //divider of 1024
OCR0A = 97; //this gives a frequency of 79.7 Hz
OCR0B = xx; //must be less than OCR0A
TIMSK = (1 << OCIE0A) | (1 << OCIE0B); // enables both interrupts OCIE0A is the PWM frequency. OCIE0B is the duty cycle
You will then need 2 interrupt routines for OCIE0A and OCIE0B respectively. The A routine is the PWM and the B one is used to change the duty cycle. Remember the counter resets when counter A is reached but it goes passed counter B. That is whey the value for B must be less than that of A. If you do it the other way around B will never be triggered.