Micro-Controllers & Robotics Robotics Club IT-BHU.

Slides:



Advertisements
Similar presentations
Introduction to Micro-controllers Anurag Dwivedi.
Advertisements

Analog-to-Digital Converter (ADC) And
The 8085 Microprocessor Architecture
Robotics Club, Snt Council2 The 3 Schools of Robotics: Mechanical Design – Types of motors – Material selection –
Indian Institute of Technology Hyderabad ROBOTICS LINE FOLLOWER HARI KISHAN TANDEY – ES12B1008 DILIP KONDAPARTHI – ES12B1010 SAI KARTIK – CE12B1015.
MICRO-CONTROLLER: A microcontroller is the brain of the robot: These are: 1. Integrated Circuits (ICs) 2. Programmable.
1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.
L.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
Programming the ATmega16
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Railway Foundation Electronic, Electrical and Processor Engineering.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Railway Foundation Electronic, Electrical and Processor Engineering.
Essentials of motors and motor driver
 Main Components:  Sensors  Micro controller  Motor drivers  Chasis.
Engineering 1040: Mechanisms & Electric Circuits Fall 2011 Introduction to Embedded Systems.
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
Robotics Research Laboratory Louisiana State University.
 C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories. 
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering.
Objectives How Microcontroller works
Programming Concepts Part B Ping Hsu. Functions A function is a way to organize the program so that: – frequently used sets of instructions or – a set.
Programming Concepts (Part B) ENGR 10 Introduction to Engineering 1 Hsu/Youssefi.
A Few Words From Dilbert
ROBONICS’ 15 Powered By:. An embedded system is some combination of computer hardware and software, either fixed in capability or programmable, that is.
MOBILE OPERATING ROBOT ON EMBEDDED SYSTEM
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
1 Lab 1: Introduction. 2 Configure ATMEL AVR Starter Kit 500 (STK500), a prototyping/development board for Lab1. ATmega16 ( V) is the chip used.
Atmel Atmega128 Overview ALU Particulars RISC Architecture 133, Mostly single cycle instructions 2 Address instructions (opcode, Rs, Rd, offset) 32x8 Register.
ATtiny23131 A SEMINAR ON AVR MICROCONTROLLER ATtiny2313.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Suleyman Demirel University CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.
CS-280 Dr. Mark L. Hornick 1 Atmel Timer/Counter System Most microcontrollers include some type of timer system Facilitates real-time monitoring and control.
Automatic accident avoiding system PROJECT MEMBERS MUTHUKUMAR.K (05ME33) SAKTHIDHASAN.S (05ME39) SAKTHIVEL.N (05ME40) VINOTH.S (05ME56) PROJECT GUIDE:
Analog to Digital Converter (ADC). Analog to Digital Converters  Microcontroller understands only digital language.  To convert the analog output from.
TIMERS AND INTERRUPTS AVI SINGH KEVIN JOSE PIYUSH AWASTHI.
Chapter Microcontroller
Components of a typical full-featured microcontroller.
MICROCONTROLLER INTERFACING WITH STEPPER MOTOR MADE BY: Pruthvirajsinh Jadeja ( ) COLLEGE:DIET BRANCH:EC.
AVR Architecture Prepared By: Avdhesh Soni ( ) Sarthak Patel ( ) Akshay Parekh ( ) Fenil Sachla ( ) Guided.
Introduction to AVR Name : 1) Abhishek Yadav ) Prakash Giri ) Kheni Niral ) Bhadresh Langadiya Branch.
GROUP MEMBERS: SUMEET HOODA. RAJDEEP PATRE. DHIRAJ GAONKAR. LIVE HUMAN DETECTION USING WIRELESS REMOTE CONTROLLED ROBOT.
Electronic instrumentation Digitization of Analog Signal in TD
1 Lab 4: D/A Converter Lab 4: D/A Converter This is a simple resistive network for a D/A converter Port 1, Port 0 are digital inputs ==> 00 (minimum),
WATER LEVEL INDICATOR AND AUTOMATIC MOTOR SWITCHING SYSTEM
1. PIC ADC  PIC18F877 has 8 analog input channels i.e. port A pins(RA0 to RA5) and port E pins(RE1 and RE2). These pins are used as Analog input pins.
INTERFACING HARDWARE WITH MICROCONTROLLER GUIDED BY: Prof. ASIF SIR 1. AKSHAY KIRAN 2. DEEP PARIKH 3. JIGAR PATEL 4. TILAK PATEL ,05,D2D06,09.
USING TV REMOTE AS A CORDLESS MOUSE FOR THE COMPUTER
CORDLESS MOUSE FEATURES BY TV REMOTE USING PIC MICROCONTROLLER
Introduction to Motors, servos and steppers
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
Application Case Study Security Camera Controller
AVR ATMEGA 8 MICRO-CONTROLLER
For further information
ARDUINO LINE FOLLOWER ROBOT
COMP2121: Microprocessors and Interfacing
AVR Addressing Modes Subject: Microcontoller & Interfacing
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
The Arduino Microcontroller: Atmel AVR Atmega 328
Timer/Counter Modified from Dr. Lam Phung’s Slides.
8-bit Timer/Counter0 with PWM
An Introduction to Microprocessor Architecture using intel 8085 as a classic processor
Introduction to Microprocessors and Microcontrollers
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
Presentation transcript:

Micro-Controllers & Robotics Robotics Club IT-BHU

Agenda Session I Introduction to Microcontrollers(uCs) Introduction to Compiler & Downloader Atmega16 Components Programming of PORTs Glowing LEDs n making dancing patterns Introduction to Motors & Motor Drivers Controlling Motors using Atmega16 Lab Session Session I Introduction to Microcontrollers(uCs) Introduction to Compiler & Downloader Atmega16 Components Programming of PORTs Glowing LEDs n making dancing patterns Introduction to Motors & Motor Drivers Controlling Motors using Atmega16 Lab Session Session II Introduction to IR LED & Rx Making IR Sensor Using IR sensor with ATmega16 Introduction to ADC Programming of Inbuilt ADC Making Line Follower Robot Lab Session Session II Introduction to IR LED & Rx Making IR Sensor Using IR sensor with ATmega16 Introduction to ADC Programming of Inbuilt ADC Making Line Follower Robot Lab Session Session III Introduction to LCD Programming of LCD Intro & Prog of Timers Making Digital Clock Lab Session Session IV Doubt Clearing Completing programs / projects Algorithms for various Robots Advanced topics – Keypad & Communication

What is a uC ? In simple words -- a single chip Computer Introduction

Computer uP RAM HDD Clock Ports (Serial/USB) Ports (Parallel) Mic/Headphone Jack (ADC/ DAC) Power Supply Unit Reset Mother-board uC uP Flash Memory EEPROM Clock Unit SPI / UART Controllers Digital I/O Ports ADC Power Supply Unit Reset IC Introduction

From uP to uC 1.uP 2.Memory 3.Oscillator 4.Buffers 5.ADC 6.Comparator 7.UART 8.Interrupt controllers 9.SPI 10.Two Wire connector 11. JTEG Connectors

Atmega16L : An Overview 8-bit Micro-cotroller 40-pin DIP 32 Programmable I/O Lines Operating Voltages V Speed Grades MHz 16K Bytes of In-System Self-programmable Flash program memory 512 Bytes EEPROM Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode 8-channel, 10-bit ADC Programmable Serial USART Master/Slave SPI Serial Interface Programmable Watchdog Timer with Separate On-chip Oscillator On-chip Analog Comparator

Pin diagram

Block Daigram

Simplified Diagram 4 8bit Parallel Input-Output ports through which you can i/p or o/p digital data.

Port Architecture PORTAPORTA DDRADDRA PINAPINA uCExternal world

Programming the Ports Normal C program int x; float y; x= 10; y= 8.97; Program for Ports DDRA = 0b or 0xFF // o/p DDRC = 0b or 0x00 // i/p PORTA = 27; // decimal PORTC= 0b ; // binary DDRx defines whether the port will act as input port or o/p port. Just as ‘int’ declares a variable as integer type. It does not assign any value to it. PORTx assigns the value to be output. If DDRx=0 ie port is defined as input port, PORTx will store the value but will not o/p it.

More Examples PORTA=0x5A ; PORTA=0b ; PORTA=5; DDRA=0xFF; DDRA=0b ; // pins A7-A4 as o/p & A3-A0 as i/p You can even address individual pins of any Port DDRA.3= 1 ; // Only Pin 3 of Port A (A4) is configured as o/p pin, rest untouched PORTA.0=1; // pin 0 of Port A outputs 1, rest retain there previous values

