0% found this document useful (0 votes)
55 views

03-AVR Microcontroller Tutorial - Eng Mina

This document provides an overview of AVR microcontrollers and their architecture. It discusses topics such as microcontroller basics and differences between microcontrollers and microprocessors. It describes the architecture of AVR microcontrollers including RAM, ROM, GPIO, analog I/O, serial interfaces, timers/counters, and interrupt controllers. It also covers the C programming language used for embedded systems on AVR microcontrollers including variables, data types, operations, functions, interrupts, analog to digital conversion, and timers/counters.

Uploaded by

mohammed ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

03-AVR Microcontroller Tutorial - Eng Mina

This document provides an overview of AVR microcontrollers and their architecture. It discusses topics such as microcontroller basics and differences between microcontrollers and microprocessors. It describes the architecture of AVR microcontrollers including RAM, ROM, GPIO, analog I/O, serial interfaces, timers/counters, and interrupt controllers. It also covers the C programming language used for embedded systems on AVR microcontrollers including variables, data types, operations, functions, interrupts, analog to digital conversion, and timers/counters.

Uploaded by

mohammed ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

AVR Microcontroller

By, Eng. Mina G. Sadek


Why Microcontrollers
• Microcontrollers common applications
[Embedded Systems]
• Microcontroller Basics
o Microcontroller Overview & Basic Layout
• Microcontroller Vs Microprocessor
Microcontroller Architecture
• CISC Vs RISC – Page-21
–Speed (No. of clock cycles)
–Code size
–Instruction set
Microcontroller Architecture
• Von Neumann Arch. Vs Harvard Arch.
– Von Neumann:
» Slower
» One memory for both instr.s and data
» Less hardware
– Harvard:
» Faster
» Two separate memories for instr.s and data
» More hardware
Microcontroller Architecture
• RAM (SRAM Vs DRAM)
– SRAM:
» Flip-Flops or Transistors
» More Speed – one clock gives data [Fast access time]
» More Power Consumption
» More Cost
– DRAM:
» Transistors
» Less Speed – requires Refreshing [Slow access time]
» Less Power Consumption
» Less Cost
Microcontroller Architecture
• ROM – Masked Rom / Flash / EEPROM
– Masked ROM
» MCU is manufactured with its program and can't be
reprogrammed
– EEPROM
» Written Byte by Byte
» Fast to read
» Slow to erase/write
» More Cost
– Flash ROM
» Written Sector by Sector [ ]
Sector:256 Byte to 16KB

