03-AVR Microcontroller Tutorial - Eng Mina
03-AVR Microcontroller Tutorial - Eng Mina
» 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 syntaxx =
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]
» ADLAR = 0 in ADMUX
» ADLAR = 1 in ADMUX
Embedded – C
• Analog to Digital Converter [ADC]:
– ADC Registers:
• ADCSRA [ADC Control and Status Register A]
– 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]