Uživatel:Fridrto1

Z MAM wiki

Přejít na: navigace, hledání

[editovat] Semestrální práce

Cílem této práce bylo navrhnout generátor průběhů a to: pily, čtverce, trojuhelníku a sinu

Schema zapojeni

Vstup je z klávesnice kde tlačítka zapínají jednotlivé průběhy.

Výstup je na pinech B a za nimi je napěťový dělič


schema zapojeni


.INCLUDE "m88def.inc"
 .EQU CITAC = 128
 ;.def ven=r28;
  .ORG 0x0000 
 rjmp Main ; Reset Handler
 .ORG 0x0008 
 rjmp Vystup ; PCINT1 Handler
 




 ; Main program start

 Main: 


 ; Stack pointer init:
 ldi r16,high(RAMEND); Main program start
 out SPH,r16 ; Set Stack Pointer to top of RAM
 ldi r16,low(RAMEND)
 out SPL,r16


 ; Hardware initialization

;ser ven
;out DDRB,ven
;clr ven


sbi DDRB, 0 ; 
 sbi DDRB, 1 ; 
 sbi DDRB, 2
 sbi DDRB, 3
 sbi DDRB, 4 ; 
 sbi DDRB, 5 ; 
 sbi DDRB, 6
 sbi DDRB, 7
 ; Keyboard init:
 cbi DDRC, 2 ; set inputs for keyboard (COL1-3)
 cbi DDRC, 3
 cbi DDRC, 4
 
 sbi PORTC, 2 ; set internal Pull-Ups for keyboard
 sbi PORTC, 3
 sbi PORTC, 4
 
 sbi DDRD, 0 ; set driving outputs for keyboard
 sbi DDRD, 1 ; (ROW1-ROW4)
 sbi DDRD, 2
 sbi DDRD, 3

 cbi PORTD, 0	; log. 0 on all rows
 cbi PORTD, 1
 cbi PORTD, 2
 cbi PORTD, 3


 ;Nastaveni citace 

 ldi R24,0b00000010 ;mod citace, kdy cita do OCR0A
 out TCCR0A, R24
 ldi R17, CITAC	; load number of clock pulses for 1 PWM period
 out OCR0A, R17  

 ldi R17, 0b00000001 ; delicka vypnuta 
 out TCCR0B, R17 


 ; Keyboard interrupt setup
  ldi r26, PCICR ; dam adresu do R26 PCICR in Y low
 clr r27 ; load high byte with 0
 ldi r16,0b00000010 ; aktivace preruseni na pinech PCINT 14-8
 st X, r16 ; store new PCINT1
 ldi r26, PCMSK1 ; ulozim adresu PCMSK1
 clr r27 ; load high byte with 0
 ldi r16,0b00011100 ; Vyberu na kterych pinech PCINT 14-8 reaguju na preruseni 2,3,4
 st X, r16 ; store new PCMSK1
 sei ; Enable interrupts	
 

 ; Nastaveni spani
 ldi r16,0b00000001 ; Idle mode 
 out SMCR,r16 ; Sleep mode set



; nekonecna smycka cekajici na preruseni

 loop:
 sleep ; now AVR sleeps
 nop 
 rjmp loop ; and sleep again

 
 

; Prubehy
 sin:
; Nacteni hodnost sinu z tabulky
ldi ZL, LOW(2*Sinus)
ldi ZH, HIGH(2*Sinus)
lpm R18,Z+ ;nacti z pameti a zvets o jedna
CPI ZL, 32 ; porovnavam jestli sem nepretek mimo tabulku
brne Vystup
ldi ZL,0
rjmp Vystup

Sinus:
.DB 64,76,88,99,109,117,123,126
.DB 127,126,123,117,109,99,88,76
.DB 64,51,39,28,19,11,5,1
.DB 0,1,5,11,19,28,39,51



 pila:
inc R19; ;inkrementuju s pretecenim
rjmp Vystup

obdelnik:

ldi r29,0xFF
ldi r28,128
out PORTB,r29
inc r19
cpse r19,r28
rjmp obdelnik
rjmp nula

nula:
ldi r29, 0x00
out PORTB,r28
ldi r27,128
inc r28;
cpse r28,r27
rjmp nula
rjmp loop


troj:
ldi r29, 0xff
out PORTB,r28
inc r28
cpse r28,r29
rjmp troj
rjmp dolu

dolu:
out PORTB,r28
dec r28
cpse r28,r30
rjmp dolu
rjmp loop




Vystup:
rcall KEYPRESS
out PORTB,R19
reti


 KEYPRESS:
  
 KEY1:
 cbi 	PORTD, 0	;log. 0 to the row with 1, 2, 3, A
 sbi 	PORTD, 1	;log. 1 on another three
 sbi 	PORTD, 2
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY2
 rjmp   sin
 	
 KEY2:
 sbic	PINC, 3
 rjmp	KEY3
 rjmp	pila
 
  KEY3:
  sbic	PINC, 4
 rjmp	KEY4
 rjmp	obdelnik


  KEY4:
 sbi 	PORTD, 0
 cbi 	PORTD, 1	
 sbi 	PORTD, 2
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY7
 rjmp	troj
 	
 
 KEY7:
 sbi 	PORTD, 0
 sbi 	PORTD, 1
 cbi 	PORTD, 2	;
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	keyret
 rjmp zvys
 
 KEY8:
 sbic	PINC, 3
 rjmp keyret
 rjmp	sniz


 zvys:
 ldi R21,0xFF
 ldi r20, 128
 inc R20;
 out OCR0A, R20
 cpse r20,r21
 rjmp zvys
 rjmp keyret

 sniz:
 ldi R20,128
ldi R21,0x00
dec r20
 out OCR0A, R20
 cpse r20,r21
 rjmp sniz
 rjmp keyret


keyret:
 cbi PORTD, 0	; log. 0 on all rows for next key press catch
 cbi PORTD, 1
 cbi PORTD, 2
 cbi PORTD, 3 
