Uživatel:Ladysmar

Z MAM wiki

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

7.3.2013


Obsah

Tlačítko

http://noel.feld.cvut.cz/vyu/a2m99mam/index.php/Maticov%C3%A1_kl%C3%A1vesnice

CV 4. detekce tlacitka blikanim diody
-----------------------------------------------------------
        .EQU DDRB = 0x17
	.EQU PORTB = 0x18
	.EQU PINB = 0x16

	.EQU DDRD = 0x11
	.EQU PORTD = 0x12
	.EQU PIND = 0x10






	.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	DDRB, LED_X	; SBI - Set Bit in I/O Register
	SBI	DDRB, LED_Y


	SBI DDRD, 2
	SBI PORTB, 2
GO:
	RCALL   COLOR1
	RCALL	WAIT
	SBIS	PINB, 2
	RCALL	COLOR2
	RCALL	WAIT
	RJMP	GO

;;;
;;;
;;; 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
---------------------------------------------------------------------------------------


Klavesnice snad funguje

Pozn. Horčík: Asi nefunguje, spousta chyb - porty B i D nastaveny jako výstupní, při přechodu mezi řádky klávesnice nedeaktivujete předchozí řádek a rovnou aktivujete další, takže nakonec budou aktivní všechny, ...


	.EQU DDRB = 0x17
	.EQU PORTB = 0x18
	.EQU PINB = 0x16

	.EQU DDRD = 0x11
	.EQU PORTD = 0x12
	.EQU PIND = 0x10

SBI DDRD, 2    ; nastaveni ctyr vstupu
SBI DDRD, 3
SBI DDRD, 4
SBI DDRD, 5
SBI PORTD,2
SBI PORTD,3
SBI PORTD,4
SBI PORTD,5

SBI DDRB, 2    
SBI DDRB, 3
SBI DDRB, 4
SBI DDRB, 5
SBI PORTB, 2    ; nastaveni pull-up rezistoru
SBI PORTB, 3
SBI PORTB, 4
SBI PORTB, 5

JEDNAW:
	CBI PORTD,2  ; 
	SBIC PINB,2 
	RJMP DVE 
	LDI R17,1 
	RET
DVE:
	SBIC PINB,3
	RJMP TRI 
	LDI R17,2 
	RET
TRI:
	SBIC PINB,4 
	RJMP ACKO 
	LDI R17,3 
	RET
ACKO:
	SBIC PINB,5 
	RJMP CTYRI 
	LDI R17,11 
	RET
CTYRI: 
	CBI PORTD,3 
	SBIC PINB,2 
	RJMP PET 
	LDI R17,4 
	RET
PET:
	SBIC PINB,3 
	RJMP SEST 
	LDI R17,5 
	RET
SEST:
	SBIC PINB,4 
	RJMP BECKO 
	LDI R17,6 
	RET
BECKO:
	SBIC PINB,5 
	RJMP SEDM 
	LDI R17,12 
	RET
SEDM:
	CBI PORTD,4 
	RJMP OSM 
	LDI R17,7 
	RET
OSM:
	SBIC PINB,3 
	RJMP DEVET 
	LDI R17,8 
	RET
DEVET:
	SBIC PINB,4 
	RJMP CECKO 
	LDI R17,9 
	RET
CECKO:
	SBIC PINB,5
	RJMP HVEZDICKA
	LDI R17,13
	RET
HVEZDICKA:
	CBI PORTD,5 
	SBIC PINB,2 
	RJMP NULA 
	LDI R17,15 
	RET
NULA:
	SBIC PINB,3 
	RJMP KRIZEK 
	LDI R17,0 
	RET
KRIZEK:
	SBIC PINB,4
	RJMP DECKO
	LDI R17,16 
	RET
DECKO:
	SBIC PINB,5
	RJMP JEDNA
	LDI R17,14
	RET

5. Výkonové spínací prvky, úloha na regulaci ventilátoru, přerušení, uspání procesor

; 
; 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	; rezim PWM, 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	; multiply key number by 4 for 10 steps in 40 clock pulses of PWM
	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 	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:
	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	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		;pressed key 7
	rjmp	KEYRET
KEY8:
	sbic	PINC, 3
	rjmp	KEY9
	ldi	R16, 8
	rjmp	KEYRET
KEY9:
	sbic	PINC, 4
	rjmp	KEYRET
	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
	ret


CV 6 klavesnice, vetracek, PCM

;ventilator - PWM,sleep.preruseni
           ;Ovladani ventilatoru klavesnici

.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)

	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	; multiply key number by 4 for 10 steps in 40 clock pulses of PWM
	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 	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:
	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	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		;pressed key 7
	rjmp	KEYRET
KEY8:
	sbic	PINC, 3
	rjmp	KEY9
	ldi	R16, 8
	rjmp	KEYRET
KEY9:
	sbic	PINC, 4
	rjmp	KEYRET
	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
	ret
--------------------------------------------------------------------------------------------------

CV 7 - dodělávání programu na větráku, práce spolu s kolegou Markem Štychem

