Techkriti 2012-Introduction To MCU
Techkriti 2012-Introduction To MCU
Rajat Arora
Micro-Controller
A single chip Computer (to some extent) Has CPU
1. 2. 3. 4. RAM EEPROM I/O in form of pins Peripherals (Timer , Communication modes , ADC etc)
Suppose we want to make a Line following Robot What do we do ? Use a computer with 2.4Ghz Intel core I7 with 4 Gb RAM , 500 Gb Hard disk , 1 Gb Graphics Card ??
Why MCU
Small reflected by the word MICRO Inexpensive Ideal for doing repetitive tasks Easy to use Highly Efficient and fast
Selecting a MCU
Two family of MCU extremely popular a) AVR b) PIC We use AVR series of MCU from Atmel The instructions are fed once in the form of a Hex file
Avr Programmer
So we need two softwares overall a) CVAVR > Editor and Compiler b) Avr Studio > Transfer Code to Atmega
Atmega 16
Basics of C language
If else block If(condition) { } else { }
Circuit Diagram
Open CVAVR
Go to File
New
Project
Open CVAVR
Go to File
Click on New
Click YES
Select Chip
Introduction to I/O
Atmega has total of 40 pins out of which 32 pins can be used as Input or Output These 32 pins are divided into 4 groups of 8 pins PORTA, PORTB , PORTC , PORTD
What Next ?
We have set the Pin as Output What else do we need to light the LED ?? Supply of 5 Volts !!! This is given by PORT Register
PORT Register
Only after you have set the Pin to Output you can control them through this Register It is a 8 bit register . It corresponds to the pin in same manner as that of DDR Register Used to set output value ( 0 or 1 ) only if the corresponding Pin has been set as output by DDR Register PORTA= 0b 00000001; or PORTA= 0x01 ; 1 stands for 5V 0 stands for 0V
MSB L L L L L L L H LSB
Simple Questions
DDRA= 0b 00101100 DDRD = 0xf4 DDRC = 0b 01111110 DDRB = 0x3b Assume all 32 pins set as output PORTA = 0b00001100; PORTD = 0b11110000; PORTB.4=1; PORTC.2=1;
Setting I/O
Go to Ports
Click on In to make that pin Output Can do so for all four ports
Click on File
NOTE : We write our code in While block While (1) { PORTA.1=1; // sets the Pin to 5 volts PORTA.1=0; // sets the Pin to 0 volts }
This makes the LED to blink but we cannot see blinking !!!
This is because Atmega runs at a frequency of 8000000 Hz We need to introduce delay so as to see blinking Use header file delay.h Function to be used delay_ms(time in millis); While (1) { delay_ms(1000); PORTA.1=1; delay_ms(1000); PORTA.1=0; }
How to compile
Code is written in C language but Atmega understands Hex file so we need to convert the C file to Hex file
Compiling
Hex File
You can find the Hex file in Bin folder or the EXE folder of the directory where You installed CVAVR
So we Have our Code ready Feed this code to Atmega using Programmer (we will see this in workshop ) Lets see the code in action
Circuit Diagram
Never leave any Input pin unconnected / floating at any point of time while your circuit is working In Last Circuit A0 is floating when button is not pressed so our Circuit Diagram is wrong
What is the Voltage at the Floating PIN ? Not 5 V Not 0V Its UNDEFINED So never leave an input pin unconnected Use the Concept of Pull up / Pull down
In Layman terms
PULL DOWN : Gives 0V when unconnected PULL UP : Gives 5V when unconnected
Connect the PIN to Ground through a resistance for pulling down Connect the PIN to 5V through a resistance for Pulling up
PIN Register
It is a 8 bit register . It corresponds to the pin in same manner as that of DDR Register It is used to read voltage at a pin To be used only after the pin has been set as input by DDR register
Code in Action
Thank You