Taking Inputs To take Digital i/p from external world first configure the port as i/p port using DDRx=0 Use PINx to read the digital value. x = PINA // read 8 bit integer value from Port A. 0 < x < 255 y= PINB x,y must be defined as unsigned integers in ur C program. Individual pins can be read in the same way. x= PINA.2 // as the individual pins are read n the value is digital, therefore x can either be 0 or 1 y= PINC.6

Complete Program A program to o/p 33 (hex) on PortD and configure and read pins 2 and 7 of Port A #include void main( ) { unsigned int x,y; DDRD=0xFF; // all pins o/p DDRA=0b ; // pin 7 & 2 i/p rest immaterial PORTD=0x33; x=PINA.2; y=PINA.7; }

LED V GND

Glowing LED PortAPortA To glow LED set PORTA.0=0; PORTA.1=1; or PORTA=0b ; or PORTA=0x02;

Glowing LED contd.. PortAPortA

LED Panel on PCB GNDGND +V

Blinking Pattern While (1) { PORTA=0xFF; delay_ms(500); PORTA=0x00; delay_ms(500); } While (1) { PORTA=0xFF; PORTA=0x00; }

Complete Program A program to make a blinking pattern #include void main( ) { DDRA=0xFF; // all pins o/p While (1) { PORTA=0xFF; // all LEDs ON delay_ms(500); PORTA=0x00; // all LEDs OFF delay_ms(500); }

Motors & Motor Drivers

Motors and Motor Drivers Types Motors  AC Motor  DC Motor  Stepper Motor  Servo Motor

DC motor

Stepper Motor Similar principle to DC motors More accurate control than DC motors Rotate in fixed angles Speed control is in our hands Multiple coils – so excitation done in pattern for rotation

Stepper Motor

Servo Motors DC motors with built in gearing and feedback Control loop Generally rotate through 90 and 180 deg. (360 deg also available) Used for extreme precision

Why do we need the motor driver? The total output current that Atmega16 can provide is 200mA. But motors need much higher current to run. Therefore a driver circuit is required which provides the necessary current. Moreover the o/p from uC can either be 0V or +5V, but motors come in various ratings 2V-36V (typically small ones). Atmega cannot provide this voltage hence we need external supply to drive motor depending upon i/p from uC Basically a motor driver connects or disconnects the motor from some external supply depending upon the i/p from uC. If the uC sends ‘1’, driver connects the motor to some external supply ( battery in our case) thus motor actually runs by drawing current from the battery. If the uC sends a ‘0’, o/p is connected to ground and hence the motor does not get the supply. Motor Driver

I/p 1 ( O/p from uC) I/p 2 I/p 3 I/p 4 O/p 4 O/p 3 O/p 2 O/p 1 If uC send ‘1’ to I/p 1, O/p is conn to + VPP GND +VPP If uC send ‘0’ to I/p 1, O/p is conn to GND Working of Motor Driver

I/p 1 ( O/p from uC) I/p 2 I/p 3 I/p 4 O/p 4 O/p 3 O/p 2 O/p 1 If uC send ‘1’ to I/p 2, O/p is conn to + VPP GND +VPP If uC send ‘0’ to I/p 2, O/p is conn to GND Working of Motor Driver

Similarly other 2 o/p are connected /disconnected by I/p 3 & I/P 4 All the O/p’s operate independently, ie if all I/p 1-I/p 4 are 1, all O/p1 – O/p4 will be connected to +VPP L298 can provide 1A current / Output channel, therefore total of 4A. But heat sinks should be installed for such high currents. VPP can be anything in the range 2V- 46V, therefore any motor can be driven even though Atmega provides only 0/5 V.

L298N Motor Driver

ATMEGAATMEGA MOTOR DRIVER MOTOR 4 Inputs 4 Outputs

