You are on page 1of 12

1, G-Mj|__+G

TP __]q_ G-M __ _ +++++++ G



1. _j _ _ j ] __

AVRj I/O Port ] LED -] _j
j__ AVR j -Q _j
j] C ] Qj, |], |_jj
I/O Port|{ j j_] _[j _j ( PORTx, DDRx, PINx )
_[ ] [| j j _ _

2. _j _ __ j_

_j } (KEST-B1 })
Atmega8535 CPU, 5V]], 4MHz ] 8MHz Crystal 1`, 22pF _ __ 2`
Reset___ [_ 1`, 10K ]j 1`
2 color _LED 1` ] 1 color _LED 2` , 330 ]j 2`
WinAVR GCC |]
Text Editor ( _ j] j __jH_ )
PonyProg ] v.2.6 __ ( v.2.6 ___] ATmega8535 _])
ISP _j Serial Cable j RS232C[ j| , Window PC 1]

3. ]_ ] _
_ _j_q AVR_j _ j__ __ j_ j I/O Port __] ]_ j[ _j j
|_|. _ j, __ _} j |j [j ]|. j |j_q CPU, _[
Clock [ ] 4MHz, ] 8MHz Crystal 18~22pF __, 5V]] ] { _ Resetj
] [ [_ 10K pull-up ]j_ [|. _ _jj ]_ j_ LED ] |{
] 330 ]j j [j q|.
|_ _ _|j j] }H __], _ _j_ __ j`j LEDq AVR CPUj 1,2 ]
{ [|. _ PORTBj 0,1 ]_ _|. }j _ _ ] PB0, PB1_ |. _ gj j
_ _ I/O Portj ]_ _|q 0| _{ 7 _| _ 1byte -Qj|q __|. _
PORTj | _|q PB1_ _ PB0_ _|.
, , _| I/O Port ]_ j[j j __j _ | _[ ]j _}_.
3-1

1, G-Mj|__+G
I/O Port |{ j _[ _ _ [|.



_ j_] _[q PORTx _[ DDRx_[_|. _ x_ AVRj j| _, A,B,C,D,E
_ j| j q _ _ _j __ j|B __jq _{ PORTB, DDRB _jj
H j|.
I/O PORT ]_ j[_ ;_j.
]_ ]jq j] I/O pin_ I/O Port ]_ j[j j_q, |] j] I/O PORTj j ;
_j gq DDRx _[ _]] { g{ ]|. _ _jj _q PORTB __j
DDRB_[ { g{ jq], {j { g{ jq?
DDRx_[q PORTj ],j[ j ;_j gq Data Direction Registerj )_ j] _| 1
_H j] _|q j[_ ;_,, 0_ _ _|q j[_ _ j|. _ 1] _{ j[, 0]
_{ ][_ ;_q_|. _] j ;_ CPU ]j j||] |_ ;_ ]j,, PORT
]_ j[, ] ][_ ;_j] j_], j _| 4_|q j[ 4_|q ][_ ___
_] ;__ ]j|.
[], ]__ ][j _ _| Pull-up]j __jq _ [q], _] _ j j
-Q _| [][, | Pull-up ]j ]] AVR ,_ [|. _ ;_j ]_ _, j
3-2

