Uživatel:Bakalbr1

Z MAM wiki

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

[editovat] Semestrální práce

Řízení stahování žaluzií. Se studentem Jan Vlach.

Soubor:Mustek.png


; tento blok pridat na zacatek kodu
  ;=====================================================================================================================================
 .equ	rychlost=0x80 ;nastaveni rychlosti otaceni motoru, definuje stridu v PWM regulaci - nutno doladit podle typu motoru a napajeciho napeti motoru, vychozi 50% strida
 .DEF	WORK=R17
 .DEF	MOTORSTAT=R16
  ;=====================================================================================================================================



  ;tento blok pridat k inicializaci
 ;=====================================================================================================================================

 ;inicializace - nastaveni portu PD5 a PD6 jako vystupu a jeho nastaveni na nulu
 SBI	DDRD,5
 SBI	DDRD,6
 CBI	PORTD,5
 CBI	PORTD,6


  ;PWM smer1 OC0A, smer 2 OC0B, no prescaler, fast PWM
 LDI	WORK,0b00000001
 STS	TCCR0B,WORK

 LDI	WORK,0b00000011 ; nastaveni fast PWM, standardni I/O porty
 STS	TCCR0A,WORK

 LDI	WORK,rychlost
 OUT	OCR0A,WORK	;nastaveni stridy pro 1. smer
 OUT	OCR0B,WORK	;nastaveni stridy pro 2. smer
 ;=====================================================================================================================================



 ;podporgram exekuce
 ;=====================================================================================================================================
 exekuce:
		SBRC	MOTORSTAT,2
		RJMP	moveSTOP
		SBRC	MOTORSTAT,0
		RJMP	moveUP
		SBRC	MOTORSTAT,1	;pokud neni ani jedna moznost, tak motor zastavi
		RJMP	moveDOWN

moveSTOP:
		LDI		WORK,0b00000011 ; nastaveni fast PWM, standardni I/O porty
		STS		TCCR0A,WORK
		CBI		PORTD,5
		CBI		PORTD,6	
		RET


moveUP:
		LDI		WORK,0b10000011 ; nastaveni fast PWM, PD6 on, PD5 off
		STS		TCCR0A,WORK
		RET

moveDOWN:
		LDI		WORK,0b00100011 ; nastaveni fast PWM, PD6 off, PD5 on
		STS		TCCR0A,WORK
		RET
;=====================================================================================================================================

[editovat] 1. Domaci Ukol

/*
 * DU1_bakala.asm
 *
 *  Created: 13.5.2013 15:22:33
 *   Author: bakis
 */ 
 .INCLUDE "m88def.inc"

 ;default 1 MHz system clock

; ADC 0 pripojen potenciometr mezi napajeci napeti a zem
; PD6 led pro palubovku pripojena j napajecimu napeti - aktivni na nulu
; PD5 led pro osvetleni dveri pripojene k napajecimu napeti - aktivni na nulu
; PD2 tlacitkovy vstup proti zemi
.def		WORK			= r21
.def		DOORPWMsetup	= r18
.def		compare	= r19
.def		closedDOORtime	= r20


.org 0x000
	RJMP	setup



;TMR1 vector - tick
.org	0x00D
rjmp	tick

;ADC conversion complete vector
.org	0x015
rjmp	aconv

