Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011.

Slides:



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

Mark Neil - Microprocessor Course 1 Device Drivers – Digital Voltmeter.
Lab7: Introduction to Arduino
More fun with Timer/Counters
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
Sistemi Elettronici Programmabili13-1 MULTI OSC + CLOCK FILTER LVD POWER SUPPLY CONTROL 8 BIT CORE ALU PROGRAM MEMORY RAM I2CI2C PORT A SPI PORT B 16-BIT.
MICRO-CONTROLLER: A microcontroller is the brain of the robot: These are: 1. Integrated Circuits (ICs) 2. Programmable.
Analog to Digital Conversion
Kuliah Mikrokontroler AVR Comparator AVR Eru©September 2009 PENS.
Analog/Digital Subsystem
1 ADC – Analog to Digital Converter and PWM Waveform Generation with EZDSP2812 Professor: Chen, Pei-Chung 陳 沛 仲 Student: Thanh-Nhan Nguyen 阮 誠 仁 2008,
Analog Comparator Positive input chooses bet. PB2 and Bandgap Reference. Negative input chooses bet. PB3 and the 8 inputs of the A/D. ACME= Analog Comparator.
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.
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.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
Arduino John Marcoux Christopher Lesch Thomas Dodge Unless otherwise noted, all information and pictures came from:
Digital Outputs LCD Display
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.
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang.
ARM Timers.
System Clocks.
A Few Words From Dilbert
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
Digital to Analogue Converter
ELE2MIC Lecture 21 The AVR Sleep Modes ATMEGA128’s Analog to Digital Converter –Features –Block Diagram –Clock Source –Input Sources –Interrupts –BandGap.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
Atmel Atmega128 Overview ALU Particulars RISC Architecture 133, Mostly single cycle instructions 2 Address instructions (opcode, Rs, Rd, offset) 32x8 Register.
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.
Timers and Scheduled Interrupts
Digital Control CSE 421.
Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.
TIMERS AND INTERRUPTS AVI SINGH KEVIN JOSE PIYUSH AWASTHI.
Analog to Digital Converter (ADC)
Analog to Digital Conversion - ADC Mark Neil - Microprocessor Course 1 Device Drivers – Measuring Voltages.
Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.
Pulse-Width Modulation: Simulating variable DC output
Sensing Algorithm using IR Sensor and ADC Soong-sil University. Robotics 기 정 두 원.
By: Shivanshi pandya(30) Ayushi chourasia (3) Pranavi chhikniwala(6)
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.
Application Case Study Christmas Lights Controller
Timers and Scheduled Interrupts
Why are Timer Functions Important?
Servos Elementary Robotics with Servos
Scrolling LCD using Arduino.
Timers and Event Counters
V.V.P. ENGINEERING COLLEGE,RAJKOT
Analog Comparator An analog comparator is available on pins PE2(AIN0), PE3(AIN1) The comparator operates like any other comparator. -when (+) exceeds (-)
Timer and Interrupts.
Microcontroller Applications
ADC,DAC and sensor interface
BITS EDU. CAMPUS , VARNAMA
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.
The Arduino Microcontroller: Atmel AVR Atmega 328
Timer/Counter Modified from Dr. Lam Phung’s Slides.
16-bit Timer/Counter1 Features
8-bit Timer/Counter0 with PWM
DC motor and PWM.
8-bit Timer/Counter2 with PWM and Asynchronous Operation
ADC and DAC Programming in AVR
Wave Generation and Input Capturing
Device Drivers – Digital Voltmeter
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
Presentation transcript:

Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011

Today’s Session  Introduction to Registers  ADC  Timers  Interrupts  Liquid Crystal Display (LCD)  PWM

Introduction  We have been using ready functions from the Arduino Library. They aren’t optimized and often don’t serve the requirements.  Timing in uC’s is critical. Code which seems to be correct and compiles without error may not work at all. You need to learn the timing aspects of the uC.  High Level Working of uC Hardware.  Learn to write your own optimized functions to interact with uC peripherals.

What’s inside a uC? AVR CPU Oscillator/ Clock Circuit Communicatio n(USART/SPI/I 2C/TWI) ADC Timer/PW M Memory Input/Outp ut

Registers  They are a small amount of storage available on the CPU whose contents can be accessed more quickly than storage available elsewhere.storage CPU  The CPU interacts and does all its operations using them.  In our case they are just 8 bit memory units which can be read/write quickly by the CPU.  They are used by the CPU to interact with the various hardware peripherals.  For more info:

Registers  Registers can be read and written by the user program.  They are used to control the hardware peripherals by the program.  The Atmega 328 has 8 bit registers.  Registers and their bits usually have intuitive names.

Registers  Why bother with registers?  They offer far greater control to the user.  Optimizing your algorithm.  Make best use of the hardware available.  Ready functions are rarely available.

Register  To modify the y bit of x register:  #define sbi(x,y) x |= _BV(y) //set bit  #define cbi(x,y) x &= ~(_BV(y)) //clear bit  #define tbi(x,y) x ^= _BV(y) //toggle bit  Bitwise Operators are used to manipulate bits here.  This has to be written before the void setup loop.  At any point of the code now if you wish to write to the yth bit of x register, y  can use commands like:  sbi(x,y) //will write 1 to the y bit  cbi(x,y) //will write 0 to the y bit  tbi(x,y) //will toggle the y bit

Analog to Digital converter  Analog to Digital Converter is a circuit used to convert analog input voltage to a digital value which can be used for further processing.  The Atmega 328 microcontroller being used on the Arduino Uno board features a 10 bit ADC hardware unit.  The ADC is connected to an 8-channel Analog Multiplexer which allows eight single-ended voltage inputs constructed from the pins of Port A. The single- ended voltage inputs refer to 0V (GND). On the UNO board only 6 of these pins are available named as A0, A1...A5.  ADC can be used in 2 modes: Single Conversion and free running.

