-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathATtiny_PWM_Phase_Corret_25kHz.ino
46 lines (39 loc) · 1.34 KB
/
ATtiny_PWM_Phase_Corret_25kHz.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* ATtiny85
* -------u-------
* RST - A0 - (D 5) --| 1 PB5 VCC 8 |-- +5V
* | |
* A3 - (D 3) --| 2 PB3 PB2 7 |-- (D 2) - A1 --> 10K Potentiometer
* | |
* A2 - (D 4) --| 3 PB4 PB1 6 |-- (D 1) - PWM --> Fan Blue wire
* | |
* Gnd ---| 4 GND PB0 5 |-- (D 0) - PWM --> Disabled
* -----------------
*/
// normal delay() won't work anymore because we are changing Timer1 behavior
// Adds delay_ms and delay_us functions
#include <util/delay.h> // Adds delay_ms and delay_us functions
// Clock at 8mHz
#define F_CPU 8000000 // This is used by delay.h library
const int PWMPin = 1; // Only works with Pin 1(PB1)
const int PotPin = A1;
void setup()
{
pinMode(PWMPin, OUTPUT);
// Phase Correct PWM Mode, no Prescaler
// PWM on Pin 1(PB1), Pin 0(PB0) disabled
// 8Mhz / 160 / 2 = 25Khz
TCCR0A = _BV(COM0B1) | _BV(WGM00);
TCCR0B = _BV(WGM02) | _BV(CS00);
// Set TOP and initialize duty cycle to zero(0)
OCR0A = 160; // TOP - DO NOT CHANGE, SETS PWM PULSE RATE
OCR0B = 0; // duty cycle for Pin 1(PB1)
}
void loop()
{
int in, out;
in = analogRead(PotPin);
out = map(in, 0, 1023, 0, 160);
OCR0B = out;
_delay_ms(200);
}