Analog on #RPi Source Files Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in a previous video I showed you how to add analog capability to Raspberry Pi using the 85 592 snack board that I'm showing you right here and in that video we use a helper file that I created to help interface with that board with the Raspberry Pi and in that video I explained how there's a there's really only six functions you really need to know how to use to use that helper file and with the 85 592 snack board but in this video we're going to go over the details of each how each one of those six functions works and that way you can customize it to your needs so let's go ahead and take a look at each one of these functions first let me explain them from a real high level we have the the eighty five five nine to underscore init function that's going to initialize the serial port interface for us and then we're going to have we have the set which channel were connected tubes is basically this set $85.99 to CH and that's to identify which cs line or chip select line or connected to on the spy and then we have the the for functions to get or set are our inputs or outputs we have a I get analog in function and that gets that basically will set the pin to an analog input and get the value and then we have the analog out function and we have the digital in function and a digital out function and well let's go ahead and dig into each one of these in detail okay here is the eighty five five nine to init function and like I said this is basically just initializing the serial interface and to do that we're actually using the DCM 2835 library and I have a previous video on that you can look at my spy on PI video or installing libraries video and they'll show you how to use that library specifically for server interface but basically it's going to initialize that library then it's going to begin the serial interface and then we're going to set our bit order and for the 85 pound 92 it's going to want the most significant bit first and so that's what this does here and then we set the the the polarity forward a serial interface data and then the then we set our clock speed and in this case where we're using a clock divider of 16 which gives us a clock speed of just a little little over 15 megahertz there so that's basically all that function does okay this set 85-pound 9 2 CH function is basically we couldn't set the channel it's going to tell tell the Raspberry Pi which CS line the 8592 is connected to and so it's going to take an integer n and basically it's either a 0 or 1 actually looking disappered probably put something in here to handle a case where it's not a 0 or 1 but just know that you need to pass either a 0 or a 1 to that function and then depending on whether you enter 0 1 it'll select the appropriate channel for the chip select line okay before we go too much further I need to talk about some of the pre-compile of definitions that I've created here in this helper file and these are right out of the datasheet for the 85 592 basically each one of these is is basically sets a bit mask to that correlates to the commands in the datasheet for the device and then we can just use the names of those to then pass to the stroke or interface to to control the device so if you have any questions about any of these you can just refer to the data sheet and so if we gives refer to page 24 of the datasheet we get this right here actually is a page 26 sorry it's actually a newer version of the data sheet that was originally looking at but you get the idea here this this you bit 15 through bit zero here you have a zero as a first bit there most going to fit a bit if it's a control register or if it's a if it's a digital out it'll be a one there so most going to a bit at zero for all these then you have those different different things that this thing does and you can you can read down through these in the datasheet at your leisure but that's what these are all doing down here or basically just have a couple definitions for the pins themselves and again this is just putting a bit in the right place so for example it let me show you this real quick here if you didn't know this up on the windows you can go to your calculator and go to program err and then so will enter in the hex for let's just say 0 X 0 for 0 so for 0 in hex you can see that 1 in the binary is the BC that would be our pin 6 so pen stick io 6 has a 1 in pin 6 so if we if we went back and I'll say n or 2 for example 2 0 and it's bit 5 so that's what those are doing it's just we can apply that as a bit mask to select different pins then we have this pin select it's just basically selecting all 8 pins ok I want to explain a few other things here we have a couple buffers here they are chart type buffers basically the so that's at an 8 bit buffer and there's two two elements each in each of those arrays and that's that those are going to be the words that we're going to be sending to your interface then we just have a few variables here that are defined globally we have their middle volts millivolt results and that's just used to convert back and forth between the digital and analog values and then we have four variables here that essentially are going to operate as a bit mask to keep track of which pins are assigned to which functions so as as you use this program its we're going to change this value direct to place a 1 anywhere that for each pin that is a defined as digital out or digital in analog out or any analog in and so each time you call those I get analog out or get analog in or set out analog out it will look look at these values see which pit if the pin that you identified is already performing that function if it's not it'll set it to that function ok let's take a look at the get analog in function so this is going to like I just said it's cursed thing you're going it's going to do is it's going to take in this pin variable that's going in this in this if statement it's looking to see if that pin number is already configured as an analog input so it's going to basically shift the analog in pins variable to the right the number of a number that you pass to it which would be the pin number so it is pin 0 it's not going to shift 0 and so that would be bit 0 then it's looking at and then we're using this bitwise operator the and operator to see if that's a 1 and if it's not a 1 up 1 so that would mean that that pin is not set to an analog and then it would call this set has a DC function and as then we pass to it all of the pins that are identified are already configured as analog inputs plus it then uses the bitwise or operator and again that variable you pass to set that new pin that you define you called as an analog yet ok and and so it depends are already an analog in this it bypasses this and then it just simply calls the analog ATC are they 85 I'm 92 ADC read and this is one of those precompiled definitions are created with a bitwise or operator and then it just reads the pin the value of on the pin that you've asked for and when you call the when you go to read the the analog input you have to then send two more words that are no operation and that gives the device enough time to do the conversion and then send the data back and so you actually have to pass in the no operation definition there and then then you can go get the result and the result will be stored in the spy in buffer and it'll be the again the I guess the it since there's two bytes there you're looking for a 12 bit value you need to do some bit shifting here and that's what that does there and I think I've have a low-level see look at the bitwise operators if you don't not understanding how these work I have a video on that alright so what I haven't showed you yet on Ness is the spy comms and the set aid as a DC so if we go to set as a DC so remember when we we passed that variable to this we basically set a bit mask that it defines all the pins that should be analog pins and so then we were set resetting that global variable there two-bit master represents all the pins that are configured as analog inputs and then we call us make word function which is going to take the it's going to take in reference to the spy out buffer and then it's going to use this previous definition here it's the ADC pin select and remember that's just a pre compiler definition that sets the bits to to the values that need to be to configure the device for that function and that's one of the things that I showed you a little bit earlier and then we use the bitwise or operator to define the pins so essentially this is going to be up here this is going to be the the upper byte or the the control bits and then the pin the pins are the masks and so this is a 16-bit value right here okay let me show you the make word so make word is simply it's translating between the 16-bit word that the 8592 wants and the 8-bit weird that the serial interfaces raspberry-pi uses so it's taken in that two byte buffer and it's then going to split up that 16 bit it's going to split up that 16-bit word for the 85 592 into those two bytes that's all it's doing and then then it's something that's happy to work with the BCM 2835 library okay next let's look at the spy comms function essentially all that's doing it's taking the command Ian it's just then calling that make word function we just looked at and then it's going to clear the spying buffer that's just so we don't get any stray bits in there to mess up our data and then we're going to call the BCM 2835 spy transfer and b function and that's going to take in both of those buffers and it's going to be the size of that spy out buffer it's only giving me two bytes here but that's all it's going to do it's just kind of an abstraction for those those three things okay now I went over some of some of that the rest of these should go pretty quick the set analog out again it's going to check to see if then those pins are already configured and again you can look at my bitwise operators video to understand what's going on here with the bit shifts and the ands and the ORS and then it's going to go ahead and and send that value to the to the pin that you identified okay now this video is titled a D analog capability Raspberry Pi but this device also allows you to expand at the general purpose i/o of the Raspberry Pi to get more digital inputs and outputs and so the get digital in function essentially again it it looks to see if the pin that you identified has is already configured as digital in and if not then it if that's it and then it reads that value and then it returns that value to to tell you whether that pin what that pillock pin is set at and likewise the set digital out does the same thing it it looks to see if the pins you identified are are already configured as digital outs and if not it sets them as digital outs and then it it writes those those values now what's a a little bit different about this one as we can we can pass the values to multiple pins all at one time and where this will come in handy is like if we're going to control say a stepper motor and we're gonna say we're controlling the gates on a different MOSFETs which we'll do maybe in a future video we can reset the the gates on eight-eight MOSFETs simultaneously with this one right and so we can control a stepper motor in both directions using this and pretty smoothly so that's about it for this video I hope you liked it please subscribe like if you liked it and we'll talk again in real soon in the future some of the things we have coming up as I'm going to do a video on Raspberry Pi data logger using this the 85 592 snack board and so the library or the source files I just showed you and are also going to use this device with the kinetise software development kit 2.0 library and we're going to use it on the freedom k64 board and we're going to create a data logger for that well so check back real soon and thanks a lot
Info
Channel: SpazzTech
Views: 821
Rating: undefined out of 5
Keywords: Tutorial, analog, RPi, Raspberry Pi, ad5592, bcm2935, library, spi, spazztech
Id: XuLudbVELhc
Channel Id: undefined
Length: 15min 34sec (934 seconds)
Published: Mon Dec 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.