Introduction To AVR Programming: Applied Electronics (28846) Fall 2014
Introduction To AVR Programming: Applied Electronics (28846) Fall 2014
Technology
Introduction to AVR
Programming
Applied Electronics (28846)
Fall 2014
Ali Shayei
Ali Shayei
Fuse Bits
Ali Shayei
Ali Shayei
Ali Shayei
Input/Output Ports
Ali Shayei
Ali Shayei
PORTB
Often bi-directional
Input
When
Output
When
Ali Shayei
Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. the
DDxn bits are accessed at the DDRx I/O address, the PORTxn bits at the PORTx
I/O address, and the PINxn bits at the PINx I/O address.
The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is
written logic one, Pxn is configured as an output pin. If DDxn is written logic
zero, Pxn is configured as an input pin.
If PORTxn is written logic one when the pin is configured as an input pin, the
pull-up resistor is activated. To switch the pull-up resistor off,PORTxn has to be
written logic zero or the pin has to be configured as an output pin. The port
pins are tri-stated when reset condition becomes active, even if no clocks are
running.
If PORTxn is written logic one when the pin is configured as an output pin, the
port pin is driven high (one). If PORTxn is written logic zero when the pin is
configured as an output pin, the port pin is driven low (zero).
Ali Shayei
9
Traditional Coding
DDRB = 0x3F
PORTB = 0x2F
Arduino
pinMode(pin_no., dir)
Ali Shayei
pinMode(3, OUTPUT);
10
Pin Voltages
Voltages
Ali Shayei
TTL
5 V (for HIGH)
0 V (for LOW)
3.3 V CMOS
0 V (for LOW)
11
pinMode(___,
______);
0
OUTPUT
Turn
on the LED
0
HIGH
digitalWrite(___,______);
Turn
ATmega328
Arduino
pin 0
(PD0)
digitalWrite(___,______);
0
LOW
Ali Shayei
12
3
INPUT
pinMode(__,
______);
Ali Shayei
ATmega328
Arduino
pin 3
(PD3)
Indeterminate!
13
ATmega328
VTG= +5V
digitalWrite(3,HIGH);
turns on
PD3
0
pinMode(3,INPUT_PULLUP);
Ali Shayei
VTG
14
Example 1
Arduino approach
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
Or if me106.h is used:
pinMode(PIN_D3, OUTPUT);
pinMode(PIN_D5, OUTPUT);
pinMode(PIN_D7, OUTPUT);
Alternate approach
DDRD = 0b10101000;
or
DDRD = 0xA8;
or
DDRD | = 1<<PD7 | 1<<PD5 | 1<<PD3;
Ali Shayei
15
Example 2
Arduino approach
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
Or if me106.h is used:
pinMode(PIN_D0, INPUT_PULLUP);
pinMode(PIN_D1, INPUT_PULLUP);
digitalWrite(PIN_D0, HIGH);
digitalWrite(PIN_D1, HIGH);
Ali Shayei
Alternate approach
DDRD = 0; // all PORTD pins inputs
PORTD = 0b00000011;
or
PORTD = 0x03;
or better yet:
DDRD & = ~(1<<PD1 | 1<<PD0);
PORTD | = (1<<PD1 | 1<<PD0);
16
setup()
loop()
setup()
loop()
like while(1) {}
Where is main() ?
Ali Shayei
18
Ali Shayei
19
Ali Shayei
20
Use the analogRead() function to read an analog value from a given pin:
void loop() {
int sensorValue = analogRead(A0);
// Note: A0 is predefined
.....
}
Ali Shayei
21
Ali Shayei
22
The term duty cycle describes the proportion of 'on' time to the
regular interval or 'period' of time
Ali Shayei
23
Ali Shayei
24
PWM in Arduino
Ali Shayei
25