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

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

Embed Size (px)

Citation preview

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

Advanced uC Session

Advanced uC Session

Speaker : Chiraag JuvekarJan 13, 2011

Speaker : Chiraag JuvekarJan 13, 2011

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

Today’s SessionToday’s Session Introduction to Registers

ADC

Timers

Interrupts

Liquid Crystal Display (LCD)

PWM

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

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

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

What’s inside a uC?What’s inside a uC?

AVR CPU

Oscillator/Clock Circuit

Communication(USART/

SPI/I2C/TWI)

ADCTimer/PWM

MemoryInput/Output

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

RegistersRegisters They are a small amount of storage available

on the CPU whose contents can be accessed more quickly than storage available elsewhere.

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

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

RegistersRegisters

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.

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

RegistersRegisters

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.

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

RegisterRegister 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

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

Analog to Digital converter

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.

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

integer)nearest tooff (rounded

1024_

REF

IN

V

VDataADC

Page 11: Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011
Page 12: Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011

Initialize ADC and read input

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

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

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

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

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

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

Liquid Crystal Display (LCD)

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

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

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

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

TimersTimers 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

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

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

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

TIMSK0=_BV(OCIE0B);

TIMSK2=_BV(OCIE2B);

OCR2A = 250;

OCR2B = 2;

OCR0A = 250;

OCR0B = 2;

OCR1AH=0;

OCR1AL=250;

}

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

InterruptsInterrupts 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

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

ISR is written as follows:

ISR(Vector name){

… Your code here

return;}

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

Initializing a timer and interrupts

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

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

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

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

8 bit-Timer0 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.

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

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

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

Controlling the brightness of an LED using Pulse Width Modulation

(PWM)

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

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

TOP

t

TCNT1

Overflow interrupt flag is set

OCR1A

0

5V

t

0V

Compare match interrupt flag is set

τ

TON

1

11

11

1

TOP

AOCRcycleDuty

prescalerTAOCRT

prescalerTTOP

CLKON

CLK

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

Fast PWM mode with non-inverted compare match

output

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

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

Note:

The next event is an :

Arduino Coding Competition(freshies)

Date : 18th Jan, Tuesday