 Okay, just before starting off, I would just like to know how many of you are familiar with microcontrollers or microprocessors, okay, that's good enough. We'll just see, as you know that microprocessors and microcontrollers are there in almost everything that we use in day to day life, from our normal cell phones to even cars, digital cameras, many lifestyle products basically, okay. So these are not only used for, say, consumer products, but also in industrial automation, etc., etc., okay. So what you would have here is a basic introduction to microcontrollers and microprocessors, what they are, how do they function, and some more in-depth sort of an analysis into the AT51. AT51 is a microcontroller which was, say around 30 years back, it was invented by the Intel company, and it is still used in commercial purposes today also. You could say that it is a mother of all microcontrollers. This is the basic, if we see, how the processing family is divided. There are microprocessors, there are microcontrollers, then we have PLDs and FPGAs, okay. In microcontroller, microprocessors, we use in our day-to-day lives in our PCs, etc. They are available in different bit varieties, like 32-bit, 64-bit, etc. The easiest example of a 32-bit would be our Intel processor, Intel Pentium processors, and we do have dual-core processors, core-to-duo processors that are the latest in the Intel family. They are also a 32-bit, but they have two cores. So they can effectively, say, act as a 64-bit processor, okay. And you have the Athlon series of processors by the rival company AMD that is generally a 64-bit processor, Athlon XP, 64-bit, they marketed as a 64-bit processor, okay. And for high-end servers, also Intel produces Xeon, which is again a 64-bit processor. What do you mean by this bits? I say 8-bit, 16-bit, 32-bit, 64-bit, everyone is aware of numbers, right, bits and bytes and megabytes, kilobytes, etc., okay. Computer basically understands only the languages of 1s and 0s at the very basic level. All the digital electronics that today works, they are at 1s and 0s, okay. That's the language they understand, okay. So each bit is a simplest type of data. It can either have two states, either one or a zero. It's not like a decimal number system, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, but it just has two states, either one or a zero. That is the most basic representation of data that we can have today, okay. So when we say that it has a 16-bit processor or a 32-bit processor or a 64-bit processor, what we exactly mean is at a particular point of time, it can process these many bits of information, okay. Say in one instruction, it can process 32 bits of instruction or say 64 bits of instruction. So you can almost directly equate it like the more the number of bits, okay, the faster your processor is. So it's almost like a 64 bit would be doubly faster than the 32-bit processor. But you can't exactly say it would be double, but it would increase proportionally, okay, because there are various other factors also coming into the picture. Then we have the microcontrollers. There is a difference between microprocessors and microcontrollers. They are not the one and the same thing. People do tend to use the same two words interchangeably, but it's not so, okay. Microprocessors generally you would say are just processing units, okay, the ones that we use. And microcontrollers have other features also built into them. They are not just processing units, but they have various features built into them. It would be explained something like microprocessors. You have your PC, okay, in that there is a microprocessor, there is a serial port, a parallel port, various different PCI expansion cards, RAM, your hard disk is your main memory, okay, where all the data is stored, non-volatile memory, even if you switch off the data is still retained, okay. All these things are, they form your entire CPU, right. It's a big unit. But a microcontroller, you can consider it as a complete CPU on a single chip, okay. Because microcontrollers have their own processing unit, that is the ALU, arithmetic logic unit. It has its own memory where you can store your programs, that you could say equate it to your hard disk, okay. Then it has its own RAM and it has various other peripherals like a serial port, parallel outputs, inputs and outputs, timers, counters, interrupts, which we shall see in-depth later on, okay. Any questions in between just let me know. Because the more we are clear with this part the more it would help us with the hands-on sessions that would be occurring later on, okay. So we are clear with the basic what a microcontroller is, right, okay. So we have the microcontrollers. In microcontrollers also we have various bits, okay, starting from 8-bit to the 32-bit microcontrollers, okay. In 8-bit we have 8051, we have AVR series, we have microchips PIC series, okay. These are some of the more famous series then there is always also companies like Xilog, Hitachi, Renesas, etc., manufacturing their own range of microcontrollers, okay. In the 16-bit the famous ones are Texas MSP430, it is a very new entrant into the market but it is quite famous and it is quite catching up with the latest trends. It is a very low-power consumption microcontroller also. There is Motorola's microcontroller, there is Intel's 80196, etc., okay. Generally the 32-bit, generally the leader in 32-bit is the ARM microcontrollers. You might have heard of ARM microcontrollers. They are used in your cell phones like my cell phone here also has an ARM microcontroller in it. Basically this company ARM, okay, they do not actually manufacture microcontrollers but what they actually do is they manufacture the cores, okay. How exactly the microcontrollers should function, okay. The basic intellectual property is developed by them and then they give this rights to different manufacturers, the actual chip manufacturers who buy this cores from there and manufacture their own chips. Intel themselves also manufacture ARM cores, ARM core-based microcontrollers. Intel's X-Scale series of microcontrollers, I think they are used in PlayStation, the remotely, the handheld gaming devices that children usually play with, okay. So, in microcontrollers also we have a wide variety ranging from the 8-bit to the 32-bit. 8-bit processors are generally, microcontrollers generally are used when they are supposed to do only say a specific task. I program the microcontroller to do a specific task and then it goes on doing that task. Say for a vending machine, a chocolate vending machine or at the airport you might see or a Coke vending machine. They have a microcontroller inside them and it's specified and designed to do only one task. You put in a coin, you get a Pepsi or a Coke, whatever, outside, okay. So as microcontrollers in your car, okay, the one which takes care of the cooling, etc., it adjusts to the outside, checks us and temperature outside, okay, adjusts the AC, the air inflow to the ACs, etc., and controls the temperature. Basically they are designed to do only a specific set of tasks. So we, for lower end control systems, we can have 8-bit microcontrollers, 16-bit slightly for higher end and as we said for things like cell phones, portable gaming stations, iPods, the latest iPhone, the hyped iPhone that also has an ARM processor inside it. It's I think manufactured by Samsung or something but it also has an ARM core, okay. So for these high end applications, we can use microcontrollers and 32-bit microcontrollers because they need to process lots of info like your cell phone needs to display colors, color screen, it needs to let you play games, connect to Bluetooth, lots of other peripheral activities also it has to control simultaneously, okay. Then there are PLDs and FPGAs, they are, you could say PLDs are programmable logic devices and FPGAs are field programmable gate arrays. What they are used, field FPGAs in specific, they are used for setting up a particular task chip, okay. Say whenever I say I want to make a new, develop a new microcontroller, say, okay. So what I do is everything today is actually modeled upon the hardware that we want. We want to design a microcontroller with X number of features. I would model all the features on a software. Like whenever I do an analysis, we generally model, make a mathematical model of those. In this we make a computer hardware description language we call it, HDL, okay. We make a hardware description language say whenever I get this input at this particular place or at this particular time, the microcontroller is supposed to do this. Such a complete flowchart, a list of whatever the microcontroller can do is written in the hardware description language is fed into a FPGA, okay. So the FPGA then acts as a microcontroller, okay. So it is used for prototyping purposes, for research purposes and also for variety of other reasons, okay. So these are basically the type of controls that we have based on digital controls, okay. And there are other features also built into microcontrollers like ADCs, analog to digital converter. Because in the real world, the actual signals, they are all analog, they are not digital. My pressure sensor won't tell me that the pressure is high or low. It won't say 1 or 0. It will basically say it is say 16 kilopascals, 1 kilopascal or whatever, okay. Something like that. It would be an analog value. It will never be a digital value. So to convert that into digital form, I would need a ADC, analog to digital converter. That is also one specific feature of some microcontrollers. Like I said, yeah, yeah, nice for pointing it out. Basically they can be divided into SISC and RISC. SISC means complex instruction set computing and RISC means reduced instruction set computing, okay. The difference between those is in a complex instruction set computing, you have lots of instructions, okay. Say if I want to multiply two numbers in a SISC based microcontroller, what I would do is I would say MUL, say just take it as an instruction, MUL A and B, okay. So it would multiply the two numbers. Say 2 into 7. So it would multiply 2 into 7 directly. But in a RISC machine, I won't have many number of instructions. The instruction set is reduced, okay. So I say in a SISC if I have say 1 in 100 instructions, I might have just 25 or 35 in a RISC machine. So when I want to multiply two numbers, what I would be doing is I would be adding it so many times. Say 2 into 7, so I would be adding 2 to itself 7 times, okay. Now the advantage is that these RISC machines have independent lanes. Say if I want to add, it is directly added by a hardware inside the microcontroller, okay. The ALU has independent channels for separate instructions. So the execution is faster. In RISC, yeah, say there is an add instruction, okay. I want to, my basic aim is to multiply 2 into 7, okay. So in a SISC, what it would do is it has an instruction say multiply. But internally what it does is it stores 2 and 7, okay. And it adds 2 to 7, 2 to itself 7 times. But it has to automatically say the ALU has to know that, okay, 1 it has added, it has become 4 at the next time, okay. It has to control both the things. But in a RISC, what you need to do is you need to say specify 7 instructions, say add 2 to 2, add 2 to 2, again 7 times, okay. So what this would have to do is it has independent lanes, independent hardware entities for carrying out add, subtract and certain other functions, okay. So since it's a hardware job, it would be done much faster. And there is another concept in this that is called as pipelining. So basic whenever you need to execute an instruction, the basic fundamental is you have to fetch the instruction from the memory, decode the instruction first as in what the instruction is saying, what has to be done and then execute it. So in pipelining what happens is when I have executing one instruction, the other instruction has already been fetched and decoded, okay. So basically you could say it's a parallel processing kind of a thing at a very basic level, okay. So that also makes it a much faster process, reduced, yeah. Nowadays there is another concept also called as ZISC, that is zero instruction set computing, okay. That basically is based upon more of comparison based systems as in you need to teach the system the inputs and accordingly it will give the output, okay. Say I have a ZISC machine with 35 instructions. So you can say I want to make color recognizer with a ZISC machine, okay. So what I will do is I will say hold a red color in my hand, the camera proceeds it and some processing goes on and the final output should be given is I would say one, okay. So I will have to teach, I can teach 35 instructions like that to a ZISC machine. Say for a green I would say two, the output will be two. The gray it would be say five, something like that. So it's a very fast machine because it's a one to one correspondence between the input and the output, okay. But there are obvious limitations, okay as a number of say things that can be compared or something like that and it's still in development means it's not completely used everywhere also. But it's a still ongoing process, okay DSPs are digital signal processors, okay. They have specialized units as I said in this there are specialized units for add etc. In this there are specified units for say making a Fourier, getting a Fourier transform or getting a Laplace transform something like that. More related to the signal processing field. They are faster than those ways. I won't be using a DSP for normal day to day say like a vending machine application. But I would be using it for data acquisition cards, PCBs, CROs, etc, etc, okay. These are some various vendors, manufacturers that there are in today's market, Atmel, FreeScale, Hitachi, Intel, lots of them. And they have their own range of microcontrollers also and as similar to ARM, there are AT51 cores that are manufactured by various manufacturer. AT51 not only is manufactured by Intel but it is manufactured by Philips, analog devices. I think MaximDallas also manufactures them, Atmel manufactures them. Many other manufacturers also manufacture them. We will be using for our sessions of Philips microcontroller which is called the 89V51 RD2. At any 5 and RD2 basically we are using because one it is very closely related to the academics of today okay because and second it has a long legacy. It has been around for past 20-25 years okay. So there is lot of literature available on it. Good books, literature on the net, many projects, etc, etc. Lot of support available on it. In spite it being so old it is still an industry you could say a standard because many industries still use them for their control applications. Then it is very easy to use, very good for a beginner as a beginners level microcontroller. Very simple to understand and easy to use okay. So we will be using in 8051 core based processor the Philips P89V51 RD2 for our use okay. That is just a numbering sequence that they have, there are LV51 RD2, there are C51 RD2 that is by the manufacturer itself. I am not that sure of, but it has flash memory and that is another feature that makes it a very attractive choice for us, the inbuilt bootloader okay. This is a basic architecture of the 8051 okay. What we have on the top is a CPU okay, the ALU basically that does all the instruction crunching, etc in the instruction come what is supposed to be done that is the main central controller okay. Then we have four IO ports, IO means input output ports port 0, 1, 2 and 3 and there is just a convention that whenever we are in the digital world we are speaking about digital electronics we generally start the numbering from 0 okay. It will always 0, 1, 2, 3 it will not be 1, 2, 3, 4 okay. So we have four ports here, port 0, 1, 2 and 3 all are 8 bit ports okay. So I have basically 32 IO lines okay, I can connect say up to 32 single bit devices with this or I can connect say 4 8 bit devices okay. Then there is 64 kb of flash okay, this flash memory is a non-volatile memory okay. So even if you switch off your controller the contents of the flash is still intact okay. So what we will be using for storing our code, the main program the main programs that we will be writing will be stored in the flash memory okay. Then we have a RAM, why is RAM used for, generally it is used for storing variables, data, etc, etc okay and it is a volatile memory, volatile by volatile means when we switch off the contents are lost okay. Nowadays as you know PCs are say 512 MB RAM has become a standard nowadays, one GBO also easily many PCs do have even in institutes etc. But here if you see a microcontroller the flash that is the ROM is only 64 kb but whereas in our PCs today we have 40 GB, 80 GB, etc okay that is because obviously the task to be done by microcontroller is of far less magnitude, we do not need a microcontroller to play MP3s okay, the 8051 they did not play MP3s or say play movies or do MATLAB or AutoCAD or any such thing. So it does not need that kind of a memory, it does not need much of a RAM also because it does not need to access so many variables or calculate on so many variables at a given point of time. So 1 kb of RAM and 64 kb of flash are more or less sufficient for our use okay. Then we have different peripherals, they are timers, counters, there is a UART, there is an SPI okay, there is a crystal oscillator unit, the oscillator unit is a you could say a reference unit on which the microcontroller runs okay. Our microcontroller board will be running at 11.0592 MHz okay, this you must have heard Intel processors running at 3.4 GHz, you know they have proud themselves feeling that okay we have created a processor running at this GHz or something like that okay. Basically there is a reference clock that means at each clock cycle it will execute one instruction okay, it is basically a reference to run itself, the crystal oscillator okay. So we will be running at 11.0592 MHz, why such a peculiar number also we will come to know later. Then we have a UART, UART basically is a unit to convert your data to serial communication okay to serial data, parallel data to serial data, this is just a basic introduction, we will be covering all the sections in much detail later on okay yeah, yeah we can have a robot which can solve a maze using this also. Now we are not concerned with what we are going to build, we are concerned yes, why not all autonomous robots need to be have that level of say control or computation okay we can build and even a robot which follows the line as an autonomous robot or the robot which say a origin obstacle is an autonomous robot or robot which solves a maze is an autonomous robot okay and obviously NASA's mass rover that is an autonomous robot. So basically we have different levels obviously I cannot have a mass rover within 1851 that is a given autonomous is something that can take input from the surroundings and act accordingly can I say that sorry I did not get you yeah, autonomous means control automatically okay and the level of control can vary, the level of control can vary this would be quite a you could say a basic level that we would be covering basic to an intermediate level that we would be covering during the workshop and using the 1851 obviously you could go on to a higher level of control also, so we have various other peripherals also like SPI that is also another serial protocol okay for sending transferring data in serial is generally used for communication between two microcontrollers the SPI interface okay then we have Timer counters we have the PCA that will be coming to later, so basically the main components you could say are the CPU, the flash, the RAM, the ports and the peripheral devices that are inbuilt okay like if just taking going back to the PC again that we have in this a UART is built in in your motherboard if you see serial port near the serial port you might see a small chip called 8250 okay that is basically the serial port controller in your motherboard okay so the processor does not have it built in but it is a separate IC on the motherboard okay this is somewhat like a very very basic minimum minimalistic circuit that is required to get the 1851 up and running okay you can see here some capacitors a crystal okay and a resistor okay the crystal is connected between two pins XTAL1 and XTAL2 that are the crystal pins then at the RST pin you will see a resistor capacitor arrangement okay that is there specifically so that even when you switch the microcontroller on it directly does not start executing okay right see that when the power is first switched on what happens is the capacitor is shorted okay when you directly switch on okay so the capacitor is in a state of short okay so continuous high voltages on the RST pin that is a reset pin okay so when the capacitor starts charging at that time okay when the capacitor is fully charged what happens is acts as an open circuit a fully charged capacitor acts as an open circuit okay at that time if you see the RST pin is grounded okay so till the charging time of the capacitor the microcontroller is in a state of reset okay why this is done is because directly when we switch on the oscillator can't begin to oscillate at such a high frequency okay it takes some time to it to you know stabilize the oscillations okay and if in that period if you start the microcontroller and it starts running the programs there might be some errors because the reference itself is not correct okay so that's why we have we keep the microcontroller in a reset state for some part of time and then we start the execution okay all these values of capacitors resistor etc everything we get on in the data sheet they have specified that for minimum one millisecond it should be kept on a reset state okay so according to that the values have been calculated so there is a you could say a basic circuit that you would need to get an 8051 up and running okay these are some of the limitations that we have for the 8051 there is no analog to digital converter built in okay there is no i square c that is also a serial protocol quite widely used today there is a very limited amount of PWM PWM is pulse width modulation that is also used for a lot of control applications say a very basic example could be controlling a speed of a motor okay depending upon the duty cycle of the wave say if I have 100% duty cycle wave what is it it is a DC level right say I am having a 5 volt motor okay and I am giving a 5 volt DC to it it runs at maximum speed but suppose I give a square wave that is at 50% duty cycle so the average value of the signal that is applied would be 2.5 volts so the speed would be reduced okay so there is a application one of the applications of PWM we have a certain limited amount of PWM in the 8051 okay basically just a end of the topic small introductory session anything regards to this if you have any queries you can ask now what is the move on regarding the 8051 basic 8051 basic microcontrollers and microprocessors in general if you say fine with this single microcontroller can be used for these applications by using a different program yeah it can be and microcontroller has to be designed for various applications separately no no microcontroller is available you have to program it for different applications yeah yes I can use the same ARM processor for say having an MP3 player or as a cell phone because in nowadays if you see the cell phones it has an MP3 player it has a camera it has it makes calls obviously and lots of other things also we are just going to some more details of the 8051 as a microcontroller you know that we have chosen the 8051 and why we have chosen the 8051 okay so this was just a recap as in what we had seen the basic block diagram of the 8051 we will go into the individual functional parts in slightly more detail now this would be something your pin configuration would look like this is your 40 pin IC and these are the different ports and the pins that are there there are four ports as you see 0 1 2 and 3 there are various other pins also you could see that in a single pin itself you have you can see two functions say if you see p 0.0 slash ad 0 or something like that isn't it in p3 underscore 0 it's also rxd isn't it that is called something as pin multiplexing okay basically we are giving two functions to a single pin and why we are doing that is obviously ICs are expensive to manufacture the first stage okay the more the silicon you use the size of the IC becomes more and there are different factors coming into consideration like heat dissipation etc etc okay so for that we need to keep it compact and if say I add individual pins for each of this I would have around a 75 pin micro controller so it is much difficult to you know actually manufacture the device manufacture devices based on that okay so basically I would be using one pin for two functions okay this is just an example as the p3 it's highly multiplexed there are different things like rxd txd I can just tell you that it's a it's for the serial port the uart okay receive and transmit there are different t1 t0 int0 int1 rd w bar we'll come to what their functions are in a while okay an IO port what is basically an IO port in microcontroller there are things called as registers okay what these registers basically they are pieces of memory okay not in the flash or the ramp but independent place in the microcontroller itself okay so port also you could consider it as a specific register say p0 register that is a port 0 register okay so whenever I write everyone is familiar or say accustomed to binary to hexadecimal convergence binary to we have just given you a handout you could brush up and we don't need it right now but we would be needing it in the programming section okay you just go through it and you would get to know how to convert binary to hexadecimal extra binary to decimal etc etc okay so basically binary is the basic machine language so whenever I say if I write all 1s to it okay say I write all 1s to the port that is I am writing all 1s to the port 0 register okay and I am writing 1 means I am writing a you could say 5 volts basically high means 1 means 5 volts and 0 means 0 volts okay so whenever I check using a multimeter if I check how much the voltage is at there on the port 0 pin okay I would get 5 volts if I have written p0 equal to high okay so basically ports we will use to interface various devices okay for any device obviously you need a path from which you can access the device okay that is your that path is your port okay we will be at interfacing things like LCD okay which is an output device on which you can see things even on the displayed on the LCD we will interface LEDs we will interface switches switches have an input device okay we interface a buzzer we interface motor drivers we have already interfaced we will be interfacing the serial port etc etc okay so port basically you can say is a connection to the other devices around the micro controller okay as in my in a cell phone again if I take an example of a cell phone the I have to interface the keypad okay that is your input okay that would be done through one port the LCD of the cell phone would have to be interfaced that would be using something else say a bluetooth might not be directly on the micro controller itself but it might be a separate module that is attached okay that also I need to interface with the main micro controller okay so similar things I can interface using ports so these are some kind of a some kind of devices sensors etc that you can interface with the port you are basically the data transfer can be divided into basic two types okay you can say a parallel data transmission and serial data transmission okay now what are the pluses and minuses of either okay parallel obviously your say if it is a 8 bit data bus okay we will say 1 byte we are sending at one time so we have if I want to transmit a data in parallel how many wires would I need 8 okay but if I want to do that in a serial I would need 2 if I want to receive and transmit or if I just want to transmit I would just need 1 okay so basically what I am doing here is I am saving on number of lines but I am sacrificing slightly on the speed because parallel I can transmit 1 byte at once in serial I will be if I want to transmit a byte I would have transmit bit by bit okay I will send the one bit then the next bit and after say 8 cycles the complete byte will be transferred okay so in effect I am going for slightly lower speed but I am saving on costs if I have to transfer data say 1 kilometer across okay a serial link would provide a much better interface than a parallel link because if I want to lay 8 kilometers of cable that is 8 wires the cost would almost increase 8 times right so I am saving on the cost all of major data transfer today is in serial there are LAN cables I square C serial RS 485 is a standard used in many industries for then there is CAN controller automatic automotive network okay there are many protocols today which all have serial and wireless data okay that is serial okay otherwise if I had to send wireless data parallelly I would need say 8 different frequencies to send it okay and that would be quite a difficult task to do and it would be difficult to design a receiver and a transmitter for the same okay so basically I will use serial transmission here okay so what the you are basically does is okay so it takes the data all the data being transmitted inside the microcontroller is in parallel because the distances are short and I need high speeds okay say data between the UART and the CPU or between one register and the CPU okay all is in parallel okay but when I want to transfer it to somewhere outside say I will use this serial interface that is a UART so what the UART block does is it takes the data in parallel okay say it adds a start and a stop bit and then sends it UART here stands for universal asynchronous receiver transmitter the word asynchronous here is of a bit special importance okay our controller is running at a particular speed okay and if my receiver is not running at the same speed will I be able to receive it properly no because there would be some error because say I would send it and it would say not receive the first byte but it would receive the second third fourth till the seventh byte or seventh bit okay so that can be a problem so here the asynchronous word comes in okay and I also have synchronous protocols in which I have a dedicated clock line okay the clock from my CPU goes to the receiver also okay so there the data is synchronized here it is asynchronous so what I need to do is I need to add a start and a stop bit okay it is something like we have all seen in movies in army movies especially whenever the person in field calls the base alpha come in alpha come in that is some sort you would say a start bit okay it is indicating that I am going to send some transmission send some valid data okay then after the start bit a data packet would be there okay and then it would be there will be a stop bit something like the over and out signal that a army man gives to the base over and out okay there is a stop bit my data transmission has ended okay so basically in this kind of protocol you don't need a other dedicated clock line so that is one of the advantages okay so we are using for UART for these purposes they are different here comes the 11.0592 megahertz okay the crystal that we will be using basically it's a weird number 11.0592 megahertz it is used because there are standard baud rate set today baud rate is the how in what speed I would be sending the data okay it is like 400 bits per second 9600 bits from the 4800 bps something that you might have heard in the olden day modems 33.6 kbps modem it was 56.6 kbps modem okay those are all standard baud rates that was standardized by IBM okay still now those are in those are a standard platform so this 11.0592 megahertz can accurately generate any of these baud rates okay let's say I go in for 12 megahertz crystal it won't generate the exact baud rate but it would generate there will be slightly say one or two percent error while generating the baud rate but this 11.0592 megahertz would generate all the baud rates perfectly okay so I will be using this crystal clear with this the serial communication part okay we will come to UART sorry interrupts okay these are one of a very important feature of today's microcontrollers microprocessors etc things run on them basically interrupts as you can see they can occur any time okay and they are higher priority than the operation that is being currently performed okay example of an interrupt can be your control all del on your pc anything you are doing if you as a control all del it shows your task manager or whatever on windows okay your reset button that is also an interrupt okay so it is what exactly happens is it is something like your programming is being executed okay uh suddenly an interrupt comes okay what the CPU does is does not just go to the interrupt or jump but it stores specific data related to that program in a particular space goes to the interrupt does some work regarding the interrupt and comes back to the same space where it was executing it can be something like you are reading a book okay a phone call comes a very important phone call say from a boss or something a phone call comes and you need to attend it what you do is you place a bookmark in the book close it attend the phone call come back okay go back to where the bookmark was placed and start reading from there so here your normal execution was your reading of the book okay the interrupt was the phone call uh talking over the phone was what you did when the interrupt occurred that thing is called as ISR interrupt service routine okay a specific set of functions that you do when an interrupt is called okay now storing the bookmark okay that was a very important aspect here okay because when it comes after the interrupt it should know what it was executing previously right otherwise it would start executing something else so what it does is the CPU it stores some specific data as in what instruction it was executing what data it was working on what were the status of some registers some important registers etc it will store in a specific piece of memory okay go to the ISR do the functions there and it will again retrieve those pieces of data from the memory and then start executing again that is again you go back open the book where your bookmark was placed and continue your normal operation now where can interrupts be used I just these are some kind of interrupts that are there in the 8051 we will come to these this is a basic representation or the sorry these are just a general classification okay how interrupts can occur you can classify them into either hardware or software interrupts okay the control all del would be a kind of a software interrupt the reset can be a kind of hardware interrupt okay because it directly deals with the hardware okay control all del the interrupt occurs because of windows okay it is not a hardware interrupt okay and in if you have seen 8086 or 8085 in fact in modern day process also there is a divide by zero interrupt what is divide by zero in mathematics basically it's undefined okay so even your CPU doesn't know what it is okay so that is also capable of generating an interrupt okay and it also divided into two categories called maskable and non-maskable okay maskable means you can keep it disabled okay even if the interrupt occurs nothing will happen okay and non-maskable means I can't disable it your reset is an example of non-maskable interrupt whatever happens if you press the reset the computer will reboot okay the controller will restart so these are basic classification of interrupts and this is what occurs just what we talked about okay the program is getting executed interrupt occurs there is a ISR which is being serviced things are stored okay the things stored are called as a context okay I will be saving the context the CPU will be saving the context and while coming back it will retrieve the context huh yeah it's in the stack it's in a piece of memory location in the RAM which are called as a stack okay the next unit that we were talking we would be talking about is timer and context just before this we will just come back to the application of interrupts where interrupts can be used okay some applications can be you have a car modern day cars there are lots of micro controllers okay latest Mercedes I heard had around 60 micro controllers okay they do all sort of stuff from looking at the temperature that the brakes are okay regulating the temperature inside according to a temperature outside injecting fuel into the engine okay fuel injection okay that is also my microprocessor controlled nowadays then they are controlling the shift between two wheel four wheel drive automatic transmission lots of other lots of other areas also it is used power windows okay deployment of airbags etc that is also that can also be micro controller based so suppose you are moving in a car okay which has a micro controller which is doing 10 different tasks okay and you say RAM across a tree you dash into a tree so ideally what should happen is the airbag should be deployed okay but if the micro controller which deploys the airbag is performing some other task it won't be able to deploy the airbag okay so say the sensors with sensor crash will be connected to a interrupt okay so interrupt is a asynchronous input that can occur at any time and it has a higher priority than the program that is currently running okay so whenever my sensor detects a collision it will give an interrupt okay and I will the ISR in the ISR it will be written deploy airbags okay so the airbags will be deployed and the other processing that is there would be stop basically okay so the airbag should be deployed and the passenger would be safe that can be one example of an interrupt and they can obviously be tens and millions of examples where interrupts are used today moving on to timers and contests just before this we will take a look at the port three once again we had four pins there t0 t1 int0 and ynt1 okay int0 and int1 here are the external interrupt pins okay whenever I get a high to low transition on the pins that is whenever the voltage on the pins goes from 5 to 0 okay I will get an interrupt so basically the sensor that was detecting a collision would be connected to that pin okay so whenever the collision is detected an interrupt would be generated and the ISR would be executed and the t0 t1 pins that are there are used for the timer and counter unit okay in the timer and counter unit is the one and the same unit but it can basically do two different tasks either it can act as a timer or it can act as a counter okay the basic fundamental here is that it has a 16 bit register okay 16 bits means it can have a value from 0 to 2 raised to 16 right as 16 pieces 16 bits each can have a value of either 0 or 1 okay so the maximum value that can be there in the 16 bits is 2 raised to 16 huh 2 raised to 16 the contents can be 2 raised to 16 but since we are starting from 0 we will consider 0 also as a state so the maximum value would be 2 raised to 16 minus 1 okay so 2 raised to 16 is 65536 and minus 1 would be 65535 okay so the values that can I can have is 0 to 65535 in the register in a 16 bit register so basically what a timer does we will first see timer and then the counter unit timer would be counting the internal clock pulses now 8051 has a particular thing about it each instruction that it executes takes 12 clock cycles okay each instruction to execute it takes a minimum of 12 clock cycles there are different microcontrollers available today which execute an instruction even in one clock cycle okay but the 8051 slightly from a older generation okay is a bit slow and it executes an instruction for every 12 clock cycles so we have two things here one is a machine cycle and one is a instruction cycle machine cycle would be the 11.50 finite 2 megahertz that is each pulse is a machine cycle and the instruction cycle would be 12 such pulses okay 12 such cycles would be the instruction cycle so what the timer does is for every instruction cycle it increments the register by 1 okay the timer increments the register by 1 so what we can have is after 65535 into 12 machine cycles we can have the 16 bit register full okay all the 0 all the bits would be 1 like if this is a register 16 bit register this is 0th bit as we in digital language we always go from 0 so 0 to 15 we have 16 bits okay so whenever all the bits go 1 okay that is the 2 raise to 16 minus 1 the value is 65535 okay now when another instruction cycle happens okay so what will the timer do it can't increment by 1 now okay because the 16 bit register is a memory space it is already full okay it is something like your timer is like a bucket and a leaking tap is there okay drop by drop it falls inside the bucket and when the bucket becomes full what happens it overflows similarly the timer also overflows okay we call the next state after this as an overflow okay so when the timer overflows what happens is it has a capability to generate an interrupt okay we can have a interrupt at that point of time okay so now what can we do with this what can we do with an interrupt generated by a timer we can do timed events okay we know the frequency that it is running at so we know that 65535 would be attained the count would be attained after the particular period of time okay so after certain periods of time I can do particular functions okay say in a data acquisition system I want to sample at a specific rate the incoming signal at a specific rate so this the sampling frequency the sampling moment I can set by a timer yes to defend so for any kind of timing operation I can use this timer okay so if I now every time I wouldn't want the timer to overflow at only after 65535 okay say I want to do a particular task in 5000 counts okay so what will I do okay I can't say that a timer has to overflow at 5000 okay I can't cut the bucket okay cut the bucket at half and say okay it will overflow after this time okay so what I do is I already keep it half filled okay so what I will do is if I want to make it overflow at say 5000 counts I will fill it with 65535 minus 5000 that would be 600535 okay so when it goes 5000 counts above and at the next count it will overflow okay so I get overflow at every 5000 counts okay so in this way I can control the time at which it overflows and I get an interrupt and I can do whatever I want okay okay now the counter is has a similar basic working but is slightly different what happens is it does not count the internal clock pulses okay now the clock pulse is generated by the oscillator but it counts external events okay external events happening on the T0 and T1 pins that we have have on P3 okay every transition from a height to low on T0 and T1 pin would increment the counter by 1 again it uses the same 16 bit register okay the all the things used are the same just that the mode of working is a different okay so counter whenever say I count 685535 again the counter will overflow and I can do something say very simple example where this could be used is say I have a bottle manufacturing plant or say it is a Pepsi plant okay say a bottle comes in it fits the cap fills and fits the cap I just have a sensor over here the bottle passes by I have a receiver here the bottle passes by and I get a pulse here okay whenever a bottle passes by okay so this receiver I can connect to a counter okay so whenever 65535 bottles are passed I will say one batch is over or say I want a batch for 5000 bottles so what would I do I would just load the value 653535 in the beginning and whenever 5000 bottles pass I have one batch has passed an overflow interrupt occurs I will just say say what the ISR does is it closes the crate okay there is a there is a crate in which the bottles are packed okay after 5000 bottles come into the crate another arm or something has to come and pack the crate so that specific action I would write in the interrupt service routine okay so in this way I can use the counter counter can also be used in very interesting application would be a cricket match for counting the number of balls that has been bold by the umpire okay six balls okay say it will give a I load the count as 65529 okay so whenever 65 whenever six balls passes say it will give me a beep okay in the interrupt I will write beep okay so the umpire need not keep track okay has six balls gotten over etc whenever it beeps he will say okay it has beep the six balls are over and an over is finished so counter can be used for a variety of applications just how counters work these are also independent units okay the CPU doesn't have anything to do with the timer counter only when it overflows the CPU has to execute certain instructions which is written in the ISR okay so all I need to do is when I initializes timer I just start the timer the timer start begins counting okay and when it overflows it sets a interrupt flag and interrupt is given and if the interrupt is masked or not masked depending upon that the action will be taken this is a maskable interrupt so if it's masked I won't do anything if it's unmasked okay or not masked I would go to the ISR to whatever the routine has been written to do and come back to the normal program okay now we have certain modes in this timer okay we'll see what those modes are basically just a 16 bit timer is quite bland it's such a powerful micro controller though it has to have more features three modes okay a 13 bit mode now what does this do as I say it's a 16 bit register okay so it is made up of two 8 bit register you can say okay a TH that is a timer high register and timer low register okay so in the 13 bit mode in the TL there are only 5 bits and the TH there are 8 bits right so what happens exactly in this what is 2 raise to 5 it is 32 right 2 raise to 5 is 32 so in the 5 bits I can have up to 32 32 stage basically so what in this what happens is it's a kind of a pre-scale timer okay pre-scale by 32 now what I mean by pre-scale is I'm dividing the timing frequency by a particular number okay so in this the 8 bits of TH that are there okay one would be incremented only after 32 counts okay once the 5 bits of TL have overflowed only then one would be incremented here okay so basically I can say that the frequency has been divided by 32 that is why every 32 instruction cycles only I get one count yes okay so basically I get a slower timer okay I can use this for more amount of timing application which needs a slower timer okay then we have the normal 16 bit mode that we were all all the time discussing okay and the normal 16 bit mode there is a catch it won't say you need to count 5000 every time okay but after the overflow again it will start counting from 0 okay but we don't want that to happen right so what we would have needed to in the 16 bit mode was in the interrupt service routine we will again the first instruction itself I will write load the counter with 600535 okay so I will prevent that but say suppose I want to do this automatically so for that I have a 8 bit auto reload mode okay but the limitation is it's only a 8 bit timer it's not a complete 16 bit so what happens is the th is your actual timer which increments at every instruction cycle and the TL holds the load value okay so 2 raised to 8 is 256 that is I can have a maximum of 0 to 255 okay suppose I need say a timer overflow at every 100 counts okay every 100 counts so what I would be doing is I would be loading 155 in the TL okay and in the start I will be loading 150 and th also once it goes to 255 it will overflow go into the ISR do what is supposed to do and TL will automatically load the 155 to th so next time it will automatically start incrementing from that particular location okay that particular number it need not start again from zero okay so that is the advantage of auto reload mode we had a question what watchdog timer is worth okay till that then we are clear that is the basic functionality and the inter the peripheral part of the micro controller we will be using them and when we use them the applications would get more clear that okay that is clear for everyone any doubts with that the basic part okay so this is a watchdog timer which one it will automatically come to 0 yeah that is a property of the hardware itself once it over for its comes to 0 yes it is incremented once 12 instruction cycles in this in the 13 bit what will happen is first the 5 bit register would be incremented that would be incrementing at every 12 clock cycles but only when all the 5 bits in this have gone 1 that is 32 okay 2 raise to 5 32 at that time this would increment okay so basically 12 into 32 clock cycles after 12 into 32 clock cycles only one bit would increment in the actual timer okay watchdog time how often have we come across that we are running some application and a pc hangs okay we are running say an mp3 some simulation software etc etc etc and certainly our windows which is very famous for certain things hangs okay so to get out of such a condition what is basically a hang condition maybe it is executing in it is the processor is stuck in some infinite loop so doing a particular task again and again it is unable to come out of that loop okay that can be a cause of a hang okay so how to come out of that condition so for that purpose we can use this watchdog timer what this timer is it's a normal timer okay yes separate from these two timers okay they are independent and this is independent it is also a 16 bit register okay so what happens is when this overflows it has only a single ISR okay that it will reset the microcontroller okay it doesn't you can't specify anything else in its ISR it will always reset the microcontroller if it overflows okay so now our motive in our program should be we have to stop it from resetting at some part of time otherwise if I just keep the watchdog timer enabled it will keep on resetting my microcontroller at 65,535 okay I don't want that to happen but how does this save from a hang condition see suppose my microcontroller goes into an infinite loop somewhere it is unable to come out of it okay so I need to manually reset it but if I enable the watchdog timer since it is always resetting at 65,535 it is obvious that it will come out of the loop at one point of the time right because watchdog timer is a independent unit okay it is not again dependent on the CPU so whenever it overflows it will come back that is it will reset and it will start again okay so in my program what I would do is say I will reset the I will give a condition at every particular instance say at 60,000 counts to reset the watchdog timer okay not reset the microcontroller but reset the watchdog timer so it goes back to 0 and starts counting okay but suppose it goes into a infinite loop okay where this particular instruction is not able to execute there is a execute the instruction of sorry the execution of reset watchdog timer instruction is not executed okay so the watchdog timer will overflow since it is not getting reset after it will overflow and it will reset the microcontroller instead okay so what will happen is your program starts executing normally from the starting again there is a basic function of a watchdog timer so it can be summed up as you have to reset the watchdog timer to prevent your microcontroller from getting reset okay but this is a very handy tool used generally in lot of real-time applications where microcontrollers are used to prevent it from going into some hang conditions say for an ECG or something is also running on microcontroller maybe a more powerful microcontroller but if suppose that goes into a hang condition it is a fatal thing okay it might affect the person attached to it very badly okay so for that purpose a watchdog timer can be used say it will save it from going into a hang condition or a lock condition okay so this is a basic session that we have on microcontrollers okay I will just touch up some parts on programming as in why we would use C what are the advantages disadvantages how we will go about the programming part okay there are basic syntaxes in C like if loop, while loop, for loop, initialization of variables okay very basic things that is what we would be needing in the further coming practical sessions okay very basic stuff it is a lot of robot pictures out there because just we are into robotics so usually have such pictures on our slides okay so you know about the basic hardware okay the microcontroller that we are going to use okay but all the intelligence you have a brain now but it is not yet taught okay you would say you have a very it's not fertile you know it doesn't have any knowledge it doesn't have any learning capability or it doesn't know what to do okay it's not un-channelized the channelizing would be done by our program what we would be writing into the microcontroller okay so all the functionalities only you can harness with the program okay so that's what we'll just touch upon in this specific presentation we have two options to program a microcontroller either assembly or C what is assembly language it is a very very very low level language it's directly the language of the microcontroller okay that is the language of zeros and ones okay so in that you have different instructions say 00001111 would be one instruction 10101010 would be another instruction things like that okay this is a very low level or the hardware level programming okay the pros of this would be you are directly communicating with the hardware that is you are directly you know harnessing the entire power of the hardware so you can write very efficient codes faster codes and smaller codes because you're directly saying put this into this register access this or something like that okay but the cons would be it's cumbersome to write okay it's difficult to write it's difficult to debug okay you can't easily say where it has gone wrong even if a small instruction goes along you it is tough to find out that it does there is a mistake at this particular place for each processor that you begin to learn okay or each microcontroller that you learn the assembly language instruction set is different okay so it's not cross platform an assembly language for a PIC microcontroller would be different for an 8051 would be different for an Atmel AVR would be different for an ARM would be different okay so for anytime you migrate to a different microcontroller you need to learn the assembly language okay but on the other hand we have C okay it is cross platform okay there are C compilers available for all the microcontrollers almost okay it is easy to write okay very English like something like if this sensor detects okay do this okay it's very English like language C it's very logical to use okay in this it's where something like even if you want to write if sensor detects do this you need to write 100000 something it's a very cumbersome language to write okay you do have mnemonics for this also but it's still a cumbersome process so in C it's very logical and it is easy to debug okay and another thing is it is whenever another person sees your code he finds it readable too okay he doesn't need to know exactly what you have what you plan to write but seeing that he can understand the code more easily okay and C is not a very high level language and neither a low level language it is a mid level language okay high level language has a lots of abstractions but C can directly communicate in bits and bytes also okay so it you could say it has the best features of assembly language as well as high level languages okay and C being around for a lot of time okay it also has a very wide support available for it even if you have you're stuck up somewhere you could just google it up and you would get lots of resources on it where you're stuck up why you're stuck up this might be the mistake etc etc okay and C is cross platform you don't see on linux windows etc etc and for each micro control as I said a compiler is available okay so V for all the purposes would be using C okay some features of C mid level language long history easy logical syntax etc etc so would be whatever we discussed just a summary of those programming here we'll use a compiler we might use we actually plan to use a compiler called keel but we'll be using a compiler called sdcc okay this sdcc is generally a command line compiler it's a free open source compiler available but it's a command line compiler okay so it's slightly difficult to use but we have just modified it a bit we have developed a GUI for it at our own place so it would be it would have all the you could say again a balance of all the features between a commercial compiler like keel and a open source compiler like sdcc okay so you'll be using a compiler which is developed it's called tric it will you will see it when you use it okay very easy to use interface a graphical user interface not even like turbo cc it would be more like a visual c or something you can create c files at multiple c files to projects okay not necessarily that you add only one c file you have generally header files for everything okay so many different features are very easy to use uh IDE this is a basic process of how we go about programming a microcontroller the actual process okay for what do we do is first we write a program in c okay now the machine it doesn't understand c it understands only zeros and ones so it needs to be converted into a assembly language file okay and now since i am working on a pc and writing a program for the microcontroller i won't be using a compiler i would be using something called as cross compiler okay generally turbo c visual c ball line c etc etc are compilers because this code i would be writing in them would be executed on the pc itself okay but in this i am writing it on one platform that is on the pc but executing it on the 8051 microcontroller okay so it is called a cross compiler okay so i compile it in the assembly okay then convert it into a hex format the hex format is the actual format that is zeros and ones and then i burn it into the microcontroller the process of writing into the flash is called burning okay like we burn a cd we also burn data into flash okay there are two types in which we can program okay one is by a hardware programmer and one is by a software programmer in system programming as it is written means you don't need to take the microcontroller out of your system and program the chip separately okay using a separate hardware you keep your microcontroller as it is on the system and just insert a cable from the pc and program it uh generally you would see in labs etc there are programmer kits available which you have physically have to take the microcontroller off from your actual board wherever you are using it put it into a burner it will burn it and again insert it okay in system program where a programmer saves this number of steps it is easier for rapid product development and it's a faster way okay so and this bootloader one of the reasons why we choose phillips89v51 rd2 makes the in system programming in system programming a very easy task okay now what the bootloader is this you can see is a flash programmer a hardware based programmer okay you take the chip out put it into a programmer burn it and what we will be having here is a bootloader the bootloader software okay it's a very small piece of code around 1 kb in size that the particular place in a memory location what happens when you start the microcontroller okay when you start the microcontroller the bootloader checks whether some whether an external pc is trying to program it okay it checks the rxd txd pin the microcontroller uses the ur for programming itself also okay that is another function for which the microcontroller uses the ur okay so when the microcontroller is switched on first it checks for 400 milliseconds or so whether someone is trying to program it okay if it is if someone is trying to program it the that is a pc the pc actually sends a particular string of data okay and if this data matches with the programming sequence then it begins programming or else if for a particular period no one in the string is not matching it will start executing the code which was there in the microcontroller before okay the bootloader you can say is something like the bios you have in the pc okay for starting up like even if you have not loaded windows or anything the pc will still boot okay and say ask for something to put as in it will ask for say install install cd or something else okay so this bootloader section will check if something is trying to program it if it is programming it will take it from the pc and start writing into the flash okay so this makes our task very easier okay in system programming generally makes tasks very easy okay so basically the bootloader is used for initialization I could say and that is major reason why we use the 89v51 rd2 otherwise 8051s there are lots and lots of different variants of 8051s available but this is a added feature in this 89v51 rd2 this tool flash magic okay this is a tool that will help us in programming the microcontroller okay you can see the various settings over there like which compute to select what would be the board rate you can see 9600 written okay then there is the device select this flash magic can program lots of devices we will select our device that is 89v51 rd2 then there is a section where you enter the file you can see a browse button also okay there are various other options for erasing the entire flash for checking whether the program has been written completely and correctly or not what it does is it writes the program it again reads back and compares it with the file on the computer and say okay it's verified the program is written correctly okay something like that so this is a software interface that we would be using it's available on flashmagic tool.com we would be giving you a cd okay in that there would be everything from the compiler that we would be using some sample programs more about the kids that would be using and the flash magic also okay so it would be quite a yeah this this can be used with many hardware burners also but when it when we select the device as 89v51 rd2 what it will do is it would send in the programming sequence first okay whenever I press start here and if my microcontroller is on it will send the sequence yes it will send the sequence the microcontroller bootloader will check whether the sequence is the one which is needed for programming okay and if it's matching the bootloader will send another okay start sending me the program I will write the flash something like that okay that's it for this session