PIC16 Microcontrollers, Unit 5, Ch 2.5-2.7, Pipelining & Reset

hello this is client Halstead and this is introduction to microprocessors video series and we’re using designing embedded systems with pic microcontrollers principles and applications second edition with Tim Wilshire’s we’re currently we’re going to cover two point five to two point seven in the book this section will cover pipelining external reset and internal reset circuitry okay so a microcontroller is a sequential state machine so that means that it it follows an order a sequential process from one step to the next and that sequential process if you study sequential processes you know that every sequential process has to have something to set the sequence in order so the clock is that thing that sets the sequence or the heartbeat so the clock is used to set the instructions that get executed one after another so every time a clock goes from low to high that that edge creates a pulse or a heartbeat for the next instruction to be executed now there are several things we need to know about how that process works number one is when a microprocessor is extra executing code it the faster the clock is running or or the faster those instructions are being executed the more power is consumed so in today’s devices handheld devices where power is of utmost concern we want to make sure that we don’t have a frequency of that heartbeat be too fast we only want it to be the frequency that’s needed for the application so if it’s a wristwatch you know maybe the frequency could be slow if it’s a iPad or something like that then the frequency would be need to be high in order to meet all the requirements that for speed sometimes the devices will cycle their clock either faster or slower depending on the speed requirement that’s what happens in a computer many times is when you’re using the computer that a clock goes really to the maximum and then when it’s kind of an Iowa State or you just viewing sometimes a screen at may the clock may go down to the lower frequency but in this case we’re going to on most of the things we do we’re just gonna have a fixed clock pregnancy board where we’re going to have to set that clock frequency some way and we’ll talk about how to do that a little bit later but the power consumption is proportional to the voltage VC the VCC squared times the frequency of the clock so that’s the equation for the power so it is directly proportional to the frequency now the actual instruction cycle or machine cycle is going to be actually one-fourth of the main oscillator frequency so if you set a frequency of four megahertz for example well the instruction cycle is only going to be one megahertz okay so for a 20 megahertz crystal or clock speed it’s going to only be five so this don’t forget to 1/4 rule there okay next we’re going to start talking about the instruction cycle and the pipelining here’s a block diagram that shows the instruction register so how how do instructions get implemented in a microprocessor well you have to write code and either see your assembly that’s going to go up here so your assembly your code is going to go in this block okay that’s going to be downloaded through your pic hit 3 you’re going to connect your pic hit 3 to the microprocessor you’re going to have a USB cable it’s going to go to your computer so because this can be your computer your PC and you’re going to have a USB cable okay and then that you’re going to write the code on your PC in the dot ASM format which is assembly or a dot C format and that’s going to get downloaded through your USB cable through the pic kit.3 okay he’s going to go into your flash memory inside of the microprocessor now when you when you turn on the chip and apply power to the chip what’s going to happen is it’s going to start executing code from from the zero location so the very first spot is going to be zero that’s what the

reset function does is it sets the instruction or the program counter I’m sorry the program counter gets set to zero now when the very first clock comes in it’s going to take whatever is the very first instruction and it’s going to populate that this is called a fetch okay so we’re going to fetch whatever memory location is pointed to by the program counter in this case would be 0 and that value the first line of code may be a 0 1 for example tags or something or it could be anything remember the address is at a 0 but the actual contents of that code may be something different so that’s going to get populated in the instruction register and that’s called the fetch cycle now the next cycle is going to be the execute cycle after and there they’re separated by that happens in two clocks so it takes two clock cycles to do that so the first cycle is going to be fetch so we’ll just call that an f4 fetch so the first cycle you’re going to fetch the instruction and then the next cycle you’re going to execute so you have a fetch and then execute so it takes two of those instruction cycles to do that so if your main oscillator frequency is its members going to be four times is great so here this is an example of your main oscillator maybe four megahertz but then this one would be one megahertz because it can be divided by four and then it takes as far as an instruction goes then it’s going to take two cycles execute that but something called pipelining allows us to fetch okay and then execute the previously fetched instruction so if this is fetched zero then this would be execute one this could be fetch next and then you’re going to have fetch one and then execute zero so that’s called the pipeline we’ll have another slide to show that so what’s actually happening is you fetch during website one clock cycle the first clock cycle you fetch the code from the flash and put an instruction register and the next clock cycle you actually execute that now exit by execute we need those values actually gets populated to your addressing it gets populated your ALU they propagate through because it takes time so those signals take time to propagate through okay and then once it gets execute on the next clock then the W register is going to have its new value so this is the result register so for example this could be a plus function something like that and then when it executes it’s going to add the two numbers and then populate the W register whereas this would be the fetch instruction this would be the execute instruction per cycle so this is kind of just shows the pipelining this is called instruction pipelining this speeds up the process of this allows you to execute instruction every cycle otherwise of taking two cycles but there is something called a flush where anytime that you you have a call routine it’s going to you’re going to have to have a flush instruction first so let’s just look at this command move W to l55 hex that means the first cycle is going to be effective instruction so you’re going to fetch this command from flash in the next cycle then you can execute this command so it takes two two cycles to execute that one command so this is the second second cycle however instead of having to wait two second cycle to fetch the next instruction you can actually fetch this instruction the move WF port B at the same time you’re executing one that way that your very next clock cycle which is three then you can go ahead and execute that move WF command and at the same time you’re doing that you can fetch the the next command which is going to be call sub one okay so you’re going to fetch this call sub one then you’re going to execute it down here on the next clock cycle which is going to be the fourth clock cycle or a not clock cycle the

internal instruction cycle but once you call this command you get to call command you’re going to fetch as your as your executing this call command you’re going to be fetching this bit set if false command but however you’re going to have to flush that one out because the call command is actually going to take you to a completely different place in your code which may be down here somewhere so you may have your next instruction down here maybe another move literal command or something 56 or something who knows but um so since your next command is not going to be bit set F which is what you fetch then you have to kind of start over again then what that means is you’re gonna have to fetch your sub one oh this says instruction at sub one so actually this arrow should be pointing down to here so let’s go ahead and erase this thing okay so yeah so your instruction it’s going to be here so it’s going to jump to here and then that means you’re going to have to fetch this one all over again and you have to kind of just throw away this information because you’re not going to actually be executing it so that’s called the flush so that’s a description of pipelining okay let’s talk about resets internal resets external resets I mean every pic chip has a reset pin it’s usually called master clear mclr with a bar over the top of it these two things are synonymous either reset bar master clear bar so those are reset that bringing that pan low on your chip will force your reset vector or your force your program counter back to zero okay so your program counter will go to zero and cause you start executing at the beginning of your code again so that’s what the reset function does and that happens when this signal is low so when you’re low when you’re a zero or ground in other words then that’s going to be reset now when when this goes to higher logic one or five volts so logic 1 is 5 volts and then this would be zero volts zero zero logic zero would be zero there are so different ways to implement a reset circuitry on the outside of your chip one way is to have a capacitor resistor and when you turn on power and the power of the board turns on there’s going to be an RC delay time constant so it’s going to take some time for that capacitor to charge here so this capacitor is actually going to go up like this and then there’s going to be a comparator inside of the chip that once it reaches that threshold it’s going to cause it to go high now why do we want to do that because we want to make sure reset stays low for a little bit of time to make sure that the chip powers up properly because if it doesn’t then you can get in a weird unknown state so you want initially for your chip to be reset because when you very first pop your chip you don’t want to be it’s some random place in your code you want to always be at the very beginning of your code so this is called a power and reset another way to do this would be the problem with this circuitry is when when your power goes low it also takes a long time to discharge that capacitor and you won’t instantly turn off your your code execution so it’s going to take some time for that reset to get low so a better idea is actually to use a diode if you use a diode here then once you go low this diode turns on and discharges this capacitor to about 0.7 volts okay so you can imagine this when this goes to ground and if this was five volts before well then the diode is going to turn on it’s going to discharge this five volts into the ground okay so that’s what the diodes for sometimes people put a series resistor just to limit any sort of current that may go through your reset another way to do it with a push-button switch you can add it I have a push button switch here push button and you could type have this resistor here to vdd and then you could have you know you could have when you push the put but push button it resets the chip so that’s if you want to have a push button reset

once you push this button it’s going to ground this pin this pin will go low if you leave the push button high then it’s just going to go to logic one which is going to be not reset so nice that’s a reset button it’s going to do that okay I wanted to talk about you know we have this reset circuitry on the chip a lot of times this isn’t really required because in our labs a lot of times we’re going to have a pic hit 3 or a pic – and I didn’t want to mention that on the pic hit 3 one of the the pins is actually a master clear so this this would be routed to your microprocessor on the reset pin so you have a master clear on your chip on your micro micro controller so this is to say that this is the pic the pic 84 a chip and one of those pins is going to be master clear so you can wire one of these directly here and then you can you can reset and clear your part through your software control through your MP lab program there’s a feature on your MP lab software that will force your microcontroller in to reset or release it from reset and we’ll talk about that when we go over the mplab x software also it’s kind of nice to hear it here as well because your your pick it 3 also can provide your VDD power and also your ground it also your pickets we also provides the instance circuits your programming data in plot so we’ll talk about this little bit later when we start talking about the when we start working on the labs okay now we’re going to talk a little bit about we talked about what’s external to the reset we’re talking on what’s what’s inside of the chip on the reset side there’s several things that happen so all of the circuitry on the screen here is actually inside of the sixteen f-84 a micro controller and these are the external pins you have EDD oscillator master clear so this is your reset this is your power now let’s just assume for a minute that we want to see some kind of normal operation of VDD is five volts let’s say that we’re in clear so we have the the reset the master clear pin pulled low and we the the the signal that actually goes to the internal circuitry is called chip under spur reset it’s inside of the the chip so in this case let’s look and see what happens with the logic so with five volts here you have a bdd rise detect circuit and this is just like a comparator and when it sees that you have a correct voltage of 5 volts then it’s going to sort of set this to this is going to be a logic one you also have a watchdog timer module that will that you may have as well we’re talking about watchdog timers later but just just keep in mind that watchdog timer can affect your reset signal so that’s many times probably wants to disable that watchdog timer module because we don’t sometimes we don’t want this watchdog timer to mess up our reset to keep us from being on or off so let’s look here if let’s just assume that master clear is long so we we want this to be zero so if this is zero then this is an inverter so this gives us someone this comes in here to the or gate and gives us some warm now we know that or gate if even just one input isn’t one then we always get in one so we know the output of this will be a which means we know that this set reset latch here is going to be a 1 this inverter makes this a zero so this 0 goes to this and gate and we have a 0 but now we need to know what is the other state of this reset pin here so we know that set isn’t one now we need to know what’s the value of reset okay so enable powerup timer is zero let’s just assume that the enable power timer is disabled and the enable OST is this is disabled as well so when I set those to zero that means that this or gate is going to be a 1 on the input here because the 0 gets inverted by the bubble and in terms of 1 and then that gives us some 1 here it gives us some 1 there at the input of this and gate ok this 0 comes in here to this bubble and makes it a 1 and that means we’re going to have a 1 on the output of this or

gate is going to give us a 1 1 and in the 0 so just because there’s just one 0 on the and gate that means there’s no way the output can be once of it yeah the output of this angle has to be 0 okay so now what we’re going to get is a a set that means that this glass here is going to be set on the Q output so this Q output is going to be set to a 1 because the set is a 1 in the reset to 0 that gives us a 1 on Q now this is Q naught so that means it’s going to be the opposite of that so I mean it’s going to give us a 0 so everything looks good because this matches this 0 matches this 0 so that’s what we wanted when we put a 0 here on the input the master clear we want this to say 0 now what’s obvious is if you enable the powerup timer then you may be you may get something other you have to go through the analysis and see that another thing that could keep you from being reset is is your watchdog timer module as well so you have to be careful with that so also notice that along with the watchdog timer and the VDD rise to take we also have this oscillator powerup timer the way this works is it just simply counts your clock cycles and will give you a delay a powerup delay and sometimes that’s that’s needed and we’ll talk about that on the next slide here so the way this sequence and the timing works on it is when your VDD reaches a certain threshold that is declared as being good by a comparator and then a comparator will set your internal power on reset signal high okay and then then there’s going to be a little bit of a delay on your master clear so they have a delay from here to there and then you have a power power timer it’s gonna give you this time okay which is set by powerup timer okay and then you have an oscillator timer that you have as well OST that’s that timer to get these two timers so that’s what gives you those two delays and those delays just help the chips start up correctly because you always want to have you always want to make sure that your power is up and nice and stable before you enable the chip so then II had and finally your internal reset so your internal reset is the function of all these delays you have your VDD rising delay then you have your power up timer delay and their t oscillator delay and these these can be set or adjusted if you want to make those shorter longer or shorter it just gives you a little bit of flexibility on when the internal reset occurs in case there’s ever any problems with power up okay so thank you for watching this series and I’ll post the next series later thank you very much before we do that I forgot this is summarize this section really quick chapter two summary is in this chapter we learned about the pic mid-range devices it’s a diverse and effective family microcontrollers the 16 f84 architecture is representative of all mid-range controllers it has a Harvard structure uses pipelining and RISC instruction set the pic16f54 it has a limited set of peripherals but it’s chosen for its small and low cost applications it’s easy it’s a good chip to learn on because it’s not really overwhelming you can read the datasheet whereas a lot of the chips have finishes that maybe two or three hundred pages long whereas this one is within reason it’s a it’s a nice smaller size datasheet that we can really wrap our brains around and learn on ok but the the larger family devices are just a subset of this one so even though the 84 is small well the 84 everything that’s in the 84 is going to be in the larger parts but the larger parts just add extra peripherals the 16 f-84 has three distinct memory technologies it uses SRAM flash and EEPROM so the flash is used for program memory the SRAM is used for file registers and an EEPROM is used for non-volatile data storage and then a particular type of memory location is a special function register and that acts as the link between the CPU and the peripherals okay we talked about those special function registers like to trust

a the port a and the status register and things like that it controls the peripherals and then we have the reset mechanisms ensure that the CPUs starts running when the appropriate operation conditions have been met okay so we want to make sure that all the voltages are stable and that we can control the reset a little bit in case there’s issues or glitches it’s startup so now we’re finally at the end of the chapter 2 thank you very much for watching you