To drive a motor A motor has two wires : +ve & -ve. To drive it u need to apply a voltage between them Lets connect +ve wire to O/p1 & -ve to O/p2 of L298 If we give I/p1 =1 & I/p2 = 0, O/p1 will be +VPP & O/p2 GND The voltage diff between two wires of motor = +VPP, therefore it will run in one direction If we give I/p1 =1 & I/p2 = 1, O/p1 will be +VPP & O/p2 +VPP The voltage diff between two wires of motor = 0, therefore it will NOT run If we give I/p1 =0 & I/p2 = 0, O/p1 will be GND& O/p2 GND The voltage diff between two wires of motor = 0, therefore it will NOT run If we give I/p1 =0 & I/p2 = 1, O/p1 will be GND & O/p2 +VPP The voltage diff between two wires of motor = -VPP, therefore it will run in reverse direction Driving a Motor

Lets Connect I/p 1 to PortB.0 & I/p 2 to PortB.1 As these ports will o/p data from uC, therefore their DDR should be 1 DDRB= 0b ; Or DDRB=0x03; For running in forward dir, PORTB.0 =1 & PORTB.1=0 PORTB=0b ; Or PORTB=0x01; For running in reverse dir, PORTB.0 =0 & PORTB.1=1 PORTB=0b ; Or PORTB=0x02;

Sample program for running motor forward for 1s, reverse for 1s & stop for 1s in sequence #include void main() { DDRB=0x03; // other part included by CVAVR comes here while (1) { PORTB=0x01; // forward delay_ms(1000); PORTB=0x02; // reverse delay_ms(1000); PORTB=0x00; // stop delay_ms(1000); } Complete Program

Session II

Sensors A sensor is a device which measures a physical quantity and converts it into a signal which can be read by an observer or by an instrument. They are used to provide feedback from the external environment

IR Sensor Pair Transmitter (Tx) Receiver (Rx) IR sensor Pair Object / Line Transmitter = LED( Light Emitting Diode) Receiver = Photodiode

Principle of Operation R1< R2 WHITE surface Maximum reflection T1 is turned ON Vout is LOW BLACK surface Minimum or No Reflection T1 is turned OFF Vout is HIGH

Interfacing IR sensor with ATmega16 ATMEGAATMEGA PORTA Out: 1 Out: 0 In 1 0 2

Programming IR sensor with ATmega16 #include void main() { int x; DDRA=0b ; // last two pins as o/p for Vcc & GND PORTA.0 =1; // Vcc PORTA.1= 0; // Gnd x= PINA.2; // now you can use x for any calculation u want }

Vout is ANALOG But uC being digital can only read it as 0 or 1. 0 if Vout < 1.2 V 1 if Vout > 1.2 V However most applications require much higher resolution n multiple levels.

Analog to Digital Convertor (ADC) The ATmega16 has an inbuilt 10 bit ADC (1024 Levels) 8 Multiplexed Single Ended Input Channels 7 Differential Input Channels

Registers used in the ADC  ADMUX – ADC Multiplexer Selection Register  ADCSRA – ADC Control and Status Register A These registers are used to configure ADC However when using CVAVR this will be done by code wizard

Configuring ADC using Code Wizard 1.Open a new project 2.Select ‘yes’ to use code wizard 3.Goto ADC tab on Code Wizard ADC

Configuring ADC Set the parameters as shown or as per your requirement. Finally Generate Save & Exit

Programming ADC When you generate the program after setting the code wizard you will a find a function unsigned int read_adc(unsigned char adc_input) declared & defined just after #include It accepts an Unsigned character as argument which is basically the hex address of the pin on which Analog i/p is applied. However we don’t need to give the address but just the name of pin and the conversion is done by compiler. Return value is the digital value ( or 0-255) of the analog i/p.

Programming ADC To use ADC just call the function read_adc( ) as shown unsigned int s; Argument specifies which pin the analog i/p (ie o/p of sensor) is connected s = read_adc(PINA.2) ; S can now be used for any calculations. Its value ranges from (if Use 8 bits was NOT checked in configuration) (if Use 8 bits was checked in configuration)

Interfacing IR sensor with ATmega16 ATMEGAATMEGA PORTA Out: 1 Out: 0 In GND 7 6

Sample program This program glows Red Led if the digital value is less than 200 and green otherwise.

#include read_adc as generated by Code Wizard Void main ( ) { unsigned int x; DDRA=0b ;// bits 0,1,6,7 as o/p coz dey will o/p data to glow LEDs while(1) { PORTA=0b ;// glow only IR LED, rest switched off x=read_adc( PINA.2);// read analog i/p & conv to digital value if ( x<200) PORTA.7=1; // glow RED LED if val <200 else PORTA.6=1;// glow GREEN LED if val > 200 delay_ms(500); }

