Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CodeVision AVR 1.25.7, user manual.pdf
Скачиваний:
228
Добавлен:
12.08.2013
Размер:
1.22 Mб
Скачать

CodeVisionAVR

3.13 Accessing the I/O Registers

The compiler uses the sfrb and sfrw keywords to access the AVR microcontroller’s I/O Registers, using the IN and OUT assembly instructions.

Example:

/* Define the SFRs */

sfrb PINA=0x19; /* 8 bit access to the SFR */ sfrw TCNT1=0x2c; /* 16 bit access to the SFR */

void main(void) { unsigned char a;

a=PINA; /* Read PORTA input pins */ TCNT1=0x1111; /* Write to TCNT1L & TCNT1H registers */

}

The addresses of I/O registers are predefined in the following header files, located in the ..\INC subdirectory:

tiny13.h

tiny22.h

tiny2313.h

tiny24.h

tiny25.h

tiny26.h

tiny261.h

tiny44.h

tiny45.h

tiny461.h

tiny84.h

tiny85.h

tiny861.h

90can32.h

90can64.h

90can128.h

90pwm2.h

90pwm2b.h

90pwm216.h

90pwm3.h

90pwm3b.h

90pwm316.h

90usb1286.h

90usb1287.h

90usb162.h

90usb646.h

90usb647.h

90usb82.h

90s2313.h

90s2323.h

90s2333.h

90s2343.h

90s4414.h

90s4433.h

90s4434.h

90s8515.h

90s8534.h

90s8535.h

mega103.h

mega128.h

© 1998-2007 HP InfoTech S.R.L.

Page 93

CodeVisionAVR

mega1280.h

mega1281.h

mega16.h

mega161.h

mega162.h

mega163.h

mega164.h

mega165.h

mega168.h

mega169.h

mega2560.h

mega2561.h

mega32.h

mega323.h

mega324.h

mega325.h

mega3250.h

mega329.h

mega3290.h

mega406.h

mega48.h

mega603.h

mega64.h

mega640.h

mega644.h

mega644p.h

mega645.h

mega6450.h

mega649.h

mega6490.h

mega8.h

mega8515.h

mega8535.h

mega88.h

43usb355.h

76c711.h

86rf401.h

94k.h

The header file, corresponding to the chip that you use, must be included at the beginning of your program.

Alternatively the io.h header file can be included. This file contains the definitions for the I/O registers for all the chips supported by the compiler.

© 1998-2007 HP InfoTech S.R.L.

Page 94

CodeVisionAVR

3.13.1 Bit level access to the I/O Registers

The bit level access to the I/O registers is accomplished using bit selectors appended after the name of the I/O register.

Because bit level access to I/O registers is done using the CBI, SBI, SBIC and SBIS instructions, the register address must be in the 0 to 1Fh range for sfrb and in the 0 to 1Eh range for sfrw. Example:

sfrb PORTA=0x1b; sfrb DDRA=0x18; sfrb PINA=0x19;

void main(void) {

/* set bit 0 of Port A as output */ DDRA.0=1;

/* set bit 1 of Port A as input */ DDRA.1=0;

/* set bit 0 of Port A output */ PORTA.0=1;

/* test bit 1 input of Port A */

if (PINA.1) { /* place some code here */ }; /* ....... */

}

To improve the readability of the program you may wish to #define symbolic names to the bits in I/O registers:

sfrb PINA=0x19;

#define alarm_input PINA.2 void main(void)

{

/* test bit 2 input of Port A */

if (alarm_input) { /* place some code here */ }; /* ....... */

}

It is important to note that bit selector access to I/O registers located in internal SRAM above address 5Fh (like PORTF for the ATmega128 for example) will not work, because the CBI, SBI, SBIC and SBIS instructions can’t be used for SRAM access.

© 1998-2007 HP InfoTech S.R.L.

Page 95