Mạch AVR đơn giản

10 Apr

.

Mạch sử dụng chip AVR Atmega8515, phần mềm biên dịch Atmel AVRstudio


Mạch điện

IC1 (7805) giúp ổn áp, D1 để chống cắm ngược điện, tụ C2 và C3 luôn theo IC1. Tụ C4 dùng lọc nguồn.

Với kết nối ISP (mạch bé) , bạn có thể đẩy chương trình trực tiếp lên chíp để test.

Linh kiện

IC1: 7805

IC2: ATmega8515

C1: 1uF

C2=C3: 10 nF

C4: 1000uF/25V

D1: 1N4001

Mặt sau để nối dây

Thi công làm mạch

Chương trình

; ==================== Example no1 (in Assembly) ====================

; Test timers in ATmega8515

; every time that tc0 produces a match interrupt the counter cnt

; is incremented and output to portA.

; The square wave is present at pin no1 (B0)

.NOLIST

.INCLUDE “m8515def.inc”

.LIST

.def poA = r17 ; out buffer

.def poB = r18

.def poC = r19

.def poD = r20

.def poE = r21

.def cnt = r24 ; counter

; Code starts here

.CSEG

.ORG $0000

; Reset- and Interrupt-vectors

rjmp main ; 1 Reset-vector

rjmp IInt0 ; 2 External Interrupt Request 0

rjmp IInt1 ; 3 External Interrupt Request 1

rjmp TCpt1 ; 4 Timer/Counter1 Capture event

rjmp TCmp1 ; 5 Timer/Counter1 Compare matchA

rjmp TCmp2 ; 6 Timer/Counter1 Compare matchB

rjmp TOvf1 ; 7 Timer/Counter1 Overflow

rjmp TOvf0 ; 8 Timer/Counter0 Overflow

rjmp SrTrC ; 9 SPI/Serial transfer complete

rjmp URxCp ;10 USART Rx complete

rjmp UTxDe ;11Uart tx data register empty

rjmp UTxCp ;12 Uart Tx complete

rjmp AnaCp ;13 Analog comparator

rjmp XINT2 ;14 External Interrupt2

rjmp TCMch ;15 Timer / Counter0 Compare match

rjmp EERdy ;16 EEPROM ready

rjmp SPRdy ;17 Store Program Memory Ready

; ************** Interrupt service routines ********

IInt0: reti ; 2

IInt1: reti ; 3

TCpt1: reti ; 4

TCmp1: reti ; 5

TCmp2: reti ; 6

TOvf1: reti ; 7

TOvf0: reti ; 8

SrTrC: reti ; 9

URxCp: reti ;10

UTxDe: reti ;11

UTxCp: reti ;12

AnaCp: reti ;13

XINT2: ; external interrupt2

; In Extended MCU Interrupt Register EMCUIR

; bit0: ISC2: Interrupt Sense Control 2

; If ISC2=0 then the falling edge of INT2 generates the interrupt

; Provided that I-flag=1 in SREG and in bit5=INTF2=1 (interrupt-enable bit) in GICR.

reti ;14

TCMch: inc cnt

ldi r16,100

out OCR0,r16

ldi r16,1 ; set OCF0 flag for compare interrupt

out TIFR,r16

reti ;15

EERdy: reti ;16

SPRdy: reti ;17

; **************** End of interrupt service routines ***********

; ———————————————————————————-

; Timers notes

; T/C0 output OC0: pin1

; TCCR0 Timer/Counter Control register

; Bit7 = FOC0 = 0 in fast PWM mode

; Fast PWM: (bit6=WGM00, bit3=WGM01) = (1,1) = FAST pwm MODE

; clock select

; bits 2,1,0 = cs02, cs01, cs0 function

; 0 0 0 no source (stoped)

; 0 0 1 clock

; 0 1 0 clock/8

; 0 1 1 clock/64

; 1 0 0 clock/256

; 1 0 1 clock/1024

; (bit5,bit4)=(com01,com00)=(1,0) non-inverting mode oc0=0 on match and oc0=1 on bottom.

; so TCCR0 = 0110 1101 = $6d

; TIMSK Timer Interrupt Mask Register: set bit0 = Output Compare Match Interrupt Enable = OCIE0

; TIFR Timer Interrupt Flag Register: bit0 = ocf0 = Output Compare Flag 0 goes 1 when match occurs

;

; ******************** Main program ********************************

main: ldi r16,LOW(RAMEND) ;Initiate Stackpointer.

out SPL,r16

ldi r16,HIGH(RAMEND)

out SPH,r16

ldi r16,$ff

out ddrA,r16

out ddrB,r16

out ddrC,r16

out ddrD,r16

out ddrE,r16

com r16

out portA,r16

out portB,r16

out portC,r16