Points to note ADC is multiplexed only on Port A. Therefore analog inputs can only be applied to Port A. The LEDs used in prev example can be connected to any port A/B/C/D as they require a digital o/p. Even the Vcc & GND of IR sensor can be connected to any other port as they simply require 1 & 0 to be outputted. However it is advised to connect the Vcc & GND pins of sensor directly to the Vcc/GND points on PCB or battery so that uC is not loaded. (although it consumes more power as LED is always ON) As there are 8 pins on Port A, therefore max of 8 sensors can be connected,(with their Vcc & Gnd connected to directly battery). For more than 8 sensors, external MUX is to used.

Line Follower Line Follower is a robot that can follow a black line drawn on a white/bright surface or a white line on black/dark surface without any human intervention. It is the simplest autonomous robot.

Line Follower : Design The bot has two IR sensors facing downwards (towards the floor). The separation between the sensors is such that in normal (correct) position of the bot, both the sensors are on white surface. ie separation > strip width The bot is driven by two wheel differential drive ie the two back wheels are driven by independent motors. S left S right

Line Follower : Algo When the bot is in correct pos, both sensors are on white & read LOW. The robot should move forward in this case. S left S right Movement LOW Forward S left S right

Line Follower : Algo When the bot is over line from right side, left sensor is on white & reads LOW whereas right sensor is on black & reads HIGH The robot should take a right turn in this case to come back in correct pos. S left S right Movement LOWHIGHRight Turn S left S right

Line Follower : Algo When the bot is over line from left side, right sensor is on white & reads LOW whereas left sensor is on black & reads HIGH The robot should take a left turn in this case to come back in correct pos. S left S right Movement HIGHLOWLeft Turn S left S right

Line Follower S left S right Movement Left MotorRight Motor LOW Forward LOWHIGHRight TurnForwardBack HIGHLOWLeft TurnBackForward

ADC Advanced topic Differential Mode

Single Ended Vs Differential Mode In single ended mode the absolute value of the voltage (wrt to ground) applied to the ADC pin is converted to digital. But if we want to measure differential voltage ie the voltage difference between two points and convert this difference to digital value.. Like in digital voltmeter … What are the options ?

Single Ended Vs Differential Mode Option 1: X = read_adc(PINA.0) y = read_adc(PINA.1) D= x-y; But each conv takes 13 cycles. So total time = 26 cycles or 26us Moreover the quantization error of two i/p gets added, hence ‘d’ has double error in it.

Differential Mode Atmega provides an efficient way of doing this In Differential Mode, the difference in the analog voltage between two pins is quantized, instead of the absolute value of potentials on each pin. To use ADC in Differential mode, only the value of ADMUX is to be changed.

Session III

LCD It is basically an array of LEDs. Already has a inbuilt diver/controller, so you just need to specify the location and character to be printed. Two broad divisions:- Character LCD’s : ex calculators Graphic LCD’s : ex Mobile phones, Xerox Machines. Commonly available config for character LCD’s 16 X 2, 20 X 2, 40 X 4

The LCD that has been given to you comes under character LCD and has 2 rows with 16 columns each. The coordinates mapping is shown below. 0,0 0,115,1 15,0 It has 16 pins whose description in given on the next slide. Character LCD

Pin Configuration of character LCD’s

All the connections for LCD are already printed on PCB, except you have to connect them to any port. As shown below LCDATMEGAATMEGA Connect this to any of the ports but the pin alignment must be there, ie PORTx.0 should be connected to ‘0’ shown above and so on

LCD Commands lcd_clear( ); clears the LCD lcd _gotoxy(col,row); places cursor at cordinates (col,row), see slide2 for cord lcd_putsf(“ Hello”); prints the string on LCD. If the string is more than 16 chars it overflows to next line lcd_puts(d); prints the string in array d on LCD.(simlar to puts function in C) If the string is more than 16 chars it overflows to next line lcd_putchar(s); prints the character in variable ‘s’ on LCD. Related functions itoa(x,s); converts interger ‘x’ to string ‘s’. This is required when u want to print an integer. You must first convert it to string as puts accepts only strings ftoa(x,s); converts float ‘x’ to string ‘s’. This is required when u want to print a float no. You must first convert it to string as puts accepts only strings Note: you must include “stdlib.h” for using these functions Programming of LCD

