Skip to content

Commit bfffb17

Browse files
Peter Van HoyweghenPeter Van Hoyweghen
Peter Van Hoyweghen
authored and
Peter Van Hoyweghen
committed
Improve SPI clock configuration.
1 parent 82e5d4a commit bfffb17

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

build/shared/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@
3838

3939
#define PROG_FLICKER true
4040

41-
// Configure SPI clock.
42-
// E.g. for an attiny @128 kHz:
43-
// (pulseWidth should be > 2 cpu cycles, so take 3 cycles:)
44-
// #define SPI_CLOCK (128000/3)
41+
// Configure SPI clock (in Hz).
42+
// E.g. for an attiny @128 kHz: the datasheet states that both the high
43+
// and low spi clock pulse must be > 2 cpu cycles, so take 3 cycles i.e.
44+
// divide target f_cpu by 6:
45+
// #define SPI_CLOCK (128000/6)
4546
//
46-
// A clock slow enough for an attiny85 @ 1MHz, is a reasonable default:
47+
// A clock slow enough for an attiny85 @ 1MHz, is a reasonable default:
48+
49+
#define SPI_CLOCK (1000000/6)
4750

48-
#define SPI_CLOCK (1000000/3)
4951

5052
// Select hardware or software SPI, depending on SPI clock.
5153
// Currently only for AVR, for other archs (Due, Zero,...),
@@ -161,7 +163,7 @@ public:
161163
}
162164

163165
void beginTransaction(SPISettings settings) {
164-
pulseWidth = 1000 / (settings.clock / 1000);
166+
pulseWidth = (500000 + settings.clock - 1) / settings.clock;
165167
if (pulseWidth == 0)
166168
pulseWidth = 1;
167169
}

0 commit comments

Comments
 (0)