18. Arduino for Production!! How to Interface an LCD to the ARM Microcontroller Part 4

18. Arduino for Production!! How to Interface an LCD to the ARM Microcontroller Part 4

Let's get into the code and clean up a little a bit.

The first thing I'd like to do is, create definitions for these Bits, this will also help us understand how the circuit is connected to the microcontroller and the LCD, so which Pins and Ports to go to, which Data Pins and the enabled and read rights and register select.

Now let's set up the Defines up here, I want to create something very understandable in the way that we have the circuit wired, and that would be LCD D zero through D seven would be my names for the defines. So, let's call this one LCD D0, 10, which would be twelve and we can define the LCD D0 port, which will be GPIOB. We'll do this for each connection so we can just do a copy and paste.

Next Pin is number thirteen and it's also B, that's D- one, D two on Pin fourteen Port B, D three on Pin fifteen and this is Port B, and now for the Port C, D four and Pin number six, seven, eight and nine, for D five, D six and D seven, and the Pins that I'm referring to are these Pins 12, 13, 14, 15, 6, 7, 8 and 9, going to these LCD D0, D1, D2, D3, D4, D5, D6 and D7 so it really tells you how the circuit is put together, and if you need to make a change in the circuit, all you have to is change the Pin for the D0, which Pin it's going to go to and what Port it's going to go to and none of the program, will have to change, because it always will be set here and the program will use these Defines within the function.

So we're going to take this portion and put it into its own function and use those Defines in that function. Instead of making a Prototype, I'm going ahead and make the function up here, at this point I don't think it needs to have a return, so we are going to have a void, and I'll call it "Send Bits To Port and Pin" and I'll have some inputs to this function, and it's going to be, If we want to pass in the Ports we can get an idea of what the Data type is by hovering over one of the ports, you can see it's GPIO type "Def" and you'll see it's a pointer, I'm just going to do a copy and paste of this and then I'm just going to call it Port. This will be the actual variable name.

So, the next thing we're going to be bringing in, is the actual Pin number. This number is only going to be a pretty small number, I'm just going to use a hint for this and then we actually have to send the Bit, whether it's true or false, so just this, this would be a, ok I don't I can use bull in this language, using a Bit mixture unsigned and it will be I’ll call it "Bit State" for now, I don't know of a better variable yet, so just take this condition and we'll copy and do that. Alright, so we are going to replace the Port here so instead of using the Ports that we had hard coded. We're going to pass it in and then this, I think we're going to use the "Bit Reset Register." So, we're going to try, because we can see this number is at the fifth position, no fourth position zero, one, two, actually the third position and this one, is actually all the way over here, so we want to use the same Pin number in the same way so we can do is, we can actually just take out this S, we have a BRR and you can see it's at the same position, because this is the Bit Reset Register and this is the Bit Set and Reset Register and the Bit Reset Register it's only sixteen Bits, It only uses the first sixteen bits and Bit Reset uses all Thirty-two Bits, but the Bits sixteen through thirty-two are the Reset Bits. Ok, so now we have to change this to the Reset Register and we are going to actually, put the Pin number here, we're going to use the Y shift operation and remove this number, now by putting the Pin number let's say twelve, it's going to, it's going to put this Pin number into a very long into the thirty-two Bit number at the position, so if the pin number is let's say nine, it is going come over here and go zero it's going left shift it 0, 1, 2, 3, 4, 5, 6, 7, 8 and then nine, so it will actually put a one right here, where we want it, which we did for the setting and resetting, when we're lighting up the LEDs initially, and for the Bit reset, we're going do the same thing, we don't actually have to make any changes to this portion of it, because we want to reset at the same Pin number since we're using the Bit Reset Register and have the Bit Set Reset Register. The only thing we need to change now is the condition and this is going be the State. So, we have created a more tidy code for this bit of code here, so let's go ahead and invoke that. I'm just going take my Prototype function and I'm going to copy and paste it here and for this one, we're going to use Port B GPIOB and we're going use, Pin number is going be twelve and the Bit State will be this, let's see why doesn't this, oh I have the void here, ok, so this does the same thing as all of this and we have made so, actually I don't want twelve to be there, I want to use the Defines, so the Define would be, this would be for the Ports and Pin ok, that's what I really want D zero, Ten will be Twelve, you can see that it says here, and the Port will be the Port for, it's really difficult to read in this way but you'll see that zero eight and the(inaudible 08:02) position look it here it should show the same thing, the eight and the fourth, so it's the correct Port as Defined here, ok, we can go in and erase this portion of it, and we're going to just do copy and paste of all of these. We should have eight of them, this should be D-zero and D- one, two, two, three, three, four, four, five, five, six, six and the last one seven and seven, this is going to change as well, so, we'll have the mask going up in Bits, so we're testing for this Bit within the character, we're testing with this Bit with the character and so on down the line. We have just taken out a lot of code and made a much tidier and more readable. We can also tidy up this, we could put this in its own function and we'll do that as well, but I want to test this to make sure it works first. I'm going to build it, looks like we have no errors everything's ok. I'm going to flash it to the Mac Controller and as you see we have the same results, we have one with the five zeros, and then the one and then the zero. So, our program worked and we didn't have to actually put in the specific Ports or Pins in here, all we're doing is we're testing the character, we're testing the position of the character and we're sending it to the D-zero through D seven, Ports and Pins that's assigned to those pins, and just by looking at this we can see, how to wire the LCD using these Defines, this is where you have to wire the Ten, Twelve to the D-Zero, Ten Thirteen of Port B to the D one and so on and we're not specifying any of that here in the code. In the next video, we'll tackle putting this into a Single function, so we can put out different characters and we'll also tidy up this code because we want this to be able to follow the same convention, using these Defines as well.

[[TUTORIALMENU(tutorial-arm" frameborder="0" allowfullscreen>

Back to blog

Leave a comment