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

03 AVR Programming

The document discusses bitwise operations in embedded systems using AVR microcontrollers. It provides examples of using logic operators like NOT, AND, OR, and XOR on bit patterns. It also demonstrates left and right shift operations, and how to use masks to set, clear, or flip bits in a pattern. Finally, it reviews other common bitwise operators like ~, &, |, ^ that can be used to access or manipulate individual bits in a microcontroller system.

Uploaded by

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

03 AVR Programming

The document discusses bitwise operations in embedded systems using AVR microcontrollers. It provides examples of using logic operators like NOT, AND, OR, and XOR on bit patterns. It also demonstrates left and right shift operations, and how to use masks to set, clear, or flip bits in a pattern. Finally, it reviews other common bitwise operators like ~, &, |, ^ that can be used to access or manipulate individual bits in a microcontroller system.

Uploaded by

Priscilla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

The ATmega

Software Down link


• AVR Studio
– https://atmel-studio.software.informer.com/do
wnload/
– http://studio.download.atmel.com/7.0.2389/as-
installer-7.0.2389-full.exe
Fix the IDE – Bug (update AVR-Studio)
Repeat blink.c source
Use the Simulator
Look at avr-gcc, avr-objdump, make
Binary Operations
Blink.c – sourcecode review
#include <avr/io.h> Include device specific definitions (SFRs,..)
#include <util/delay_basic.h>

#define Bit(x) (1<<x) Macro definition, shift left operation


void delay_ms(uint16_t ms) Function definition, call by value
{
uint16_t t; (call by reference: pointer)
for (t=0;t<ms;t++)
_delay_loop_2(1843); // 1843 * 4 clock cycles gives 7372 cycles:
// (approx. 1 millisecond at 7,3728 MHz
}

int main( void ) SFRs for Port D configuration


{
DDRD = Bit(5); // DataDirection Port D: Pin D5 = Output (Led)
PORTD= Bit(5); // Switch on led and deactivate pullups on the inputs

while (1)
{
PORTD ^= Bit(5); // toggle D5 Bit manipulation Port D (XOR)
delay_ms(500); // wait 500 milliseconds
}
return(0);
}
Using the AVRStudio - Simulator
AVRStudio Simulator Flow Control Peripherals
Overview

CPU
status

Peripheral
Details
AVRStudio Simulator
Overview

Breakpoint

Watch Window
AVRStudio Simulator
Overview

Register and Memory Views

Watch Window

Disassembly Window
AVRStudio Simulator notes

● Select the Crystal / Oscillator frequency to get


accurate time measurements in the Simulator

● beware of delay loops (they take long to simulate)

● use „run to cursor“ or breakpoints for getting into


interrupt handlers

● on-chip debugging is useful,


needs devices with JTAG interface
or debugWire interface
Using the WinAVR-Toolchain from commandline
WinAVR-Toolchain – avr-gcc.exe
Compile

Link

Convert .elf
to .ihex

Download
to Device

.elf:
Executable
and Linking
Format,
segment and
section
information
WinAVR-Toolchain – make.exe
MAKEFILE
SRC = blink.c test.c Variables & The makefile
CFLAGS= -O2 -g -Wall
constants automates the build
PRG = $(SRC:.c=.hex) process

all: $(PRG) Target only changed modules


are compiled
# Create .hex files from .o files
%.hex : %.o Environment
echo Creating $@ Rules variables
avr-objcopy -O ihex $< $@
add flexibility
# Compile
%.o : %.c
echo Compiling $@
avr-gcc $(CFLAGS) -mmcu=atmega8 $< -o $@ current target
clean:
rm -f *~ *.bak *.lst *.o *.hexc current prerequistite

Browse the GNU MAKE User Manual: http://www.gnu.org/software/make


WinAVR-Toolchain – avr-objdump.exe

Avr-objdump:

Extract information
from .elf – file

Here: show
disassembly
information
Where to find AVR C - specific information ?

● AVR-GCC is ANSI-C compatible