» Fast to read
» Moderate to erase/write
» Low Cost
Microcontroller Architecture
• Digital GPIO
–Sink Current – 1 mA
–Source Current – 10 uA
• Analog I/O [ADC]
–Resolution [No. of bits]
–Conversion time – Sampling rate
Microcontroller Architecture
• Serial Interface
–USART / UART
–SPI [Serial Peripheral Interface]
–IIC – I2C (Inter-Integrated Circuit) –
TWI (Two Wire Interface)
• Timers / Counters
• Interrupt Controller
Microcontroller Architecture
• AVR ATmega8 microcontroller biasing for
running
• Why C-For-Embedded?
– "C" Vs "Assembly"
Embedded – C
• Software Layers
– App
– MCAL (MicroController Abstract Layer)
– MCU
Embedded – C
• C – to – hex generation steps [Tool-Chain]:
– Editor
• File.c
• File.h
– Pre-Processor
• File.pre
– Compiler
• File.lst
• File.obj
– Linker [inputs: File.obj + libraries]
• File.hex
Embedded – C
• Pre-processor directives in Embedded-C
– File Inclusion
• #include
– Macro Substitution
• #define
– Conditional
• #ifdef – #elif – #else – #endif – #ifdef – #ifndef – #undef
– Other
• #pragma – #error – #line
Embedded – C
• Driver Files
– File.h
• Functions Declaration (Function Prototype)
• Extern int y;  public
• Void func2(void);  public
• Static func3(void);  private
– File.c
• Functions Implementation (Functions Definition)
• #include "File.h"
• Int y;
• Int x;  private
• Void func1(void);  private
Embedded – C
• Variable Scope
– File Scope
• Global variables are stored at RAM,
Variables are private over the module
Stored at Heap memory
– Function Scope {int x; }
• Local variables [considered as temporary data]
Variables are private over the function
Stored at Stack memory
– Prototype Scope void f1(int x, int y);
Embedded – C
• Important DataTypes
– Char  8-bit
– Int  compiler dependent (mostly 16-bit) –
problem
– Long  32-bit
– Float  32-bit
– Double  64-bit
– Signed & Unsigned
– Decimal vs ASCII
Embedded – C
• Numbering
– Binary: unsigned char a = 0b00000011;
– Hex: unsigned char a = 0x03;
– Decimal: unsigned char a = 3;
Embedded – C
• MCU pins:
– Digital GPIO
• DDRx
• PORTx
• PINx
– Analog inputs
• ADC0 – ADC5
– Communication
• UART [Tx, Rx]
• SPI [MOSI, MISO, SCK, CS]
• I2C (TWI) [SDATA, SCLOCK]
Embedded – C
• Program Main function
–Main() {
while(1)
{
//code
}
}
• While(1)  the super loop
Embedded – C
• TypeCasting
– Char x = 0xAA; int y = 0x5533;
– Y = x;  true syntax  y = 0x00AA;
Implicit Casting
– X = y; wrong syntaxx =
0x0033;correction x = (char)y;  Explicit
Casting
Embedded – C
• Operations
–Unary Operators:
• X++; & ++X; -- int x = 5; int y = 6;
• Y = x++ + ++x;  x = 7 -- y = 12
–Binary Operators:
•+ - * / %
Embedded – C
• Operations
– Trinary Operators:
• A ? b : c;
• If(a) {b} else {c}
– Operational Operators:
•> < >= <= == !=
• 0  false
• 1 or any other number  true
Embedded – C
• Operations
– Logical Operators:
• && AND
• || OR
•! NOT
•^ XOR
• << Shift Left
• >> Shift Right
Embedded – C
• Operations
– Bit-wise Operators [BIT Manipulation]:
•& AND  RESET(clear) bit
 x &= ~(1<<bit);
• | OR  SET bit  x |= (1<<bit);
• ~ NOT
• ^ XOR  Toggle bit x ^= (1<<bit);
• << SHL
• >> SHR
Embedded – C
• Functions:
– Normal Functions
– MACROS
• #define sum(x, y) x + y
• #define max (a, b) a>b?a:b
– Inline
• Inline void func1(void) { }
• Replace each call for the function with its code.
• Increase size, but, better timing.
Embedded – C
• Examples of MACROS functions:
– #define SET_BIT(ADDRESS, BIT)
ADDRESS |= (1<<BIT)
– #define RESET_BIT(ADDRESS, BIT)
ADDRESS &= ~(1<<BIT)
– #define TOGGLE_BIT(ADDRESS, BIT)
ADDRESS ^= (1<<BIT)
– #define READ_BIT(ADDRESS, BIT)
((ADDRESS & (1<<BIT))>>BIT)
Embedded – C
• External Interrupts: [ATMega8A-p.64]
– INT0, INT1
– MCUCR: MCU Control Register
• ISC00, ISC01, ISC10, ISC11
– GICR: General Interrupt Control Register
• INT0, INT1
– GIFR: General Interrupt Flag Register
• INTF0, INTF1
Embedded – C
• External Interrupts: [ATMega8A-p.64]
– ISR: Interrupt Service Routine
– Interrupt Vectors: [p. 44]
• INT0_vect
• INT1_vect
– Example:
• Read push button input.
Embedded – C
• Analog to Digital Converter [ADC]:
[ATMega328PA-p.250]