out portD,r16

out portE,r16

clr cnt

ldi r16,$6d ; TCCR0 set. clock/1024, fast PWM mode

out TCCR0,r16

ldi r16,1 ; set OCF0 flag for compare interrupt

out TIMSK,r16

sei

loop: out portA,cnt

rjmp loop

; ==================== Example2: (in Assembly) ====================

; Test timers in ATmega8515

; The pwm is changing it’s duty cycle between 0 anf 255 and output to pin1 (B0)

; The duty cycle value is stored in register “pwm”.

; The pwm is counting increasing and when it reaches 255 it

.NOLIST

.INCLUDE “m8515def.inc”

.LIST

.def cnt = r17 ; counter

.def pwm = r18

; Code starts here

.CSEG

.ORG $0000

; Reset- and Interrupt-vectors

rjmp main ; 1 Reset-vector

rjmp IInt0 ; 2 External Interrupt Request 0

rjmp IInt1 ; 3 External Interrupt Request 1

rjmp TCpt1 ; 4 Timer/Counter1 Capture event

rjmp TCmp1 ; 5 Timer/Counter1 Compare matchA

rjmp TCmp2 ; 6 Timer/Counter1 Compare matchB

rjmp TOvf1 ; 7 Timer/Counter1 Overflow

rjmp TOvf0 ; 8 Timer/Counter0 Overflow

rjmp SrTrC ; 9 SPI/Serial transfer complete

rjmp URxCp ;10 USART Rx complete

rjmp UTxDe ;11 Uart Tx data register empty

rjmp UTxCp ;12 Uart Tx complete

rjmp AnaCp ;13 Analog comparator

rjmp XINT2 ;14 External Interrupt2

rjmp TCMch ;15 Timer / Counter0 Compare match

rjmp EERdy ;16 EEPROM ready

rjmp SPRdy ;17 Store Program Memory Ready

; ************** Interrupt service routines ********

IInt0: reti ; 2

IInt1: reti ; 3

TCpt1: reti ; 4

TCmp1: reti ; 5

TCmp2: reti ; 6

TOvf1: reti ; 7

TOvf0: reti ; 8

SrTrC: reti ; 9

URxCp: reti ;10

UTxDe: reti ;11

UTxCp: reti ;12

AnaCp: reti ;13

XINT2: ; external interrupt2

; In Extended MCU Interrupt Register EMCUIR

; bit0: ISC2: Interrupt Sense Control 2

; If ISC2=0 then the falling edge of INT2 generates the interrupt

; Provided that I-flag=1 in SREG and in bit5=INTF2=1 (interrupt-enable bit) in GICR.

reti ;14

TCMch: tst pwm

brne tcm

inc cnt

tcm: sbrs cnt,0

rjmp tcm1

inc pwm

rjmp tcm2

tcm1: dec pwm

tcm2: out OCR0,pwm

ldi r16,1 ; set OCF0 flag for compare interrupt

out TIFR,r16

reti ;15

EERdy: reti ;16

SPRdy: reti ;17

; **************** End of interrupt service routines ***********

; ———————————————————————————-

; Timers notes

; T/C0 output OC0: pin1

; TCCR0 Timer/Counter Control register

; Bit7 = FOC0 = 0 in fast PWM mode

; Fast PWM: (bit6=WGM00, bit3=WGM01) = (1,1) = FAST pwm MODE

; clock select

; bits 2,1,0 = cs02, cs01, cs0 function

; 0 0 0 no source (stoped)

; 0 0 1 clock

; 0 1 0 clock/8

; 0 1 1 clock/64

; 1 0 0 clock/256

; 1 0 1 clock/1024

; (bit5,bit4)=(com01,com00)=(1,0) non-inverting mode oc0=0 on match and oc0=1 on

; bottom.

; TIMSK Timer Interrupt Mask Register: set bit0 = Output Compare Match Interrupt

; Enable = OCIE0

; TIFR Timer Interrupt Flag Register: bit0 = ocf0 = Output Compare Flag 0 goes 1

; when match occurs

;

; ******************** Main program ********************************

main:

ldi r16,LOW(RAMEND) ;Initiate Stackpointer.

out SPL,r16

ldi r16,HIGH(RAMEND)

out SPH,r16

ldi r16,$ff

out ddrA,r16

out ddrB,r16

out ddrC,r16

out ddrD,r16

out ddrE,r16

com r16

out portA,r16

out portB,r16

out portC,r16

out portD,r16

out portE,r16

clr cnt

clr pwm

ldi r16,$6a ; TCCR0 set. clock/256, fast PWM mode

out TCCR0,r16

ldi r16,1 ; set OCF0 flag for compare interrupt

out TIMSK,r16

sei

loop: rjmp loop

Theo cdtvn.net

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: