HC1. Since the SPI protocol uses full duplex synchronous serial data transfer method, it could transfer the data and at the same time receiving the slave data using its internal shift register. From the SPI master and slave interconnection diagram above you could see that the SPI peripheral use the shift register to transfer and receive the data, for example the master want to transfer 0b. E to the slave and at the same time the slave device also want to transfer the 0b. By activating the CS chip select pin on the slave device, now the slave is ready to receive the data. On the first clock cycle both master and slave shift register will shift their registers content one bit to the left the SPI slave will receive the first bit from the master on its LSB register while at he same time the SPI master will receive its first data from slave on its LSB register. Continuously using the same principal for each bit, the complete data transfer between master and slave will be done in 8 clock cycle. By using the highest possible clock allowed such as the Microchip MCP2. S1. 7 SPI slave IO device 1. MHz than the complete data transfer between the microcontroller and this SPI IO port could be achieve in 0. As you understand how the SPI principal works, now its time to implement it with the Atmel AVR ATMega. The following is the list of hardware and software used in this project 7. HC5. 95, 8 bit shift registers with output latch. Microchip MCP2. 3S1. SPI IO Expander. AVRJazz Mega. AVR ATmega. 16. 8 microcontroller board schema. Win. AVR for the GNU s C compiler. Atmel AVR Studio 4 for the coding and debugging environment. STK5. 00 programmer from AVR Studio 4, using the AVRJazz Mega. STK5. 00 v. 2. 0 bootloader facility. C 20171127 2214. Expanding Output Port with 7. HC5. 95 8 bit Shift Registers. Because the basic operation of SPI peripheral is a shift register, then we could simply use the 8 bit shift register with output latch to expand the output port. The 1. 6 pins 7. 4HC5. The 7. 4HC5. 95 device has 8 bit serial in, parallel out shift register that feeds directly to the 8 bit D type storage register. The 8 bit serial in shift register has its own input clock pin named SCK, while the D Latch 8 bit registers use pin named RCK for transferring latching the 8 bit shift registers output to D Latch output registers. In normal operation according to the truth table above the 7. HC5. 95 shift registers clear pin SCLR should be put on logical high and the 8 bit D Latch buffer output enable pin G should be put on logical low. By feeding the serial input pin SER with AVR ATMega. MOSI and connecting the master synchronous clock SCK to the 7. HC5. 95 shift registers clock SCK, we could simply use the 7. HC5. 95 as the SPI slave device. Optionally we could connect the 7. HC5. 95 Q H output pin shift registers MSB bit to the master in slave out pin MISO this optional connection will simply returns the previous value of the shift registers to the SPI master register. Now let s take a look to the C code for sending simple chaser LED display to the 7. HC5. 95 output Description SPI IO Using 7. HC5. 95 8 bit shift registers Target AVRJazz Mega. Board Compiler AVR GCC 4. Win. AVR 2. 00. 80. IDE Atmel AVR Studio 4. Programmer AVRJazz Mega. STK5. 00 v. 2. 0 Bootloader AVR Visual Studio 4. STK5. 00 programmerunsigned char SPIWrite. Read unsigned char dataout Wait for transmission complete Latch the Output using rising pulse to the RCK Pindelayus 1 Hold pulse for 1 micro second Return Serial In Value MISO Initial the AVR ATMega. SPI Peripheral Set MOSI and SCK as output, others as input Enable SPI, Master, set clock rate fck2 maximum. AVR Serial Peripheral Interface. The principal operation of the SPI is simple but rather then to create our own bit bang algorithm to send the data, the build in SPI peripheral inside the Atmel AVR ATMega. SPI programming become easier as we just passing our data to the SPI data register SPDR and let the AVR ATMega. SPI peripheral do the job to send and read the data from the SPI slave device. To initialize the SPI peripheral inside the ATMega. SPI master and set the master clock frequency using the SPI control register SPCR and SPI status register SPST, for more information please refer to the AVR ATMega. How To Install Vnc Server In Aix La. The first thing before we use the SPI peripheral is to set the SPI port for SPI master operation MOSI PB3 and SCK PB5 as output port and MISO PB4 is the input port, while the SS can be any port for SPI master operation but on this tutorial we will use the PB2 to select the SPI slave device. The following C code is used to set these SPI ports. After initializing the ports now we have to enable the SPI by setting the SPE SPI enable bit to logical 1 and selecting the SPI master operation by setting the MSTR bit to logical 1 in the SPCR register. For all other bits we just use its default value logical 0 such as the data order DORD bit for first transferring MSB, using the rising clock for the master clock on clock polarity CPOL bit and sampled the data on leading edge clock phase CPHA bit. Because the 7. 4HC5. Mhz clock rate, then I use the fastest clock that can be generated by the ATMega. SPI peripheral which is fsc2 the AVRJazz Mega.