LDI R16,6	;Znak "1" do registru R16
;LDI R16,31 ;Znak "0" do registru R16
REGISTR: 
LDI R17, 128 ; pro osmibitove slovo (pro 7-bitove ulozit64)
POSUN_DALSI: 
CBI PORTC, 0	 ; vynuluje vystup 
SBRC R16, 0	 ; otestuje 0 bit v R16 
SBI PORTC, 0	 ; vystup do 1
SBI PORTC, 1	 ; hodiny na shift registr - vzestup hr. 
ROR R16	 ; rotace vpravo R16 
CBI PORTC, 1	 ; hodiny na shift registr - sestup hr. 
SBRC R17, 0	 ; neni posledni 
RJMP POSUN_KONEC 
ROR R17	 ; rotace vpravo R17 
RJMP POSUN_DALSI
POSUN_KONEC: 
SBI PORTC, 2	 ; hodiny na storage registr - vzestup hr. 
NOP 
NOP 
CBI PORTC, 2	 ; hodiny na storage registr - sestup hr.
RET


Domácí úkol 1 - tváří se jako funkční.

Nejsem žádný programátor v AVR se na mě rozsvítilo zelené světýlko, tak to snad bude fungovat tak jak má, ale ruku do ohně za to nedám.

inspiroval jsme se zde na MAM WIKI a i jinde :)

.INCLUDE "m88def.inc"

.equ PWM_interier = 100	; PWMfreq 25  kHz pro osvetleni interieru
.equ PWM_palubka = 64	; PWMfreq 14.3kHz pro osvetleni palubi desky

.org 0x000 
	rjmp zacatek 				; reset vektor
.org 0x00D
	rjmp Time1			; preruseni pri preteceni TMR1
.org 0x001A
	rjmp svetlo_palubka


zacatek: 

	ldi r17,high(RAMEND); Main program start
	out SPH,r17 ; Set Stack Pointer to top of RAM
	ldi r17,low(RAMEND)
	out SPL,r17
	
	cbi DDRC,0		;nastavení vstupu pro dveře
	sbi PORTC,0

	sbi DDRD, 5	        ; PWM output OC0B (PD5 pin)
	sbi DDRD, 6	        ; PWM output OC0B (PD6 pin)	


PWM_inter:
 	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_interier	; load number of clock pulses for 1 PWM period
	out OCR0A, R17 

	
PWM_palub:

	ldi r26, PCICR 
	clr r27 
	ldi r17,0b00000010 
	st X, r17 
	ldi R26, TCCR2B		; 
	ldi R17, 0b00001001	; WGM2:1 (Fast PWM), CS2..0:001 (internal clock, prescale=1)
	st X, R17
	ldi R26, OCR2A
	ldi R17, PWM_palubka	; load number of clock pulses for 1 PWM period
	st X, R17

spozdeni_inter:

	ldi R26, TCCR1B
	clr R27
	ldi R17, 0x01 ;bez předděličky - 15krát za sekundu přeteče a vyvolá přerušení
	st X, R17
	

ADC_interier: 

	ldi	r16,0b10000011	
	sts	ADCSRA,r16
	ldi	r16,0b01100000	
	sts	ADMUX,r16
	ldi	r16,0b00000000	
	sts	ADCSRB,r16		
	ldi	r16,0b00000001	
	sts	DIDR0,r16

	sei    
	
;nastavení časovače   

	ldi r26, TIMSK1 
	clr r27 
	ldi r16,0b00000001 
	st X, r16               

;nastaveni kontaktu

	ldi r26, PCICR 
	clr r27 
	ldi r16,0b00000010 
	st X, r16 

	ldi r26, PCMSK1 
	clr r27 
	ldi r16,0b00000001 
	st X, r16 

	sei 
;sleep setup

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

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


Time1:
	rcall KEYDOOR
	ldi	r24, 0x2F  

svetlo_palubka:

	ldi R26, ADCSRA
	clr R27
	ldi R17, 0b11000010	;Start conversion, division factor 4
	st X, R17	
	ldi  r18, 13	;čekání na dokončení převodu
	HOP:	dec  r18
	brne HOP
	
	ldi R26, ADCH		;zápis výstupu z ADC do PWM
	clr R27
	ld  R17,X
	ldi R26,OCR0B
	clr R27
	st X,R17

	ret
	                 

KEYDOOR:
	sbis PINC,0			
	rjmp POHASINANI	
	ldi R16,100			
	mov R17, R16
	sbi DDRD,5
	out OCR0B, R17  ; output pulse "high" lenght to PWM compare unit 

ret

POHASINANI:

		ldi R18, 250
		CEKANI:				  ;5s plného svitu - neměníme nastavení PWM
		rcall WAIT20ms
		dec R18
		BRNE CEKANI

		ldi R18, 100
		CEKANI2:			;2s ztmívání - postupně snižujeme střídu PWM
		mov R17, R18
		out OCR0B, R17
		rcall WAIT20ms
		dec R18
		BRNE CEKANI2
		cbi DDRD,5			;nakonec výstup vypneme
ret


WAIT20ms:
	 ldi  r19, 26
    ldi  r20, 255
	L2:	dec  r20
		brne L2
		dec  r19
		brne L2
ret

Osobní nástroje