An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative...

57
An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited

Transcript of An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative...

Page 1: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

An Approach to LearningMCU-Programming

By KCPadhy, B.E., PGDBA(HRM&FM)

KCTRONICS Innovative Consultancy Services Private Limited

Page 2: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

An Introduction to MCU

• Generally micro-controllers control & sense the surrounding electronics and the environment viz., temperature, light key board inputs etc..

• Lets give an example of a microcontroller Atmel AVR Atmega32 .

• The program language we will be using is C++

Page 3: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Atmega32 chip

Page 4: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

PINS confign :Atmega32

Page 5: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Details of Pin-Confign

• This is a 40 pin MCU. • The first starting from the top are the PB0-7• This is the PORT B, & there are other 3 PORTs lebelled

as A, C & D.• These ports can be set to receive and called as INPUT

and can also be set to send voltage out as output.• There are power pins called as Vcc & Gnd• All but one pin of Port D (PD0-6)are on the left side

(lower section ) & PD7 is on the right hand side of the mcu.

Page 6: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Contd…• Continuing on the right side with PD7 pin , Port C

continued from the lower corner Up.• From there, PORT A analog to digital pins are available

that have the capability to sense and converts to voltage levels.

• E.g., sensing temperature , one thermistor when connected to one of the port A pins, the MCU will convert this voltage to a number from 0 to 255 (an 8 bit number –higher resolution is possible at 10 bits).

• When the program is written and stored in the MCU can use this temperature and respond at another pin in the form of beep/light flash.

Page 7: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Other features• Other features of this and other microcontrollers, other

than the actual programming is the programming space where the program is stored in the chip and

• How much space you have, to accommodate the memory, or space for data and variables that the program will use, and finally, there is a clock built into the chip that counts.

• The counting can be in many different speeds depending on the speed of the chip and the divisor that is selected for the speed.

• The counting can be in seconds, milliseconds, microseconds, or whatever you determine for the program and application that you select.

Page 8: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Interface

Page 9: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Serial Peripheral Interface

Page 10: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

SPI-construct

• We will construct a small board that contains a header (little metal pins that stick up) that the SPI connector can use, and also a header that will correspond to the appropriate pins on the microcontroller.

• The latter can simply be a single row of six pins since the makers of the Atmel AVR Atmega32 microcontroller so thoughtfully located these pins together.

• This will allow us to make our MCU interface board with a very narrow size, which will reduce the area covered on the breadboard

Page 11: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Programmer• To reiterate from the last pages, there is a

programmer that is needed between the computer and the microcontroller.

• It should be noted that there are several different programmers that can be used.

• Some of these programmers look totally different from others, but all of them basically do the same thing--provide an interface between the computer and the AVR microcontroller.

Page 12: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Programming-Environ• Note that if you are not using the AVR Atmega32

microcontroller, then you must check the compatibility of the programmer you choose to use.

• Also note that many of these programmers use the same drivers.

• Remember that the purpose of making such an interface board is to insure a proper connection each and every time we need to load our program into the MCU.

• The SPI connector pin-out is to the left. There are arrows from this SPI interface block to the corresponding pins on the AVR Atmega32 microcontroller.

Page 13: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

SPI –MCU:connections• Let's run through the connections between the SPI device

and the MCU:• Top left SPI pin is connected to the MISO (Master In Slave

Out)• Middle Left SPI pin is connected to the SCK (the clock pin)• Bottom Left SPI pin is connected to the Reset (Reset )• Bottom Right SPI pin is connected to the GND (Ground, or

zero volts)• Middle Right SPI pin is connected to the MOSI (Master

Out Slave In)• Top Right SPI pin is connected to the VCC (+5 Volts)

Page 14: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

SPI-Drg

Page 15: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Transferring Program into MCU• To be able to make the microcontroller do

something useful, we must write instructions for it to follow.

• While we can write these instructions with a simple text editor, many people find it much easier to use a dedicated "programming environment" to write their programs.

• Install one such programming environment (WinAVR) on the computer.

Page 16: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Programming Environment• This particular programming environment allows

you to create and edit programs in various languages (we will focus on C), and then compile the program into an executable format that the microcontroller will understand.

• Finally, WinAVR will help us transfer this file into the microcontroller.

• WinAVR will also help us to do many other things, like debugging our programs and giving warnings when there are compilation and syntax errors.

Page 17: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Win AVR -Installation• The installation process for WinAVR is very quick

and concise. Here are the detailed steps:• Download the latest files from the WinAVR

sourceforge files location.• Click on the latest release.• You will need to answer the security question

asking whether you want to save the download or not, since you will be downloading an executable file.

• Once it is downloaded, execute the file by clicking on it. The installation process will start.

Page 18: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

……

• The installation process will have the usual Windows install wizard information, and some questions offered to help you tailor the process.