ret



Alternativni reseni prubehu

; Prubehy
 sin:
; Nacteni hodnost sinu z tabulky
ldi ZL, LOW(Sinus)
ldi ZH, HIGH(Sinus)
lpm R18,Z+ ;nacti z pameti a zvets o jedna
CPI ZL, 32 ; porovnavam jestli sem nepretek mimo tabulku
brne Vystup
ldi ZL,0
rjmp Vystup





 pila:
ldi ZL, LOW(Pilaa)
ldi ZH, HIGH(Pilaa)
lpm R18,Z+ ;nacti z pameti a zvets o jedna
CPI ZL, 32 ; porovnavam jestli sem nepretek mimo tabulku
brne Vystup
ldi ZL,0
rjmp Vystup

obdelnik:

ldi ZL, LOW(Obdelnikk)
ldi ZH, HIGH(Obdelnikk)
lpm R18,Z+ ;nacti z pameti a zvets o jedna
CPI ZL, 32 ; porovnavam jestli sem nepretek mimo tabulku
brne Vystup
ldi ZL,0
rjmp Vystup

troj:
ldi ZL, LOW(Trojj)
ldi ZH, HIGH(Trojj)
lpm R18,Z+ ;nacti z pameti a zvets o jedna
CPI ZL, 32 ; porovnavam jestli sem nepretek mimo tabulku
brne Vystup
ldi ZL,0
rjmp Vystup


Vystup:
rcall KEYPRESS
out PORTB,R19
reti


 KEYPRESS:
  
 KEY1:
 cbi 	PORTD, 0	;log. 0 to the row with 1, 2, 3, A
 sbi 	PORTD, 1	;log. 1 on another three
 sbi 	PORTD, 2
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY2
 rjmp   sin
 	
 KEY2:
 sbic	PINC, 3
 rjmp	KEY3
 rjmp	pila
 
  KEY3:
  sbic	PINC, 4
 rjmp	KEY4
 rjmp	obdelnik


  KEY4:
 sbi 	PORTD, 0
 cbi 	PORTD, 1	
 sbi 	PORTD, 2
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY7
 rjmp	troj
 	
 
 KEY7:
 sbi 	PORTD, 0
 sbi 	PORTD, 1
 cbi 	PORTD, 2	;
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	keyret
 rjmp zvys
 
 KEY8:
 sbic	PINC, 3
 rjmp keyret
 rjmp	sniz


 zvys:
 ldi R21,0xFF
 ldi r20, 128
 inc R20;
 out OCR0A, R20
 cpse r20,r21
 rjmp zvys
 rjmp keyret

 sniz:
 ldi R20,128
ldi R21,0x00
dec r20
 out OCR0A, R20
 cpse r20,r21
 rjmp sniz
 rjmp keyret


keyret:
 cbi PORTD, 0	; log. 0 on all rows for next key press catch
 cbi PORTD, 1
 cbi PORTD, 2
 cbi PORTD, 3 
ret



Sinus:
.DB 64,76,88,99,109,117,123,126
.DB 127,126,123,117,109,99,88,76
.DB 64,51,39,28,19,11,5,1
.DB 0,1,5,11,19,28,39,51

pilaa:
.DB 0,8,16,32,40,48,56,64
.DB 72,80,88,96,104,112,120,128
.DB 136,144,152,160,168,172,180,188
.DB 196,204,212,220,228,236,244,252

obdelnikk:
.DB 0,0,0,0,0,0,0,0
.DB 0,0,0,0,0,0,0,0
.DB 255,255,255,255,255,255,255,255
.DB 255,255,255,255,255,255,255,255

trojj:
.DB 0,16,32,48,64,80,96,112
.DB 127,144,160,176,200,218,234,255
.DB 234,218,200,176,160,144,127
.DB 112,96,80,64,48,32,16,0



7 Segment

 

#include <avr/interrupt.h>
#include <avr/sleep.h>

#define SER		PD5
#define SRCLK	PD6
#define RCLK	PD7
#define SRCLR	PB0 // nastaveni serioveho mazani

// global constants and variables
// static code table used for 7-segment led display decoding (common anode)
/*unsigned char code [] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x9B, 0x92, 0x93, 0xF8, 0x80, 0x98,
				 		  0x88, 0x80, 0xC6, 0xC0, 0x86, 0x8E };*/
unsigned char code [] = { 0x3F, 0x06, 0x5B, 0x4F, 0x64, 0x6D, 0xFC, 0x07, 0x7F, 0x67,
				 		  0xF7, 0x7F, 0x39, 0x3F, 0x79, 0x71 };

// character to show on display
static volatile unsigned char anumber = 0;
// Interrupt vector table is created by compiler
// the ISR macro defines interrupt service handlers

// Pin change interrupt 1
// keyboard handler
ISR(PCINT1_vect)
{
	unsigned char keys[10];
	unsigned char nkeys = 0;
	register unsigned char column;

//    DDRD 	&= 0b00000001;
	DDRD	&= 0b11110001;
	PORTD	&= 0b11110000;		// set keyboard rows at port D to zero
	if (!(PINC & 4))
		keys[nkeys++] = 1;  // 4 8 16. kontroluju PINC,2,3,4 a to je 4 8 16
	if (!(PINC & 8))
		keys[nkeys++] = 2;
	if (!(PINC & 16))
		keys[nkeys++] = 3;

	DDRD	&= 0b11110000;
	DDRD	|= (1 << PD1);
	PORTD	&= 0b11110000;		// set keyboard rows at port D to zero
	if (!(PINC & 4))
		keys[nkeys++] = 4;
	if (!(PINC & 8))
		keys[nkeys++] = 5;
	if (!(PINC & 16))
		keys[nkeys++] = 6;

	DDRD	&= 0b11110000; // vynuluju 4 bity
	DDRD	|= (1 << PD2); //nastavim  PD2 do 1(Prvni bit do 1) prvni 4 piny sou seriova komunikace. NESMIM MENIT
	PORTD	&= 0b11110000;		// set keyboard rows at port D to zero
	if (!(PINC & 4))
		keys[nkeys++] = 7;
	if (!(PINC & 8))
		keys[nkeys++] = 8;
	if (!(PINC & 16))
		keys[nkeys++] = 9;

	DDRD	&= 0b11110000;
	DDRD	|= (1 << PD3);
	PORTD	&= 0b11110000;		// set keyboard rows at port D to zero
	if (!(PINC & 8))
		keys[nkeys++] = 0;

	DDRD 	|= 0b00001111;		// configure rows as outputs
	PORTD 	&= 0b11110000;		// set keyboard rows at port D to zero

	if (nkeys > 0) {
		OCR1A = keys[0] * 100;
		TCNT1 = 0; // v dobe stisku klavesy zacinam od 0
	}
}

