You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
4.0 KiB
Groff
113 lines
4.0 KiB
Groff
4 years ago
|
.TH "<avr/sfr_defs.h>: Special function registers" 3 "24 Jun 2019" "Version 2.0.0" "avr-libc" \" -*- nroff -*-
|
||
|
.ad l
|
||
|
.nh
|
||
|
.SH NAME
|
||
|
<avr/sfr_defs.h>: Special function registers \-
|
||
|
.SS "Modules"
|
||
|
|
||
|
.in +1c
|
||
|
.ti -1c
|
||
|
.RI "\fBAdditional notes from <avr/sfr_defs.h>\fP"
|
||
|
.br
|
||
|
.in -1c
|
||
|
.SS "Bit manipulation"
|
||
|
|
||
|
.in +1c
|
||
|
.ti -1c
|
||
|
.RI "#define \fB_BV\fP(bit) (1 << (bit))"
|
||
|
.br
|
||
|
.in -1c
|
||
|
.SS "IO register bit manipulation"
|
||
|
|
||
|
.in +1c
|
||
|
.ti -1c
|
||
|
.RI "#define \fBbit_is_set\fP(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))"
|
||
|
.br
|
||
|
.ti -1c
|
||
|
.RI "#define \fBbit_is_clear\fP(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit)))"
|
||
|
.br
|
||
|
.ti -1c
|
||
|
.RI "#define \fBloop_until_bit_is_set\fP(sfr, bit) do { } while (bit_is_clear(sfr, bit))"
|
||
|
.br
|
||
|
.ti -1c
|
||
|
.RI "#define \fBloop_until_bit_is_clear\fP(sfr, bit) do { } while (bit_is_set(sfr, bit))"
|
||
|
.br
|
||
|
.in -1c
|
||
|
.SH "Detailed Description"
|
||
|
.PP
|
||
|
When working with microcontrollers, many tasks usually consist of controlling internal peripherals, or external peripherals that are connected to the device. The entire IO address space is made available as \fImemory-mapped IO\fP, i.e. it can be accessed using all the MCU instructions that are applicable to normal data memory. For most AVR devices, the IO register space is mapped into the data memory address space with an offset of 0x20 since the bottom of this space is reserved for direct access to the MCU registers. (Actual SRAM is available only behind the IO register area, starting at some specific address depending on the device.)
|
||
|
.PP
|
||
|
For example the user can access memory-mapped IO registers as if they were globally defined variables like this:
|
||
|
.PP
|
||
|
.PP
|
||
|
.nf
|
||
|
PORTA = 0x33;
|
||
|
unsigned char foo = PINA;
|
||
|
.fi
|
||
|
.PP
|
||
|
.PP
|
||
|
The compiler will choose the correct instruction sequence to generate based on the address of the register being accessed.
|
||
|
.PP
|
||
|
The advantage of using the memory-mapped registers in C programs is that it makes the programs more portable to other C compilers for the AVR platform.
|
||
|
.PP
|
||
|
Note that special care must be taken when accessing some of the 16-bit timer IO registers where access from both the main program and within an interrupt context can happen. See \fBWhy do some 16-bit timer registers sometimes get trashed?\fP.
|
||
|
.PP
|
||
|
\fBPorting programs that use the deprecated sbi/cbi macros\fP.RS 4
|
||
|
|
||
|
.RE
|
||
|
.PP
|
||
|
Access to the AVR single bit set and clear instructions are provided via the standard C bit manipulation commands. The sbi and cbi macros are no longer directly supported. sbi (sfr,bit) can be replaced by sfr |= \fB_BV(bit)\fP .
|
||
|
.PP
|
||
|
i.e.: \fBsbi(PORTB, PB1)\fP; is now PORTB |= \fB_BV(PB1)\fP;
|
||
|
.PP
|
||
|
This actually is more flexible than having sbi directly, as the optimizer will use a hardware sbi if appropriate, or a read/or/write operation if not appropriate. You do not need to keep track of which registers sbi/cbi will operate on.
|
||
|
.PP
|
||
|
Likewise, cbi (sfr,bit) is now sfr &= ~(\fB_BV(bit)\fP);
|
||
|
.SH "Define Documentation"
|
||
|
.PP
|
||
|
.SS "#define _BV(bit) (1 << (bit))".PP
|
||
|
.nf
|
||
|
#include <avr/io.h>
|
||
|
.fi
|
||
|
.PP
|
||
|
.PP
|
||
|
Converts a bit number into a byte value.
|
||
|
.PP
|
||
|
\fBNote:\fP
|
||
|
.RS 4
|
||
|
The bit shift is performed by the compiler which then inserts the result into the code. Thus, there is no run-time overhead when using \fB_BV()\fP.
|
||
|
.RE
|
||
|
.PP
|
||
|
|
||
|
.SS "#define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit)))".PP
|
||
|
.nf
|
||
|
#include <avr/io.h>
|
||
|
.fi
|
||
|
.PP
|
||
|
.PP
|
||
|
Test whether bit \fCbit\fP in IO register \fCsfr\fP is clear. This will return non-zero if the bit is clear, and a 0 if the bit is set.
|
||
|
.SS "#define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))".PP
|
||
|
.nf
|
||
|
#include <avr/io.h>
|
||
|
.fi
|
||
|
.PP
|
||
|
.PP
|
||
|
Test whether bit \fCbit\fP in IO register \fCsfr\fP is set. This will return a 0 if the bit is clear, and non-zero if the bit is set.
|
||
|
.SS "#define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))".PP
|
||
|
.nf
|
||
|
#include <avr/io.h>
|
||
|
.fi
|
||
|
.PP
|
||
|
.PP
|
||
|
Wait until bit \fCbit\fP in IO register \fCsfr\fP is clear.
|
||
|
.SS "#define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit))".PP
|
||
|
.nf
|
||
|
#include <avr/io.h>
|
||
|
.fi
|
||
|
.PP
|
||
|
.PP
|
||
|
Wait until bit \fCbit\fP in IO register \fCsfr\fP is set.
|
||
|
.SH "Author"
|
||
|
.PP
|
||
|
Generated automatically by Doxygen for avr-libc from the source code.
|