Exploring Crucial Concepts in ARM Cortex M4

Lect 4: IO Ports and Branches- ARM Cortex M4 Microcontroller [Embedded Systems]

Estimated read time: 1:20

    Summary

    In this engaging lecture by Learning Orbis, the focus is on understanding the nuances of input/output ports and branches in ARM Cortex M4 Microcontroller, crucial for embedded systems. The lecturer begins by discussing precautions regarding the microcontroller's current limitations, emphasizing the need for buffers when connecting peripherals like LEDs. Various interfacing techniques for LEDs and switches using positive and negative logic are thoroughly explained. The lecture then transitions into the complexities of branch instructions, illustrating their use in conditional operations and loops. The discussion culminates with an introduction to the SysTick timer module and its configuration, highlighting its role in managing timed operations within an embedded system. By the end, attendees gain deep insights into utilizing these features for efficient microcontroller programming.

      Highlights

      • Beware of the current limitations in microcontrollers when connecting peripherals like LEDs! ⚡️
      • Use buffers to safely sink high current away from microcontrollers! 🛡️
      • Master LED and switch interfacing with both positive and negative logic! 💡
      • Branch instructions are key in managing conditional operations and loops! 🔄
      • The SysTick timer module is a powerhouse for managing timed operations in embedded systems! ⏱

      Key Takeaways

      • Beware of current limits in microcontrollers when connecting peripherals like LEDs and use buffers to protect your setup! ⚡️
      • Understanding of LED and switch interfacing using both positive and negative logic gives more control over your circuits! 💡
      • The intricacies of branch instructions help you code smarter with conditional operations and loops! 🔄
      • Mastering the SysTick timer aids in precise timing operations, essential for efficient microcontroller programming! ⏱
      • Embedded system programming becomes easier and more efficient with a grasp on these foundational concepts! 🚀

      Overview

      In this detailed lecture, an immersive exploration of input/output ports and branches within the ARM Cortex M4 Microcontroller environment is undertaken. As part of this journey, attendees receive essential training on the importance of managing current limitations when interfacing peripherals. This foundational knowledge is vital for preventing damage and ensuring optimal performance through the use of buffers like ULN 2003.

        LED and switch interfacing are demystified with comprehensive examples demonstrating both positive and negative logic. Such control techniques are pivotal for efficient circuitry management in complex embedded systems. Alongside, the nuances of branch instructions are uncovered, equipping programmers with the skills to implement efficient conditional operations and loops—cornerstones of advanced microcontroller programming.

          The lecture further delves into the SysTick timer module, showcasing its setup and operation to manage precise timing tasks within the ARM Cortex M4 Microcontroller. By understanding this timer's role and configuration, participants can effectively incorporate timed operations crucial for developing robust and responsive embedded systems. These takeaways provide a robust framework for bolstering one's embedded programming expertise.

            Chapters

            • 00:00 - 02:30: Introduction to Lecture 4 and IO Ports Lecture 4 introduces the topic of embedded systems with a focus on input/output ports. It aims to build on previous discussions related to I/O ports, describing their functions and uses.
            • 02:30 - 06:30: Microcontroller and Peripherals Current Precautions The chapter discusses important precautions related to the current output and input limitations of microcontrollers. It emphasizes that peripherals, such as LEDs and switches, have specific current requirements which must not be surpassed by the microcontroller's outputs. The chapter also notes the importance of not exceeding the current entering the microcontroller, a process referred to as 'sinking the current,' although the exact values for these currents are not specified.
            • 06:30 - 10:30: Direct Connection and Buffering for LEDs This chapter discusses the importance of consulting data sheets when connecting peripherals to a microcontroller, particularly focusing on LEDs. It highlights the need to be cautious about the current limits of a microcontroller to prevent damage or failure. It advises care in interfacing LEDs, ensuring the combined current does not exceed the device's limit, which in this case is suggested as 12 units.
            • 10:30 - 17:30: Switch Interfacing and Internal Resistors Switch Interfacing and Internal Resistors']
            • 17:30 - 23:00: ARM Assembly Branch Instructions: Basics The chapter introduces the basics of ARM Assembly Branch Instructions, focusing on the interaction between microcontrollers and LEDs. It discusses the direct connection of an LED to port pins, considering an example where the LED requires only 2 milliamps, allowing for a direct connection without additional components. This scenario assumes that the LED is the sole peripheral connected to the system.
            • 23:00 - 30:00: Conditional Branch Instructions The chapter explains the significance of using resistance when connecting an LED directly to a port pin. It covers how the resistance limits the current and ensures the LED turns on with adequate brightness. Detailed calculations may be required to determine the right resistance values if the LED needs specific amperage.
            • 30:00 - 45:00: Implementing Conditional Branches This chapter focuses on implementing conditional branches in microcontroller programming, specifically discussing the direct connection of an LED to a microcontroller. The only component between the port pin and the LED is a resistor, facilitating a direct connection aimed at driving the LED using positive logic. This means the LED will turn on when the specific positive condition is met, emphasizing the simplicity and efficiency of direct connections in conditional branching tasks.
            • 45:00 - 49:00: Implementing While Loops in Assembly This chapter covers the implementation of while loops in assembly language specifically for controlling electronic circuits. It discusses modifying circuit setups, such as adjusting the LED's connection in a circuit involving a microcontroller, to achieve desired outcomes based on different output states. The example provided involves connecting the LED and resistance to the microcontroller port pin to work with 3.3V positive supply voltage.
            • 49:00 - 57:00: For Loop Implementation in Assembly The chapter titled 'For Loop Implementation in Assembly' discusses the basic setup for a for loop in assembly language. It begins by explaining the orientation of components within a circuit, particularly focusing on the LED connection. The cathode of the LED must be connected to a low voltage while the anode is connected to a higher voltage, specifically 3.3 volts in this example. To ensure the circuit functions properly and safely, appropriate resistance must be included. This involves practical application and parallels drawn between hardware circuitry and program loops to demonstrate how control flow in programming can be likened to directing electrical currents. The summary may also touch upon various essential details and considerations when setting up a loop structure in assembly, but these aspects aren't fully covered in the provided transcript.
            • 57:00 - 70:00: Introduction to SysTick Timer Module In this chapter, the discussion centers around connecting LEDs to a microcontroller using the SysTick Timer Module. It emphasizes that while directly connecting LEDs to a microcontroller is common, caution is advised if the LED requires a higher current, like 10 milliamps. Direct connections under such conditions might not be ideal and could require alternative approaches to prevent damage.
            • 70:00 - 92:00: Configuring SysTick Timer in Assembly This chapter explains how to configure a SysTick Timer using assembly language. It discusses the use of power and logic controls, specifically in the context of controlling LEDs. It highlights the use of buffers such as 7405, 7406, or ULN 2003B, which control LED operations by setting the microcontroller output to high voltage to trigger a low voltage output from the buffer, thus allowing current to flow through the LED. The explanation emphasizes the direction of current and the role of buffers in ensuring correct LED functionality.
            • 92:00 - 105:00: Monitoring and Clearing Timer Flags This chapter discusses the function of microcontrollers in setting voltage levels at the input of buffers. It explains that the microcontrollers do not handle high currents; instead, all the current is managed by the buffers. The microcontroller's sole responsibility is adjusting the voltage level, while the buffer handles the actual current flow, whether in positive or negative logic scenarios.
            • 105:00 - 132:00: Delay Example using SysTick Timer The chapter discusses using a 7407 microcontroller to manage current and control an LED. It mentions the risk of sinking a large amount of current and how the 7407, when used with negative logic, can be beneficial for turning on an LED when the output pin is low. The 7407 outputs low voltage when the input is low and outputs high voltage when the input is high.
            • 132:00 - 160:00: Advanced Delay Example and Routine Call The chapter discusses advanced delay examples and routine calls in electronic circuits. It starts with the concept of high impedance state, where the output is considered as an open circuit due to high resistance, thus no current flows through the LED.
            • 160:00 - 166:30: Conclusion and References The final chapter concludes the discussion on various topics covered in the text, summarizing key takeaways. It touches on technical aspects of interfacing microcontrollers with switches, as well as the importance of understanding output states like high impedance in practical applications. Lastly, the chapter likely includes references that provide additional resources or foundational works that support the material presented throughout the text.

            Lect 4: IO Ports and Branches- ARM Cortex M4 Microcontroller [Embedded Systems] Transcription

            • 00:00 - 00:30 welcome to lecture four of embedded systems in which we shall continue our discussion on the input output ports and once done we shall move on to describe the branches and the branches instruction in this cortex m4 assembly language and we shall conclude our lecture with the introductory discussion on the system tick time in continuation of our discussion regarding the led interfaces
            • 00:30 - 01:00 there is an important precaution which is that your microcontroller can only output limited amount of current the peripherals such as leds switches and others and there's also some limitation on the amount of current entering the microcontroller we call it uh sinking the current at this point i don't know the exact value
            • 01:00 - 01:30 but it might be helpful to consult the relevant data sheet so if there is an upper limit on the amount of current entering or leaving the microcontroller then we must be careful in interfacing the peripherals for instance if we are connecting too many leds or led few leds needing a current uh that accumulates higher than 12
            • 01:30 - 02:00 milliamps and if those leds are connected directly to the microcontroller that may result in damaging the microcontroller so they must not be directly connected to the port pins instead we shall insert buffers that can sink that higher amount of current such as uln 2003 7405 or 7406 or 7407
            • 02:00 - 02:30 uh between the microcontroller and the led when we wish to interface the peripherals and let's take a look uh with the help of an example uh on the left hand side we have an led connected to the port pin that requires just 2 milliamps in that case we can directly connect that led to the port pins imagine that this is the only led that we are going to connect to the system um therefore we
            • 02:30 - 03:00 connect this led directly to the port pin by means of a resistance here and the resistance is going to obviously limit the current and also make sure that the led turns on with adequate brightness so if the requirement is too many amps for an led to lit up the led then um we can do some simple calculation to find out the values of these resistances
            • 03:00 - 03:30 uh in this case we are saying that the led is directly connected the microcontroller uh the only component that is between the port pin and the led is the resistance so there is a direct connection so direct connection between the led and the microcontroller that is when we want to drive the led in the positive logic meaning that the led must be turned on
            • 03:30 - 04:00 when the output is high and if we want to uh turn the led on when the output is low then we wish to we may wish to modify our circuit slightly so in this case the positive supply voltage 3.3 volts is connected to the resistance and which is in which in turn is connected to the led whose other terminal is connected to the microcontroller port pin so that when the output goes
            • 04:00 - 04:30 low um the current uh the cathode of the led is connected to the low voltage and the anode is connected to the high voltage 3.3 volts over here and then the current can flow through the led uh but we must be careful and insert an appropriate resistance resistance as we did here in case of positive logic nevertheless
            • 04:30 - 05:00 in both cases the led was directly connected to the microcontroller that's what how we say it this is very different from a situation when the led demands current which is higher so imagine that the current is close to 10 milliamps such as 10 milliamps so what we might be careful and not to connect led directly to the microcontroller
            • 05:00 - 05:30 instead we are going to use power so in case of uh positive logic control of the led we may want to use buffers such as 7405 or 7406 or uln 2003b um and the way it works is that when the microcontroller outputs high voltage it sends it sets the output of the buffer to the low voltage so that the current can flow through the led but the the current will not enter the
            • 05:30 - 06:00 microcontroller instead the crunch goes to the ground through the buffer so the only job of the microcontroller is to set the voltage level at the input of the buffer that's it there is no high current that is flowing to the microcontroller in fact the buffer is carrying all the current uh likewise in case of positive negative logic sorry if
            • 06:00 - 06:30 there is a danger of um sinking a higher amount of current a large larger large amount of current then again we can use 7407 microcontroller and because we are working with negative logic so we want led to turn on when the output of the pin is low so 7407 outputs low voltage when the input is low and if the input is high by the way the
            • 06:30 - 07:00 output is high impedance state that is this connection there is this output is at high impedance meaning that there's a very high resistance essentially this is an open circuit situation and therefore there is no current that can flow through the led so that's in case of high voltage but if the input is low then the output is low and then you can see that the current enters led and it after and then
            • 07:00 - 07:30 it sinks to the ground through the buffer uh remember the output of the 7405 and sun 406 is at high impedance state c when the input is low next switch interfacing we describe how to interface switches to the microcontroller and we looked at examples where the
            • 07:30 - 08:00 switches were connected through external resistances to the microcontroller so there were external resistances in our last lecture and again here the two examples the positive logic and the negative logic switch interface uh we see that we have external resistance 10 kilo ohms here and also 10 kilo ohms over here the only difference between this circuit and the one below here is that the upper circuit works with positive logic then the lower
            • 08:00 - 08:30 circuit works with the negative logic but in both cases we have external resistance to limit the amount of current entering the microcontroller so again if we take some nodes we can say that to limit the current of micro controller there is however another way to limit
            • 08:30 - 09:00 the current without using uh external resistances we can activate internal resistances internal resistance okay the two examples on the right hand side of the table describe how to activate the internal resistances um in in the first case a positive logic switch interface the switch is
            • 09:00 - 09:30 directly connected uh to the port pin such as pin number four of port a there is a switch that can connect pin number four port a to three point three volt or it can disconnect uh it to or it can produce a disconnection between this uh the positive supply voltage and the pin number four port a but whenever there
            • 09:30 - 10:00 is whenever the switch goes on turns on um then there's a danger of excessive current entering market controller because we haven't attached any external resistance so note that there is no external resistance here however there is an internal resistance and there that is set by setting certain bits of the configure port configuration registers that i'm
            • 10:00 - 10:30 going to describe shortly but once the internal resistance is activated then we are safe uh we have essentially limit the current of the microcontroller and these are designed to work with three point three volts i believe or maybe up to 5 volts but not higher than that in case of negative logic again we are going to activate the internal resistance by setting the bits of certain registers port
            • 10:30 - 11:00 configuration register so in this case the 3.3 volts will be uh supplying the current that the current flowing through the 3.3 volt power supply will be limited by this internal resistance once the switch is turned on and the switch is essentially short-circuiting the port pin to the crown so this resistance is going to limit the current but the difference between
            • 11:00 - 11:30 the two circuits on the right hand side is that in the first case uh the internal resistance is the pull down resistance because it is uh it is pulling the pin the pin here port pin to zero volts when when the switch sorry about that when the switch is open so when the switch is open
            • 11:30 - 12:00 uh such as shown here then to make sure that the voltage at the pin is zero volt it is grounded uh we have the pull down resistance that's why the resistance in this case is pull down pd pull down resistance in the other case however if the switch is open then by means of this resistance the pin pa6 the pin number six of port a will be
            • 12:00 - 12:30 short-circuited to the supply voltage because there is no current that is flowing through the pull pull up resistance so call this resistance pull up resistance and one of the things to note um is that if so so you either activate pdn or pun if one is activate one is activated then the two cannot be simultaneously activated so either
            • 12:30 - 13:00 pull down resistance or pull up resistance but not both because in that case we might be running the risk of uh enabling large amount of current to the microcontroller and also but more importantly the logic level at the pin might be ambiguous all right so how can we
            • 13:00 - 13:30 enable the pull down or pull up resistance we have a port configuration register gpio port x pdn underscore r that's the pull down enable we can just say it's a pull down enable register we are interested in only the lower eight bits of this register and the one after that
            • 13:30 - 14:00 that is the pull up enable register pull up enable register only lower eight bits are required to be set to reset and we are going to set basically meaning that for example uh in the first case if we are interested in enabling the pull down resistance corresponding to the pin number four on
            • 14:00 - 14:30 port a over here pin number 4 port a if we are interested in activating the pull down resistance then we are going to set the fourth bit of the pull down enable register in the other example we are interested in enabling the pull-up resistance on pin 6 of port a for that just set bit 6 of the pull up resistance
            • 14:30 - 15:00 pull up enable register of port a activating pull up and pull down resistances provide a safe way to interface particularly switches next whenever we are interfacing switches mechanical switches with the microcontroller we should always remember that mechanical switches may oscillate between on and off state due to their inertia friction
            • 15:00 - 15:30 mass spring and other mechanical properties in that case it is better if we wait for some time typically ranging up to 10 milliseconds before reading the data from the port with the switches interface and there are several ways to introduce delays uh we saw that in the last lecture and we shall see that again with the
            • 15:30 - 16:00 help with a new uh module that we haven't yet discussed that is the timer but in the meantime please look at figure 4.24 of textbook number one in which there's a floor chart that implements a pseudo code to read the status of the switch the logic level at the switch and it
            • 16:00 - 16:30 does that by introducing 10 millisecond delays it's not a complicated program very simple and hopefully you can follow that easily we're now going to move on to describe the syntax of the conditional branch instruction for that it is important to recall the role of suffix we appended after the arithmetic logical or shift or rotate instructions
            • 16:30 - 17:00 to help you recall i give you an examples the example of the an instruction with a suffix s and we said in lecture two that when the suffix s is there it means that in addition to performing the normal bitwise and operation we're also going to the the microcontroller is also going to update the code bits in this case
            • 17:00 - 17:30 it is going to update only two chord bits the n and z the negative result uh chord bit and the zero result code bit described in the last lecture another example is that of logical shift right with suffix s performing logical shift write operation but also updating the chord bits three of them in this case nz and c because the carry the bit that is shifted out goes into the carry bit
            • 17:30 - 18:00 and finally the add s instruction performs the addition on the registers and also updates the codebears in this case all four chord bits may be changed after the execution of this instruction so why is that discussion important because based upon the value of the code bits we
            • 18:00 - 18:30 are going to dictate the course of the execution of our program to describe that and to be able to understand in more detail let us look at some examples the check of an equal of equality when we wish to perform the equality check we may be interested we we can just say that check the status of the zero flag because the previous instruction which may have compared the two numbers
            • 18:30 - 19:00 and if those two numbers were equal then it that instruction must have set zero flag to one uh i'm using the word flag and the chord bits interchangeably so we can also say the chord bit or the flags these are just uh synonymous chord bit of the flag they're just synonymous so anyway so z chord way to the z flag set equals to one that is a signature of the equality
            • 19:00 - 19:30 check not equal unequal z frag equals to zero similarly uh what about the com so you know the perspective of our discussion is that we are sort of comparing two numbers just imagine for the sake of understanding as well so comparing two numbers uh they are going to be stored somewhere in the microcontroller
            • 19:30 - 20:00 but such as one of them may be in register the other one is just some operand two uh which can be a register which can be a constant uh it can be a constant as well or a register anyway so we're comparing two numbers we don't care where they are for the moment we we can just think of them the the contents of the registers as two numbers then uh for example uh in case of noting that if one number is higher
            • 20:00 - 20:30 or it is higher than or equal to the other number when the two numbers are unsigned then the carry bit should be equal to one um carry bits is equal to zero when uh one number is lower than the other number that's unsigned comparison so again the emphasis is on the comparison negative beta set equal to one uh when
            • 20:30 - 21:00 the number that we have number one number compared to the other is less than that's negative uh if n is zero then the the comparison answer where i was either positive or zero overflow v equals to one uh nor flow v equals to zero and the list goes on and one of the things to note is that uh sometimes it is important for the signed comparison
            • 21:00 - 21:30 that's where we use less than uh or greater than or less than or equal to or greater than or equal to so these four instructions are used whenever the sign comparison is required so what we gain from this discussion is that whenever we are going to compare two numbers
            • 21:30 - 22:00 and to be able to make decisions based upon the answer the same depending upon the answers of the comparison then we are always going to look at some of these chord bits so the flags and remember again we are we are going to work with the four chord bits the overflow zero
            • 22:00 - 22:30 negative and carry now let us look at some of the examples let us try to parse a simple conditional branch instruction example the branch of equal in this case the instruction should be read as follows beq branch if equal eq is actually a suffix and we saw in the last slide that eq suffix corresponds to
            • 22:30 - 23:00 the zero flag and it checks if zero flag is equal to one the other part of the instruction is the branch b for branch which in simple english means go to go to the instruction whose label is some instruction label so what we're really seeing over here is that check if 0 flag equals to 1
            • 23:00 - 23:30 if so then go to the instruction whose label is some instruction label the conditional branch instruction often proceeds the compare instruction the syntax is cmp followed by the registers or constants in this case we are going to compare r naught with r1
            • 23:30 - 24:00 the compare instruction only performs a subtraction operation except that the result of fraction is not stored in either register or the operands however what it does is that it only updates the code bits set or reset so it is only going to update the code bits
            • 24:00 - 24:30 that's what it does so compare instruction only updates the code bits and those code bits are used in the following conditional branch instruction an example we saw in the last slide beq it is going to check the z chord bit or the c flat and jumps to some instruction
            • 24:30 - 25:00 whose label is some instruction label if z flag is equal to one we have different types of branch instructions and the syntax is given the first one is branch if equal and then the target it says that if the previous comparison if the two numbers the two numbers
            • 25:00 - 25:30 were equal in comparison and where do we compare we are going to comp there is going to be some instruction over here so there will be an instruction like cmp uh let's say two registers are not r1 and then we say beq target so this cmp instruction syntax will be
            • 25:30 - 26:00 described later so this is going to [Music] effect the chord bits n z c and v so based upon their values the beq instruction will be executed in this case we are testing the equality signed or unsigned so basically we we're looking for the z flag and the target what is this target
            • 26:00 - 26:30 target is actually a label of some instruction so we are really instructing our microcontroller to jump branch means jump so we say that jump to so if r naught is equal to r1 so i think it is already given over here so we compare r naught with r1 for example and if
            • 26:30 - 27:00 r naught is equals to r1 then we are going to go to the target forget about this for us let's say this is b e q so if r naught is less than r one go to the target otherwise just proceed to just proceed to if r naught equals to r one and if r naught is not equal to r one just proceed to the next instruction
            • 27:00 - 27:30 and this is how the flow works the example actually given in the slide is that of the blo the branch if unsigned less than and you can consult the previous slide to see what flags what chord bits it is going to check anyways so if r nord is less than r1 in sign sense then we are going to jump to the instruction whose label is the target
            • 27:30 - 28:00 if not then just simply proceed to the next instruction in the sequence and likewise we have branch if uh less than or equal to in unsigned sense branch if unsigned greater than or equal to in the sign sense and the branch if unsigned higher than similarly we have conditional branches conditional branch instruction when the
            • 28:00 - 28:30 comparison is performed in the sign sense so branch less than uh branch greater than or equal to uh and branch greater than and branch less than or equal to and again these instruction the brand instruction can follow when you can follow the sub instruction setting the
            • 28:30 - 29:00 code bits or the compare instruction an example over here is that if you perform a comparison between resistance resist register r naught and r one and if the sign sense r naught is less than r1 go to the target instruction otherwise just proceed to the next instruction and it will be further clarified with the help of examples later some simple examples highlighting the role of the brand instruction the first one being uh the equality test
            • 29:00 - 29:30 code so there is a number of the name variable name g stored somewhere in the memory we're going to load its address in r2 as we've seen several times before and then we are going to invoke the index addressing to store the number the actual number into r not and then we are going to compare r naught with seven that is the same as asking the question is g
            • 29:30 - 30:00 equals to seven if they're not equal then it says that go to the instruction whose label is next one right now here we are we haven't shown what is the instruction corresponding to the label next one but assuming there will be some instruction and if they are equal now if g is actually equals to seven if this condition is true then it means that g is equal to seven they
            • 30:00 - 30:30 are not g and seven are not unequal so we are going to skip this instruction and we are going to come down to this instruction and this instruction is bl this is remember this is unconditional brand so this is unconditional unconditional branch so it it is not going to look at the values of the code it doesn't care about the code bit it doesn't care
            • 30:30 - 31:00 about code bits it is not going to care about the code bits that is this instruction it will simply jump to some instruction with label g equals seven and that might correspond to a routine that may be utilizing this result to execute some some course section uh for for the given
            • 31:00 - 31:30 application all right in the next example we have the inequality test that is uh when we are interested in comparing g naught equals to 7. so we say that again we are going to load the value of g into r node and then we are going to compare r naught with seven we are asking if g is not equal to seven so in this case we are saying if g is equal to seven then
            • 31:30 - 32:00 uh we are just going to jump to next two and if g is not equal to seven then this instruction will not be executed and we arrive at this instruction again this is an unconditional branch now let us look at some examples of unsigned conditional jumps comparing g greater than seven and again
            • 32:00 - 32:30 g is some number stored in the memory which has been referenced and the value of who whose value is now in r north so r naught compared with seven if g is less than 7 go to next one the instruction at next one if not then we are going to skip we are going to skip we are not going to go to next one we come here
            • 32:30 - 33:00 and similarly if we want to compare g e greater than or equal to 7 then b l o that is if g is less than or equal to g is less than seven so really over here we are checking if g is less than seven then go to next two otherwise if g is equal to seven then go to an instruction whose label is
            • 33:00 - 33:30 g greater than or equal to seven j less than seven a common technique that you may have learned by now is that we check the complementary condition so if we are if we want our program to jump to g less than or equal to 7 to the label g less than 7 then the ins the conditional branch instruction after the comparison is the complementary uh condition to what we are looking for
            • 33:30 - 34:00 so we say that if b if g is higher than or equal to seven go to next three if g is less than 7 come over here and finally if we're checking if g is less than or equal to 7 then the complementary condition is that if check if g is greater than 7 again all of these comparison are performed in the science sense um given the conditional brand instruction that
            • 34:00 - 34:30 we are using uh if g is less than or equal to uh fd less than or equal to seven uh in that case uh we come over here and execute branch and link g less than or equal to seven and in case of sign conditional branches again uh we are going to replace the unsigned comp uh branch instruction with the
            • 34:30 - 35:00 signed brand instructions so b l e b lt bge and bgt and everything else remains the same so far the way we have used the conditional band instruction was analogous to the if statement in the high-level language such as python or c now we look at a scenario where we are interested in implementing a structure which is similar to the if-else statement for example
            • 35:00 - 35:30 we wish to compare the two numbers if g1 is greater than g2 uh then execute this block that is is greater there is some code that we don't care about and then if g1 is less than or equal to so this so you can consider the condition on the right hand side as the if condition and the condition on the left hand side is as the else condition so in that case we're going to execute a different block to some extent the same flavor was present in the in the previous situation
            • 35:30 - 36:00 but it is much more explicit over here all right so the two numbers g1 and g2 are in the memory they are loaded uh into the registers r naught and r one and then we are going to compare r naught with r one and if g one is greater than g two then we are going to so this instruction the brand instruction is going to take us to the instruction whose label is
            • 36:00 - 36:30 high that's where the block of instruction is placed starting so the is greater is um is the block obviously it is a label it is a label of a set is a label of instruction is the label of maybe i should say label
            • 36:30 - 37:00 of the first instruction is the level of first instruction of the code of of the chord block is greater and likewise is less than or equal to is the label of the instruction of the code block of the function instruction of the code block
            • 37:00 - 37:30 is less than or equal and we don't care what is inside the chord block shown by the squares over here but the only purpose purpose was to make sure that we understand how the if else structure is implemented in the assembly language among best uses of the conditional band instruction lies the implementation of the while loops the for loops we're going to first look at the while loop implementation
            • 37:30 - 38:00 whose pseudo code is shown here essentially we are going to condition the execution of the while loop based upon the comparison of the two numbers g2 and g1 they are stored in the memory for we can only compare the two numbers uh when they are brought into the registers of the microprocessor
            • 38:00 - 38:30 therefore we are going to load g1 and g2 into registers r naught and r1 in a way that r naught carries g2 and r1 carries g1 and then we compare the compare instruction r naught comma r1 and the remainder of the program execution is dependent on the result of this
            • 38:30 - 39:00 comparison back to the sk the pseudo code of the while loop if g2 is greater than g1 then we are going to enter the body of the loop that is the body of the loop and once the execution of the body is complete we go back to check again if the while loop condition is true or false each time the while loop condition as long as
            • 39:00 - 39:30 the condition of the while loop remains true that is the true condition true condition of the while loop as long as g2 remains greater than g1 we keep on executing the body and return to check the condition again if however the condition is false the false condition we exit the while loop and we proceed to execute the part of the code that
            • 39:30 - 40:00 is to be executed after exiting the while loop in this case we are going to execute next the the the the program was first instruction the section of the program whose first instruction has a label next so we perform a comparison over here between r naught and r one and then we check b l s the conditional brand instruction b l s that is to say if
            • 40:00 - 40:30 r naught is less than or equal to r one what is r naught containing r naught contains g two and r one contains g one therefore we are actually comparing g one with g two by comparing r naught with r one and if r naught is less than r one
            • 40:30 - 41:00 then the false condition will assert the program to jump to the next instruction the instruction whose label is next that is it comes over here if however this condition is not true that is r naught is greater than r one which means that g naught is greater than g one
            • 41:00 - 41:30 then we enter the body of the loop with the branch and link instruction now this is important because bl ensures that once the body of loop is executed is complete completely executed is finished
            • 41:30 - 42:00 execution return need to return return to the next instruction which is here below we return and once this instruction is executed that is an unconditional branch simple b is remember unconditional
            • 42:00 - 42:30 branch it is unconditional branch it says that jump to the instruction whose label is loop so we go back to the beginning of the loop and we are going to reload the value of these variables u2 and g1 into the registers r naught and r1 respectively and then repeat the whole process and this is how the while loop has been implemented
            • 42:30 - 43:00 likewise we can also implement four loops with the help of conditional branch instruction two types of for loops can be used the ascending for loop and descending for loop in the ascending for loop we have a loop variable as used in the high level language such as i initialized with 0 and we increment i and
            • 43:00 - 43:30 in each iteration we execute the body of the for loop that implements some function such as process and the loop will run 100 iterations that is when i exceeds 99 and becomes equal to 100 and this condition becomes false there are four components of the for loop here the initialization the conditional check the increment and
            • 43:30 - 44:00 execution of the loop body that is process the descending forward loop works the same way except that the variable is decremented and the condition often checks if i is higher than zero therefore the two for loops are essentially equivalent and now we are going to look at the corresponding assembly code to implement the for loop using
            • 44:00 - 44:30 the conditional branch instruction for example if we are interested in implementing the ascending for loop we initialize a variable such as i with zero in that case because the value is known this constant we can directly store this we can store this value directly into the register r4 so essentially that i variable corresponds to register
            • 44:30 - 45:00 r4 and then we are going to compare r4 with 100 and see if it is higher than or equal to if r4 is so this let me write it over here so we're really checking if r4 is uh greater than or equal to 100. if that is the case uh then this will force if this condition is true then we jump to the done and then proceed there
            • 45:00 - 45:30 from we are not going to execute the process of the blue body that's not executed now if however this condition is false that is r4 is not higher than 100 that is r4 is less than 100 then we go to the next instruction and execute the
            • 45:30 - 46:00 subroutine or routine or subroutine routine let's this is called this routine we don't need to use subroutine here it's a routine process we could jump to an instruction uh that is going to be the first instruction of the routine and the label of the instruction is process and we are going to after executing the process we are going to return that's why this bl is used for we are going to return after executing the process wherever that process is written once
            • 46:00 - 46:30 the instruction has been executed and the last instruction is probably going to be xlr that is going to bring us to the instruction next to the bl process and that is going to increment the loop variable this comment really says that we are incrementing the loop variable i equals to i plus 1 it's just that i is in the register it has been placed directly into the register and once incremented we go back
            • 46:30 - 47:00 to the loop once again this simple b is unconditional branch important to understand the role the the the synergetic integration of conditional and unconditional branches so this is again unconditional branch b this is how you implement account up for loop on the other hand if we are interested
            • 47:00 - 47:30 in implementing the descending for loop we are going to initialize the loop variable i which is in r4 so initialize i with hundred and then we are going to execute the body of the loop first so it says that go and execute the body of the loop which is actually the process
            • 47:30 - 48:00 and then return to the next instruction that is going to decrement the loop variable that is i equals to i minus 1 and then the check condition b and e remember we are not so we haven't used any compared instruction over here we haven't and what is the reason well because we already have this sub instruction that is decrementing and this sub instruction also so remember
            • 48:00 - 48:30 sub s also effect the chord bits just as the c compare instruction affects the chord so far all of our example have been utilizing the compare instruction but the same effect can be achieved with the sub instruction and that is usually preferred if sub is inevitable uh is inevitably appearing in
            • 48:30 - 49:00 our assembly code so we don't need separate uh com compare instruction we can just utilize the result of sub instruction so sub instruction is going to decrement the register content values r4 equals to r4 minus one in fact r4 assigned r4-1 and then b and e branch if not equal so it is going to check 0 flag meaning that as long as r4
            • 49:00 - 49:30 is not equal to zero that is it is higher than zero so as long as r4 is greater than zero meaning i greater than zero keep on executing this instruction it is going to uh sorry it is going to go back to this loop and once r4 becomes 0 i equals to 0 then we are done and then we proceed to the instruction whose label is done and from there onwards this is an
            • 49:30 - 50:00 example of a countdown or descending loop moving on to the systick timer module it is a module that is very similar to the counter that you designed in your digital logic design course timer works the same way cystic timer is a 24 bit down counter so it decrements its value at the bus clock frequency
            • 50:00 - 50:30 and if we're using 80 megahertz plus clock using the internal pll whose discussion will come at some point later in this course then the time period is 12.5 nanosecond which means that uh the clock rising edge occurs after every 12.5 nanoseconds and that's when the timer decrements its value the value of the timer the initial value
            • 50:30 - 51:00 this n will be some non-zero value which will gradually recreate to zero initially we are going to load or reload the timer with some value n and it is going to decrement n to n minus one in the next clock cycle then n minus 2 then n minus 3 and so on
            • 51:00 - 51:30 up to 0. this is how the timer works and at any point you can read the value of the timer when you load the value n into the timer it is essentially a modulo n plus one counter meaning that it is going to count uh the content the contents the current value will be modulo
            • 51:30 - 52:00 n that is it will be less than or equal to n so the current value will be less than uh n uh maybe i should say so the current the the value of the timer so the at any point timer value
            • 52:00 - 52:30 is uh less than n plus one so for example if we set n equals to 16 the value uh n equals to 15 then the value of the timer at any stage is less than 16. now in order to be able to use the timer
            • 52:30 - 53:00 module similar to the case of i o ports configuration registers we have the timer configuration register and vic systick timer which are memory mapped meaning that they are stored in the memory whose addresses are given over here three registers needs configuration the control register envic control register the nvik reload
            • 53:00 - 53:30 register and nv current register there are four initialization steps so in order to use timer it needs to be initialized and by initialization we mean we are going to set the values of we are going to update the value of these registers the first step step number one is to clear the enable bit over here this bit
            • 53:30 - 54:00 we want to clear meaning we want to set this bit equals to zero to stop the counter because the timer in order to be able to properly configure or reconfigure the timer it must be first stop the counter must be first come must be brought to halt and stop that's why we want to set enable base to 0 because otherwise
            • 54:00 - 54:30 because there are multiple instructions that are required to be executed in the configuration process and if we leave the timer uh ticking then it might not be properly configured so it better stop it first set the enable bit equals to zero the next step is to set the value of the reload and reload register it is essentially because it's a 24-bit timer
            • 54:30 - 55:00 so we're going to use the lower 24 bits specify the reload value 24 bit long reload value so and so if the reload value is uh let's say n some value n then the timer is going to count down so then so the moment it starts running the next lock cycle it is going to decrement to n minus 1 n minus 2 and so on so the reload value has been updated in step number two
            • 55:00 - 55:30 after that by the way the timer is still in the configuration phase and uh it is we haven't allowed it to start ticking yet therefore we proceed to the remaining remainder of the configuration step number three the current uh clear the clear counter by reading the nvec current register now we are talking about the third register over here all we have to do
            • 55:30 - 56:00 is to reset the current value meaning that we are going to set the current value of the timer we are going to reset the current value of the timers equals to zero that means clearing just as we clear or set bits so clear stands for setting the current
            • 56:00 - 56:30 value of the time is close to zero and we really want to replace this zero with the reload value over here anyways the last step uh we're going to further configure the timer uh by setting or clearing bits in the anvik control register so back to the first register because in step number one we only set the enable bit equals we cleared the
            • 56:30 - 57:00 naval bit however clock uh source there's a bit clock source we can all we can run timer at the speed at the clock speed which is either the external clock source external clock which might be in the form of external oscillator uh that occurs when um
            • 57:00 - 57:30 this is equal to zero for external to enough to to connect um a timer clock to the external oscillator set the speed equals to zero or you can set equals to one meaning that the timer will now run so if if clock source is equal to one it means that uh timer is going to run is going to
            • 57:30 - 58:00 run at run at internal clock so there is an external clock and there is an internal clock you get to decide which clock is used to run the timer and that is essentially the speed at which the bus is running so that's also known as the bus clock or the internal clock
            • 58:00 - 58:30 uh and by the way i should mention here that you can adjust the frequency of the internal clock by using the phase lock loop and therefore you can control the frequency at which the internal clock is running or the bus is running interrupt enable bit what about that the interrupt enable bit the interrupt enable is used if we want to enable the interrupt on the timer that is
            • 58:30 - 59:00 essentially if we want to use interrupt because we haven't yet discussed in traps so we are going to clear this bit uh we do not wish to associate any interrupt signaling with the timer if we wish to enable interrupt that would have been useful to uh interrupt the microcontroller
            • 59:00 - 59:30 whenever the timer stops running enable bit is equal to 1 meaning that we are really asking we are now setting our timer to run so as soon as you set this number bit equals to one then it is now allowed to run and it is now it can now
            • 59:30 - 60:00 start decrementing so this is how we set we configure the timer by setting or clearing the individual bits or we can just load the value of the enve control register as appropriate and also specify the reload value and the specif and then clearing the current value of the timer register configuring the systick timer requires
            • 60:00 - 60:30 updating the contents of the three registers described in the previous slide the four steps are implemented in the assembly language as shown here and the first part we're going to load the address of the n with control register into r1 and then we are going to store 0 into register r not and then store the value 0 which is in
            • 60:30 - 61:00 register r note back to the address location containing the anvik control register that will automatically clear the enable bit by setting all of his contents to zero the enable bit is also cleared and soon after that over here we are going to set the selective bit equal to one but after clearing the anvik control
            • 61:00 - 61:30 register uh enable bit and in fact the all the bits have been cleared we are going to reload set the reload value for that read in the address where the nvic reload register resides in the memory put it in an r1 store specify the reload value its highest the maximum 24 the highest 24 bit number
            • 61:30 - 62:00 hex double zero ffff we store this value into our node and then our node contents are written back to the address location where the nvec reload register is placed so we specify the reload value next we wish to set the current value of the timer equals to zero we put zero into r naught and
            • 62:00 - 62:30 we reference the nvec current register by storing its address in r1 and then stores the values equals to 0 which is an r node to the nvec current register this will clear the counter so the counter has been reset the timer has been reset the current value is zero and whenever it starts running it will start running with the reload value
            • 62:30 - 63:00 and finally we refer back to the end with control register in which we disabled we cleared the enable bit now it is time to set the enable bit to one so enable bit is set equals to one that's the least significant bit you remember we want no interrupt and the clock source is also one so interrupt uh is this interrupt so
            • 63:00 - 63:30 clock source one enable one and in turn interrupt enable or en is equal to zero and all other bits are zero so that corresponds to so this setting that enable is one clock source is one and in turn is zero this corresponds to the value uh five in the least significant eight bits the upper
            • 63:30 - 64:00 24 bits uh are all zeros so once we have put the value hex triple zero five into r naught we store it back to r1 we could have individually set to reset those bits by using the bit addressing covered in the last lecture if not then this is the default way to do it and then once the timer has been configured uh assuming that
            • 64:00 - 64:30 this is a routine which configures the timer that initializes the timer and then whosoever the call this routine we give the control back to the caller bx lr is the final instruction of this routine by convention in order to utilize the timer for our application we need to regularly monitor its countdown status bit number 16 of the envic control register
            • 64:30 - 65:00 configuring the timer therefore is quite important once the timer has counted all the way from its reload value to zero it sets the con flag that's when this count bit is set equals to one and the timer keeps running the next it begins its next iteration starting from its reload value that's
            • 65:00 - 65:30 specified by this register contents and the next cycle of the countdown begins the timer doesn't stop whenever the first count on iteration is complete the count flag is set to one and it stays one unless we explicitly have it clear what are the ways to clear the count flag one of the ways
            • 65:30 - 66:00 is that we write some data to the current register the anvik current register which is holding the current value of the timer so remember nvik current register holds the current value current count or current current count or current value of the timer
            • 66:00 - 66:30 writing anything to this register clears the register so that the end the current register is cleared but it also clears the count flag meaning that the count is cleared the other way is that we read the anvik control register whenever the anvik control register is read it returns the value the current value of the count
            • 66:30 - 67:00 and once that is read off once it is read it clears the count flag therefore we can say that reading the anvik control register or writing the environ current register both both will clear the count flag let's take an example of introducing a delay using the timer we're assuming that the
            • 67:00 - 67:30 timer is running at a clock whose frequency is 80 megahertz which corresponds to the time period 12.5 nanosecond that's that is a timer decrements every 12.5 nanosecond now for example if we wish our delay to be let's say of 125 nanosecond the question is what should be the value of the envic reload register so remember we said earlier that
            • 67:30 - 68:00 if the value of the reload and reload register is n the timer count timer is going to count down all the way from n to zero it is actually counting modulo n right so we want our timer uh we want our timer to
            • 68:00 - 68:30 produce how many ticks 10 ticks or 10 counts 10 countdowns we want our timer to perform 10 countdowns that's and each countdown takes 12.5 nanoseconds so that will amount to the total delay of 12.5 125 nanoseconds and after that and then the count should be set equals
            • 68:30 - 69:00 to one so we want our timer to count down 10 ticks before count is set equals to 1. so the value that we are going to put here is equal to 10. so the way to do it is that uh we um so our if the reload value
            • 69:00 - 69:30 is uh 10 our timer is going to count from 10 all the way to zero now that is not 10 that is actually 11 right so this value really should be 9 and obviously this is not so consistent because you think that well when a 9 multiplied by 1 12.5 is not going to give you 12 125. so what we do is that the we put the value 10 into register
            • 69:30 - 70:00 r node instruction before and then we decrement the value and then store that value uh using index addressing to the nvec reload register that's the ultimate objective if you like this weight is fine if you want to have the value value 9 stored in some other manner it's also again fine but the value should be
            • 70:00 - 70:30 correct then we're going to read the current and the current register that essentially is going to uh so we are going to write to the convict current register that's part of the initiation process and remember any value written to the current and the current register clears it so this register so we are really saying that the timer
            • 70:30 - 71:00 uh so we're really saying that this register is set equals to zero even if the value in r node is not equal to zero still is going to clear and also clear the count and finally we are going to store the address of the nvic control register and we are going to keep referring to this address once we enter this part of the code so once stored uh this address will be continuously referred
            • 71:00 - 71:30 as i should describe below uh and now we are set up nicely to run our timer and this cord assumes that the other part of the initialization is complete that is the routine that we saw before cystic init has been executed
            • 71:30 - 72:00 has already been executed and in case of introducing 125 nanosecond delay our notes contain value equals to 10. that so these are the things that are assumed uh before we talk about executing the code described here on this slide all right so this is the um the course segment
            • 72:00 - 72:30 in which we are going to iterate several times what it does is that it reads the status of the timer and where's the status of the timer it is here i'm using a different color now envic control register and within that register we have quite a few bits but what one we are interested in is bit number 16 the count flag that's where we that's we in which we are entrusted
            • 72:30 - 73:00 and we are going to compare the value of r naught with this hex zero zero triple zero one zero zero zero zero essentially we are taking a bitwise and operation and we are also allowing the flags to be affected by this and operation as a consequence if r naught is all zero this is going to set the zero flag
            • 73:00 - 73:30 and if the zero flag is set equals to one uh which essentially means that the count is zero meaning that the sixteen bit the count flag as long as it is zero uh our note contains zero and the zero flag is equal to one the next instruction it looks at the zero flag branch if equal if that is set and this is true as long as the zero
            • 73:30 - 74:00 flag is one which is when count is equal to zero go back to the instruction whose label is cystic weight loop again read the status again compare using uh well it's not comparison it is a bitwise and but it works the same way and so we keep on monitoring the status of the count flag as long as
            • 74:00 - 74:30 the count flag is equal to zero we keep looping we keep looping essentially my controller is just you know doing nothing it is just uh i trading between these three instructions that's not doing any productive task so the moment the timer has counted all the way uh from nine to zero that is when the
            • 74:30 - 75:00 ten number of ticks are complete when the ten number of ticks are complete the count flag is set equals to one and then this instruction will not be executed and we exit we exit so this will not be executed and we come down over here and exit the routine so this occurs when z flag is equal to zero count is equal to one and r naught
            • 75:00 - 75:30 r naught value is hex zero zero zero one zero zero zero zero that's when we are going to exit this routine and the delay has been completed note that we have just developed a routine cystic underscore weight that introduces a delay or not times 12.5 nanoseconds and in that process the register r node has inevitably become an input to this
            • 75:30 - 76:00 routine now let us look at a more complex example in which our delay composed of two numbers in fact it is a product of two numbers one coming from the register r naught and the other is a is a constant 10. uh the product of two number uh times the millisecond that is the total delay we are interested in introducing and this example also assumes 80 megahertz clock
            • 76:00 - 76:30 now our main objective of implementing this routine is to utilize the one that we have already written that was the cystic weight routine and remember the cystic weight routine introduced a delay it introduce a delay corresponding to the counts are not
            • 76:30 - 77:00 number of clock cycles or texts or takes are not clock cycles a delay corresponding to are not clock cycles takes and in terms of uh time it was r naught times uh 12 point sorry it was not 12 it was 12.5 nanoseconds so that was the delay so this in the the cystic underscore weight routine introduce a
            • 77:00 - 77:30 delay r naught times 12.5 nanoseconds so the routine assumes that the input is in r naught that is the number of clock cycles that for which we have to wait and each clock cycle is obviously 12.5 so nothing has changed from the last program except just that our delay now composed of two numbers a product of two numbers so the important thing to note is that because we are going to invoke this routine we first need to
            • 77:30 - 78:00 store its value uh somewhere in the register r4 so value of register r naught will be stored in a register r4 so now we systematically try to understand the program noting that the pre-factor although is contained in our node but because our node is also going to be used later in when we call the routine cystic underscore weight that we developed before therefore it is
            • 78:00 - 78:30 important to first save the value of r not in some other register r4 so the pre-factor is technically in r4 now if we want this whole program to be called by some other program like we do very often in case of modular programming then we also don't want our r4 value to be changed
            • 78:30 - 79:00 therefore it will be much safer if we first push the value of r4 onto the stack and because we are going to call a subroutine systek underscore bits and that is going to modify the value of the link register so because of this subroutine so this routine imagine this is the routine this is a routine and this is a subroutine that is a routine that is
            • 79:00 - 79:30 called within a routine so we are going to store the value of link register that's what we do that that's what we have seen in the previous lectures when we talked about the addressing modes in the stack all right so once the value of r4 has been saved on top of the stack now we are going to store the value of r naught into r4 so that's this guy r naught is stored in r4 and we're going to check if we are saying that if r4 is equal to 0
            • 79:30 - 80:00 meaning r naught equals to 0 then we are done do not introduce any delay pop the value of r4 and pop the value of link register into the pc and return to the caller return to the caller so go back from where we actually invoke this routine that's what we mean by return to the caller so whosoever called this routine returned to the instruction after that
            • 80:00 - 80:30 if that is not the case if that is not the case this beq is false that is r4 is not equal to 0 then go to the next instruction okay so the next instruction label systick wait 10 milliseconds loop and you're expected to loop in this part of the court quite a lot and let us try to understand how this
            • 80:30 - 81:00 happens we're going to load the address of delay 10 millisecond constant that is stored as part of the code that is 800 000. so this 800 000 is used this corresponds to 10 milliseconds so if you allow timer to count from 800 000 all the way to 0 then it corresponds to the 10 milliseconds delay so put the value set the value of r
            • 81:00 - 81:30 naught equals to eight hundred thousand that is actually eight hundred thousand and invoke the routine that is going to introduce a delay ten milliseconds that we've seen before and then once you once you return from that routine subtract the value of r4 so remember r4 corresponds to this prefactor that is actually r north so remember this prefecture this pre-factor our north is going to be
            • 81:30 - 82:00 contained in r4 all the time it's going to be containing r4 and we decrement and as long as r4 is greater than zero uh that's the conditional branch branch higher than branch higher than so as long as r4 is greater than zero and
            • 82:00 - 82:30 uh the flags for this branch were set by this sub instruction the sub instruction the sub instruction is going to subtract 1 from r4 and see if the answer is greater than 0. if that is so we are going to loop we are going to loop we are going to go back to this instruction we are going to load the delay amount wait for another 10 milliseconds and once return decrement r4 so this will continue
            • 82:30 - 83:00 again and again and again unless the time when we get r4 equals to 0 that's when we exit this loop and proceed to the end of the routine that is return to the caller with that i would like to conclude a lengthy lecture i hope you enjoyed listening to this lecture and you may have
            • 83:00 - 83:30 uh some questions in your mind for that we are going to have our regular q a session on thursday afternoon but before i sign off i would like to give slice credit to jonathan volvano these slides have been taken and modified and tailored to the needs of our mechatronics embedded system course thank you very much and have a nice day