AVR-GCC SRAM 에서의변수및상수특별히속성을정하지않고변수를정의하면 SRAM 영역에저장된다. 이것들은외부메모리에저장되는경우에비하여빠르게엑세스할수있다. 축약표현은 <stdio.h> 에정의되어있다. 그러나, <io.h> 를인클루드하면내부적으로 <stdio.h> 파일을자동인클루드하도록연결되어있다. - AVR-GCC 에서데이터의형및이름축약하여표현방법 일반적인데이터형의표현 AVR-GCC 의데이터형축약표현 바이트수 표현범위 signed char int8_t 1-128 ~ +127 unsigned char uint8_t 1 0 ~ 255 signed int int16_t 2-32768 ~ +32767 unsigned int uint16_t 2 0 ~ 65535 signed long int32_t 4-2147483648 ~ +21474783647 unsigned long uint32_t 4 0 ~ 4294967295 signed long long int64_t 8-9.22 10^18 ~ +9.22 10^18 unsigned long long uint64_t 8 0 ~ 1.844 10^19 intptr_t 2-32768 ~ +32767 uintptr_t 2 0 ~ 65535 float 4 ±8.4 10^-37 ~ ±3.4 10^38 double 8 ±2.2 10^-308 ~ ±1.8 10^308 프로그램메모리에서의상수 AVR 의프로그램메모리인플래시메모리에상수를정의가능하며읽는것만가능하다. SRAM 에저장할수도있지만플래시메모리는 SRAM 에비하여용량이훨씬크므로유용하게사용할수있다. 아래의상수데이터표현을사용하려면 <pgmspace.h> 를인클루드하여야한다. - AVR-GCC 에서프로그램메모리상수를정의하는방법 AVR-GCC 에서프로그램메모리데이터의형상수 바이트수 표현범위 signed char prog_char, prog_int8_t 1-128 ~ +127 unsigned char prog_uchar, prog_uint8_t 1 0 ~ 255 signed int prog_int16_t 2-32768 ~ +32767 unsigned int prog_uint16_t 2 0 ~ 65535 signed long prog_int32_t 4-2147483648 ~ +21474783647 unsigned long prog_uint32_t 4 0 ~ 4294967295 signed long long prog_int64_t 8-9.22 10^18 ~ +9.22 10^18 unsigned long long prog_uint64_t 8 0 ~ 1.844 10^19 - AVR-GCC 에서프로그램메모리상수를읽는방법 프로그램메모리상수의형 16 비트어드레스를사용하여읽기 32 비트어드레스를사용하여읽기 prog_char prog_uchar prog_int8_t prog_uint8_t prog_int16_t prog_uint16_t prog_int32_t prog_uint32_t pgm_read_byte(address_short) pgm_read_byte_near(address_long) pgm_read_word(address_short) pgm_read_word_near(address_long) pgm_read_dword(address_short) pgm_read_dword_near(address_long) pgm_read_byte_far(address_long) pgm_read_word_far(address_long) pgm_read_dword_far(address_long) - 바이트상수의저장과읽기
prog_int8_t aaa = 1; int8_t value; value = pgm_read_byte(&aaa); - 바이트상수배열의저장과읽기 prog_int8_t array[] = 0, 1, 2, 3, 4; int8_t value; value = pgm_read_byte(&array[3]); - 문자열의저장과읽기 int8_t *str1 = PSTR( ABC DEF ); int8_t letter; letter = pgm_read_byte(str1 + 2); 다른방법으로는 int8_t str2[] _attribute_ ((progmem)) = ABC DEF ; int8_t letter; letter = pgm_read_byte(&str2[2]); EEPROM 의사용 AVR 에서 EEPROM 을엑세스하는것은 I/O 레지스터를사용하여야하므로 AVR-GCC 에서는 EEPROM 을엑세스하기위한특별한함수들을제공하는데, <eeprom.h> 에정의되어있다. 데이터의종류 EEPROM 읽기 EEPROM 쓰기 8 비트바이트 eeprom_read_byte() _EEGET() eeprom_write_byte() _EEPUT() 16 비트워드 eeprom_read_word() eeprom_write_word() 바이트블록 eeprom_read_block() eeprom_write_block() - EEPROM 의상태조사 eeprom_is_ready(); 리턴값이 1 이면 ready, 0 이면 busy 상태를나타낸다. - 8 비트바이트또는 16 비트워드의읽기, 쓰기 void eeprom_write_byte(uint8_t *addr, uint8_t value); void eeprom_write_word(uint16_t *addr, uint16_t value); uint8_t eeprom_read_byte(const uint8_t *addr); uint16_t eeprom_read_word(const uint16_t *addr); - SRAM 의 n 바이트의 *buf 버퍼값과 EEPROM 의지정된어드레스값과의읽고쓰기 void eeprom_write_block(const void *addr, void *buf, size_t n); void eeprom_read_block(void *buf, const void *addr, size_t n); - IAR C 컴파일러의호환 _EEGET(val, addr); _EEPUT(addr, val); I/O 레지스터및병렬 I/O 포트의액세스
AVR-GCC 에서는 I/O 레지스터또는확장 I/O 레지스터를액세스하기위한여러가지함수나매크로를지원하며, 헤더파일 <sfr_defs,h> 에정의되어있다. <io.h> 를인클루드하면자동으로인클루드가된다. - 8 비트 I/O 레지스터또는확장 I/O 레지스터의액세스 unsigned char a; PORTB = 0x80; a = PINB; - 특정비트의조사 #define bit_is_set(sfr, bit) #define bit_is_clear(sfr, bit) - 레지스터의특정비트의값을기다리는매크로 #define loop_until_bit_is_set(sfr, bit) #define loop_until_bit_is_clear(sfr, bit) 외부 I/O 나메모리의절대번지액세스 ex) #define keyword (*(volatile unsigned char *) 0x2000) 인터럽트처리 - 인터럽트서비스루틴함수 ISR(vector) - 모든사용하지않는인터럽트에대한인터럽트처리함수 ISR(_vector_default) - 다른인터럽트서비스루틴함수 SIGNAL(signame) 이방식은나중에폐지될예정이므로사용하지않는것이좋다. 예전에사용하던 INTERRUPT(signame) 형식은폐지되어서현재는사용하지않는다.
- 인터럽트처리함수를정의하는 vector 및 signame 번호 vector ( 새로운방식 ) signame ( 옛방식 ) 인터럽트발생조건 1 INT0_vect SIG_INTERRUPT0 External Interrupt Request 0 2 INT1_vect SIG_INTERRUPT1 External Interrupt Request 1 3 INT2_vect SIG_INTERRUPT2 External Interrupt Request 2 4 INT3_vect SIG_INTERRUPT3 External Interrupt Request 3 5 INT4_vect SIG_INTERRUPT4 External Interrupt Request 4 6 INT5_vect SIG_INTERRUPT5 External Interrupt Request 5 7 INT6_vect SIG_INTERRUPT6 External Interrupt Request 6 8 INT7_vect SIG_INTERRUPT7 External Interrupt Request 7 9 TIMER2_COMP_vect SIG_OUTPUT_COMPARE2 Timer/Counter2 Compare Match 10 TIMER2_OVF_vect SIG_OVERFLOW2 Timer/Counter2 Overflow 11 TIMER1_CAPT_vect SIG_INPUT_CAPTURE1 Timer/Counter1 Capture Event 12 TIMER1_COMPA_vect SIG_OUTPUT_COMPARE1A Timer/Counter1 Compare Match A 13 TIMER1_COMPB_vect SIG_OUTPUT_COMPARE1B Timer/Counter1 Compare Match B 14 TIMER1_OVF_vect SIG_OVERFLOW1 Timer/Counter1 Overflow 15 TIMER0_COMP_vect SIG_OUTPUT_COMPARE0 Timer/Counter0 Compare Match 16 TIMER0_OVF_vect SIG_OVERFLOW0 Timer/Counter0 Overflow 17 SPI_STC_vect SIG_SPI SPI Serial Transfer Complete 18 USART0_RX_vect SIG_UART0_RECV USART0 Rx Complete 19 USART0_UDRE_vect SIG_UART0_DATA USART0 Data Register Empty 20 USART0_TX_vect SIG_UART0_TRANS USART0 Tx Complete 21 ADC_vect SIG_ADC ADC Conversion Complete 22 EE_READY_vect SIG_EEPROM_READY EEPROM Ready 23 ANALOG_COMP_vect SIG_COMPARATOR Analog Comparator 24 TIMER1_COMPC_vect SIG_OUTPUT_COMPARE1C Timer/Counter1 Compare Match C 25 TIMER3_CAPT_vect SIG_INPUT_CAPTURE3 Timer/Counter3 Capture Event 26 TIMER3_COMPA_vect SIG_OUTPUT_COMPARE3A Timer/Counter3 Compare Match A 27 TIMER3_COMPB_vect SIG_OUTPUT_COMPARE3B Timer/Counter3 Compare Match B 28 TIMER3_COMPC_vect SIG_OUTPUT_COMPARE3C Timer/Counter3 Compare Match C 29 TIMER3_OVF_vect SIG_OVERFLOW3 Timer/Counter3 Overflow 30 USART1_RX_vect SIG_UART1_RECV USART1 Rx Complete 31 USART1_UDRE_vect SIG_UART1_DATA USART1 Data Register Empty 32 USART1_TX_vect SIG_UART1_TRANS USART1 Tx Complete 33 TWI_vect SIG_2WIRE_SERIAL Two-wire Serial Interface 34 SPM_READY_vect SIG_SPM_READY Store Program Memory Ready 기본헤더파일 1 C: WinAVR avr include 폴더에위치 2 C: WinAVR avr include avr 폴더에위치 3 C: WinAVR avr include compat 폴더에위치 4 C: WinAVR avr include util 폴더에위치 - WinAVR 에제공하는헤더파일 헤더파일파일의내용비고 assert.h Diagnostics 1 boot.h Bootloader support utilities 2 crc16.h CRC-16 computations 24 ctype.h Character operations 1 delay.h Busy-wait delay loops 24 eeprom.h EEPROM handling 2 errno.h System errors 1 ina90.h Compatibility header for IAR C compiler EWB 3.x 3
interrupt.h Interrupt handling 2 inttypes.h Integer type conversions 1 io.h AVR device-specific I/O definitions 2 iom128.h I/O definitions for various AVR microcontrollers 2 math.h Mathemathics 1 parity.h Parity bit generations 24 pgmspace.h Program space string utilities 2 portpins.h I/O port bit number definitions 2 setjmp.h Non local goto 1 sfr-defs.h Special function registers 2 signal.h 기능이모두 interrupt.h 파일로흡수되어사용하지않음 2 sleep.h Power management and sleep modes 2 stdint.h Standard integer types 1 stdio.h Standard I/O facilities 1 stdlib.h General utilities 1 string.h More string manipulation routines 1 twi.h TWI bit mask definitions 34 version.h avr-libc version macros 2 wdt.h Watchdog timer handling 2