● browse the AVR Libc – Documentation

● examine module header files (#include <…>)


subdirectory avr/include

● use example code, play with open source firmware


Where to find AVR C - specific information ?
<assert.h>: Diagnostics
<ctype.h>: Character Operations Useful modules,
<errno.h>: System Errors
all documented in
<inttypes.h>: Integer Type conversions
<math.h>: Mathematics the AVR-Libc manual !
<stdint.h>: Standard Integer Types
<stdio.h>: Standard IO facilities
<stdlib.h>: General utilities
<string.h>: Strings, memory functions
<avr/boot.h>: Bootloader Support Utilities
<avr/eeprom.h>: EEPROM handling
<avr/interrupt.h>: Interrupts
<avr/pgmspace.h>: Program Space Utilities
<avr/power.h>: Power Reduction Management
<avr/io.h>: Special function registers and IO definitions
<avr/sleep.h>: Power Management and Sleep Modes
<util/delay_basic.h>: Basic busy-wait delay loops
<util/parity.h>: Parity bit generation
<util/twi.h>: TWI bit mask definitions
Where to find AVR C - specific information ?

Standard integer and character types, defined in <stdint.h>:


8, 16, 32 and 64 bit, signed and unsigned

• typedef signed char int8_t


• typedef unsigned char uint8_t
• typedef signed int int16_t
• typedef unsigned int uint16_t
• typedef signed long int int32_t
• typedef unsigned long int uint32_t
• typedef signed long long int int64_t
• typedef unsigned long long int uint64_t

#define INT8_MAX 0x7f Constants for


#define INT8_MIN (-INT8_MAX - 1) min and max values


Where to find AVR C - specific information ?

Some functions from <string.h>:

• void memcpy (void , const void , size_t)


• void memmove (void , const void , size_t)
• char strcat (char , const char )
• char strstr (const char , const char )
• char strupr (char )
Logic operations at pattern level
The same four operators (NOT, AND, OR, and XOR)
can be applied to an n-bit pattern. The effect is the
same as applying each operator to each individual bit
for NOT and to each corresponding pair of bits for the
other three operators. Figure 4.2 shows these four
operators with input and output patterns.

4.18 Figure 4.2 Logic operators applied to bit patterns


Use the NOT operator on the bit pattern 10011000.

Solution
The solution is shown below. Note that the NOT operator changes every 0 to 1
and every 1 to 0.
Use the AND operator on the bit patterns 10011000 and 00101010.

Solution
The solution is shown below. Note that only one bit in the output is 1, where
both corresponding inputs are 1s.
Use the OR operator on the bit patterns 10011001 and 00101110.

Solution
The solution is shown below. Note that only one bit in the output is 0, where
both corresponding inputs are 0s.

4.21
Use the XOR operator on the bit patterns 10011001 and 00101110.

Solution
The solution is shown below. Compare the output in this example with the one
in Example 4.5. The only difference is that when the two inputs are 1s, the
result is 0 (the effect of exclusion).

4.22
Applications

The four logic operations can be used to modify a bit


pattern.

 Complementing (NOT)

 Unsetting (AND)

 Setting (OR)

 Flipping (XOR)

4.23
Use a mask to unset (clear) the five leftmost bits of a pattern. Test the mask
with the pattern 10100110.

Solution
The mask is 00000111. The result of applying the mask is:
Use a mask to set the five leftmost bits of a pattern. Test the mask with the
pattern 10100110.

Solution
The mask is 11111000. The result of applying the mask is:

4.25
Use a mask to flip the five leftmost bits of a pattern. Test the mask with the
pattern 10100110.

The mask is 11111000. The result of applying the mask is:


SHIFT OPERATIONS

Shift operations move the bits in a pattern,


changing the positions of the bits. They can move
bits to the left or to the right. We can divide shift
operations into two categories: logical shift
operations and arithmetic shift operations.

4.27
Bitwise Shift Operators
• The bitwise shift operators are:
<< left shift
>> right shift

• The expression i << j represents i shifted left j positions,


zero-filled.

• The expression i >> j represents i shifted right j positions.


If i is of an unsigned type or if the value of i is not negative,
then zero bits are added at the left as needed. If i is
negative, the result depends on the implementation.
Bitwise Shift Operators
• For portability, it’s best to perform shifts only on unsigned
numbers.

• Examples:
unsigned short int i, j;
i = 13; /* i is now 13 (binary 0000000000001101) */
j = i << 2; /* j is now 52 (binary 0000000000110100) */
j = i >> 2; /* j is now 3 (binary 0000000000000011) */
Other Bitwise Operators
• The other bitwise operators are:
~ bitwise not
& bitwise and
^ bitwise xor
| bitwise or
Other Bitwise Operators
• Examples:
unsigned short int i, j, k;
i = 21; /* i is now 21 (binary 0000000000010101) */
j = 56; /* j is now 56 (binary 0000000000111000) */
k = ~i; /* k is now 65514 (binary 1111111111101010) */
k = i & j; /* k is now 16 (binary 0000000000010000) */
k = i ^ j; /* k is now 45 (binary 0000000000101101) */
k = i | j; /* k is now 61 (binary 0000000000111101) */

• Warning: Don’t confuse the bitwise operators & and | with


the logical operators && and ||.
Using the Bitwise Operators to
Access Bits
• Setting a bit. To set a bit, use a “mask” that contains a 1
bit in the desired position:

i |= 0x0010; /* sets bit 4 */

If the position of the bit is stored in a variable, use a shift


operator to create the

mask:
i |= 1 << j; /* sets bit j */
Using the Bitwise Operators to
Access Bits
• Clearing a bit. To clear a bit, use a mask that contains a 0
bit in the position to be cleared:
i &= 0xffef; /* clears bit 4 */

An alternative:
i &= ~ 0x0010; /* clears bit 4 */

The latter form is particularly useful if the position of the bit


is stored in a variable:
i &= ~ (1 << j); /* clears bit j */
Using the Bitwise Operators to
Access Bits
• Testing a bit. Testing a bit requires a mask containing a 1
bit:
if (i & 0x0010) … /* tests bit 4 */

To test whether bit j is set, use the following statement:


if (i & 1 << j) … /* tests bit j */
Using the Bitwise Operators to
Access Bit-Fields
• Dealing with several consecutive bits (a bit-field) is slightly
more complicated.

• Modifying a bit-field. Use a mask containing 0 bits in each


position to be modified:
i = i & 0xff8f | 0x0050; /* stores 101 in bits 4-6 */

If the new value of the bit-field is stored in a variable, the


variable must be shifted before the | operator is applied:
i = i & 0xff8f | j << 4; /* stores j in bits 4-6 */

The << operator has higher precedence than & and |, so


this statement is equivalent to
i = (i & 0xff8f) | (j << 4);
Using the Bitwise Operators to
Access Bit-Fields
• Retrieving a bit-field. Retrieving a bit-field at the right end
of a variable is easy:
j = i & 0x0007; /* retrieves bits 0-2 */

If the bit-field is not at the right end, extract the field using
the & operator, then shift the result to the right:
j = (i & 0x0070) >> 4; /* retrieves bits 4-6 */
Bit-Fields in Structures
• A structure may contain bit-fields—members whose size
is specified as a number
of bits:
struct file_date {
unsigned int day: 5;
unsigned int month: 4;
unsigned int year: 7;
};

• The order in which bit-fields are allocated within a word


depends on the implementation.
Visual C++ allocates bit-fields from right to left (the first bit-
field occupies the low-order bits of the word):
Bit-Fields in Structures
• A bit-field can be used just like any other member of a
structure:
struct file_date fd;
fd.day = 28;
fd.month = 12;
fd.year = 8;
After these assignments, fd has the following appearance:

• Bit-fields must have type int, unsigned int, or signed int.


(Using int by itself is ambiguous; some compilers treat the
high-order bit as a sign bit, while others don’t.)

You might also like