Embedded – C
• Analog to Digital Converter [ADC]:
– Resolution = 10-bit
– Steps = 2^Resolution = 2^10 = 1024 steps
– ADC Digital Value:
Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Channels:
• ADC0 – ADC5 [6 channels]
in the 28 pins package
• ADC0 – ADC7 [8 channels]
in the 32 pins package
– ADC Prescaler
– ADC Registers:
• ADMUX
• ADCSRA
• ADCH, ADCL
Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Registers:
• ADMUX [ADC Multiplexer Selection Register]

– 1. Reference Selection: REF1, REF0


Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Registers:
• ADMUX
– 2. Analog Channel Selection
» MUX0, MUX1, MUX2,
MUX3
– 3. ADLAR
[ADC Left Adjust Result]
Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Registers:
• ADCL, ADCH [ADC DATA Register]

» ADLAR = 0 in ADMUX

» ADLAR = 1 in ADMUX
Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Registers:
• ADCSRA [ADC Control and Status Register A]

– ADEN: ADC Enable


– ADSC: ADC Start Conversion
– ADIF: ADC Interrupt Flag
– ADIE: ADC Interrupt Enable
– ADPS2:0 : ADC Prescaler Select Bits
Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Registers:
• ADCSRA
– ADPS2:0 : ADC Prescaler Select Bits

» At CPU Frequency of 1MHz,


ADC Frequency range of 50kHz-200kHz,
We choose a prescaler of 8, Thus F_ADC = 1M/8 = 125kHz.
Embedded – C
• Analog to Digital Converter [ADC]:
– Coding
– ADC Initialization:
• Void adc_init()
{
// AREF = Avcc .. Internal
ADMUX = (1<<REF0);
// ADC Enable and prescaler of 8
// 1000000 / 8 = 125000
ADCSRA = (1<<ADEN)| (1<<ADPS1)|(1<<ADPS0);
}
Embedded – C
• Analog to Digital Converter [ADC]:
– Reading ADC value:
• Unsigned int adc_read(unsigned char ch)
{
// select the corresponding channel 0~7
ADMUX |= ch;
// start single convertion, by writing ’1′ to ADSC
ADCSRA |= (1<<ADSC);
// wait for conversion to complete, ADSC becomes
’0′ again, till then, run loop continuously
while(ADCSRA & (1<<ADSC));
return ADC;
}
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
Timer/Counter0: 8-bits
– TCCR0A: Timer/Counter 0 Control Register A
• COM0A1,COM0A0,COM0B1,COM0B0,WGM01,WGM00
– TCCR0B: Timer/Counter 0 Control Register B
• WGM02, CS00, CS01, CS02
– TCNT0: Timer/Counter Register [8-bit]
– TIMSK: Timer/Counter Interrupt Mask Register
• TOIE0: Timer/Counter 0 Overflow Interrupt Enable
– TIFR: Timer/Counter Interrupt Flag Register
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Timer/Counter0: 8-bits
• TCCR0B: Timer/Counter Control Register B
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Timer/Counter0: 8-bits
• TCNT0: Timer/Counter Register
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Timer/Counter0: 8-bits
• OCR0A: Output Compare Register A
• OCR0B: Output Compare Register B
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Timer/Counter0: 8-bits
• TIMSK0: Timer/Counter Interrupt Mask Register

– TOIE0: Timer/Counter 0 Over-Flow Interrupt Enable


– OCIE0A: Output Compare A Match Interrupt Enable
– OCIE0B: Output Compare B Match Interrupt Enable
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Timer/Counter0: 8-bits
• TIFR0: Timer/Counter Interrupt Flag Register

– TOV0: Timer/Counter0 Over-Flow Interrupt Flag


– OCF0A: Output Compare A Match Flag
– OCF0B: Output Compare B Match Flag
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Timer/Counter0: 8-bits
• TCCR0A: Timer/Counter Control Register A
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
(WGM02:0)
• 1. Normal Mode 0
– Interrupt after a time on overflow or on compare match
• 2. CTC Mode [Clear Timer On Compare Match] 2
– When interrupt on compare match, TCNT resets automatically
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
(WGM02:0)
• 3. Fast PWM Mode 3 or 7
– Generate PWM on OC0A (PD6) or OC0B (PD5) pins
– Duty cycle depends on the value stored in OCR0A for OC0A or
OCR0B for OC0B
» E.g. given OCR0A = 150, then the Duty Cycle is
180 / 255 = 0.705  which gives 70.5 % Duty Cycle
– Has 2 modes:
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
• 3. Fast PWM Mode 3 or 7
– Has 2 modes:
» A. Using the TOP value of the TCNT as the TOP of the
counter, that TCNT0 starts from 0 until 255 [0xFF]
(WGM02:0 = 3)
• OCR0A or OCR0B can be used for Duty Cycle on OC0A
or OC0B pins respectively.
• This limits the PWM frequency to depend only on the
prescaler value
E.g. for prescaler 64 and F_CPU 1 MHZ,
PWM Frequency = (1 MHZ / 64) / 256 = 61 HZ
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
• 3. Fast PWM Mode 3 or 7
– Has 2 modes:
» B. Using OCR0A as the TOP of the counter, that TCNT0
starts from 0 until OCR0A value (WGM02:0 = 7)
• Only OCR0B can be used for Duty Cycle on pin OC0B.
• Here, the PWM frequency depends on the value
stored in OCR0A, and it will be the max value that
TCNT0 can reach before resetting, so we can set the
exact frequency we want easily.
E.g. for prescaler 256, F_CPU 1 MHZ and OCR0A=78
PWM Frequency = (1 MHZ / 256) / 78 = 50 HZ
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
(WGM02:0)
• 3. Fast PWM Mode 3 or 7
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
(WGM02:0)
• 4. Phase Correct PWM Mode 1 or 5
– Differs from Fast PWM Mode in that:
Each cycle of the PWM has almost double time of its Fast
PWM mode, that TCNT0 starts from 0 incrementing up to 0xFF
then decrementing until it reaches 0 again.
– The counter counts repeatedly from BOTTOM to TOP and then
from TOP to BOTTOM.
– TOP is defined as 0xFF when WGM2:0 = 1, and OCR0A when
WGM2:0 = 5.
Embedded – C
• Timer/Counters: [ATMega8A-p.67]
– Modes of Operation:
(WGM02:0)
• 4. Phase Correct PWM Mode 1 or 5
Embedded – C
• Timer/Counters:
Embedded – C
• Timer/Counters:

