Моя тестовая программа: (константы регистров для кварца 16 МГц) .include "m168def.inc"
; r16, r17
; Interrupt Vectore Table
.cseg
.org 0x00 ; Reset-Address
rjmp Reset
.org ICP1addr ; Timer/Counter1 Capture Event
rjmp TIM1_CAPT
Init_TIM1:
lds r16,TCCR1B
sbr r16,193 ; CS10, ICNC1 (65 - выключен, 193 - включен), ICES1
sts TCCR1B,r16 ; timer clock = system clock - START Timer1
; Input Capture Noise Canceler (four samples)
; Input Capture Edge Select (rising)
ldi r16,1<<ICF1
out TIFR1,r16 ; Clear ICF1/clear pending interrupts
lds r16,TIMSK1
sbr r16,32 ; ICIE1
sts TIMSK1,r16 ; Timer/Counter1 Capture Event Interrupt
sbi DDRB,PB2 ; Set PB2 as output (Green LED)
sbi DDRB,PB4 ; Set PB4 as output (IR-LED)
cbi DDRB,PB0 ; Set PB0 as input (Timer1)
ldi r16, 20
out PORTB, r16 ; Включаем IR-диод (PB4=1),
; Выключаем встроенный светодиод (PB2=1)
ret
; Прерывание TIM1_CAPT
TIM1_CAPT:
cli ; Запрет прерываний
push r16
in r16,SREG
push r16
in r16,PORTB ; Читаем PORTB
cpi r16,20 ; Если встроенный светодиод выключен (PB2=1),
; IR-диод включен (PB4=1), то
breq LED_Enable
ldi r16, 20
out PORTB,r16 ; Выключаем встроенный светодиод (PB2=1)
rjmp Continue01
LED_Enable:
ldi r16,16
out PORTB,r16 ; Включаем встроенный светодиод (PB2=0)
Continue01:
ldi r16,'a';$61 ; передача символа "a"
rcall tx_hex_1
;rcall USART0_Transmit
ldi r16,'0' ; передача цифры "0"
rcall tx_hex_1
;rcall USART0_Transmit
pop r16
out SREG,r16
pop r16
sei ; Разрешаем прерывания
reti ; Выходим из прерывания TIM1_CAPT
Init_USART0:
;Asynchronous Normal mode
.equ N103=0x0067 ; 9600 baud
; Set baud rate
lds r16,UBRR0H
sbr r16,high(N103)
sts UBRR0H,r16
lds r16,UBRR0L
sbr r16,low(N103)
sts UBRR0L,r16
; 1X asynchronous operation
lds r16,UCSR0A
cbr r16,$00
sts UCSR0A,r16
; Enable receiver and transmitter
lds r16,UCSR0B
sbr r16,(1<<RXEN0)|(1<<TXEN0)
sts UCSR0B,r16
; Set frame format: 8 data, 1 stop bit
lds r16,UCSR0C
sbr r16,(1<<UCSZ01)|(1<<UCSZ00)
sts UCSR0C,r16
ret
;-----------------------------------------------------
; ТАК ТОЖЕ ПРОБОВАЛ
;-----------------------------------------------------
;USART0_Transmit:
;; Wait for empty transmit buffer
;lds r16,UCSR0A
;sbrs r16,UDRE0
;rjmp USART0_Transmit
;; Put data (r17) into buffer, sends the data
;lds r16,UDR0
;mov r16,r17
;sts UDR0,r16
;ret
;****************************************************************************
;* Send 2 hex digits to the UART. The byte to be sent must be in register
;* 'r16'.
;****************************************************************************
tx_hex_2:
mov r17, r16
swap r16 ;* Send high order nibble first
rcall tx_hex_1
mov r16, r17 ;* Send low order nibble next
; Fall through to tx_hex_1
;****************************************************************************
;* Send a hex digit to the UART. The digit to be sent will be the low
;* nibble of register 'r16'. The high nibble will be ignored.
;****************************************************************************
tx_hex_1:
andi r16, 0x0F
subi r16, ( - '0' ) ;* Convert 0-9 to ASCII
cpi r16, ( '0' + 10 ) ;* Was it >9 ?
brcs tx_byte
subi r16, ( '0' - 'A' + 10 ) ;* Convert A-F to ASCII
; Fall through to tx_byte
;****************************************************************************
;* Send a byte to the UART. The byte to be sent must be in register 'r16'.
;* This function will spin until the UART is ready to transmit.
;****************************************************************************
tx_byte:
; Wait for empty transmit buffer
lds r16,UCSR0A
sbrs r16,UDRE0
rjmp tx_byte
lds r16,UDR0
mov r16,r17
sts UDR0,r16
ret
; *** Begin of Program Execution ***
Reset:
; Initialization of the Stack Pointer
ldi r16,low(RAMEND)
out SPL,r16
ldi r16,high(RAMEND)
out SPH,r16
rcall Init_TIM1
rcall Init_USART0
sei ; Global Interrupts enabled
; Endlessloop
loopforever:
rjmp loopforever