// Timer/Counter1 Compare Match A
// the value shown on the display will be updated here
ISR(TIMER1_COMPA_vect)
{
	unsigned char tmp = code[anumber]; // prectu do tmp cislo co se ma poslat na 595
	volatile char loopcounter = 8;  // citac cyklu

	do {
		PORTD &= ~(1 << SRCLK);		//nuluju 	// assert 0 on serial clock line
		if (tmp & 0x80)				// 80=1000 0000 * xxxx xxx je li tam 1 tak to probehne jinak else	// test LSB
			PORTD |= (1 << SER);		// if 1, assert 1 on serial data line
		else
			PORTD &= ~(1 << SER);		// assert 0 otherwise
		PORTD |= (1 << SRCLK);			// assert 1 on serial clock line - rising edge writes bit into the serial shift register
		tmp <<=  1;				//<<= rotuju temp o 1 bit doleva. Krajni bit se nuluje	// shift right to proceed next bit
		loopcounter--; // aktualizace citace cyklu
	} while (loopcounter > 0); 
	PORTD &= ~(1 << RCLK);				// toggle parallel register clock 0-1 to make a rising edge
	PORTD |= (1 << RCLK);
	
	// update value to display for next time
	if (anumber < 15)
	    anumber++;
	else
		anumber = 0;
}

void main(void)
{
	// keyboard (and serial shift register connection) ports init
	DDRD 	|= 0b11101111;		// |= logicky soucet configure rows as outputs (PD0 ... PD3), serial shift register pins PD5 .. PD7
	PORTD 	= 0;				// set port D to zero
	DDRC	= 0;				// configure port C as input port
	PORTC	|= 0b00011100;		// activate pul-up resistors on three columns

	DDRB	|= (1 << SRCLR);	// configure PB0 as output - serial shoft register clear
	PORTB	|= (1 << SRCLR);	// toggle PB0 1-0-1 to clear the serial shift register
	PORTB	&= ~(1 << SRCLR);// | logicky soucin a ~ bitova negace. zmeni vsechny bity v bajtu
	PORTB	|= (1 << SRCLR);

	///Povolení pøerušení
	PCICR 	|= (1 << PCIE1);		// Zmìn pin se jmenem PCIE1 v PCICRna log 1 
	PCMSK1 	= 0b00011100;		// allow pin change interrupt on portC bits 2,3,4

	// timer configuration
	TCCR1A	= 0;				// OC1A/OC1B disconnected 0 0 0 0 , CTC bits WGM11, WGM10 0 0 
	TCCR1B 	= (1 << CS12) | (1 << CS10) | (1 << WGM12);// |logicky soucet vsech bitu co chci mit v 1 // preddelicka 1M/1024= 1ms
								// CTC mode, prescaler division factor 1024
	OCR1A 	=  976;				// timer compare value casovac nastaven na 1 vterinu 1M/1024

	TIMSK1 = (1 << OCIE1A);		// enable timer interrupt

	set_sleep_mode(SLEEP_MODE_IDLE);
	sei();						// Globální povolení pøerušení!!!
// cli zakázání pøerušení
	while (1) {
		sleep_cpu();
	}
} //po vykonani preruseni opet usne





7 segement v C x3 [[1]]

[[2]]

[[3]]

 

.include "m88def.inc"
.

///////////////////////////////




.equ PWM_door = 40	; PWMfreq 25  kHz pro osvetleni interieru
.equ PWM_ADC = 70	; PWMfreq 14.3kHz pro osvetleni palubi desky

/////////////////////////////
;reset a interruption vektory
/////////////////////////////

.org 0x000 
	rjmp Start 				; reset vektor
.org 0x00D
	rjmp Tim1_Ovf			; preruseni pri preteceni TMR1

;###############################################


Start:

////////////////////////////////////////////////
	;inicializace portu
////////////////////////////////////////////////
	ldi	r16, 0b011110		; PC0 je vstup ADC a PC5 je vstup pro tlacitko
	out	DDRC, r16			;
	ldi	r16, 0b000101		; vystupy do 0 a interni pull-up pro PC5
	out	PORTC, r16			; ok
	ser	r16				    ; PORTD a PORTB jako vystupy (implicitne v 0)
	out	DDRD, r16			; ok
	out	DDRB, r16			; ok
	clr	r16			    	; PORTD a PORTB jsou vsechny v 0
	out	PORTB, r16			; ok
	out	PORTD, r16			; ok

; vystupy su vsetky v 0, tak ako aj minuly a sucasny stav svetla (PB0 a PB1)

////////////////////////////////////////////////
	;inicializace PWM
////////////////////////////////////////////////
;TMR0 
	ldi r16, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
	out TCCR0A, r16		; 
	ldi r16, 0b00001001	; WGM2:1 (Fast PWM), CS2..0:001 (internal clock, prescale=1)
	out TCCR0B, r16
	ldi r16, PWM_door; load number of clock pulses for 1 PWM period
	out OCR0A, r16


;TMR2 
;nastaveni shodne jako TMR0, jen jiny zpusob, protoze registry nejsou dostupne pomoci "out" nepouzivam TIMER1 - je 16ti bitovej.
;timer 0 a 2 sou stejny 8bitovy
	ldi	r16, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
	sts	TCCR2A	, r16		; ok
	ldi	r16, 0b00001001	; WGM2:1 (Fast PWM), CS2..0:001 (internal clock, prescale=1)
	sts	TCCR2B, r16		; ok
	ldi	r16, PWM_ADC; load number of clock pulses for 1 PWM period
	sts	OCR2A, r16



////////////////////////////////////////////////
	;inicializace preruseni 
////////////////////////////////////////////////

 ldi r26, PCICR ; dam adresu do R26 PCICR in Y low
 clr r27 ; load high byte with 0
 ldi r16,0b00000010 ; aktivace preruseni na pinech PCINT 14-8
 st X, r16 ; store new PCINT1
 ldi r26, PCMSK1 ; ulozim adresu PCMSK1
 clr r27 ; load high byte with 0
 ldi r16,0b00011100 ; Vyberu na kterych pinech PCINT 14-8 reaguju na preruseni 2,3,4
 st X, r16 ; store new PCMSK1
 sei ; Enable interrupts	
 
 			

////////////////////////////////////////////////
	;inicializace ADC	str 255
////////////////////////////////////////////////		
	ldi	r16, 0b11100000 	; Zleva 1-vlastni 1,1V reference;1 s externim Cap na pinu AREF ; 1 zaokrouhleni doleva- asi fuk
							;  bit 4-nedefinovan; 3-0  nastaveni ADC u nas ADC0
	sts	ADMUX, r16			; ok
	ldi	r16, 0b10000110 	; ADC enabled, 3bit rozhoduje jestli to bude reagovat na preruseni 1 ano 0 ne,autotriger off,
							; prvni 3 bity 0-2 urcuje prescaler 2-128 My 64
	sts	ADCSRA, r16	    	; ok  ADC control and status register A
	clr	r16			    	; autotriger source -> autotriger je off, takze jen pro poradek

	sts	ADCSRB, r16	    	; ok

;#########################################
;HLAVNY PROGRAM  (probiha v interupte)
;#########################################
Main:
rjmp Main     ; nekonecna smycka, cekame na interupt


Tim1_Ovf:

; testovani tlacitka
    sbis PINC,2  ; preskoc ak je spinac vypli
	rjmp Tim1_Ovf
	rcall Zap
    sbic PINC,2  ; preskoc jestli je spinac zapli
	rjmp Tim1_Ovf
	rcall Vyp
	reti

;******************************************
; OBSLUHA TLACITKA
;******************************************

Zap:
ldi r16,0b00101000
out OCR0A, r16   ; zapnuti spinace
sbi PORTB,0   ; nastaveni minulyho stavu jako 1
ret

Vyp: ; vypnuty spinac
sbic PINB,0  ; preskoc jestli i v minulem kroku byl vypli
rcall Dim    ; 5s cekani + 2s zatemnovani
sbic PINB,0  
ldi r16,0b00000000
out OCR0A, r16   ; vypnuti spinace
cbi PORTB,0

ret


Dim:
ldi r16,0b00101000
out OCR0A, r16  ; nastaveni vystupu na zapli
rcall wait5sec
ldi r16,0b00100111
ldi r17,0b00000000
out OCR0A, r16


  	loop:   ; zatemnovaci smycka
   	 	rcall wait50ms    ;(2s/40 kroku v PWMku = 50ms)
		dec r16
		out OCR0A, r16
		cpse r16,r17 
	rjmp loop
  
cbi PORTB,0
ret

wait50ms:

ldi r18, 0b00000000    ; horni bit
ldi r19, 0b00000000    ; spodni bit
ldi r20, 0b11000011    ; pro porovnani horniho bitu, 50000 cyklu = 50ms
ldi r21, 0b00000000    ; pro porovnani preteceni spodniho bitu
loop1:
inc r19;
cpse r19,r21
inc r18
cpse r18,r20
rjmp loop1


ret

wait5sec:

ldi r21, 0b00000000  
ldi r22, 0b01100100  
loop3:
rcall wait50ms
inc r21;
cpse r21,r22
rjmp loop1

ret


Nevyzkousene osetreni stisku vice tlacitek


Pokus o nastaveni tretiho stavu. Dle datasheetu: PORTB=0 a DDRB=0 dá stav vysoké impedance



KEYPRESS:
 KEY0:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,1
 cbi     DDRD,2	

 cbi    DDRC,2
 cbi    DDRC,4
 ; toto by melo zajistit stav vysoke impedance vsude krom klavesy 0 	
 sbic	PINC, 3
 rjmp	KEY1
 ldi	R16, 0
 ldi R17, 0b0001100 ;st PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17		;pressed key 0
 rjmp	KEYRET

 KEY1:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,1	
 cbi     DDRD,2
 cbi     DDRD,3	

 cbi    DDRC,3
 cbi    DDRC,4	

 sbic	PINC, 2
 rjmp	KEY2
 ldi	R16, 1
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17		;pressed key 1
 rjmp	KEYRET

 KEY2:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,1
 cbi     DDRD,2	

 cbi    DDRC,2
 cbi    DDRC,4

 sbic	PINC, 3
 rjmp	KEY3
 ldi	R16, 2
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET

 KEY3:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,1	
 cbi     DDRD,2
 cbi     DDRD,3	

 cbi    DDRC,2
 cbi    DDRC,3

 sbic	PINC, 4
 rjmp	KEY4
 ldi	R16, 3
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET

 KEY4:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,2
 cbi     DDRD,3	

 cbi    DDRC,3
 cbi    DDRC,4	
 sbic	PINC, 2
 rjmp	KEY5
 ldi	R16, 4	
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17	;pressed key 4
 rjmp	KEYRET

 KEY5:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,2
 cbi     DDRD,3	

 cbi    DDRC,2
 cbi    DDRC,4	
 sbic	PINC, 3
 rjmp	KEY6
 ldi	R16, 5
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET

 KEY6:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,2
 cbi     DDRD,3	

 cbi    DDRC,2
 cbi    DDRC,3	
 sbic	PINC, 4
 rjmp	KEY7
 ldi	R16, 6
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET
 
 KEY7:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,1
 cbi     DDRD,3	

 cbi    DDRC,3
 cbi    DDRC,4		
 sbic	PINC, 2
 rjmp	KEY8
 ldi	R16, 7	
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17	;pressed key 7
 rjmp	KEYRET
 
 KEY8:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,1
 cbi     DDRD,3	

 cbi    DDRC,2
 cbi    DDRC,4	
 sbic	PINC, 3
 rjmp	KEY9
 ldi	R16, 8
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET
 
 KEY9:
 cbi 	PORTD, 0
 cbi 	PORTD, 1
 cbi 	PORTD, 2
 cbi 	PORTD, 3
 
 cbi     DDRD,0	
 cbi     DDRD,1
 cbi     DDRD,3	

 cbi    DDRC,2
 cbi    DDRC,3	
 sbic	PINC, 4
 rjmp	KEYRET
 ldi	R16, 9
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17





Osetreni ze na nule je nula a pote to opet funguje

.INCLUDE "m88def.inc"
 .EQU PWM_PULSES = 36	; value for 27.8 kHz PWM
 ;.EQU PWM_PULSES = 45	; value for 22.2 kHz PWM
 .ORG 0x0000 
 rjmp Main ; Reset Handler
 .ORG 0x0008 
 rjmp isr1 ; PCINT1 Handler

 ; Main program start

 Main: 

 ; Stack pointer init:
 ldi r16,high(RAMEND); Main program start
 out SPH,r16 ; Set Stack Pointer to top of RAM
 ldi r16,low(RAMEND)
 out SPL,r16


 ; Hardware initialization

 ; Keyboard init:
 cbi DDRC, 2 ; set inputs for keyboard (COL1-3)
 cbi DDRC, 3
 cbi DDRC, 4
 
 sbi PORTC, 2 ; set internal Pull-Ups for keyboard
 sbi PORTC, 3
 sbi PORTC, 4
 
 sbi DDRD, 0 ; set driving outputs for keyboard
 sbi DDRD, 1 ; (ROW1-ROW4)
 sbi DDRD, 2
 sbi DDRD, 3

 cbi PORTD, 0	; log. 0 on all rows
 cbi PORTD, 1
 cbi PORTD, 2
 cbi PORTD, 3


 ; Timer controlled PWM init:
 ;
 ; There are registers TCCR0A and TCCR0B for config, DDRD for output bit
 ; enabling, OCR0A for counter lenght and OCR0A for pulse "high" lenght.
 ; Register TIMSK0 controlls interrupts - not used, 0x00 by default 
 
 PWM_INIT:
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17			; 
 ldi R17, 0b00001001	; WGM2:1 (Fast PWM), CS2..0:001 (internal clock, prescale=1)
 out TCCR0B, R17
 ldi R17, PWM_PULSES	; load number of clock pulses for 1 PWM period
 out OCR0A, R17   

 sbi DDRD, 5	; set pin 11 as PWM output OC0B (PD5 pin)


 ; Keyboard interrupt setup
 ; Activation of pin change interrupt - PROBLEM!!! PCICR and PCIMSK are 
 ; extended I/O registers (0x68 and 0x6C), and must be handled as 
 ; a memory location
 ldi r26, PCICR ; load address of PCICR in Y low
 clr r27 ; load high byte with 0
 ldi r16,0b00000010 ; activate PCINT1
 st X, r16 ; store new PCINT1
 ldi r26, PCMSK1 ; load address of PCMSK1 in Y low
 clr r27 ; load high byte with 0
 ldi r16,0b00011100 ; allow pin change interrupt on portC bits 2,3,4
 st X, r16 ; store new PCMSK1
 sei ; Enable interrupts

 ; Set sleep mode of the CPU
 ; SMCR: 0,0,0,0,SM2,SM1,SM0,SE   SM2..0: Sleep Mode Select Bits, SE: Sleep enabled)
 ldi r16,0b00000001 ; Idle mode 
 out SMCR,r16 ; Sleep mode set


 ; Sleep loop, wake up by the Interrupt, return with RETI back to sleep loop

 loop:
 sleep ; now AVR sleeps
 nop ; return from Interrupt to this instruction
 rjmp loop ; and sleep again

 ; PCINT0 Service Routine:
 ;
 ; To do: *** rpm reading, regulation, etc.
 
 isr1: 
 rcall KEYPRESS
 mov R17, R16
 lsl R17
 lsl R17	; nasobeni x4 cisla 6-9


;dyz 0, tak ne. zakazat propojeni OCOB na pin 11 a poslat 0 




 out OCR0B, R17  ; output pulse "high" lenght to PWM compare unit 

 reti ; return from Interrupt
 


 ; Keyboard decoding:

 KEYPRESS:
 KEY0:
 sbi 	PORTD, 0
 sbi 	PORTD, 1
 sbi 	PORTD, 2
 cbi 	PORTD, 3	;log. 0 to the row with *, 0, #, D	
 sbic	PINC, 3
 rjmp	KEY1
 ldi	R16, 0
 ldi R17, 0b0001100 ;st PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17		;pressed key 0
 rjmp	KEYRET

 KEY1:
 cbi 	PORTD, 0	;log. 0 to the row with 1, 2, 3, A
 sbi 	PORTD, 1	;log. 1 on another three
 sbi 	PORTD, 2
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY2
 ldi	R16, 1
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17		;pressed key 1
 rjmp	KEYRET

 KEY2:
 sbic	PINC, 3
 rjmp	KEY3
 ldi	R16, 2
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET

 KEY3:
 sbi 	PINC, 4
 sbic	PINC, 4
 rjmp	KEY4
 ldi	R16, 3
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET

 KEY4:
 sbi 	PORTD, 0
 cbi 	PORTD, 1	;log. 0 to the row with 4, 5, 6, B
 sbi 	PORTD, 2
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY5
 ldi	R16, 4	
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17	;pressed key 4
 rjmp	KEYRET

 KEY5:
 sbic	PINC, 3
 rjmp	KEY6
 ldi	R16, 5
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET

 KEY6:
 sbic	PINC, 4
 rjmp	KEY7
 ldi	R16, 6
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET
 
 KEY7:
 sbi 	PORTD, 0
 sbi 	PORTD, 1
 cbi 	PORTD, 2	;log. 0 to the row with 7, 8, 9, C
 sbi 	PORTD, 3	
 sbic	PINC, 2
 rjmp	KEY8
 ldi	R16, 7	
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17	;pressed key 7
 rjmp	KEYRET
 
 KEY8:
 sbic	PINC, 3
 rjmp	KEY9
 ldi	R16, 8
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17
 rjmp	KEYRET
 
 KEY9:
 sbic	PINC, 4
 rjmp	KEYRET
 ldi	R16, 9
 ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
 out TCCR0A, R17

 KEYRET:
 cbi PORTD, 0	; log. 0 on all rows for next key press catch
 cbi PORTD, 1
 cbi PORTD, 2
 cbi PORTD, 3 
ret


example LED-show program for ATtiny 2313
2-color LED with 300 Ohm resistor in series is supposed to be
connected between pins 12 (PB0, LED_X) and 13 (PB1, LED_Y).
Color1 shines when LED_X is high and LED_Y is low
Color2 shines when LED_Y is high and LED_X is low
LED is off when LED_Y and LED_X are both low or both high

.EQU PINB = $16 .EQU DDRB = $17 ; DDRB address .EQU PORTB = $18 ; PORTB address


.EQU PIND = $10 .EQU DDRD = $11 .EQU PORTD = $12


.EQU LED_X = 0 ; LED_X is on PB0, pin 12 of ATtiny2313 .EQU LED_Y = 1 ; LED_Y is on PB1, pin 13 of ATtiny2313

Pins connected to LED are outputs, DDRx=1 (set)

SBI DDRD, 0 SBI PORTB, 2

SBI DDRB,LED_X SBI DDRB,LED_Y


GO: RCALL COLOR1 RCALL WAIT SBIS PINB, 2 RCALL COLOR2 RCALL WAIT RJMP GO ; RJMP - Relative Jump

P R O C E D U R E S

SMALLWAIT: INC R1 ; INC - Increment BRNE SMALLWAIT ; BRNE - Branch if Not Equal (Z flag) RET ; RET - Return from Subroutine

WAIT: LDI R16, 4 ; LDI - Load Immediate WAIT1: INC R1 BRNE WAIT1 INC R2 BRNE WAIT1 DEC R16 BRNE WAIT1 RET

COLOR1:

   SBI	PORTB, LED_X

CBI PORTB, LED_Y ; CBI - Clear Bit in I/O Register RET

COLOR2:

   SBI	PORTB, LED_Y

CBI PORTB, LED_X RET

COL3W: LDI R16, 2 COL3X: RCALL COLOR1 RCALL SMALLWAIT RCALL COLOR2 RCALL SMALLWAIT INC R2 BRNE COL3X DEC R16 BRNE COL3X

DARK:

   CBI	PORTB, LED_X

CBI PORTB, LED_Y RET







       .EQU PINB = $16

.EQU DDRB = $17 ; DDRB address .EQU PORTB = $18 ; PORTB address


.EQU PIND = $10 .EQU DDRD = $11 .EQU PORTD = $12


SBI DDRD,0 SBI DDRD,1 SBI DDRD,2 SBI DDRD,3 ;4 vystupy

CBI PORTD,0 CBI PORTD,1 CBI PORTD,2 CBI PORTD,3

CBI DDRB,0 CBI DDRB,1 CBI DDRB,2 CBI DDRB,3 ;4 vstupy

SBI PORTB,0 SBI PORTB,1 SBI PORTB,2 SBI PORTB,3  ;nastaveni pull upu


START: RCALL KLAVESNICE


KLAVESNICE: JEDNA:

SBI PORTD,1 SBI PORTD,2 SBI PORTD,3 ;nateveni radku 0 do log. 0 ostatni na log. 1 SBIC PINB,0 RJMP DVA LDI R15,1 RET

DVA:

SBIC PINB,1 RJMP TRI LDI R15,2 RET

TRI:

SBIC PINB,2 RJMP ACKO LDI R15,3 RET

ACKO:

SBIC PINB,3 RJMP CTYRI LDI R15,11 RET

CTYRI: SBI PORTD,0 CBI PORTD,1 SBI PORTD,2 SBI PORTD,3 SBIC PINB,0 RJMP PET LDI R15,4 RET

PET:

SBIC PINB,1 RJMP SEST LDI R15,5 RET

SEST:

SBIC PINB,2 RJMP BECKO LDI R15,6 RET

BECKO:

SBIC PINB,3 RJMP SEDUM LDI R15,12 RET

SEDUM:

SBIC PINB,0 RJMP OSM LDI R15,7 RET

OSM:

SBI PORTD,0 SBI PORTD,1 CBI PORTD,2 SBI PORTD,3 SBIC PINB,1 RJMP DEVET LDI R15,8 RET

DEVET:

SBIC PINB,2 RJMP CECKO LDI R15,9 RET


CECKO:

SBIC PINB,3 RJMP HVEZDA LDI R15,13 RET


HVEZDA:

SBI PORTD,0 SBI PORTD,1 SBI PORTD,2 CBI PORTD,3 SBIC PINB,0 RJMP NULA LDI R15,15 RET


NULA:

SBIC PINB,1 RJMP HEX LDI R15,0 RET

HEX:

SBIC PINB,2 RJMP DECKO LDI R15,16 RET


DECKO:

SBIC PINB,3 RJMP RET LDI R15,14


/rec

example fan program for ATmega88
P4 CPU fan Ucc pin is supposed to be connected
to +12 V, GND pin is supposed to be connected
to FET Drain, Source on GND, Gate on
pin 16 (PB2, FAN). Fan runs when FAN output is log. H.
FAN is switched ON with switch between PC2 and PD2


  .EQU DDRB = $04

.EQU PORTB = $05 .EQU PINB= $03

.EQU DDRC = $07 .EQU PORTC = $08 .EQU PINC = $06

.EQU DDRD = $0A .EQU PORTD = $0B .EQU PIND = $09


.INCLUDE "m188def.inc"  ; replaces previous lines of the program

.EQU FAN = 4


Pin driving FAN is set as output

SBI DDRB, FAN

Basic keyboard init
One switch of matrix keyboard is connected between PINC 2 a PIND 2
Zero values of DDRC, 2 and PORTB, 2 are implicit

SBI DDRD,0 SBI DDRD,1 SBI DDRD,2 SBI DDRD,3 ;4 vystupy

CBI PORTD,0 CBI PORTD,1 CBI PORTD,2 CBI PORTD,3

CBI DDRB,0 CBI DDRB,1 CBI DDRB,2 CBI DDRB,3 ;4 vstupy

SBI PORTB,0 SBI PORTB,1 SBI PORTB,2 SBI PORTB,3  ;nastaveni pull upu

Init

RCALL FAN_OFF

GO: RCALL KEYPRESS RCALL PWM  ; Simple PWM control of the FAN, if the key is pressed RJMP GO


P R O C E D U R E S


VERYSMALLWAIT: LDI R20, 0xFC MOV R1, R20 SMALLWAIT: INC R1 BRNE SMALLWAIT RET

WAIT: LDI R21, 4 WAIT1: INC R1 BRNE WAIT1 INC R2 BRNE WAIT1 DEC R21 BRNE WAIT1 RET

FAN_ON: SBI PORTB, FAN RET

FAN_OFF: CBI PORTB, FAN RET

FAN3W: LDI R16, 0x20 FAN3X:

	RCALL	FAN_ON

RCALL VERYSMALLWAIT  ; pulse ratio controlled by number of waits RCALL VERYSMALLWAIT  ; 4:1 in this case RCALL VERYSMALLWAIT RCALL VERYSMALLWAIT RCALL FAN_OFF RCALL VERYSMALLWAIT


INC R2 BRNE FAN3X DEC R16 BRNE FAN3X RET

PWM: MOV R17, R16 INC R17 LDI R18, 0xF8 OR R18, R16 PWM_ON: DEC R17 BREQ PWM_OFF SBI PORTB, FAN RCALL VERYSMALLWAIT RJMP PWM_ON PWM_OFF: INC R18 BREQ PWM_RET CBI PORTB, FAN RCALL VERYSMALLWAIT RJMP PWM_OFF PWM_RET: RET

KEYPRESS: JEDNA:

SBI PORTD,1 SBI PORTD,2 SBI PORTD,3 ;nateveni radku 0 do log. 0 ostatni na log. 1 SBIC PINB,0 RJMP DVA LDI R16,1 RET

DVA:

SBIC PINB,1 RJMP TRI LDI R16,2 RET

TRI:

SBIC PINB,2 RJMP ACKO LDI R16,3 RET

ACKO:

SBIC PINB,3 RJMP CTYRI LDI R16,11 RET

CTYRI: SBI PORTD,0 CBI PORTD,1 SBI PORTD,2 SBI PORTD,3 SBIC PINB,0 RJMP PET LDI R16,4 RET

PET:

SBIC PINB,1 RJMP SEST LDI R16,5 RET

SEST:

SBIC PINB,2 RJMP BECKO LDI R16,6 RET

BECKO:

SBIC PINB,3 RJMP SEDUM LDI R16,12 RET

SEDUM:

SBIC PINB,0 RJMP OSM LDI R16,7 RET

OSM:

SBI PORTD,0 SBI PORTD,1 CBI PORTD,2 SBI PORTD,3 SBIC PINB,1 RJMP DEVET LDI R16,8 RET

DEVET:

SBIC PINB,2 RJMP CECKO LDI R16,9 RET


CECKO:

SBIC PINB,3 RJMP HVEZDA LDI R16,13 RET


HVEZDA:

SBI PORTD,0 SBI PORTD,1 SBI PORTD,2 CBI PORTD,3 SBIC PINB,0 RJMP NULA LDI R16,15 RET


NULA:

SBIC PINB,1 RJMP HEX LDI R16,0 RET

HEX:

SBIC PINB,2 RJMP DECKO LDI R16,16 RET


DECKO:

SBIC PINB,3 RJMP KERET LDI R16,14


KERET: RET

/rec


Upraveny program. Pro mala cisla do 5ky nasobeni x2 6-9 nasobeni x4. Nazornejsi zrycheleni vetraku
 ; example fan program for ATmega168 with Timer controlled PWM&Interrupt&Sleep
;
; Rotation speed  is controlled by keyboard, 
; keys 0-9 represent the speed between 0 and 100 %
;
; Fan starts to rotate with the first valid key press
;
; To do: *** Catch only the key press, not key release
;
; To do: *** Set higher value of PWM (=higher fan rpm) for  
; a few first periods to start the fan reliably
;
;
; P4 CPU fan Ucc pin is supposed to be connected 
; to +15 V, GND pin is supposed to be connected 
; to NMOS Drain,, Source on GND, Gate on 
; pin 11 (OC0B, PD5). Fan runs when pin 11 is high.
.INCLUDE "m88def.inc"
.EQU PWM_PULSES = 36	; value for 27.8 kHz PWM
;.EQU PWM_PULSES = 45	; value for 22.2 kHz PWM
.ORG 0x0000 
rjmp Main ; Reset Handler
.ORG 0x0008 
rjmp isr1 ; PCINT1 Handler
; Main program start
Main: 
; Stack pointer init:
ldi r16,high(RAMEND); Main program start
out SPH,r16 ; Set Stack Pointer to top of RAM
ldi r16,low(RAMEND)
out SPL,r16


