+ All Categories
Home > Documents > 교재3-LED 켜기

교재3-LED 켜기

Date post: 03-Jun-2018
Category:
View: 219 times
Download: 0 times
Share this document with a friend

of 12

Transcript
  • 8/12/2019 3-LED

    1/12

    1. AVR I/O Port LED AVR C , , I/O Port ( PORTx, DDRx, PINx )

    2. (KEST-B1 ) Atmega8535 CPU, 5V, 4MHz 8MHz Crystal 1, 22pF 2 Reset 1, 10K 1 2 color LED 1 1 color LED 2 , 330 2 WinAVR GCC Text Editor ( ) PonyProg v.2.6 ( v.2.6 ATmega8535) ISP Serial Cable RS232C Window PC 1

    3. AVR I/O Port

    . , . CPU,

    Clock 4MHz, 8MHz Crystal 18~22pF , 5V Reset

    10K pull-up . LED

    330.

    , LED AVR CPU 1,2

    . PORTB 0,1. PB0, PB1.

    I/O Port 0 7 1byte .

    PORT PB1 PB0.

    , I/O Port.

    3-1

  • 8/12/2019 3-LED

    2/12

    I/O Port .

    PORTx DDRx. x AVR, A,B,C,D,E

    B PORTB, DDRB

    .

    I/O PORT.

    I/O pin I/O Port, I/O PORT

    DDRx . PORTB

    DDRB, ?

    DDRx PORT , Data Direction Register 1

    , 0. 1, 0

    . CPU, PORT

    , , 4 4

    .

    , Pull-up ,

    , Pull-up AVR.

    3-2

  • 8/12/2019 3-LED

    3/12

    SFIOR PUD.

    SFIOR 2, PUD 0 I/O Pull-up

    , 1 Pull-up. PUD Pull-up disable.

    Atmega8535 1 I/O pin 5V .

    LED Anode(+) I/O 5V High , 1 ,

    LED. High 5V,

    1. , PORTB

    0x01 1 LED 2 LED.

    , LED PORTB?

    0x03 . 16 2 , 0b00000011 . 0 1 1

    8535 1 2 LED.

    LED?

    , PORTB 0x00 0 1 0 LED.

    3-3

  • 8/12/2019 3-LED

    4/12

    4.

    SW1 reset SW

    U19

    Atmega8535_0

    RST9

    XTAL212

    XTAL113

    GND11

    AVcc 30AGND 31ARef 32

    VCC10

    PC0 22PC1 23PC2 24PC3 25PC4 26PC5 27PC6 28PC7 29

    PD0/RXD14

    PD1/TXD15

    PD2/INT016

    PD3/INT117

    PD4/OC1B18

    PD5/OC1A19

    PD6/ICP20

    PD7/OC221

    PB0/T01

    PB1/T12

    PB2/AIN03

    PB3/AIN14

    PB4/SS5

    PB5/MOSI6

    PB6/MISO7

    PB7/SCK8

    PA0/AD0 40

    PA1/AD1 39

    PA2/AD2 38

    PA3/AD3 37

    PA4/AD4 36

    PA5/AD5 35

    PA6/AD6 34

    PA7/AD7 33

    10k

    2colorLED

    330

    VCC

    Y1

    4MHz

    C1

    22pF

    C2

    22pF

    VCC

    3-4

  • 8/12/2019 3-LED

    5/12

    5. //**************************************************************************************////

    //

    // Atmega8535 KEST-B1 @ Kaywon University 2004//// by Professor S.J. Park

    //// Atmega8535 LED I/O test ver. 1.0

    //

    //**************************************************************************************

    #include

    #define TRUE 1#define FALSE 0

    typedef unsigned char INT8U;typedef signed char INT8S;

    typedef unsigned int INT16U;

    typedef signed int INT16S;typedef unsigned long INT32U;

    typedef signed long INT32S;

    typedef float F32;typedef double F64;

    typedef unsigned char BOOL;

    //-----------------------------------------------------------------

    //

    // LED Port //#define LED_PORT PORTB

    #define LED_DDR DDRB#define LED0 PB0

    #define LED1 PB1

    //-----------------------------------------------------------------------------

    //Delay void UTIL_DelayUS(INT16U wUS)

    {INT16U wDelayLoops;

    register INT16U i;

    wDelayLoops = (wUS + 3) / 8;

    for (i=0; i

  • 8/12/2019 3-LED

    6/12

    outp(0xFF,DDRC);outp(0xFF,DDRD);

    }

    //----------------------------------------------

    int main(void)

    {

    IO_Init();

    for(;;){

    PORTB=0x01;

    UTIL_DelayMS(1000);

    PORTB=0x00;

    UTIL_DelayMS(1000);

    }

    return TRUE;

    }

    I/O Port IO_Init.

    void IO_Init(void){

    outp(0xFF,DDRA);

    outp(0xFF,DDRB); // PORTBoutp(0xFF,DDRC);

    outp(0xFF,DDRD);

    }

    LED, main() for(;;) .int main(void)

    {

    IO_Init();

    for(;;){

    PORTB=0x01; // CPU 1 LED. PORTB0 1

    UTIL_DelayMS(1000); // LED 1 Delay

    PORTB=0x00; // CPU 1 LED. PORTB0 0

    UTIL_DelayMS(1000); // LED 1 Delay

    }

    return TRUE;

    }

    3-6

  • 8/12/2019 3-LED

    7/12

    6. WinAVR Makefile C make, make Makefile,

    . makefile.

    AVR CPU, target

    . ,

    # MCU name

    MCU = atmega8535 --------------------- AVR CPU

    # Output format. (can be srec, ihex, binary)

    FORMAT = ihex ---------------------

    # Target file name (without extension).TARGET = main ---------------------

    makefile # MCU nameMCU = atmega8535

    # Output format. (can be srec, ihex, binary)FORMAT = ihex

    # Target file name (without extension).TARGET = main

    # Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization.# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)OPT = s

    # List C source files here. (C dependencies are automatically generated.)SRC = $(TARGET).c

    # If there is more than one source file, append them above, or modify and# uncomment the following:#SRC += foo.c bar.c

    # You can also wrap lines by appending a backslash to the end of the line:#SRC += baz.c \#xyzzy.c

    # List Assembler source files here.# Make them always end in a capital .S. Files ending in a lowercase .s# will not be considered source files but generated files (assembler# output from the compiler), and will be deleted upon " make clean"!# Even though t he DOS/Win* filesys tem matches bot h .s and .S the same,# it will preserve the spelling of the filenames, and gcc itself does# care about how the name is spelled on its command-line.

    ASRC =

    # List any extra directories to l ook for include files here.# Each directo ry must be seperated by a space.EXTRAINCDIRS =

    # Optional compiler flags.# -g: generate debugging infor mation (for GDB, or for COFF conversi on)# -O*: optimi zation level# -f...: tuning , see gcc manual and avr-libc documentati on# -Wall...: warnin g level# -Wa,...: tell GCC to pass this to the assembler.# -ahlms: c reate assembler listi ngCFLAGS = -g -O$(OPT) \-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \-Wall -Wstrict-prototypes \-Wa,-adhlns=$(

  • 8/12/2019 3-LED

    8/12

    #CFLAGS += -std=c89#CFLAGS += -std=gnu89#CFLAGS += -std=c99CFLAGS += -std=gnu99

    # Optional assembler flags.# -Wa,...: tell GCC to pass this to the assembler.# -ahlms: create list ing# -gstabs: have the assembler create line number infor mation; note that# for use in COFF files, addition al infor mation about filenames# and functi on names needs to be present in the assembler sourc e# files -- see avr-libc docs [FIXME: not yet describ ed there]

    ASFLAGS = -Wa,-adhlns=$(

  • 8/12/2019 3-LED

    9/12

    OBJDUMP = avr-objd umpSIZE = avr-size

    # Programming support using avrdude.AVRDUDE = avrdu de

    REMOVE = rm -fCOPY = cp

    HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hexELFSIZE = $(SIZE) -A $(TARGET).elf

    # Define Messages# EnglishMSG_ERRORS_NONE = Errors: noneMSG_BEGIN = -------- begin --------MSG_END = -------- end --------MSG_SIZE_BEFORE = Size before:MSG_SIZE_AFTER = Size after:MSG_COFF = Converting to AVR COFF:MSG_EXTENDED_COFF = Converting to AVR Extended COFF:

    MSG_FLASH = Creating load fil e for Flash:MSG_EEPROM = Creating load f ile f or EEPROM:MSG_EXTENDED_LISTING = Creating Extended Li sting:MSG_SYMBOL_TABLE = Creatin g Symbo l Table:MSG_LINKING = Linkin g:MSG_COMPILING = Comp ilin g:MSG_ASSEMBLING = As semblin g:MSG_CLEANING = Cleaning project:

    # Define all object files.OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)

    # Define all listing files.LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)

    # Combine all necessary flags and optional flags.# Add target processor to fl ags.

    ALL_CFL AGS = -mmcu=$(MCU) -I. $(CFLAGS)ALL_ASFLAGS = -mmc u=$(MCU) -I. -x assemb ler-w ith -cpp $(ASFLAGS)

    # Default target.all: begin gccvers ion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \

    $(TARGET).lss $(TARGET).sym si zeafter fini shed end

    # Eye candy.# AVR Studio 3.x does not check make's exit code but relies on# the following magic st rings to be generated by the compile job.begin:

    @echo@echo $(MSG_BEGIN)

    finished:@echo $(MSG_ERRORS_NONE)

    end:@echo $(MSG_END)@echo

    # Display size of file.sizebefore:

    @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi

    sizeafter:@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi

    # Display compiler version information.gccversion :

    @$(CC) --version

    # Convert ELF to COFF for use in debugging / simulating in# AVR Studio or VMLAB.COFFCONVERT=$(OBJCOPY) --debugging \

    --change-section-address .data-0x800000 \

    --change-section-address .bss-0x800000 \--change-section-address .noinit -0x800000 \

    3-9

  • 8/12/2019 3-LED

    10/12

    --change-section-address .eeprom-0x810000

    coff: $(TARGET).elf

    @echo@echo $(MSG_COFF) $(TARGET).cof$(COFFCONVERT) -O coff-avr $< $(TARGET).cof

    extcoff : $(TARGET).elf@echo@echo $(MSG_EXTENDED_COFF) $(TARGET).cof$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof

    # Program the device.progr am: $(TARGET).hex $(TARGET).eep

    $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)

    # Create final output files (.hex, .eep) from ELF output file.

    %.hex: %.elf@echo@echo $(MSG_FLASH) $@$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@

    %.eep: %.elf@echo@echo $(MSG_EEPROM) $@-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \--change-section-lma .eeprom=0 -O $(FORMAT) $< $@

    # Create extended listing file from ELF output file.%.lss: %.elf

    @echo@echo $(MSG_EXTENDED_LISTING) $@$(OBJDUMP) -h -S $< > $@

    # Create a symbol table from ELF output file.%.sym: %.elf

    @echo@echo $(MSG_SYMBOL_TABLE) $@avr-nm -n $< > $@

    # Link: create ELF output file from object files..SECONDARY : $(TARGET).elf.PRECIOUS : $(OBJ)%.elf: $(OBJ)

    @echo@echo $(MSG_LINKING) $@$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)

    # Compile: create object files from C source files.%.o : %.c

    @echo@echo $(MSG_COMPILING) $


Recommended