03 AVR Programming
03 AVR Programming
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
Watch Window
Disassembly Window
AVRStudio Simulator notes
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
Avr-objdump:
Extract information
from .elf – file
Here: show
disassembly
information
Where to find AVR C - specific information ?
…
Where to find AVR C - specific information ?
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
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.
4.27
Bitwise Shift Operators
• The bitwise shift operators are:
<< left shift
>> right shift
• 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) */
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 */
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;
};