setup:
 ;ADC setup
 LDI	WORK,0b01100000	;set ADC0 input, left adjust result, voltage reference Vcc
 STS	ADMUX,WORK
 LDI	WORK,0b00000000	;set free running mode
 STS	ADCSRB,WORK
 LDI	WORK,0b11001011	;start ADC, interrup enable
 STS	ADCSRA,WORK

 ;PWM palubni deska OC0A, osvetleni OC0B, 64 prescaler, fast PWM
 LDI	WORK,0b00001010
  STS	TCCR0B,WORK
 LDI	WORK,0b10100011
  STS	TCCR0A,WORK
 LDI	WORK,0x80       ;nastaveni 50% stridy pro palubku
  OUT	OCR0A,WORK	;na zacatku sviti na 
   LDI	WORK,0xFF       ;nastaveni 0% stridy pro dvere
  OUT	OCR0B,WORK	;na zacatku sviti na 

 ;Tick timer setup
 LDI	WORK,0x00		;normal mode operation, overflow interrupt
   STS	TCCR1A,WORK
 LDI	WORK,0x06		; prescaler 1024
   STS	TCCR1B,WORK
 LDI	WORK,0x01		; tick=98ms
   STS	TIMSK1,WORK

 ;input/output setup

 LDI	WORK,0b01100000 ; D5,D6 outputs, other inputs, D2 jako vstup z dverniho spinace
 OUT	DDRD,WORK
 sbi	PORTD,2	;set pull up od D2

 ;initialize variables
 LDI	closedDOORtime,0x00

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

; Enable interrupts
sei 

loop:	        sleep
		nop
		rjmp	loop


;kompletni regulace osvitu palubni desky
aconv:	LDS	WORK,ADCH	; rozliseni pouze 8 hornich MSC
		OUT	OCR0A,WORK	; hodnota ADC do PWM, vyzaduje aby byl potenciometr pripojen mezi napajeni kontroleru a zem. 
		ORI	WORK,0xFF	;logicka negace, aby pri vytoceni doprava byla na vystupu 0 a maximalni svit
		LDI	WORK,0b11001011	;start ADC, interrup enable
		STS	ADCSRA,WORK
		RETI

;kompletni regulace dverniho osvetleni v obsluze po 100ms
tick:	IN		WORK,PIND				;test tlacitka
		SBRS	WORK,2
		rjmp	doorOpen
		LDI		WORK,0x00
		CPSE	WORK,closedDOORtime		;skok pokud se registry rovnaji=neni nutne regulovat osvetleni
		rjmp	doorClosed
		RETI
;snastavi citac ticku 70*100ms
doorOpen:LDI	closedDOORtime,0x47		;nacte 70 do registru 70*100ms=7s, konstanta pro zpozdeni svitu
		 LDI	compare,0x14			;nacte 20 do compare 20*100ms=2s, citac pro stmivani, cas kdy zacne stmivat
		 LDI	DOORPWMsetup,0x03
		 OUT	OCR0B,DOORPWMsetup		;na zacatku sviti temer od nuly
		 RETI

;nastavi citac ticku 70*100ms
doorClosed:
		DEC		closedDOORtime
		CPSE	closedDOORtime,compare	;pokud se rovnaji, uplynulo 5s a zaciname stmivat, preskoceni navratu z preruseni			
		RETI							
		MOV		compare,closedDOORtime	;zmeni si compare o 1, aby priste skocil znovu
		LDI		WORK,0x0C
		ADD		DOORPWMsetup,WORK		;0+20*12=255 zmena stridy PWM po 100ms, na konci by mela byt na vystupu temer porad jednicka
		OUT		OCR0B,DOORPWMsetup		;nastaveni PWM
		RETI

[editovat] Cviceni 6

/*
 * AssemblerApplication1.asm
 *
 *  Created: 21.3.2013 9:43:30
 *   Author: bakis
 */ CVICENI 6


 ; 
; 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 "m168def.inc"

.EQU PWM_PULSES = 36	; value for 27.8 kHz PWM
;.EQU PWM_PULSES = 45	; value for 22.2 kHz PWM


.ORG 0x0000 
	jmp Main ; Reset Handler

.ORG 0x0008 
	jmp 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	; multiply key number by 4 for 10 steps in 40 clock pulses of PWM
	BRBS	Z,setF	;test na Z flag
	sbi	TCCR0A,5
continue:
	out OCR0B, R17  ; output pulse "high" lenght to PWM compare unit 

	reti ; return from Interrupt


setF:	;set normal output on PD5
	cbi	TCCR0A,5
	cbi PORTb,5
	cbi	SREB,Z
	RJMP


; 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
Osobní nástroje