Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011."— Presentation transcript:

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

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

3 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.

4 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

5 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: http://en.wikipedia.org/wiki/Processor_register http://en.wikipedia.org/wiki/Processor_register

6 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.

7 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.

8 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

9 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.

10

11

12 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

13

14 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 13-260uS.  ADCL & ADCH: 10 bit value obtained on conversion is stored here. The CPU access it from these registers.

15 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.

16 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

17 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

18 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 0-255. 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

19 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);

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

21 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

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

23 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

24 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.

25 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.

26 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.

27 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

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

29 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

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


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

Similar presentations


Ads by Google