Initialize ADC and read input Setting up the ADC ◦ Select reference voltage : REFS1:0 ◦ Select prescaler : ADPS2:0 ◦ Select output format (left adjust/right adjust) : ADLAR ◦ Enable the ADC : ADEN Reading an analog input ◦ Select input pin to read : MUX4:0 ◦ Start conversion : ADSC ◦ Wait for conversion to finish : ADSC ◦ Read the result registers : ADCH:ADCL

ADC  The ADC unit interact with the CPU using these registers.  ADMUX: 4 bits are used for selecting Input Pin at the multiplexer. 2 bits for VREF Selection  ADCSRA & ADCSRB: Used to select mode and prescalar, start conversion, enabling interrupts and other settings. Prescalar is the most important setting at this stage. You can vary the conversion time from uS.  ADCL & ADCH: 10 bit value obtained on conversion is stored here. The CPU access it from these registers.

ADC  The analogRead() function we used sets the prescalar resulting in conversion time ~100uS. The uC is idle all this time.  It uses the single conversion mode, without interrupts.  You could write your own function and set it to automatically start conversion at fixed times and interrupt on completion!  Read the ADC Section in the Atmega 328 datasheet for more information. We have uploaded a tutorial on the stab wiki as well.

Liquid Crystal Display (LCD) Alpha-numeric display with backlight 16 columns x 2 rows (larger sizes available too) 8-bit data interface, 3 control signals For pin-starved applications : 4-bit data interface +5V supply, separate power for backlight Readymade libraries available PTO

Working with multiple files 1. main.c – the main code 2. lcdroutines.c – has functions for LCD interfacing 3. lcdroutines.h – defines connections and function prototypes For more information, consult the datasheet of HD44870

Timers  A timer in simplest term is a register. Timers generally have a resolution of 8 or 16 Bits. So a 8 bit timer is 8Bits wide and capable of holding value within But this register has a magical property ! Its value increases/decreases automatically at a predefined rate (supplied by user). This is the timer clock. And this operation does not need CPU’s intervention.  It can be used to keep time accurately and upon certain condition take certain action or inform the CPU.  Atmega 328 has two 8-bit timers(Timer 0 and 2) and one 16-bit timer(Timer1).  Each T/C has different modes of operation : normal, CTC and PWM  Special waveform generation options : variable frequency pulses using CTC, variable duty-cycle pulses using PWM

void setup() { motion_init(); } motion_init() { TCCR1B=_BV(CS11)| _BV(CS10)| _BV(WGM12); TCCR0A = _BV(COM0B1) | _BV(WGM01) | _BV(WGM00); TCCR0B=(1<<WGM02)|(1<<CS00)|(0<<CS01)|(1<<CS02); TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); TCCR2B=(1<<WGM22)|(1<<CS20)|(1<<CS21)|(1<<CS22);

TIMSK0=_BV(OCIE0B); TIMSK2=_BV(OCIE2B); OCR2A = 250; OCR2B = 2; OCR0A = 250; OCR0B = 2; OCR1AH=0; OCR1AL=250; }

Interrupts Interrupt is a special “function” which gets called when a specific hardware condition is met When condition is met, an interrupt flag is set If interrupt is enabled, the interrupt flag goes to CPU CPU stops executing main program and jumps to an interrupt service routine (ISR) After ISR is done, CPU resumes main program from where it left PTO

ISR is written as follows: ISR(Vector name){ … Your code here return;}

Initializing a timer and interrupts  Select mode of operation : Normal  Select prescaler  Select the event which causes the interrupt  Set the time delay to interrupt every 250ms

Timers  We consider the 8 bit timers here. Both have similar characteristics.  There are 3 independent interrupts associated with these timers. On interruption control will be passed to the Interrupt Service Routine(ISR) for further action. They can be programmed to perform suitable tasks.  Overflow Interrupt(1): Can be set to interrupt when the timer overflows ie reaches the value 255.  Output Compare Interrupts(2): The timer value is continuously compared to Output Compare Registers and can be set to interrupt when there is a match.

8 bit-Timer0  Following Registers are used to control it:  TCCR0A and TCCR0B(Timer Counter Control Register): Used to decide mode of operation and prescaler of timer.  TCNT0: This register contains the current timer value.  OCR0A and OCR0B: Contain the 8 bit value which is continuously compared with the counter value.  TIMSK0: Used to set up the 3 interrupts.

Timers  The delay function uses timers to keep time.  The analogWrite function generates PWM using timers.  Each 8 bit timer can be used to generate two PWM’s as there are 2 Output Compare Units.  The PWM frequency and duty cycle can be changed by setting the registers suitably.  Refer to the Atmega 328 datasheet for exact register and bit details.

Controlling the brightness of an LED using Pulse Width Modulation (PWM)  Apply a variable duty-cycle high frequency clock to the LED  Duty-cycle decides the brightness of the LED  Timer 1 in PWM mode ◦ Special output pins – OC1A (PD5) and OC1B (PD4) ◦ Function of output pins depends on the compare output mode : COM1A and COM1B bits in TCCR1A ◦ No interrupt is needed

TOP t TCNT1 Overflow interrupt flag is set OCR1A 0 5V t 0V Compare match interrupt flag is set τ T ON

Fast PWM mode with non- inverted compare match output Initialize the timer ◦ Set timer to Fast PWM 8-bit mode ◦ Set compare output mode ◦ Set prescaler ◦ Set OC0 pin to output Initialize the ADC Use ADC result to change OCR0

Note: The next event is an :  Arduino Coding Competition(freshies) Arduino Coding Competition(freshies)  Date : 18th Jan, Tuesday