• Most of these can simply be left as their default value, but select as you see appropriate.

• But you will probably notice the many options in the start menu folder for the WinAVR programming environment.

• Don't fear, you will typically use only one of these programs called the "Programmers Notepad." Clicking on this icon will start the user interface for the application that allows us to write our programs (creation and editing).

• The program also contains menu commands that will help us compile the code and then transfer it into the microcontroller.

Page 19: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Testing the Programmer• To check to see if the drivers and the development

software installed correctly, we will test the programmer with a program called avrdude.

• Avrdude is a program that is installed with the latest WinAVR installation, and is responsible for the actual transfer of the file going into the microcontroller .

• This is the “.hex” file which is basically the binary code that the microcontroller can understand and execute.

• If the test is not successful, the programmer will not be able to transfer the file--therefore this step is crucial to the whole process.

Page 20: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Steps to test the programmer• Go to a DOS prompt by clicking the start menu and

typing cmd.exe in the search box. By the way, if you didn't know, DOS stands for Disk Operating System.

• This was a prompt created so computer users would be able to organize their files on floppy disks, thus making it easy to execute (run) programs from the DOS Prompt.

• Prompt means the place where the cursor is located and you can start typing.

• The prompt is labeled with the drive letter along with the folder name separated with backslashes "\". (We called these directories in the past.)

Page 21: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

• To execute the avrdude program that was installed with WinAVR, simply type avrdude -c usbtiny -p m32 at the DOS prompt, and the DOS shell output will report the success of the connection.

• The "-c" is a flag that is followed by the parameter used to specify the programmer (usbtiny), and the parameter following the "-p" flag is used to specify the microcontroller ("m32" for the Atmega32).

• If you are using a different microcontroller, you will need to use the appropriate specification.

• To exit out of the DOS window, you can type "exit" at the DOS prompt .

Page 22: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Circuit

• So, you probably wonder, why can't we program yet! • Well, we still need to create a circuit that the

program will control. • It would be pointless to simply load a program into a

microcontroller and run it without any devices connected to it.

• We wouldn't have much to look at! In fact you will soon see that many electronic components can be controlled by microcontrollers, however one of the easiest devices to control is an LED.

Page 23: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

LED-switching Circuit

Page 24: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Calculate the value of R(Resistance)

Page 25: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Here is the bread board circuit

Page 26: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Powering the LED • "LED" stands for Light Emitting Diode, and this

component generally has two leads.• Leads are the metal legs (wires) hanging off of the

actual LED itself.• These leads are the polar connections that allow

current to flow into the LED from one lead (called the anode) and then out of the LED from the other lead (called the cathode).

• One very important caution about powering and using LEDs: The current running through the LED must be limited so it does not burn out.

Page 27: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Driving the LED• LEDs have both a current rating, and a voltage

rating. • The current rating is the maximum current limit that

the LED can handle--any higher current and the LED life will be shortened; but less current will result in a dimmer LED that does not emit light as brightly.

• So, we are faced with having to compute the optimum value for the resistor we will choose.

• Note that if you don't want to do this calculation for some reason, it is generally safe to use a 1k--but the light will be pretty dim.

Page 28: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Calculation: Resistance

• So to calculate the resistance needed, we will use Ohm's law which states that resistance (in Ohms) is found by dividing the voltage by the current. The formula is:

• Resistance = volts/current• This is typically written as R = V / I. But, how do we find the voltage

and current values for the LED.• Typically, LEDs are rated at either 2 or 4 volts, and have either a 10

milliamp (mA) or 20mA current rating. My green LED has a 2 volt rating.

• In the website linked in the previous sentence, white and blue LEDs have a voltage rating of 4 volts.

• I used the 10mA rating since it is safer to use this value as it results in the smallest denominator in the Ohm's law formula, and thus represents the largest resistance value for a given voltage.

Page 29: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Led-drive resistance

• Therefore it can be considered the "worst case scenario," in terms of choosing a resistor to insert into the circuit.

• Another aspect we will need to consider is the difference between the supply voltage (the voltage we are feeding into the system), and the rated voltage of the LED.

• So therefore the new formula becomes• R = (Supply Voltage - LED Voltage) / I• Therefore in the case of our green LED, R = (5v - 2v) / .01A

= 300 ohms. Oh yeah, you need to convert the current to amps.

• Therefore we need to divide the 10mA number by 1000, as there are 1000 milliamps in one ampere.

Page 30: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

…• So now, what is this formula telling us? It's telling us

that the resistance is equal to the remaining voltage after the LED is considered (i.e.; the voltage drop across the LED), divided by the desired current through the LED.

• What is that you say? We got a value of 300 out of that formula, but when I went to the store I couldn't find that number!

• 300 Ohms is a resistance that is probably not available, but don't lose hope--you can always use a resistor of the next highest value.