1, G-Mj|__+G
_q SFIOR _[j PUD_| ;_j gH j|.
SFIOR _[j 2 _|, _ PUD_| 0 _H I/Oj| ][ ]] _ Pull-up]_ ]
, 1 _H Pull-up]_ _ j|. PUDq Pull-up disablej )__|.
Atmega8535j 1 I/O pin_ 5V ] j[ _,.
_ j __ LEDj Anode(+)Q_ j I/O]_ 5Vj ] _ High [{, 1 j[_,H ,
j LEDq _ j_` j|. _)` ]jq ] High [{ 5Vj[ } j_q, ]_ j[
q j] j|j j]] j 1__ ]_ { g] jH j|. _, _ _j_ PORTB
0x01 _H 1 ] j LEDq j_ 2 ] j LEDq _ _ [`j|.
, , j`j LED _j | j j_ {j ]_ PORTB { jq?
0x03 _|. _ 16_| _ 2_| _jj }_H, 0b00000011_ j|. _ 0 1 _| 1_
_H 8535j 1 2 ] j LEDq j_` q_|.
, _ LED |_ q j_ {]` j jq?
, PORTB j 0x00 _ 0 1 _| 0 _H LEDq _` q_|.









3-3

1, G-Mj|__+G
4. _j _ j -Q


SW1 reset SW
U19
Atmega8535_0
RST
9
XTAL2
12
XTAL1
13
GND
11
AVcc
30
AGND
31
ARef
32
VCC
10
PC0
22
PC1
23
PC2
24
PC3
25
PC4
26
PC5
27
PC6
28
PC7
29
PD0/RXD
14
PD1/TXD
15
PD2/INT0
16
PD3/INT1
17
PD4/OC1B
18
PD5/OC1A
19
PD6/ICP
20
PD7/OC2
21
PB0/T0
1
PB1/T1
2
PB2/AIN0
3
PB3/AIN1
4
PB4/SS
5
PB5/MOSI
6
PB6/MISO
7
PB7/SCK
8
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

1, G-Mj|__+G
5. _j _ ] )[

//**************************************************************************************
//
//
//
// ]]Q[[[_@ ) Atmega8535 KEST-B1 @ Kaywon University 2004
//
// by Professor S.J. Park
//
// Atmega8535 LED I/O test ver. 1.0
//
//**************************************************************************************


#include <avr/io.h>


#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 Q
//
#define LED_PORT PORTB
#define LED_DDR DDRB
#define LED0 PB0
#define LED1 PB1


//-----------------------------------------------------------------------------
//Delay j )

void UTIL_DelayUS(INT16U wUS)
{
INT16U wDelayLoops;
register INT16U i;

wDelayLoops = (wUS + 3) / 8;
for (i=0; i<wDelayLoops; i++);
}

void UTIL_DelayMS(INT16U wMS)
{
register INT16U i;

for (i=0; i<wMS; i++)
UTIL_DelayUS(2000);
}

//----------------------------------------------
// I/O PORT ] Qj pQ

void IO_Init(void)
{
outp(0xFF,DDRA);
outp(0xFF,DDRB);
3-5

1, G-Mj|__+G
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 Portj j ;_ j |] |_ __ IO_Init__ j|_ _j [|.

void IO_Init(void)
{
outp(0xFF,DDRA);
outp(0xFF,DDRB); // PORTBj _ _| j[_ ;_jq _
outp(0xFF,DDRC);
outp(0xFF,DDRD);
}

LED j,q ] _q main()j| jj for(;;) j] [] jj |_ _ __|.
int main(void)
{

IO_Init();


for(;;){

PORTB=0x01; // CPU 1 ] j LED (|. _ PORTB0 1 j[

UTIL_DelayMS(1000); // LED j_ _ _ j][ ) 1j Delay

PORTB=0x00; // CPU 1 ] j LED |. _ PORTB0 0 j[

UTIL_DelayMS(1000); // LED _ _ _ j][ ) 1j Delay


}


return TRUE;

}

3-6

1, G-Mj|__+G

6. WinAVR_ __q Makefile _
C)[ |] jj_q make[ __j,, _] make[ Makefile__ ;_ |] __j, _]
_ _j|. |] j _ _j _ makefile_ ;_j gH j|. _ _g jj`
q _j AVR CPUj _ ;_j gq |], g )[ |]j __ _ _ j] target|] ;
_j gq |]_|. j ;[ jH,
#MCU name
MCU =atmega8535 --------------------- __q AVR CPU ;_j gq j

#Output format. (can be srec, ihex, binary)
FORMAT =ihex --------------------- |] j _ ]{ _q { |]j j|

#Target file name (without extension).
TARGET =main --------------------- g )[ |]j _ ;_j gq j

makefile ]_ _

# MCU name
MCU = 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 the DOS/Win* filesystem matches both .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 look for include files here.
# Each directory must be seperated by a space.
EXTRAINCDIRS =


# Optional compiler flags.
# -g: generate debugging information (for GDB, or for COFF conversion)
# -O*: optimization level
# -f...: tuning, see gcc manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -ahlms: create assembler listing
CFLAGS = -g -O$(OPT) \
-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
-Wall -Wstrict-prototypes \
-Wa,-adhlns=$(<:.c=.lst) \
$(patsubst %,-I%,$(EXTRAINCDIRS))