; Hardware initialization
; Keyboard init:
cbi DDRC, 2 ; set inputs for keyboard (COL1-3)
cbi DDRC, 3
cbi DDRC, 4

sbi PORTC, 2 ; set internal Pull-Ups for keyboard
sbi PORTC, 3
sbi PORTC, 4

sbi DDRD, 0 ; set driving outputs for keyboard
sbi DDRD, 1 ; (ROW1-ROW4)
sbi DDRD, 2
sbi DDRD, 3
cbi PORTD, 0	; log. 0 on all rows
cbi PORTD, 1
cbi PORTD, 2
cbi PORTD, 3


; Timer controlled PWM init:
;
; There are registers TCCR0A and TCCR0B for config, DDRD for output bit
; enabling, OCR0A for counter lenght and OCR0A for pulse "high" lenght.
; Register TIMSK0 controlls interrupts - not used, 0x00 by default 

PWM_INIT:
ldi R17, 0b00100011	; Fast PWM Mode, out on OC0B, non Inverting
out TCCR0A, R17			; 
ldi R17, 0b00001001	; WGM2:1 (Fast PWM), CS2..0:001 (internal clock, prescale=1)
out TCCR0B, R17
ldi R17, PWM_PULSES	; load number of clock pulses for 1 PWM period
out OCR0A, R17   
sbi DDRD, 5	; set pin 11 as PWM output OC0B (PD5 pin)


; Keyboard interrupt setup
; Activation of pin change interrupt - PROBLEM!!! PCICR and PCIMSK are 
; extended I/O registers (0x68 and 0x6C), and must be handled as 
; a memory location
ldi r26, PCICR ; load address of PCICR in Y low
clr r27 ; load high byte with 0
ldi r16,0b00000010 ; activate PCINT1
st X, r16 ; store new PCINT1
ldi r26, PCMSK1 ; load address of PCMSK1 in Y low
clr r27 ; load high byte with 0
ldi r16,0b00011100 ; allow pin change interrupt on portC bits 2,3,4
st X, r16 ; store new PCMSK1
sei ; Enable interrupts
; Set sleep mode of the CPU
; SMCR: 0,0,0,0,SM2,SM1,SM0,SE   SM2..0: Sleep Mode Select Bits, SE: Sleep enabled)
ldi r16,0b00000001 ; Idle mode 
out SMCR,r16 ; Sleep mode set


; Sleep loop, wake up by the Interrupt, return with RETI back to sleep loop
loop:
sleep ; now AVR sleeps
nop ; return from Interrupt to this instruction
rjmp loop ; and sleep again
; PCINT0 Service Routine:
;
; To do: *** rpm reading, regulation, etc.

isr1: 
rcall KEYPRESS
mov R17, R16
lsl R17
lsl R17	; nasobeni x4 cisla 6-9
out OCR0B, R17  ; output pulse "high" lenght to PWM compare unit 
reti ; return from Interrupt

isr2:
rcall KEYPRESS
mov R17, R16
lsl R17	; nasobeni x2 cisla do 5ky
out OCR0B, R17  ; output pulse "high" lenght to PWM compare unit 
reti ; return from Interrupt
; Keyboard decoding:
KEYPRESS:
KEY0:
sbi 	PORTD, 0
sbi 	PORTD, 1
sbi 	PORTD, 2
cbi 	PORTD, 3	;log. 0 to the row with *, 0, #, D	
sbic	PINC, 3
rjmp	KEY1
ldi	R16, 0		;pressed key 0
rjmp	KEYRET
KEY1:
cbi		PINC, 2
cbi 	PORTD, 0	;log. 0 to the row with 1, 2, 3, A
sbi 	PORTD, 1	;log. 1 on another three
sbi 	PORTD, 2
sbi 	PORTD, 3	
sbic	PINC, 2
rjmp	KEY2
ldi	R16, 1		;pressed key 1
rjmp	KEYRET
KEY2:
sbic	PINC, 3
rjmp	KEY3
ldi	R16, 2
rjmp	KEYRET
KEY3:
sbi 	PINC, 4
sbic	PINC, 4
rjmp	KEY4
ldi	R16, 3
rjmp	KEYRET
KEY4:
sbi 	PORTD, 0
cbi 	PORTD, 1	;log. 0 to the row with 4, 5, 6, B
sbi 	PORTD, 2
sbi 	PORTD, 3	
sbic	PINC, 2
rjmp	KEY5
ldi	R16, 4		;pressed key 4
rjmp	KEYRET
KEY5:
sbic	PINC, 3
rjmp	KEY6
ldi	R16, 5
rjmp	KEYRET
KEY6:
sbic	PINC, 4
rjmp	KEY7
ldi	R16, 6
rjmp	KEYRET2

KEY7:
sbi 	PORTD, 0
sbi 	PORTD, 1
cbi 	PORTD, 2	;log. 0 to the row with 7, 8, 9, C
sbi 	PORTD, 3	
sbic	PINC, 2
rjmp	KEY8
ldi	R16, 7		;pressed key 7
rjmp	KEYRET2

KEY8:
sbic	PINC, 3
rjmp	KEY9
ldi	R16, 8
rjmp	KEYRET2

KEY9:
sbic	PINC, 4
rjmp	KEYRET2
ldi	R16, 9
KEYRET:
cbi PORTD, 0	; log. 0 on all rows for next key press catch
cbi PORTD, 1
cbi PORTD, 2
cbi PORTD, 3 
rjmp isr1
KEYRET2:
cbi PORTD, 0	; log. 0 on all rows for next key press catch
cbi PORTD, 1
cbi PORTD, 2
cbi PORTD, 3
rjmp isr2
Osobní nástroje