AVR programming introduction/Serial Peripheral Interface

Serial Peripheral Interface, or SPI, is used to interface a multitude of common peripherals, such as:
 * various sensors, including accelerometers and magnetometers, GPS signal receivers, real-time clock chips;
 * displays and data storage devices – LCDs, Flash memory chips, SD-cards;
 * signal transceivers, such as Ethernet transceiver chips.

In this task, we will consider the use of the ATmega8 internal SPI port to interface perhaps the simplest possible SPI device based on either of the following shift register chips:
 * 74HC595 (КР1564ИР52) – having an internal latch;
 * or 74HC164 (К1564ИР8) – lacking one.

Before you begin
For this task, we connect the following circuits and components to our simplistic device.
 * 2–8 LED circuits, each of which is comprised of the following two components connected in series:
 * the LED itself, which could be of any color and shape (as long as it’s possible to connect it to the circuit), for the voltage of around 1.5 V;
 * a current-limiting resistor for the resistance in the 470–1100 Ω range (assuming a 3–5 V power source), or – provided that no more than 4 or 5 LEDs are connected to the register – 360–820 Ω.
 * The 74HC595 (74HCT595, КР1564ИР52) chip connected as follows:
 * directly to the ground (with its pin 8) and V cc (16);
 * via 100 kΩ or so series resistors to V cc – with its and  (10, 13) pins;
 * to the LED circuits described above (15, 1–7);
 * to the, MOSI, SCk (16, 17, 19; or D 10, D 11, D 13 on Arduino Uno) ATmega8 pins – with StCP, DS, ShCP (12, 14, 11.)
 * Or the 74HC164 (74HCT164, К1564ИР8) chip, connected as follows:
 * directly to the ground (with its pin 7) and V cc (14);
 * via 100 kΩ or so series resistors to V cc – with its and  (1, 9) pins;
 * to the LED circuits described above (3–6, 10–13);
 * to the MOSI, SCk (17, 19; or D 11, D 13 on Arduino Uno) ATmega8 pins – with B, Clk (2, 8);
 * Please note that the ATmega8 (16) pin in this case remains unconnected.
 * The LED circuits, in turn, are connected to either ground or V cc, according to the chosen polarity (anode-to-register or cathode-to-register) of the LEDs.

In some cases, it may be convenient to use a resistor pack (preferrably with a common lead) of the resistance given above. Similarly, instead of the individual LEDs, it’s possible to use a LED row, or a single-digit seven-segment LED display.

Reading the code
We will read the code above starting with the payload parts of its main loop and referring to the initialization part and the preamble as necessary. We will not, however, consider the fragments already covered in the simplistic program and PWM example tasks.

  The  line copies the value to be sent to the device from the i variable to the Serial Peripheral Interface buffer (data) register. This also initiates the SPI data transmission. Immediately after that,  increments the value of i by one. Since this variable is declared as, the modulo 256 (2⁸) arithmetic is used, and the value “wraps around,” becoming 0 after 255.   The  line before SPDR is loaded sets the low level on the PB2 pin (or D 11 on Arduino Uno.) The  code is evaluated both before the main loop (as part of the initialization part of the code) and upon the completion of every SPI data transmission. It sets the PB2 pin to high. When the 74HCT595 chip is used, this signal serves as a strobe – on its raising edge, the data from this chip’s shift register is copied to its storage register (or latch), whose outputs drive (assuming that the input is low) the Q₀, …, Q₇ pins.   The  fragment configures the PB5, PB3, PB2 ATmega8 pins as outputs.  The first two of them are used by the internal SPI port for transmitting the SCk and MOSI interface signals, respectively. The PB2 pin, however, deserves a special attention: if left configured as an input, and driven high during the device’s operation, the interface will stop its current transmission, raise an interrupt (if enabled), and will switch to the slave mode, – until after the MSTR  flag in the  SPCR  register is explicitly set again. In order to avoid that, and also since 74HC595 requires an additional signal anyway, we configure this pin in our example as an output. This way, the MCU does not interpret its state in any special way.   The flags and bit fields of the SPCR register are set as follows:
 * SPIE
 * 1 – the events related to the SPI operation will result in interrupts;


 * SPE
 * 1 – the interface itself is enabled;


 * MSTR
 * 1 — the interface will operate in the master mode;


 * SPR
 * 11₂ – the interface will be clocked using the MCU clock divided by 256.

  The  interrupt handler does absolutely nothing. The only purpose of the interrupt itself is to end the wait for an event, as initiated with the  line.  <li> The <var id="CS1" >CS1 bit field of the <var id="TCCR1B" >TCCR1B register is set to 011₂ – the counter will be fed with the MCU clock divided by 64. Given this counter width (16 bit), the expected overflow condition (and thus the overflow interrupt) rate is 1.76–4.77 Hz for the MCU clock frequences of 7.3728–20 MHz. All the other bit fields of this register, as well as the <var id="TCCR1A" >TCCR1A register as a whole, are set to zero, which, in particular, disables the pulse-width modulation function of this timer/counter unit. </li> </ol>

Building
<ol> <li> Add the following dependency specifications to the  created earlier: </li> <li> Create the  file as shown above. </li> <li> Build the example with : make ledspi.hex <samp style="white-space: pre-wrap;" >avr-gcc -O2 -Wall -std=gnu11 -mmcu=atmega8 -DF_CPU=7372800  ledspi.c   -o ledspi avr-objcopy -O ihex ledspi ledspi.hex <dl> <dt>NB</dt> <dd> When using an MCU other than ATmega8, or the clock frequency other than 7.3728 MHz, the  and   build parameters are to be set accordingly. See the command line given in the simplistic program task for an example on how to build this code for an Arduino Uno board. </dd> </dl> </li> <li> Check for no errors in the output, and that the  file is created, containing the image to upload in the Intel hex format. </li> </ol>

Uploading and observing operation
<ol> <li> Upload the image we got to the MCU flash memory, as described in the simplistic program task. </li> <li> Check the device and code operation as follows: </li> </ol>
 * 1) connect the power;
 * 2) reset the MCU;
 * 3) observe the counting in binary from 0 to 11111111₂ (or in the reverse direction, should the LED circuits be connected cathode-to-register);
 * 4) estimate the time the binary number displayed gets increased by 2ⁿ (which is the half-period of the signal at the “ n th” shift register output) and compare it to the one computed from the MCU clock frequency per t  = 2²⁴⁺ⁿ ∕  ƒ MCU.

Research
<ol> <li>In this example, the update (increment or decrement) frequency for the number displayed is given by dividing the MCU clock by 64. Explore how the system works using the following other divisors: Should the device cease to operate as expected when using one of these, – try to explain what causes the issue. (Tip: calculate the expected time span between the successive main loop iterations.) </li> <li> Try to change the code so that the time is counted in seconds, half-seconds or minutes: </li> <li>Try to experimentally determine the maximum SPI clock frequency for which the data reliably reaches the shift register. Propose one or more limiting factors for this frequency. If possible, verify your hypothesis by: </li> <li> Change the code so that the system counts in the Gray code. </li> </ol>
 * 8 – ;
 * 256 – ;
 * 1024 –.
 * for one specific MCU clock frequency;
 * for any MCU clock frequency in the 7.3728–20 MHz range, as passed to the code via the  macro.
 * 1) assembling a variant of the device which is free (to a greater extent) of one or more of these factors;
 * 2) verifying that the reliable MCU-to-register data transmission rate indeed gets increased.