– Examples:
• Blink a LED every 0.5 sec.
• Periodically Read push button input.
• Digital Clock
Embedded – C
• Timer/Counters:
– Examples:
• Blink a LED every 0.5 sec. – setting Timer0:
– Main clock source: 1MHz
– After 1024 prescaler:
» 1Mhz/1024 = 976.5 Hz  Clock source for Timer0
» 1/976.5 = 1.024 msec/clock  time of 1 clock
– Required 0.5 sec, then
» 0.5 sec/1.024 (msec/clock) = 488.28 clocks
» Then 488.28 clock are needed to reach 0.5 sec
– But, TCNT0 has is 256 count
» 488.28 / 255 = 1.9
» Then, we will need Timer0 to overflow 1.9 times
Embedded – C
• Timer/Counters:
– Coding
– Normal Mode using interrupt on Overflow:
• Void timer_init()
{
TCCR0A = 0x00; // normal mode of operation
TCCR0B = (1<<CS02) | (1<<CS00) // prescaler 1024
TIMSK0 = (1<<TOIE0);
}
• ISR (TIMER0_OVF_vect)
{ // OVF Service Routine }
Embedded – C
• Timer/Counters:
– CTC mode using OCR0A and Compare Match:
• Void timer_init()
{
TCCR0A = (1<<WGM01); // enable CTC mode
// either enable CTC mode, or reset TCNT0
manually in the ISR
TCCR0B = (1<<CS02) | (1<<CS00) // prescaler 1024
TIMSK0 = (1<<OCIE0);
}
• ISR (TIMER0_COMPA_vect)
{ // COMPA Service Routine }
Embedded – C
• Communication Peripherals:
– USART [Universal Synchronous Asynchronous
Receiver Transmitter]
• Tx, Rx
– SPI [Serial Peripheral Interface]
• MOSI, MISO, SCK, SS
– I2C [TWI: Two Wire Interface]
• SDA, SCL
Embedded – C
• Communication Peripherals:
– SPI [Serial Peripheral Interface]
• MOSI, MISO, SCK, SS
Embedded – C
• Communication Peripherals:
– I2C [TWI: Two Wire Interface]
• SDA, SCL
Embedded – C
• Communication Peripherals:
– USART [Universal Synchronous Asynchronous
Receiver Transmitter]
• USART:
In synchronous mode the device require both data and
clock. The clock is recovered from the data or an
external one which is in synchronous with data.
• UART:
In asynchronous mode the device requires only data.
The data clock is internally generated and synchronized
with start and stop bits embedded in the data received.
Embedded – C
• Communication Peripherals:
– UART: Hardware:
• TXD
• RXD
Embedded – C
• Communication Peripherals:
– UART: Hardware:
• Connection to PC
– Serial RS232
» MAX232
Embedded – C
• Communication Peripherals:
– UART: Hardware:
• Connection to PC
– USB
» FT232RL
Embedded – C
• Communication Peripherals:
– UART: Frame Format:
Embedded – C
• Communication Peripherals:
– UART: Baud Rate:
• UBRR [USART Baud Rate Register]

UBRR Contents of the UBRRH and UBRRL Registers, (0 -


4095)
Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.135]
• Initialization [USART Initialization]:
void USART_Init(unsigned int baud)
{
/* Set baud rate */
UBRRH = (unsigned char) (baud>>8);
UBRRL = (unsigned char) baud;
/* Enable Receiver and Transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.137]
• Data Transmission [USART Transmitter]:
void USART_Transmit(unsigned char data)
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}
Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.140]
• Data Reception [USART Receiver]:
unsigned char USART_Receive(void)
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) );
/* Get and return received data from buffer */
return UDR;
}
Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.144]
• Flushing the Receive Buffer [USART Flushing]:
void USART_Flush(void)
{
unsigned char dummy;
while ( UCSRA & (1<<RXC) ) dummy = UDR;
}
Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.150]
USART Register Description:
• UDR [USART I/O Data Register]:

• UCSRA [USART Control and Status Register A]:


Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.150]
USART Register Description:
• UCSRB [USART Control and Status Register B]:

• UCSRC [USART Control and Status Register C]:


Embedded – C
• Communication Peripherals:
– UART: Software: [ATMega8A-p.150]
USART Register Description:
• UBRRL & UBRRH [USART Baud Rate Registers]:
Embedded – C
• Pointers:
• Arrays:
Lab projects
• Lab:
– Write to an output pin
– Write to multiple pins
– Read from input pin (push button)
– Control multiple output pins
– ---
– Writing UART library
Lab projects
• Serial Communications
• Interface with keypad
• Interface with LCD
• Interface with Temperature Sensor (ADC)
Lab projects
• H.W
– Write an Embedded-C program to use the UART to
do some work when a certain character is
received from the pc & to send data to pc when a
certain event occurs.
– Build a library for ATMega8 for:
• 4 * 4 Keypad
• LCD
– Divide to teams with max 3 persons/team & each
team choose a project.
Lab projects
• Sample Projects:
– Oscilloscope (Embedded system that displays the
voltage it reads on a PC (C# or whatever))
– Function Generator (Embedded system that is
controlled by a keypad and potentiometers or by
PC to generate some signal as desired)

You might also like