You are on page 1of 2

1 /*

2 * vince PWM using the ATtiny85

3 *

4 2018-4-4

5 from https://github.com/Jan--Henrik/Attiny-PWM-generator/blob/master/Arduino
%20code

7 todo

8 add stability code

9 see

10 https://hackingmajenkoblog.wordpress.com/2016/02/01/making-accurate-adc-readings-
on-the-arduino/

11

12 ATtiny connections.

13

14 pin

15 1 nc

16 2 analog input 0 <= 5 vdc

17 3 PWM out

18 4 GND

19 5 nc

20 6 nc

21 7 nc

22 8 VCC 5 vdc

23

24

25 */

26

28 const int pinReadDelay = 10; // ms delay to stabilize reading on Arduino ADC when
switching pin-to-pin?
29 const int loopDelay = 10;

30

31 int analogInput = PB3;

32

33 volatile uint8_t* Port[] = {&OCR1B};

34

35 void setup() {

36 pinMode(analogInput, INPUT);

37 pinMode(PB4, OUTPUT);

38 #ifdef _AVR_IOTN85_H_

39 // this is for the ATtiny85 only.

40 // Configure counter/timer1 for fast PWM on PB4

41 GTCCR = 1 << PWM1B | 3 << COM1B0;

42 TCCR1 = 3 << COM1A0 | 7 << CS10;

43 #endif

44 }

46

47 void loop() {

48 analogRead(analogInput); // Hey CPU think about ADC

49 delay(pinReadDelay); // stabilize the readings.

50 int an = analogRead(analogInput) >> 2; //divide by 4.

51 *Port[0] = 255 - an;

52 // delay(loopDelay);

53 }

You might also like