# Set a " language standard" compiler flag.
# Unremark just one line below to set the language standard to use.
# gnu99 = C99 + GNU extensions. See GCC manual for more information.
3-7

1, G-Mj|__+G
#CFLAGS += -std=c89
#CFLAGS += -std=gnu89
#CFLAGS += -std=c99
CFLAGS += -std=gnu99



# Optional assembler flags.
# -Wa,...: tell GCC to pass this to the assembler.
# -ahlms: create listing
# -gstabs: have the assembler create line number information; note that
# for use in COFF files, additional information about filenames
# and function names needs to be present in the assembler source
# files -- see avr-libc docs [FIXME: not yet described there]
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs



# Optional linker flags.
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref



# Additional libraries

# Minimalistic printf version
#LDFLAGS += -Wl,-u,vfprintf -lprintf_min

# Floating point printf version (requires -lm below)
#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt

# -lm = math library
LDFLAGS += -lm




# Programming support using avrdude. Settings and variables.

# Programming hardware: alf avr910 avrisp bascom bsd
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
#
# Type: avrdude -c ?
# to get a full listing.
#
AVRDUDE_PROGRAMMER = stk500


AVRDUDE_PORT = com1 # programmer connected to serial devi ce
#AVRDUDE_PORT = lpt1 # programmer connected to parallel port

AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep

AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)

# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
# see avrdude manual.
#AVRDUDE_ERASE += -y

# Uncomment the following if you do /not/ wish a verification to be
# performed after programming the device.
#AVRDUDE_FLAGS += -V

# Increase verbosity l evel. Please use this when submitting bug
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
# to submit bug reports.
#AVRDUDE_FLAGS += -v -v




# ---------------------------------------------------------------------------

# Define directories, if needed.
DIRAVR = c:/winavr
DIRAVRBIN = $(DIRAVR)/bin
DIRAVRUTILS = $(DIRAVR)/utils/bin
DIRINC = .
DIRLIB = $(DIRAVR)/avr/lib


# Define programs and commands.
SHELL = sh

CC = avr-gcc

OBJCOPY = avr-objcopy
3-8

1, G-Mj|__+G
OBJDUMP = avr-objdump
SIZE = avr-size


# Programming support using avrdude.
AVRDUDE = avrdude


REMOVE = rm -f
COPY = cp

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



# Define Messages
# English
MSG_ERRORS_NONE = Errors: none
MSG_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 file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cl eaning 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 flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)



# Default target.
all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \
$(TARGET).lss $(TARGET).sym sizeafter finished end


# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings 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

1, G-Mj|__+G
--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.
program: $(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) $<
$(CC) -c $(ALL_CFLAGS) $< -o $@


# Compile: create assembler files from C source files.
%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $@


# Assemble: create object files from assembler source files.
%.o : %.S
@echo
@echo $(MSG_ASSEMBLING) $<
$(CC) -c $(ALL_ASFLAGS) $< -o $@






# Target: clean project.
clean: begin clean_list finished end

clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).eep
$(REMOVE) $(TARGET).obj
$(REMOVE) $(TARGET).cof
3-10

1, G-Mj|__+G
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).obj
$(REMOVE) $(TARGET).a90
$(REMOVE) $(TARGET).sym
$(REMOVE) $(TARGET).lnk
$(REMOVE) $(TARGET).lss
$(REMOVE) $(OBJ)
$(REMOVE) $(LST)
$(REMOVE) $(SRC:.c=.s)
$(REMOVE) $(SRC:.c=.d)


# Automaticall y generate C source code dependencies.
# (Code originall y taken from the GNU make user manual and modified
# (See README.txt Credits).)
#
# Note that this will work with sh (bash) and sed that is shipped with WinAVR
# (see the SHELL variable defined above).
# This may not work with other shells or other seds.
#
%.d: %.c
set -e; $(CC) -MM $(ALL_CFLAGS) $< \
| sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $@; \
[ -s $@ ] || rm -f $@


# Remove the '-' if you want to see the dependency files generated.
-include $(SRC:.c=.d)



# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion coff extcoff \
clean clean_list program


































3-11

1, G-Mj|__+G

_ j - _ } _

^f _nrcp 1,
?Tl ]]Q[[[_@ G M ) +++++++ G
] | } ] ] _ ]
























3-12