• I have selected this to be 330 Ohms.

Page 31: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Connecting led• Let's use Pin 0 of PORT "B" on the MCU in this circuit, and

that just so happens to correspond to the number 1 pin on the Atmega32 microcontroller I am using.

• So now we are going to programmatically turn on that pin to light up the LED.

• Connect the resistor to the pin number 1 (note that saying "PORTB0" is one way of referring to pin B0 of PORT B, but you'll learn other ways as well).

• Now connect the other end of the resistor to the positive side of the LED (i.e.; the anode side, or the lead that is the longest, or the lead opposite to the flat of the LED).

• Then we will connect the cathode side to the ground (GND) pin.

Page 32: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Powering to MCU

• The programmer will be connected too of course; which will allow the program to get transferred into the chip, and

• also provide power to the microcontroller. • Finally, we can now apply the circuit to the

breadboard. • From the accompanying image, you can see

that this is a very simple circuit.

Page 33: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Check out

• Check out the close-up of the resistor and the LED.

• Can you see how the wire is connected to the resistor and how the resistor is connected to the LED?

• After this circuit is completed on the breadboard, we can start to program and make the LED light up.

Page 34: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Program to turn LED On

Page 35: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Function of MCU

• For the sake of simplification, let's categorize the function of the microcontroller into three categories: Control, sensing and communication.

• We'll leave the details of how to develop each of these functions, and delve into these details as we write the various programs.

• Note that there are many ways to program these functions. For the first program, we'll make the microcontroller "control" something.

Page 36: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

• And as you know from the previous post, we'll be using an LED for this purpose.

• Basically, we will turn the LED on. • Well I need to start somewhere! • As I take you through the experience of programming. • How do we make a program to control an LED? Well,

it's really easy: We will simply tell Pin0 on PORTB to output 5 volts.

• Remember that this(PIN0) is the pin to which the positive lead (anode) is connected.

Page 37: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

..Steps to program• The first key in this scenario is "output, " and

the next is "5 volts." • There is a way we can tell a particular pin to

be set to be an output from the MCU.• Once a pin has been set to provide output,

you will then be able to control that pin and make it either high (5 volts) or make it low (zero voltage).

Page 38: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

..• And since there are only two states for this pin in the

output mode (5v or 0v), and only two states for the mode itself (input or output), you only need to set the value to either logical 1 or a 0.

• Note that this must be accomplished for each pin we wish to use in our circuit.

• But before we get to plugging in a 1 or 0, let's talk about input versus output.

• When a pin is in input mode, it is listening for a voltage. • When the pin is in output mode, then it can be charged

at 5v, or not charged at 0v.

Page 39: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

DDR-setting for I/P & O/P• To pick the output mode for a pin, you will use

the Data Direction Register (DDR). • What is a register? Don't let this worry you.• A register is simply a memory location that

makes the microcontroller react in some way.• We use a register to set a state for the

microcontroller, or make the microcontroller do something.

Page 40: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

…• It's like reflexes, or tickles. • When a person tickles another person, it

invokes laughter. • We can make the MCU do something by

setting a specific value in a register. That's all you need to know at the moment.

• When you use the DDR register, you are able to set the pin to either output or accept data as input.

Page 41: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

….• When you are making pin 5 volts , then zero volts

, and then 5 volts again.. • You are actually sending 1s and 0s to the pin, that

is high (5volts) or and then low (zero volts) state.• The MCU sees this high/low logic and we receive

data in the same way.• There are several ways to set pin0 for port B to

output.• One way to do this is to write:• DDRB = 0b00000001

Page 42: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

• Let me explain. "DDRB" refers to the Data Direction Register for port B;

• "0b" is to tell the compiler that what follows is the binary expression of a number; and the "1" on the end denotes the pin 0 position (the first pin in port B).

• Recall that there are 8 pins for port B; pins 0 though 7. • There are also 8 digits in our line of code. So each digit

represents a pin on the port, and we can use the individual digits to specifically refer to any one of the pins in port B.

• So the '1' at the end of our code statement refers to the first pin in port B, which in this case is pin 0.

Page 43: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Programming in C/C++

Page 44: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Programming• (Recall that C and C++ are zero-based languages, so the

first index of a data structure refers to is the zero'th element; the second index refers to the first element, etc. )

• To apply 5 volts to Pin is to make DDR Code statement in binary numbers as :

• PORTB= 0b00000001• Here the only difference to the previous is: to put

5volts to (pin 0) using this statement.• We are making the PORT register , which knows the

pins of a specific port and I gives us access to actual specific date value (logical 0 or 1) for these pins.

Page 45: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Overall structure of Program• All program need a specified place to start the execution.• We will create the main function :

• Int main(void)• {• }• In order to understand the DDR and PORT register and how these

work in the MCU, an include statement must be added which contains the information of the MCU.

• #include <avr/io.h>• Int main(void)• {• }

Page 46: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

explanation

• When the compilation starts , the pre-processor portion of the compiler looks in the “avr” directory for the “io.h” .

• The “.h” extension here indicates that this is a header file and as its name implies the code within that file will be inserted at the beginning (head) of the source file .

• Now we can insert DDR and PORT statements into our code, since the inclusion of the “io.h” header file has informed the compiler about them.

Page 47: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Program up to now :• #include <avr/io.h>• Int main(void)• {• DDRB=0b00000001; //DDR setting pin0 to

output and the remaining pins as input• PORTB=0b00000001; //Set pin0 to 5 volts• }

Page 48: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Infinite run with While loop• Here we will use while loop to make the program to run for

endlessly.• #include <avr/io.h>• Int main(void)• {DDRB-0b00000001;PORTB=0b00000001;While(1){}}We have added 1 as the argument to the while loop , because anything

other than 0 is logically true.

Page 49: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Program to make LED blink

Page 50: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Program: Led-blink • Lets give a bi-polar personality , then we set up a notch to make it

blink faster: & the Program follows:• #include <avr/io.h>• #include <util/delay.h>• Int main(void)• {• DDRB |=1<<PINB 0;• While(1)• {• PORTB ^=1<<PINB 0;• _delay_ms(100);• }• }• Let us know these(| & ^) symbols !!

Page 51: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Explanation :Symbols• There are logical operators called AND,OR, NOT and

XOR. • 1 AND 1 is true or 1 , 0 AND 0 is false or 0 . • But what is 0 AND 1 and vice versa ? Well , its False or

Zero (0) obviously.• Example of AND binary Operation: (“ & “ is used in C

program for AND).• 01001011 (&)• 10001101 • Equals• 00001001 ( for “&” -AND function)

Page 52: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

OR (‘|’) and XOR (‘^’) Function: • Lets go to OR function:| • 01001011 (|)• 10001101• Equals • 11001111 (for ‘|’ OR Function)• Lets see XOR function , which similar to OR , but with a

difference as: 1 XOR 1 is actually “0”• 01001011 (^)• 10001101• Equals • 11000110 (for “^” XOR Function)

Page 53: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Description on delay function• Let us see the program: code:• #include <util/delay.h>• ‘delay.h’ provides to create a delay in our program• Now we have: DDRB=0b00000001 i.e., making the

pins 1 to 7 as i/ps and 0 pin as o/p.• This can be done with a mask:0b00000001;• Lets take our number value of:0b00000001 and

OR it to a mask value of 0b00000001 .• The result is :0b00000001.• This looks like the actual binary number .

Page 54: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

Know what is it?:“DDRB|=1<<PINB”?• 1<<PINB is the act of creating the mask.• The “1” represents what will be inserted into the mask, the << is a left

shift operator and it does Exactly what it says and• PINB0 is a number of positions that the “1” will shift left.• In essence ,PINB is just equal to 0. • So we will start with a 0b00000000, and • add a “1” to make it 0b00000001 and then• you shift it left ‘0’ positions.• So you are left with 0b00000001, the same number as above.• So what if it was PINB4?• The statement would be :1<<PINB4.• The “1” would be left shifted 4 times resulting:0b00010000.• Remember ,we are using zero index, so there is four 0s after the 1.

Page 55: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

While delay loop?• PORTB |=1<<PINB0;• _delay_ms(100);• PORTB &= ~(1<<PINB0);• _delay_ms(100);• The first couple of lines turns the PINB0 bit to 1(light) and pauses

for 100ms .The second two lines turn the PINB0 bit to 0 positions (no light) .

• No, the AND comparison cannot just make a 0 from the bit, but if you NOT “~” the binary number mask , it will turn all the 0s to 1 and 1s to 0s.

• This will permit you to only affect the PINB0 bit and turn it into a “0”. The added parenthesis just to contain the masking operation so that NOT could NOT the whole mask and but NOT just the 1 before the left shift.”<<“.

Page 56: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

XOR -operation• If the delay is going to be the same for On & Off ,

we could shorten the previous 4 lines to only two lines by using XOR operation.

• Remember the XOR will turn our specific pin to a 1 if it is 0 and vice versa.This will affect the PINB0 only .

• Every time the instruction is executed, it will flip the bit to the opposite.

• Now the program using XOR (^) operator as:• PORTB ^=1<<PINB0;• _delay_ms(100);

Page 57: An Approach to Learning MCU-Programming By KCPadhy, B.E., PGDBA(HRM&FM) KCTRONICS Innovative Consultancy Services Private Limited.

End of the Part-I

See you in the next Part-II Programming Slides with many more applications

THANK YOU !

Its an effort to teach the freshers through our Web Portals ! http://www.kctronics.in