Electronic Basics #36: SPI and how to use it

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In a previous video I showed you how to utilize the I2C protocol, which in a nutshell is a popular communication protocol. That is commonly used to let one master device talk to a bigger number of slave devices in order to tell them what to do. Sounds like an awesome concept that every ic should have integrated, right? Well, I recently stumbled upon the DS3234 IC which is a real-time clock that automatically keeps track of the seconds, minutes, hours, days, date, month and year information and even offers tool as programmable alarms with square wave signal outputs Needless to say I want to use it in a project But what I'm missing are the SCL and SDA pins, which are necessary for the I2C communication protocol instead it offers a CLK, MISO MOSI and SS pin which not surprisingly since the datasheet spoiled the surprise are part of the SPI communication protocol so this video Let's find out how we can use SPI with the Arduino in order to write and read data from my precious RTC IC Let's get started This video is sponsored by JLC PCB their partner site is Easy EDA gets regular updates and now even features a 3d view of your design that Only requires one click to turn it into a PCB order whose prices start as low as $2 First off how do we need to connect the Arduino to the IC? Well, according to the block diagram of the Arduino microcontroller It features the SPI functionality at the pins PB for 3 5 & 2 Which after checking depend out of the microcontroller? correlates with the digital pins, 13 12 11 and 10 But it is mentionable that we are not restricted to pin 10 as the Slave select pin In fact, we could use any pin of the microcontroller for that so according to my simplified wiring diagram I connected the Arduino to the ds3234 IC with simple hookup wires and Once that was done it was time for the programming Since the breakout board I'm using is from spot fun I quickly found a bit of code from them on github which We can go through line by line in order to understand how SPI works at first the SPI library of the Arduino is included and Then the CS or chip select pin is assigned This pin basically starts the SPI communication when it is pulled low And hence the communication when it is pulled high. And yes You would need one chip select line for every SPI IC you want to control with a master device, which is kind of the bottleneck of this communication protocol But anyway next we call the function RTC_init there we declared the CS pin as an outputs and Starts the SPI protocol by firstly setting its bit order to MSB first To understand that statements we have to look at the data sheet of the DS3234 IC Which tells us that we can either read or write from or two addresses with 8 bits of data those 8 bits represent either the seconds minutes and hours Or even the basic settings for the IC like whether an alarm should be set What is more important though is whether we should firstly send the most significant bits So it's seven first or the least significant bits So bit zero first and the data sheets clearly defines MSB first Which we just declared in the sketch Next the SPI data mode is set to SPI mode one Which we could blindly accept because the datasheet States it's on its first page But what does this SPI mode actually mean? Well, for that we have to look at the remaining SPI pins besides chip select CLK stands for clock which like the name implies is a square wave clock signal that the microcontroller Generates when it is sending or receiving data MOSI stands for master out slave in which gets utilized when the Arduino sends over data to the IC And MISO is the opposites and stands for master in slave out Which gets used when the slave I see here sends over data to the master Arduino You already noticed that the data I sent over consists of a square wave signal as well which basically represents a 1 bits if the voltage is higher and the 0 bits if the voltage is low But here comes the details and also the modes into play in Mode 0 and 1 you got a clock signal which is normally low and only high when the SPI communication occurs In mode 0 though the data on the MOSI and MISO lines gets read at the rising edge of the clock signal And in mode 1 the data gets read on the falling edge of the clock signal. in mode 2 and 3 the clock voltage potential is the opposite meaning it is normally higher and Only lower when the SPI communication takes place in mode 2 the data then get read on the falling edge and in mode 3 on the rising edge Memorizing all those modes can be a bit tricky But honestly not necessary because the datasheet will most of the time tell you the required modes So let's move on with sketch Next the chip select pin gets put low which means we are about to send or receive data Then the hexadecimal number 8E gets sent over from master to slave. But why? Well by once again having a look at the data sheets We can find a bit address map for the DS3234 registers Which I already talked about earlier with the read and write addresses and the contents of the registers So the data we just sent over was actually an address that we can write you the addresses register is the special purpose register where we can for example Enable the oscillator D square wave outputs or the alarm interrupts Now this sketch sends over the hexadecimal number 60 which converted into binary stands for B R 0 1 1 0 0 0 0 0 That means we activate the square wave outputs and the temperature conversion But I want to check whether the SPI communication truly works So I added two more ones to the RS 2 and RS 1 bits in order to set the square wave outputs to 8.192 KHz as you can see here. I did not convert this binary value into hexadecimal because it works either way and after ending the SPI communication by pulling the chip select pin high it was time to upload the codes and have a look at the square wave output pin on the oscilloscope Which does infects output the precise 8.192 KHz signal Which means our SPI communication was successful The next part of the codes was to set time date function, which looks complicated at first But is easy to grasp if you understand bit math what they do here is simply converting the entered values for day, month, year, hour, minute and seconds into binary value suitable for the registers then through a four loop the function writes to the addresses 80 81 82 84 85 and 86 in order to set the current time and date The write function works the same way as I described it before. But what follows is more interesting with the read time/date function Here we got once again a whole lots of rather complicated bit math It is once again used to convert the contents of the registers but this time converting it into a format that we as humans can easily understand. What is interesting though is how we read data from the IC For that we firstly sent over the address we want to read from Then we have to save the now incoming SPI data in an integer while also sending out data. In this case, the data is 00 but it can be anything like 99. That does not matter. As soon as we get the receive data in the integer, we can then work with it. Like they do it here with bit math. And just like that, you are now familiar with the basics of SPI and how we can use it with the Arduino Of course you cannot use it for as many slave devices as the I2C protocol Due to the limited number of chip select lines But on the other hand the SPI protocol in this case reach the transmission speeds of 4 MHz while most of my I2C projects only utilize 100 KHz So if you need fast speeds, like for example to write to an SD card, then SPI is the way to go. I hope you enjoyed this video and learned something new along the way. As always don't forget to like, share, and subscribe Stay creative and I will see you next time
Info
Channel: GreatScott!
Views: 257,153
Rating: 4.9345455 out of 5
Keywords: SPI, spi, i2c, I2C, compare, comparison, tutorial, guide, beginner, beginners, arduino, nano, µC, microcontroller, explain, bit, bits, CLK, clock, MISO, master, in, out, MOSI, CS, select, chip, wiring, wire, programming, code, sketch, example, program, sparkfun, RTC, IC, real, time, DS3234, library, practical, read, write, greatscott, greatscott!, electronics, basics
Id: fvOAbDMzoks
Channel Id: undefined
Length: 10min 31sec (631 seconds)
Published: Sun Sep 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.