Обработка прерываний по совпадению для отсчета интервала времени

в 1 секунду. Пороговое число: 43200; коэффициент делителя clk/256; число совпадений 1


.include "m128def.inc"

.equ CS_clk256 = 0x04; коэф.делителя

.equ cycles = 1; число переполнений

.equ presetup = 0; предварительная уст.сч.

.equ comp_value = 43200

.def temp = R16; рег.временного хранения

.def key_code = R17; код нажатой кл.

.def ovf_count = r18; счетчик совпадений

jmp RESET

.org $0018

jmp T1_COMPA; вектор прер.по совп. АТ1

.org $0046

RESET:

; инициализация стека

ldi temp,high(RAMEND)

out SPH,temp

ldi temp, low(RAMEND)

out SPL,temp

; инициализация порта В(кнопки В0-В2)

ldi temp,0xE0

out PORTB, temp; подтяжка 11100000

ldi temp, 0x00

out DDRB,temp; все выводы приемники

;инициализация порта Д

ldi temp,0xFF

out PORTD,temp; все диоды погашены

out DDRD,temp

; инициализация переменных

ldi ovf_count,0

ldi key_code,0

;инициализация таймера счетчика Т1

ldi temp,high(presetup)

out TCNT1H,temp

ldi temp,low(presetup)

out TCNT1L,temp

;инициализация регистра сравнения

ldi temp,high(comp_value)

out OCR1AH,temp

ldi temp,low(comp_value)

out OCR1AL,temp

ldi temp, (1<<OCIE1A)

out TIMSK,temp; разрешение прер.по совп.

Sei

ldi temp, (1<<CS12); clk /256

out TCCR1B,temp

Main:

in temp,PINB

andi temp,0xE0

sbrs temp,7

ldi key_code,4

sbrs temp,6

ldi key_code,2

sbrs temp,5

ldi key_code,1

rjmp Main

T1_COMPA:

inc ovf_count

sbrc ovf_count,5

rjmp bit_is_set

sbi PORTD,6

rjmp go_on

bit_is_set:

cbi PORTD,6

go_on:

cpi ovf_count,cycles

brne exit_int

com key_code

out PORTD,key_code

clr key_code

clr ovf_count

exit_int:

reti


 

 

Алгоритм работы программы:

 

Алгоритм обработки прерываний:

2 ) Программа по обнулению:


.include "m128def.inc"

.equ cycles = 42 ;chislo perepolneniya do 2 sek.

.equ presetup = 0 ;nachalo yst. t\ch=1

.def temp = R16

.def key_code = R17 ;kod nawatoyi clavishi

.def ovf_count = R18 ;chetchit perepolnen

; segment vectorov prerivaniya

jmp RESET

.org $001C

jmp T1_OVF

.org $0046 ; segment osnovnogo koda

RESET:

; initcializ steka

ldi temp, high(RAMEND)

out SPH, temp

ldi temp, low(RAMEND)

out SPL, temp

;iniz porta B

ldi temp , 0xE0

out PORTB, temp

ldi temp, 0x00

out DDRB, temp

;inizializatciya porta D(diodi)

ldi temp, 0xFF

out PORTD, temp

out DDRD, temp

;iniz global. peremennix

ldi ovf_count, 0

ldi key_code, 0

;iniz t\ch 1

ldi temp, high (presetup)

out TCNT1H, temp

ldi temp, low (presetup)

out TCNT1L, temp

;clk/8: CS1(2:0) = {010}

ldi temp , (1<<TOIE1)

out TIMSK, temp

sei

ldi temp , (1<<CS11)

out TCCR1B, temp

MAIN:

;chitivanie i fikcaciya koda nawatoi klavishi

in temp, PINB

andi temp, 0xE0

sbrs temp, 7

ldi key_code, 4

sbrs temp, 6

ldi key_code, 2

sbrs temp, 5

ldi key_code, 1

rjmp MAIN

;obrabotka preriv po perepopl T1

T1_OVF:

; indicator raboti t/ch 1

inc ovf_count

sbrc ovf_count, 5

rjmp go_on

bit_is_set :

cbi PORTD, 7

go_on:

cpi ovf_count, cycles

brne exit_int

com key_code

out PORTD,key_code

clr key_code

clr ovf_count

exit_int:

reti



Программа №3:

 


.include "m128def.inc"

.equ CS_clk256 = 0x04

.equ cycles = 1

.equ presetup = 43936

.equ comp_value = 43200

.def temp = R16

.def key_code = R17

.def ovf_count = r18

jmp RESET

.org $001c

jmp T1_OVF

.org $0046

RESET:

ldi temp,high(RAMEND)

out SPH,temp

ldi temp, low(RAMEND)

out SPL,temp

ldi temp,0xE0

out PORTB, temp

ldi temp, 0x00

out DDRB,temp

ldi temp,0xFF

out PORTD,temp

out DDRD,temp

ldi ovf_count,0

ldi key_code,0

ldi temp,high(presetup)

out TCNT1H,temp

ldi temp,low(presetup)

out TCNT1L,temp

;ldi temp,high(comp_value)

;out OCR1AH,temp

;ldi temp,low(comp_value)

;out OCR1AL,temp

ldi temp, (1<<TOIE1)

out TIMSK,temp

sei

ldi temp, (1<<CS12|1<<CS10)

out TCCR1B,temp

Main:

in temp,PINB

andi temp,0xE0

sbrs temp,7

ldi key_code,4

sbrs temp,6

ldi key_code,2

sbrs temp,5

ldi key_code,1

rjmp Main

T1_OVF:

inc ovf_count

sbrc ovf_count,5

rjmp bit_is_set

sbi PORTD,6

rjmp go_on

bit_is_set:

cbi PORTD,6

go_on:

cpi ovf_count,cycles

brne exit_int

com key_code

out PORTD,key_code

clr key_code

clr ovf_count

exit_int:

ldi R19,high(presetup)

out TCNT1H,R19

ldi R19,low(presetup)

out TCNT1L,R19

reti


 

 

Вывод: входе лабораторной работы были изучены основные принципы программирования таймера /счетчика Т1 в различных режимах его работы, а так же построен алгоритм работы программы прерывания по совпадению.