hello guys here on this video we'll be doing an introduction to peak microcontrollers in the language looking specifically of the pix16f and t4m okay let's get started at the end of the video i'm expecting you guys to be able to explain what a microphone is the basic microcontroller system basic control capabilities what are the building blocks of your microcontroller functions of those building blocks and how to connect digital inputs and outputs to your microcontroller now we look at the pix16f 84a looking at its hardware and then the functions of those hardware pins now we look at the memory organization and special function registers and then we look at the basic assembly instructions and programming now what is a microcontroller a microcontroller can be seen as a small computer on a single integrated circuit because it contains in that integrated circuit the processor memory programmable io pins and other peripherals which includes avc serial ports and timers and other form of communication can be either serial or usb depending on the microphone so all of these are in the single integrated segment hence we say it can be seen as a small computer on a single integrated silver but of course it has limitations compared to what a computer can do the basic microcontroller system is where you have an input control and output basic microcontroller system the input can be either switches sensors or some sort of communication to the microcontroller the microcontroller will process and compute the information and then give you the results or the output as either visual audio some sort of communication or switching on and off whatever is connected to the ipos of the microcontroller right now what are the capabilities of a microcontroller microcontroller is programmable that's one thing very important it has configurable hardware functionality here we mean that the pins certain pins can be configured as either input or output they are not they are not fixed inputs they are not fixed output they can be configured to be either input or outputs it can perform specific or multiple tasks depending on the program that has been written and programmed into the microcontroller it has other peripherals built in in it it has timers built in in it it has adc's built in it has serial communication uh built in in it and other forms of communication like the squad c or spi as part of the control now what are the building blocks of the microcontroller microcontroller it has the cpu for fetching and executing instructions and also for performing arithmetic it has program memory for storing the program during the programming process data memory for storing user data and then the data can be stored in a volatile ram or non-volatile eeprom memory and now you have input output pins iops for interfacing the rest of the circuit to your microcontroller for connecting your sensors to your microcontroller for connecting your leds to a microcontroller you do that through the input output pins right it has built-in timers which can be used as counters of to implement delays or even to implement a real-time drop right and it has other peripherals like uh adcs serial communication which you may use if there's a need for that in your application these are the basic building blocks of your microcontroller and now how do you connect inputs and outputs to your microcontroller we're looking at the digital inputs and outputs here now the first thing to note is that your peak microcontroller can sink or source a maximum of 25 milliamps per pin each and every pin can sink or source a maximum of 25 milliamps a sink means when the current is flowing into the microcontroller into the pin of the microcontroller that current should not exceed 25 milliamps source means if the current is flowing out from the pin of the microcontroller that current will not exceed 25 milliamps connecting inputs to a microcontroller we can use the pull down configuration where the input of the peak is connected to ground through a pull down resistor the reason is the pin can never be left floating we cannot erase this resist in the ground and just have a switch connecting to the input because when that pin floats the input state of that pin will not be reliable at some point it will jump between high and low giving an unreal unreliable state we have to pull it down through the pull down resistor and then when the switch is open the logic input is zero the logic input is 0 when the switch is open when you close the switch the current flows through into the input of the microcontroller yes some current will also flow through the cooldown resistor but surely the current will flow into the input of the microcontroller giving it logic one right second configuration pull up configuration in this case the pin of the microcontroller is connected to vcc to logic one through a pull up resistor right so when the switch is open current is flowing into the microcontroller through that pull up resistor when you close the switch because there will be zero resistance between uh the input and the ground the current will flow directly to the ground and no current will be thrown into the input of the microcontroller giving logic zero there when the switch is closed right basically these are two configurations you can do to connect your digital inputs to your microcontroller of course when connecting sensors sensors can provide logic one and zero directly so you won't need the pull ups and pull downs some sensors you can connect them directly to your input or to pin right connecting outputs to your microcontroller there's two ways as well where in the first case it is the sinking where the current is flowing from the supply through the resistor and the led into the i o of the microcontroller we see it is sinking and then we said that current must not exceed 25 milliamps the error configuration is when the current is flowing out of the big microcontrollers i open through the resistor and the led to ground and say it is sourcing now this current should also not exceed 25 milliamps right and now let's look at the pic16f84a basically depending on which microcontroller you are interested in and you are willing to use in your application it is important that you know the hard way of that microcontroller so that you can construct your circuit correctly and you can assign your inputs and output pins where you have to where it is possible for you to do so right now looking at the pix16f84a we have what we call port a pins but a pins are for r a is pin ra0 ra1 three and ra2 a four pins in total we have five pins on port a and we have put b pins starting from b zero b one b two b three four five six seven we have eight port b pins both port a and port b pins are programmable input output pins port a and port b pins are programmable input output pins meaning you can choose either of those pins to be either your inputs or your outputs right and then we have the m clear pin pin 4 mta this is a reset pin this pin should be always uploading one for normal operation for the peak to execute the program installed in it but when that pin when the logic on this pin goes to zero when this pin is connected to ground somehow the peak will stop executing instructions it will stop executing instructions and it will only start again from the beginning when that pin is released back to logic one hence m clear or sometimes known as a reset i'm clear reset right pin five is vss it's your ground i've talked about b0 to b7 pin 14 into your vdd it's your supply pin meaning pin 5 and 14 are for supply wear 5 is for ground and then 14 is for your positive supply the peak can start operating from 2 volts to a maximum of five point five volts so you put in two volts to a maximum of 5.5 volts it will start executing instructions and perform as it's expected right pin 15 and 16 for this microcontroller therefore oscillator input there are different oscillator configurations but pin 15 and 16 are for the oscillator input right and on the next slide that's what i'm i'm explaining vdd it's for supply normally we supply the microcontroller with five volts but it works from two volts to 5.5 volts vss is your ground it's pin five and create your reset when the pin is low the peak pulls into reset right and then pins 15 and 16 are for your oscillator part a is pin one two pin three pin 17 and 18. these are programmable input of two pins on point a and then for b is pin s6 up to pin 13 these are programmable input pins on port b right now uh programming the big microcontroller what you need to know before you can start programming your big microcontroller in assembly download you first need to know your microcontroller hardware in this case we have looked at the pic16f84a hardware you need to know your hardware where your pins your inputs output pins are and what are the capabilities of those input output pins you need to know that and now you also need to know the memory organization of your microcontroller and more especially what we call special function registers because these registers are the ones that helps you control your microcontroller and then the third thing that you need to know is your assembly instructions once you know your microcontroller hardware you know the memory organization and special function registers and you know your assembly instructions then you can use your assembly instructions together with your special function registers to configure and control your microcontroller hardware you use your assembly instructions together with the special function registers to configure and control your microcontroller you first need to know your microcontroller hardware you need to know your special function registers and then you need to know your assembly instructions then you use the assembly instructions together with the special function registers to control your microcontroller that way right now let's get started looking at the memory organization and then the special function registers we have already looked at the hardware now we are looking at the memory organization and then the special function registers now the pix16f 84a has two memory blocks being the program memory and the data memory now the data memory is divided into special function registers the ones here on top with names and then the general purpose registers the 64 general proposed registers now this data memory is also divided into belts being bend zero first block and then band one the second block right now the general purpose registers are used to store user data and then the special function registers controls and monitors the specific of various aspects of your microcontroller right the special function registers they either monitor on control certain aspects of your microcontroller but we look at what those aspects are and which special function registers controls or monitors what part of your microcontroller the most important thing to note here is that your data memory is divided into two banks two blocks being bend zero and band one if you want to access a register in bank zero you must be in bank zero if you want to access a register in bank one you must be in bank one there's no way you want you want to write to a register in bank one yet you are inventing you're going to write to the wrong register we'll also look at how you switch between and the two banks right now examples of special function resistance we set special function resistors they control or monitor specific aspect of your microcontroller now let's look for example at the truth register the trace register we have trace8 entries b register both they are in bank 1 3s a and b registers as the special function registers what are their roles in this microcontroller that is a register found in bank one its purpose is to configure your pins as either input or output specifically to this a for port a pins so to this a is used to set up your piece as either inputs or outputs but your pins which are on part a right this b register also in bank one it is used to configure your io pins on port b right now the part registers port registers found in bank 0 we have port a and port b the part registers are for reading or writing to the i o pins right for a you use it to read or write for eight pins and then part b you use it to read or write to point b pins right you read or write to the pins depending on whether they are configured as inputs or they are configured as outputs so if a pin is an input you are going to read it using the port register if a pin is configured as an output you are going to write to it using the same port register right now the trees registers are for configuring the pins as either inputs or outputs the part registers are for reading or writing to the pins right now how do we switch between the two bends bit five of the status register is used to switch between the banks now the status register is found in both banks in bank zero you have state has a register in bank one you still have status register now bit five of the status register is used to switch between the banks when you set that bit you are switching to bank one meaning when bit five is set it means you are in bank one one bit five of the status register is clear it means you are in bank zero that's how you switch between the banks you want to jump to bank one you have to face set bit five of the status register that will take you to bank one you want to go to bank zero you have to clear with five of the status register that will take you to bank zero right now basic registers and instructions all the resistance we have been referring to point a point b and trace a this b the status register all of the registers found in here even the general purpose registers they are all eight bit registers they are all eight bit registers and then they are non-volatile they actually volatile meaning when you remove power the data on those registers is lost they actually volatile not the other way around and then since all registers are 8 bit registers in some registers some bits will be useful some bits will be useless or unused to say now all registers are 8-bit registers we have bit 0 up to bit 7 on the right-hand side the least significant bit on the left-hand side the most significant bit and now we have instructions that can affect a single bit in the 8-bit register now those instructions are called bid oriented instructions they modify or affect only one bit in the whole 8-bit register and now these bits like i said they affect a single bit in a register and then examples are the bit set file so let's say for example say bit set file port a bit four your file or your register is for a and then the bit you want to set is bit four let's assume that this is your port a register and then bit four will be set according to the instruction bits at five set bit 4 of the for a register that bit will be made one the rest of the other bits will remain unchanged if it was one it's gonna remain one if it was zero is gonna remain zero only this one will be set if it was set it's going to remain set but if it was zero it's gonna change to being set being one right so these are called bit oriented instruction they affect only one bit of the 8-bit register then we have byte-oriented instructions and literal instructions now these instructions they affect the whole eight bits of a register prior to radar destructions and literal instructions they actually affect the whole eight bits of a register right examples in this case let's say we use the move a literal value into w w being our register normally said to be the working register move a value into the working register the value that we want to move into the region register it's 3f in hexadecimal hexadecimal value 3f we move it into w and now this is our w register the first and nibble is three the second label it's f three f so that's actually supposed to be one not a zero so supposed to be three f right we move 3f into w that's what will be in w right and then we have what we call branching instructions or control instructions and these instructions jumps from one location of the program to another and then sometimes you can return back to the original location and sometimes you don't even have to return now example of those instructions are go to instructions the paul instructions and then the retail instructions to get a clear picture of all the possible or the basic um instructions for the big 16 f-84a please download the pix16f84a datasheet and then look under instruction set for explanation on all the instructions and what they can do right now how do we configure that pins how do we configure the i o pins of your microcontroller like i said the threes registers are used for configuring the pins as either input or output now the trace registers are the special function resistors and then the pose of the purpose of the trace register is to configure your pins as either inputs or outputs now trace a is for configuring port a pins and entries b is for configuring part b pins for aps we have a0 to a4 for part b pins we have b0 to b7 meaning we have eight pins on part b we have five pins on point a right when at least bit is set either it is a or b when any trees bit is set meaning the logic level of that bit is made one logic one the corresponding part pin is configured as an input if you want to make a a pin and input you must set the corresponding trees bit of the pin right now when a trace bit is created the corresponding port pin is configured as an output to make a pin and output you must clear its corresponding trees beat right an example make a zero a3 and a4 inputs a1 and a2 output on port a we want you to make a 0 8 3 and a4 inputs and then a a1 and a2 output on port a then what volumes you move into twist a to configure a0 a3 and a4 as input a0 must be set because when that triggs bit is set the corresponding pin is an input a0 must be set on twist a a0 must be set and then a3 must be set and then a4 must be set and then remember on point a or interest a we don't have a5 a6 a7 and for that reason they will all be 0 a5 a6 a7 they will always be 0 because we don't have input or 2 pins corresponding to a5 e6 b7 right now we want to make a1 and a2 inputs outputs a1 and a2 outputs to make a pin and output the corresponding trees bit must be 0 so a1 must be 0 a2 must be 0 right and now what is the overall 8 bit value higher level that is 1 in hexadecimal the lower nibble that is 9 in hexadecimal so we move one line into two a we will have configured our pins a zero a three and a four s inputs a one and a two s outputs right now on plus b now on part b make b 0 to b 3 inputs make b 4 to b 7 outputs on part b make b 0 to b 3 inputs make b4 to b7 output to do that for inputs the corresponding three bits must be logic one must be set at this bit is set it means the pin is an input and this bit is cleared it means the pin is an output so to make b 0 to b 3 inputs we must set b 0 b 1 b 2 and b 3 for trees b b 0 b 1 b 2 and b 3 are set to make b0 to b3 inputs b4 to b7 they must be output so b4 b5 b6 b7 those three bits must be zero to make the pins outputs right so we move value in exercise much zero f into trees b to make those pins and b 0 to b 3 inputs before to b7 output now onto this a and on port a although we don't have a5 a6 a7 those resistors are still eight bit resistance so the a5 a6 a7 we can make them zeros all the time because they are unused but they are still there because the resistor it is still an 8 bit resistor now how do we switch between the banks we talked about using bit five of the state as a register to switch between the banks right now to do that um when we set with five of the status register that means we go into bank 1 when bit 5 of the status register is set it means we are going to bend 1 and then that means we can access read or write to the registers in bank one after switching to bank one we first switch to bank one then we can access registers in them one be able to read and write those registers now to go to bank zero we need to create bit five of the status register we need to make bid five of the status register logic zero and now after doing that it means we can read or write to registers in bank zero one bank zero you can access registers in bank zero you want to access registers in bank one you must first switch to bank one if you're in bank zero and then if you invent one to access registers in 0 you must first switch to bank 0 now we switch between the banks by setting or clearing bit 5 of the status register now this is our status register it has 8 bits like all other registers in this microcontroller and then bit five of the state has a register the name of that bit is also rp0 rp0 is the name given to the bit 5 of the status register try and as an example switching to bank one we say bid set file status register comma five we said bit five of the state has a register and then this becomes what is in your status register all of the other pins they remain unchanged but bit five becomes one and then this means we are in bank one we can access registers in bank one meaning we can access the trees a and threes b registers we can configure our pins when we invent one by setting bit five of the status register right to go to bank zero we need to clear bit five of the state has a register so as an example switching to bank zero there we say bit clear five the final state has a register the bit is bit five be aware that this time around i'm using rt0 instead of 5 these are the same you can say comma 5 or comma rp0 because i said rp0 is the name for bit five of the status register so whether you say comma five or you say comma rp0 it is the same thing we are accessing and switching the same with five of the state as a register in this case say b clear file status register comma rp 0 bit 5 for the status register becomes 0 this means we are in bank 0 and now that you're in bank 0 we can access registers in bank zero according to your memory organization we're looking at registers in bank zero we can access special function registers in bank zero and also the general vapors registers right now reading them right into the input output pins how do we read or write to the pins to the actual pins of your microcontroller now the port registers are used to read or write to the pins the three resistors are used to configure the pins as either inputs or output pins with five of the status register is used to switch between the banks and now the part registers are used to read or write to the pins right well an iop is configured as an input you can read the state of the pin by accessing the corresponding part register right when a an i o pin is configured as an output you can switch it on or off through the corresponding part resist and now remember to access the port register you must be in bank zero right now as an example this is how we'll read part a3 if port a3 is configured as an input we'll use an instruction bit test file skip if set port a comma b3 so to access port a3 pin a3 we use port a bit three we can read the state of b3 of port a meaning we are reading the state of pin a3 of your microcontroller now switch off pin before of your microcontroller we'll say b clear 5 part b comma 4 we are switching off that bit 4 with um pin b4 of your microcontroller now to switch on pin a1 of your microcontroller bitset file for a comma 1 we are switching on that a1 pin of your peak microcontroller right now for all of these instructions because you are accessing the port register port a and port b regardless of which part is it both part a and port b are in bank 0 so these instructions should be executed while you are in bank 0 right now to be able to successfully write to an to an iop remember we write to 18 we switch it on or off we write it when it is an output so to successfully write to a pin that pin should be configured as an output right if a pin is configured as an input and you write to it that right is not going to be successful because the state of that pin is determined by the state of the input if it's an input but now if that pin is configured as an output you can successfully write logic zero or logic one to it because you are allowed to write with system output right to read to read the state of the iop the pin should be configured as an input for instance if you want to read the state of the sensor the pin on which the sensor is connected to that pin should be configured as an input right you cannot configure that pin as an output and then you connect a sensor to it and then you want to read the state of that sense it's not going to work yes the pin has to be an output an input and then you connect that sensor to that input pin and then you can read the state of the sensor by reading that into p right now let's look at uh a simple example of a peak program or a big instruction or an example problem let's say you want to switch off all port a pins and you want to switch on all four b pins so you want to write zero on all put a pins and you want to write ones on all part b pins the fact that we want to write to these pins it means those things should be configured as outputs we write to outputs and then we read inputs right so the fact that we want to write zeros to put a pins for a pins must be outputs and then the fact that we want to write ones to put b pins those four b pins must be output right now our pins must be configured as outputs through the truth registers and then we need to write 0 0 to 3 a and then we need to write 0 0 to trees b in exodus remember the trace register when a bit is cleared the corresponding pin is an output so we want to clear all the trees bits so that all put all port pins are output so onto this a when we clear all this a bits all put ap's are output when we clear or when we make all these b between zeros all part b pins are outputs right now to access the trace register we must switch to bank one because the three registers are in bank one before we can write zeroes to three a and b we must first switch to birth one and then write zero to three a and then write zero to trust b now after configuring the pins as outputs we now want to access the part registers so that we can write zeros on point a and then we write ones to part b but now remember we went to bank one so that we can access and write zeros to a and b we now have to switch back to bank zero so that we can access point registers so we can access port a and port b and then once we're in bank 0 we can write 0 to port a and then write ff to part b right that's basically what we need to do so the solution to that is first we need we need to go to bank one so what do we do is we say bitset file status comma five remember we said setting bit five of the status register takes us to bengua meaning whatever we do under this instruction just underneath this instruction whatever we do we do it while we're in bank because we have switched to bank one by setting bit five of the status register we are now in bank one we can access the transit register two ways we can write zeros to the transistor we move zero into w w is our working register so the instruction says move a literal value into our working register which is w so move literal to w the literal value that we want to move into w to w is small zero z w has now in it zero zero now move the contents of w move w to 5 what is the file this a the contents of w now move them to trust a initially we moved 0 0 into w meaning w as 0 0 and now we are moving that 0 0 to this a clearing all the bits off this a making all pod a pins outputs right now the easiest and simplest way of clearing a resistor instead of moving a zero into that register is by using the clear instruction clear file it is b i could have said clear file trace a but i wanted to show you two ways you can do that it's either you move zero into w and then you move w to that register or you can directly clear that register and then in this case you say clear file which is b making all this b uh bits zeros and then making all part b pins outputs all of these we are doing it while we are in bank one because we set with five we have managed to make for eight pins outputs and part b pins outputs by moving zeros in the trees registers now we want to access port resistance to access port registers we must go into bank 0 so what we do is we beat clear file status comma 5 with here bit five of the state has registered here in bit five of the status register takes us into bank zero and now in bank zero we want to write zero on point a and we want to write ff on point b we want to switch off all but a pins we want to switch on all part b pins so how do we do that we can do that easily by clearing port a resistor or by moving zero into port a either way will work and now we move a value ff into w which is all once and then we move w into part b switching on all part b pins right so we move f f into w and then we move a w into for b meaning we move this f into for b switching on all part b pins although there's a shortcut for moving zero into a resistor there's no shortcut for moving ff into a register you have to move that into w and then move w into that particular register you want to move it in so this is the simplest peak assembly program and that you guys will come across and in this case we were asking you to switch off all pod a pins and to switch on all part b pins meaning that you have to configure all these pieces outputs before you can switch them off on default all your microcontroller pins are inputs on default meaning you have to configure them if maybe the program required that your pins are inputs since they are on default since only folder inputs you don't really need to configure them as inputs or reconfigure them as inputs since on default they input but it is important that you configure your pin so that when you go and execute your instruction you know that you have configured your pins in the right configuration before you can read or write to them right now on this previous example we set switch off all point a pins and switch on all four b pins meaning right zeroes to put a right ones to put b this is a statement from the statement you should be able to identify how you you should configure and there is the program for configuring your inputs and how you will access and write to those pins now here we are actually looking at how do you interpret a problem statement let's say you are giving a statement the statement doesn't tell you directly how to configure your pins but you should identify from the statement how your pins should be configured if the statement says you are expected to read for a0 to a4 and then write the value to part b 0 to b4 meaning you read for a0 to a4 they're not telling you that those particular things are inputs but they're telling you that you are expected to read those things the pin that you read it means that pin must be an input the pin that you write to it means that pin must be an output so you need to be able to interpret from the problem segment how your pins should be configured if you are not directly told what should be input and what should be output other specifications like maybe using timers and all of that they will tell you how to set up your microcontroller how to initialize your inputs outputs how to set up other peripherals like your microcontrollers and so on which we are going to take a look at in the later stage now the problem statement should also be able to tell you what is this beginning point what is the next point and what is the end point right now in the previous example we said you have switch off all for eight pins and switch on all part b pins initially you will set up your pins as outputs and then you go directly to switch off for eight pins and then the next step you switch on all four pins and then from there there's nothing else to do according to the statement it doesn't tell you what to do next in this case or in any other case when you read the statement it should tell you where to start what is the initial point what is the logic to achieve the next step what is the next step and then what is the final stage and then once you have reached the final stage whether you repeat the process or there's nothing else to do after reaching the final stage the problem statement will give you those steps right so you need to be able to interpret your problem statement find the keywords which will tell you which pins should be input which one should be output how to configure or initialize your microcontroller and then where to start what is the next step and then how to reach your final goal what is your final goal problem statement should be able to lead you through all of these processes now problem statement to flowchart when you write a program the first thing is mostly you get a problem statement you have to transfer your problem statement into a flowchart and then from a flowchart is only then you can start writing a program right the process of writing a program you take your problem statement you convert it into a flowchart flowchart is the step-by-step process which will tell you how to approach the program writing process step by step meaning at the beginning what to do at the beginning we should go and initialize that's where you set up your input output pins you configure your input output pins and you configure other peripherals which may be necessary depending on your problem statement right now after initializing you then go into the main program now the main program will be will depend on what the problem statement was and then the main program can be broken down into smaller blocks smaller steps so that the main function can be achieved this main function can be broken down into smaller steps but those smaller steps they will be guided by the problem statement itself what is the initial state what is the logic for the next state and then what is the final state and whether you keep on repeating the process all over again now initialization why do we initialize the our microcontroller and how do we initialize our microphone now we initialize our microcontroller because we need to inform it where we want our input and where we want our outputs right depending on the problem statement or on the circuit we've constructed where we want our inputs where we want our output and also to inform it or to set up other special function registers if we are using other modules or adam peripherals in the microcontroller we do that in the initialization process we set up our inputs and outputs and we set up other special function resistors so that we configure our microcontroller depending on what the problem statement required now to initialize we need to access the special function resistance for example setting input and output pins we need to access the trace resistance which our which is our special function resistance so we initialize our microcontroller by writing to special function resistors so that we can configure our pins and the microcontroller depending on the circuit and the requirements of the problem statement and now to access special function registers remember we have uh two banks been zero and bank one we must switch between the banks ensure that we are in the correct bank so that we can access the correct resist if you want to access the trace register we must switch to bank one if we want to access the port register we must switch to bank zero and so on right and then bit five of the status register is the one that we use to switch between the banks when that bit is set it means we switched to bank one when that bit is cleared it means we switched to bank zero right now let's look at creating a simple uh mp lab program let's look at creating a sample project and then in this project we are going to solve the very same problem and in this program which we did and which i explained here is actually an unstructured problem it does exactly what we are looking for but i've omitted some overhead uh things which were going to look at when we create a project so i'm using mplab 10 ide version 5. maybe by the time you look at this video there will be a newer version of mplab to create a project we say file new project and then microchip embedded standalone project next looks like my computer is thinking and then you select the processor or the ic which you want to write the program for i will type in here pick 16 f 84 a this is the one we are using select the pick next okay the peak the hardware the programming tool we shall be using is a pic3 so i'll select the pixel 3 as a programming tool i'll be using them click next i'll be using assembly language so i'm gonna not a high tech pixies c language i'll be using assembly language so i'll select mpasm tool and then give your project a name i'll say tutorial 1 and then where do i want this program to be saved or browse let me browse to my memory device h create a folder there and then call it tutorial one and then okay it says new folder there okay it's fine it didn't rename that folder i distilled new folder it's fine and then just click finish now you have your project file here created we need to create or to edit the project file what you call the source file the source file is where you will be writing your program so right click new pic sample asm give it a name i can give it the name the same name as the project name tutorial the location it is still the same location click finish and then the file will be created with the file name dot a s m right the file name dot asm now to to make things easy i'm going to get rid of this a already arranged structure problem so that we can create our own problem now this software here it's mplab10 ide it's your compiler right it's your compiler software and now this one is the source file now this mplab can write programs for almost all big microcontrollers you can write programs for all the big microcontrollers using the very same compile so when we write a program we need to inform this compiler for which peak microcontroller are we writing this program for so the instruction for that these are the overheads i said we skipped we could not when we implemented this solution we ignored those and overhead instructions which are supposed to be on your file on your program file as i said this is an unstructured program now let's look at what is needed for us to build the simplest peak assembly program you need to tell the compiler which peak microcontroller you are writing a program for and then you do that by using the list instruction this p equals to 16 f 84 a that's how you inform your microcontroller your compiler that you are writing a program for this particular microcontroller list p equals to 16 f 84 8. now this next instruction it's also very important i will explain the purpose of this instruction later on hashtag include p16f 84 a dot inc dot include dot inc this instruction basically says make use of the file p16f84a we're going to look at what does this file contain but it just says in this program make use of the file including this program the file p16f84ad now one other most important thing when writing a problem is there's always at the beginning there's always at the beginning the program doesn't just run randomly from any location it has to start running from someone and now for a big microcontroller that is our reset vector we need to start running or the program will always start to run from what we call the reset vector whether it's at power on or whether you reset the microcontroller using mta or whether it is watchdog timer reset any form of reset or even that power on reset the microcontroller will start executing instructions at the reset vector and then for a big 16 f84a the reset vector is program memory location 0 right so we say origin location 0 0 0 x in front here 0x in front means hexadecimal i can just say instead hexadecimal value 0 it is still the same thing but most of the time i use 0x to denote a hexadecimal thing so origin 0-0 this is at the beginning we want to go somewhere go to [Music] ini allies right now a go to is our instruction or reason zero zero says this is at the beginning this is the reset fit so a semicolon means whatever comes of the semicolon is a comment so let me try and maximize the window here come on how do i do that okay it looks like i can't do that i don't know how to do it it's refusing to work the way i've always done before but i'm assuming you can see there so whatever comes after the semicolon it's actually a comment you can write anything to state what is happening or what is that line of code so basically here is our reset vector this is where the program will start will always be will always start executing at after any reset the program will start executing from zero zero from program memory address zero zero and now what is that one we want the peak to jump to the location initialize at startup at any reset after any reset we wanted to jump to initialize so we say go to initialize initialize is what we call a label so you can say go to start there you must have start you can say go to whatever you state there you must have it somewhere so that the people know where to jump to where that initialize is so when we write your big problem let me also do this i'll add these things as we write the program so here at the margin i leave spaces at the margin because the margin is for labels imagine i'm leaving the margin for labels like initialize and stuff and now the next part of your assembly program structure will be the instruction the assembly instruction and then the next part let me do that they call it the operand where the instruction is being imposed or whatever the case may be right now we say go to initialize and now initialize must be stated somewhere as our label so we go to label initialize initialize remember we are still implementing this program so at initialize that's where we want to set up our input and output pins that's where we want to make all of these things output so at initialize this is the program that comes in executing this program so what's going to happen is at initialize we're going to say bitset file status comma 5 or we can say comma r p 0 i said rp 0 is the name for bit 5 of the status register bit set 5 status comma rp 0 and then move literal value into w the value is hexadecimal value zero zero move w to five define stress a and then we say clear file which is b remember i said these are the two ways of moving zero into a register either you directly clear the register or you move zero into that register by firstly moving zero into the working resistor which is w and then moving w into that particular register right now we want to go back to bank zero bit here fine status comma rp0 so here basically by setting our p0 we actually switch to bank one we move zero into w okay on this instruction our w register has a value in it zero and now when we move that zero into trees a this a becomes zero itself which means that point a pins are outputs all pot a pins are out here by clear increase b it means we make this b still same as for a zero which means r or b pins are outputs now here what do we do we switch back to when zero now we are inventing what is it that we want to do intensive according to the program we want to according to the statement here we want to switch off all port a pins and switch on all four b pins so we clear port apis and we write ff to point b pins so we'll do here move lateral value into w okay we can use the shortcut by saying clear file which is a we're already switching off all port a pins and then we can say here move little w the value that we want to move into w is f now it means here now our w register has the value in it f right now we move that value move whatever is into w moving into the file the file is part b right this instruction tells us now our port b has the value in it f f meaning all for b pins are now one logic high they are now i right we have accomplished that task what we can do we can do what we call an endless loop go to loop so basically here the peak will be stuck in this loop it will execute go to the voice loop loop it is right here so it will restart in this loop after configuring your inputs and outputs and writing 0 to port a writing ff to put b it will be stuck in this loop because there's nothing else to do right and then to conclude your program there should always be the end instruction this end instruction is never ever ever going to be executed by the peak but is an instruction that tells the compiler that this is the end of your program it's an instruction that tells your compiler that this is the end of your proof right now we can build and see if this program builds successfully i'm not expecting any troubles here so it does this is basically a basic assembly program where we have switched off all port 18s and switched on all part b pins now let's use a simulator program peak simulator program sexually ocean soft peak simulator program this program has a trial version of 30 days can download the peak simulator ide program and use it but it has a trial version of 30 days right now you need to show your microcontroller first we need to select the correct microcontroller so by double clicking here you can select which microcontroller so we are using the pic16f84a so i select that microcontroller and then tools microcontroller view it pop up the hardware configuration of your microphone this is your microcontroller these are the pin out a018283 a4 b0 up to b7 vss and vdd you can they are not part of your ios you cannot control them via your program so they are excluded from accessibility here so these pins like i said initially they are configured as inputs but if we load our pro say file load i'm going to browse to my memory device browse to that new folder the project folder in the dist production access the tutorial x5 the hex file is what is actually programmed although we are writing our program on your asm file when we program our controller we actually use the hex file so i'm gonna import my hex file and i'm gonna start i can choose the rate at which the instructions are executed let's leave it at first start executing instructions you see what happens your pins are all outputs the buttons have disappeared to show that the pins are now output and now all of point a pins a0 to a4 they're all off b 0 to b7 they are all on right how or how did that happen let's look at it step by step i'm going to change the program rate to step by step and say start execution now initially like i said your microcontroller the pins are inputs all of them on default now the first instruction we'll have is go to initialize that's our instruction go to there and then our next stretch is bit set file status rp0 right this makes us or helps us switch to bend one now after we switch to bank one we move zero into w our working register it's w when we move zero to it the value into w is zero zero right and then we are going to move that to this a our pins on default are inputs because it is n on default all the usable pins to a0 a1 a2 up to a4 they are all set on default entries b all the pins are also set on the front red means they are set white means they are clear gray color means they are unused right so because three eight bits on default they are said for eight point eight pins on default they are inputs there is a bits on default they are set put eight pins on default their input we are about to move zero into three a w is zero we're going to move that w into this a so what happens when we move w to this a trees a becomes serums and then what happens the buttons which indicates that the pin is an input those buttons disappear to show now that pin is now in output we have now made only to this a output because our next instruction b is the one that's going to make part b pins output when we execute meaning when this instruction moves into this site when it is executed when we execute this instruction trace b becomes zeros the buttons on port b pins disappear all of these pins are now output right our next instruction we are switching back to bank zero bcr file status are p0 when we execute this instruction is gonna come to this side and now we clear part a we switch off all port a pins they are already off so even if we switch off they're going to remain off so with clear port a it remains off our next instruction we are going to move ff into w move a little value ff into w currently w has zero z when we execute this instruction while it comes on this side w now has f hence all those blocks are great w now has ff now we want to take this ff in w we want to move it into part b now right into part b means we are going to write these fields we are going to write f f to this piece by writing to point b so when we write f f to part b all part b beans are high right all properties are high and then for b pieces f even the part b the resistor has ff on it there right now the next thing is endless loop may you go to main endless loop that's the end of your problem right you can use the peak simulator ide to simulate and see if your program is going to run accordingly and if not see what might be the possible cause of your problem right so basically this is the simplest assembly program you can do let's assume that you had leds connected on port b and on port a the one zone for b you want to switch off the ones on part b you want to switch on that's how you would have done it oh you want to do it the other way around you want to switch off all part b i will say here clear file is b move ff into trees into point a h one clear file part b this must be part b i'll explain why it worked even if you did this thing clear file port b and then move ff into port a cfl for b move ff into port a let's stop the execution reload the program and then start execution this time around ff will be written to port a and 0 will be written to part b so let's start and then we say now go to bank one bit set file move zero into twice a make these outputs tier five 5 b make those outputs go to bank 0 here with 5 from the status register here is b make twist b make clear part b actually make pot b pins off and now move ff into w move that to part a when you move that to port a put 18s are the ones that are high now although we have a 0 to a4 we set move ff to point a a5 a6 a7 they are not used so it doesn't matter whether you move once to them or you move zeros to them they are not going to play any role because you don't have pins associated to a5 a6 and a7 so only a0 to a4 will switch on right now you will or you must have been aware that i had trace here that this register here to stay and i managed to the program managed to work well even though that the file here was at least instead of a port why did that happen we are in bank zero we switched to bank 0 meaning whatever we are doing after switching to bank 0 whatever we are doing underneath we do it while we're in bank 0 right now in bank 0 can we access the trees registering bank zero no let's look at this trace register is in bank one when we end bank zero we access the port register even though i said move to trace a but the fact that i mean bank zero i'm actually accessing the port a instead of the this thing so when i say what whatever let's say i'm in bank zero and then i say right to trust register i'm not going to successfully write to the address register and as a result i'll be actually right into the port resist but because i'm in the wrong bank if i want to write to the chess register i must first be in bank 1 then i can access the transmission if i want to access the port register must be in bank zero then i can access the quadrants if i try to access the trust register while in in the in the wrong bank in bank zero i'm actually going to access the port the corresponding port register if i mean bank one and i want to access the port register i'm actually going to access the corresponding threes register because i'm in the main one i'm not invent zero you must be in the correct bank to access the correct uh register else you will access the corresponding address right if you want to access the trees while you're going back zero you access the point if you want to access the port while you're in bank one you will access the trees and then that will create or that will cause a malfunction in your program because the address register you are setting input output piece you might be modifying your input output setup yet you wanted to switch on and off the pins but you are switching you are setting them as inputs and outputs because you are in the wrong bank you are accessing the wrong register right so that's why it is important to switch between the banks before you can access the particular register you want to access right so this is the simplest um peak assembly program language that we can do with you guys and now to add to this there are other examples we can look into we have already done this first one where we want to write zero on point a and then we want to write ff on port b that's basically what we just did let's just do the second one very importantly you are expected to read point a and write the value on point b they didn't tell you where the inputs are where the outputs are they just tell you read port a and then the village you read from point a write it to part b right and then maybe this you can do it indefinitely you can keep on reading and writing reading port a or you can do it once you can read what a once and then write to for b and then after reading and writing then you go into an endless root or you can repeat the reading and the writing process let's do this second one we're going to do both ways of reading ones and writing ones and then also repeating the reading and the writing process now to start with it says read for a meaning point a must be inputs but a pins must be inputs a0 up to a4 must be input and then it says right to part b but b0 up to b7 can be input but obviously only b0 to b4 will be usable because we have the corresponding a0 to a four pins on the port a pin right we don't have b we don't have a5 a6 a7 we need b5 b6 b7 will be redundant sort of to say now but we are going to set the whole of port as inputs they hold up for bs output and let's do that how do we do that we go back to our program i'm going to modify this program i want all of point a pins to be inputs and i want a0 a1 a2 a3 and a4 a5 a6 a7 are zeros right let me do it simply by saying the binary value a7 is zero a6 is zero a5 is zero a4 is one a3 is one a2 is one a one is one a zero is one so in total it's eight bits one two three four five six seven eight right this is how you will denote a binary value on this compiler program b dash the value plus right now in hexadecimal this value will be 1 f move that one f into w move that to three a you are making all part aps inputs all port a pins are inputs now to make part b these outputs we leave it as it is three h is b all part b pins are output now the question says we are supposed to read for a now i'm going to introduce more instructions here these instructions i said you can get them or you can find them on the peak data sheet just download the pic16f84a datasheet and then you can familiarize yourself with these instructions right the the instruction which you are going to use here we want to read and this time around we are not reading just one bit of pot a we want to read the whole eight bits of port a register so we use a byte oriented instruction move file what is the file the file that we want to read is for a we move it into w we read the state of 28 pins and we temporarily save them into the w register right and then what do we do now w has the state of port a pins we just take that into part b we just release this part and then we say just move five put a into w move w5 move w to 5 meaning now that w has the state of port a move that into part b now part b is equals to whatever that is in part a and then after reading for a and writing it to part b we go into an endless loop right we go into an endless loop let's build this program and then simulate it so let's stop and then reload the hex file start what we initialize is our first instruction and then bits at five rp0 of the state has a register we go into bank one we move one f into w then w is going to be one f cw now has one f in it we're going to move that into two a but it is a already has one f so it's gonna remain one f so we move that which is a to a remains one f the next instruction we're going to clear this b which currently has f f in it because on default the pins are inputs meaning all the trees bits are set we need to clear them to make all the pins outputs so we cleared this b because b becomes zero zero all the bits are zeros and the pin on part b are now outputs now we go to bank zero now by clearing bit by encoding bit five of the state has registered and then the next thing is we want to read the state of point a let's say a0 is high and a2 is high i just click on those buttons there to make them on we can click again to make it off click on it to make it on a0 1 and a 2 on and that's what we are about to read and when we read part a with a 0 and a 2 on w will have the state of point a so we say read but a w has five in it why because one bit two and b zero are set in a binary where b two and b zero are said the rest b the rest are trade that value is 5 bit 0 is set bit 1 is clear bit 2 is same and then the rest of the other bits are created that value is 5 meaning we read for a and then the value 5 was moved into the w register and now that 5 that value which we read from port a is the very same value we're going to write to part b meaning b 0 and b 2 will be set so we say move w to file for b b0 and b2 are set the rest of the bits are still here right and then the next thing you go into an endless loop right meaning we perform the read and write once and then we will go to an endless loop the problem with going to an endless loop is if i change the state of the inputs here even though the program is running the state of the output won't change because we read and written once we are not repeating the process meaning we are not going to update or be with the changing state of part a so to be able to update part b with this changing state of port a we need to repeat the read and the right process so we can say go to loop still but our loop is not here our loop is where we start reading again so if we put our loop here so if we say go to loop the program actually jumps where it jumps here where it reads port a is going to go to the next instruction right to port b and then the next instruction go to loop jumps back there read put a right but b is going to do that indefinitely so whenever the state of port a changes the state of part b will be updated we build that and then reload our hex file i'm going to run this a bit faster now because you already know so if i change this see when i change that when i change code a for b also changes when i change the state of point a for b also changes right this is actually where our introduction to big microcontroller assembly language video ends please add more to this program try and do those other sample example where you read for a you invert the value and then you write the results to part b maybe meaning you complement you report a you complement the value you write the value or the result on part b the next thing is use implement a two input and gate you implemented three input and gate you can decide where you want your two inputs and where you want your output now implement and gate and or gate in one program in one big program implement an end gate and or gate so add more things to that program and then see how far you can go i'll see you guys on the next video enjoy