Goto LCD tab in CodeWizard. Select the port on which you have connected LCD. Keep the Chars/Line to 16 as the given LCD is 16X2. Generate the code Initializing LCD

// whatever Code wizard generates #include void main() { // whatever code wizard generates lcd_clear(); While (1) { lcd_gotoxy(0,0); lcd_putsf(“Hello”); delay_ms(1000); lcd_clear( ); delay_ms(1000); } Blinking LCD, it prints Hello for 1s and then is cleared for 1s Sample Program

Timer & Counter A timer/Counter is a circuit that repeatedly counts from BOTTOM to TOP A simple way to do this is run a loop… Is there any harm???

Timer & Counter Atmega16 has 3 counters, namely TIMER0 ( 8 bit) : TIMER1 (16 bit): TIMER2 (8 bit) : Each timer has 3 registers associated with them that hold the count values and used for configuration.

TCNT0- Timer/Counter Register TCNT0[7:0] It holds the counter value at any point of time. Similarly we have TCNT1[15:0] & TCNT2[7:0]. TCNT1 [15:0] is also written as TCNT1H[7:0] & TCNT1L[7:0]

OCR0 – Output Compare Register OCR0[7:0] OCR holds the value to which the count is to be compared. Similarly we have OCR1[15:0] & OCR2[7:0]. Timer 1 has two compare match registers – OCR1A & OCR1B

TCCR0 – Timer/Counter Control Register FOC0WGM00COM01COM00WGM01CS02CS01CS FOC0 Force Output Compare WGM00:01 Waveform Generation Mode COM01:00 Compare Match Output Mode CS02 : 01 Clock Select TCCR is used for configuring the timer, deciding modes of operation etc.

Timers : Modes of Operation Normal Mode Clear Timer on Compare Match (CTC) Fast PWM Phase Correct Commonly used are : Normal & CTC

Normal Mode The timer counts from Initial value to 0xFF ( 0xFFFF for timer 1) and then loops back to initial value. If the interrupt on compare match is enabled, an interrupt is given when the value of TCNT = OCR. CTC Mode The timer counts from Initial value to OCR and then loops back to initial value.ie whenever TCNT = OCR the timer is cleared to zero. If the interrupt on compare match is enabled, an interrupt is given when the value of TCNT = OCR. Timers : Modes of Operation

Overflow Interrupt This interrupt is issued at the next clock after the timer value reaches 0xFF (0xFFFF for time 1). ie when TCNT=0xFF Compare match interrupt This interrupt is issued when the timer value equals OCR. ie when TCNT=OCR Timers : Interrupts Two types of interrupts are available in timers.

Timers : Configuration 1.Goto Timers tab. 2.Select the appropriate timer 3.Set the parameters as required.

Programming Timers When you generate the program after setting the code wizard you will a find a function interrupt [TIM0_COMP] void timer0_comp_isr(void) * & / or interrupt [TIM0_OVF] void timer0_ovf_isr(void) ** declared & defined just after #include * If compare match interrupt is enabled ** If overflow interrupt is enabled These functions are automatically called whenever the interuppt is generated by the timer, irrespective of where the execution of main program was. Just place the code for corresponding actions in these functions.

Sample Program This program glows LEDs attached on PORTD for 5us after every 250us Lets use timer 0 Clock : 1000 KHz // timer increments every 1us Counter Value :0 // initial value Compare Value:250 = FAh// as timer is incrementing every 1us, n we need to measure 250us so Compare value of 250 will do that CTC Mode: // otherwise counter will counter upto 255 before going to zero, so it will measure 255us when it comes to 250 next time

Sample Program #include interrupt [TIM0_COMP] void timer0_comp_isr(void) { PORTD=0xFF;// glow LEDs delay_us(5);// wait 5us PORTD=0x00;// Put off LEDs } Void main () { DDRD = 0xFF;// as uC o/ps data on all pins to glow LED // gen by codewizard TCNT0=0; OCR0=0xFA; TCCR0= something While (1) { } }

Contact Robotics Club IT-BHU For any queries:- Contact: Ritesh Harjani Phone no Contact: Sourabh Malwe Phone no