Uživatel:Koubazde

Z MAM wiki

(Rozdíly mezi verzemi)
Přejít na: navigace, hledání
Řádka 83: Řádka 83:
........................................
........................................
-
tyden 5.
+
==tyden 5.==
Řádka 280: Řádka 280:
===================================
===================================
-
=== tyden 6
+
==tyden 6==
; example fan program for ATmega88 with Timer controlled PWM&Interrupt&Sleep
; example fan program for ATmega88 with Timer controlled PWM&Interrupt&Sleep
Řádka 502: Řádka 502:
=======================================================================
=======================================================================
-
Tyden 7
+
==Tyden 7==
Ovladani vetraku, osetreni stisku 2 tlacitek na klavesnici, rozbeh z 0
Ovladani vetraku, osetreni stisku 2 tlacitek na klavesnici, rozbeh z 0
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Řádka 794: Řádka 794:
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
-
tyden 9.
+
==tyden 9.==
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Řádka 884: Řádka 884:
RET
RET
 +
 +
 +
===================================
 +
==Domaci ukol 1==
 +
 +
 +
 +
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 +
;%%%%%1. home work - JELINEK Martin, KOUBA Zdenek%%%%%
 +
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 +
;
 +
;po startu dojde k inicializaci HW
 +
;==nastaveni vstupu a vystupu
 +
;==vystupy vynulovany
 +
;==nastaveni PWM a AD prevodniku
 +
;==nastaveni a nasledne pouziti SLEEP MODu
 +
 +
 +
.INCLUDE "m88def.inc" ; info pro prekladac
 +
 +
 +
.EQU OSV_INT = 1 ; prirazeni pinu pro osvetleni interieru
 +
.EQU DOOR_IN = 2 ; prirazeni pinu pro dverni kontakt
 +
 +
.EQU OSV_PALUB = 5 ; prirazeni pinu pro osvetleni interieru (PD5)
 +
.EQU AD_IN = 4 ; prirazeni pinu pro potenciometr
 +
 +
.EQU STMIVANI = 100 ; pocet stmivacich kroku interniho osvetleni (0-255)
 +
.EQU PWM_PULSES = 50 ; pocet pulzu osvetleni palubni desky za 1 periodu PWM
 +
 +
.ORG 0x0000 rjmp START ; Resetovaci vektor/skok na hl. program
 +
 +
.ORG 0x0008 rjmp PALUBKA ; vektor preruseni/skok na obsluhu nastaveni palubniho osvetleni
 +
 +
 +
 +
 +
;Main program start
 +
START:
 +
 +
 +
 +
;%%%%NASTAVENI%%%%
 +
 +
;nastaveni IN/OUT ; zvolit port podle pouziteho procesoru!!!!!!!!!!!!!!!!
 +
 +
SBI DDRB, OSV_INT ; nastaveni vystupniho pinu pro osvetleni interieru
 +
CBI DDRB, DOOR_IN ; nastaveni vstupniho pinu pro dverni kontakt
 +
 +
SBI DDRD, OSV_PALUB ; nastaveni vystupniho pinu pro osvetleni interieru
 +
CBI DDRB, AD_IN ; nastaveni vstupniho pinu pro potenciometr
 +
 +
 +
 +
; nastaveni SLEEP modu
 +
; 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
 +
 +
 +
 +
;nastaveni A/D converteru
 +
ldi R16,0b10001111 ; ADC enable (8.bit), enable interrupt (4.bit), prescaler 128
 +
out ADCSRA,R16
 +
; vystup:
 +
; 2. a 1. bit ADCL (ADC9, ADC8)
 +
; 8. - 1. bit ADCH (ADC7 - ADC0)
 +
 +
;nastaveni hardwarove PWM
 +
 +
PWM_PALUB_INIT: ; timer 0
 +
ldi R17, 0x00 ; disable all interrupts
 +
out TIMSK0, R17
 +
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, OSV_PALUB ; PWM output - pin pro osvetleni palubni desky
 +
 +
;-----------------------------------------------------------------------------
 +
;-----------------------------------------------------------------------------
 +
;==========ovladani osvetleni palubni desky potenciometrem
 +
;(popis funkce)
 +
;
 +
;AD prevod nastaven pravidelne
 +
;dokonceni AD prevodu vyvolava preruseni
 +
;v preruseni je cislo z AD prevodniku predano do PWM
 +
 +
;%%%%OBSLUHA_PRERUSENI%%%%
 +
 +
 +
PALUBKA: MOV R20, ADCH
 +
out OCR0B, R20 ; output pulse "high" lenght to PWM compare unit
 +
reti ; return from Interrupt
 +
 +
 +
 +
;==========ovladani vnitrniho osvetleni dvernim kontaktem
 +
;(popis funkce)
 +
;
 +
;kontakt pripojen na pin (DOOR_IN)
 +
;zmena na pinu probudi mikrokontroler ze sleep modu
 +
;rozhodne se, zda se jedna o sepnuti 1), ci rozepnuti kontaktu 2)
 +
;1) nastaveni vystupu do log. 1
 +
;1) sleep mode
 +
;2) cekaci smycka 5 sec
 +
;2) softwarova klesajici PWM
 +
;2) sleep mode
 +
;-----------------------------------------------------------------------------
 +
;-----------------------------------------------------------------------------
 +
 +
;%%%%PROCEDURY%%%%
 +
 +
 +
 +
; Sleep loop, wake up by the Interrupt, return with RETI back to sleep loop
 +
 +
LOOP:
 +
sleep ; prechod do sleep modu
 +
nop ; return from Interrupt to this instruction
 +
SBIC DDRB, DOOR_IN ; je kontakt sepnut? NE-pokracuje ZHASINEJ1
 +
rjmp TURN_ON ; skok na sepnuti svetla
 +
 +
 +
ZHASINEJ1:
 +
ldi R16, 0x0
 +
cpse R16, R17 ; je R17 nula? NE-pokracuje CEKEJ5SEC
 +
rjmp CEKEJ5SEC ; skok na CEKEJ5SEC
 +
rjmp LOOP ; skok na LOOP, R17 bylo=0, je zhasnuto
 +
 +
 +
CEKEJ5SEC: ; cekaci smycka pred zapocetim stmivani
 +
LDI R3, 20 ; doba je nastavovana cislem ukladanym do registru R3
 +
WAIT1: INC R1
 +
BRNE WAIT1
 +
INC R2
 +
BRNE WAIT1
 +
DEC R3
 +
BRNE WAIT1
 +
 +
LDI R17, STMIVANI ; ulozi pocet stmivacich kroku(=x) do R17=x´
 +
LDI R19, STMIVANI ; R19=y
 +
 +
ZHASINEJ2:
 +
MOV R18, R17 ; R18=x´´
 +
DEC R17
 +
SUB R19, R17
 +
 +
PWM_PIN_ON:
 +
SBI PORTB, OSV_INT ; zapne interni svetlo
 +
 +
WAIT2: INC R1 ; po zapnuti ceka
 +
BRNE WAIT2
 +
DEC R18
 +
BRNE WAIT2
 +
 +
 +
PWM_PIN_OFF:
 +
CBI PORTB, OSV_INT ; vypne interni svetlo
 +
 +
WAIT3: INC R1 ; po vypnuti ceka
 +
BRNE WAIT3
 +
DEC R19
 +
BRNE WAIT3
 +
 +
cpse R16, R17 ; je R17 nula? NE-pokracuje ZHASINEJ2:
 +
rjmp ZHASINEJ2: ; skok na ZHASINEJ2:
 +
rjmp LOOP ; skok na LOOP, R17 bylo=0, je zhasnuto definitivne
 +
 +
 +
TURN_ON:
 +
SBI PORTB, OSV_INT ; zapne interni svetlo
 +
rjmp LOOP ; skok na LOOP
 +
 +
 +
TURN_OFF:
 +
CBI PORTB, OSV_INT ; vypne interni svetlo
 +
rjmp LOOP ; skok na LOOP

Verze z 16. 4. 2013, 13:58

Prosím zformátujte. Horčík


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

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

SBI DDRB, LED_X ; SBI - Set Bit in I/O Register SBI DDRB, LED_Y

SHOW:

pro klavesnici

SBI DDRB, 7  ; output pin 19 SBI PORTB, 2 ; enables pull-up rezistor on pin 14

GO: RCALL COLOR1 RCALL WAIT SBIS PINB, 2 ; skip if bit is set RCALL COLOR2 RCALL WAIT RJMP GO

RJMP SHOW ; 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, 1 ; LDI nastaveni casu 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


........................................

Obsah

tyden 5.

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 DDRC = $07 .EQU PORTC = $08 .EQU PINC = $06

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

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

.EQU FAN = 2


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

LDI R17, 0b00001111 ; pripravi hodnotu k presunu OUT DDRD, R17 ; D0-3, piny 2-5 nastavi vystupy OUT PORTC, R17 ; C0-3, piny 23-26 pull up rez


Init

RCALL FAN_OFF

GO
SBIS PINC, 2
RCALL FAN3W  ; Simple PWM control of the FAN, if the key is pressed
RJMP GO

LDI R16, 0x02 ; konstanta pro pwm




PWM: ; nastaveni 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

RJMP PWM




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


obsluha preruseni


...........UDELAT


obsluha CTENI KLAVESNICE PRI preruseni


KEYPRESS: 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 KEY1: 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

etc.

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 KEYRET LDI R16, 0 ;pressed key 0

KEYRET: RET


=======================

tyden 6

example fan program for ATmega88 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/maximalni hodnota pro PWM counter

.EQU PWM_PULSES = 45 ; value for 22.2 kHz PWM


.ORG 0x0000 rjmp Main ; Resetovaci vektor/skok na hl. program

.ORG 0x0008 rjmp isr1 ; PCINT1 Handler/skok na obsluhu preruseni


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
 !!!!!zapojeni!!!!
col1 col3 row1 row3
col2 col4 row2 row4
!!!kabel!!!

; vstup z klávesnice = sloupce 1-3 cbi DDRC, 2 ; 25 cbi DDRC, 3 ; 26 cbi DDRC, 4 ; 27

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

; set driving outputs for keyboard (ROW1-ROW4) sbi DDRD, 0 ; 2 sbi DDRD, 1 ; 3 sbi DDRD, 2 ; 4 sbi DDRD, 3 ; 5

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 ; PWM output OC0B (PD5 pin cislo 11)


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

===========================================================

Tyden 7

Ovladani vetraku, osetreni stisku 2 tlacitek na klavesnici, rozbeh z 0




===================================================
======= by Jelinek Martin and Kouba Zdenek=========
===================================================
fan(ny) program for ATmega88 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/maximalni hodnota pro PWM counter/podle pouziteho vetraku

.EQU PWM_PULSES = 45 ; value for 22.2 kHz PWM


.ORG 0x0000 rjmp Main ; Resetovaci vektor/skok na hl. program

.ORG 0x0008 rjmp isr1 ; PCINT1 Handler/skok na obsluhu preruseni


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

ldi r19, 0 ;reg R19 je pouzit v rutine pro rozbehovy puls

Keyboard init
 !!!!!zapojeni!!!!
col1 col3 row1 row3
col2 col4 row2 row4
!!!kabel!!!

; vstup z klávesnice = sloupce 1-3 cbi DDRC, 2 ; 25 cbi DDRC, 3 ; 26 cbi DDRC, 4 ; 27

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

; set driving outputs for keyboard (ROW1-ROW4) sbi DDRD, 0 ; 2 sbi DDRD, 1 ; 3 sbi DDRD, 2 ; 4 sbi DDRD, 3 ; 5

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 ; PWM output OC0B (PD5 pin cislo 11)


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: mov R19, R17 rcall KEYPRESS cpse R19, R16 ;pokud nejsou rovny, zmena cisla - muze jit o prechod 0 - non0 rcall TEST_START

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

===============================

TEST_START: ldi R18, 0x0 cpse R18, R19 ; was the old length 0? rcall STARTING_PULSE ; if yes, run the starting pulse ret

STARTING_PULSE: sbi PORTD, 5 rcall WAIT cbi PORTD, 5 ret

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

===============================


Keyboard decoding

KEYPRESS:

inicializace

cbi DDRD, 0 ; nastaveni vstupu na PORTD cbi DDRD, 1 cbi DDRD, 2 cbi DDRD, 3

sbi PORTD, 0 ; nastaveni pull-up rezistoru na PORTD sbi PORTD, 1 sbi PORTD, 2 sbi PORTD, 3


KEY0: sbi DDRD, 3 ;nastaveni vystupu cbi PORTD, 3 ;log. 0 na radek *, 0, #, D

sbic PINC, 3 rjmp KEY1 ldi R16, 0 ;pressed key 0 rjmp KEYRET


KEY1: cbi DDRD, 3 ;obnoveni vstupu sbi PORTD, 3 ;obnovení pull-up rezistoru

sbi DDRD, 0 ;nastaveni vystupu cbi PORTD, 0 ;log. 0 na radek *, 0, #, D


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: cbi DDRD, 0 ;obnoveni vstupu sbi PORTD, 0 ;obnovení pull-up rezistoru

sbi DDRD, 1 ;nastaveni vystupu cbi PORTD, 1 ;log. 0 na radek *, 0, #, D


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: cbi DDRD, 1 ;obnoveni vstupu sbi PORTD, 1 ;obnovení pull-up rezistoru

sbi DDRD, 2 ;nastaveni vystupu cbi PORTD, 2 ;log. 0 na radek *, 0, #, D


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:

sbi DDRD, 0 ; nastaveni vystupu na PORTD sbi DDRD, 1 sbi DDRD, 2 sbi DDRD, 3

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


tyden 9.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%JELINEK Martin, KOUBA Zdenek%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


.INCLUDE "m88def.inc" ; info pro prekladac


.ORG 0x0000 rjmp START ; Resetovaci vektor/skok na hl. program


Main program start

START:

%%%%NASTAVENI%%%%
nastaveni IN/OUT ; PORTC ADC in!!!

SBI DDRC, 0 ; clk SBI DDRC, 1 ; serial clk SBI DDRC, 2 ; paralel clk CBI DDRC, 4 ; change number input


LOOP:


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



SBIS PINC,4 RJMP HODJEDNA RJMP HODDVA

HODJEDNA: LDI R16, 0b11111010 RCALL REGISTR RJMP LOOP

HODDVA: LDI R16, 0b01111001 RCALL REGISTR RJMP LOOP


RCALL REGISTR
by MARTIN JELINEK
v registru R16 ocekava cislo
ovlada PORTC
pro posuvny registr 74595
PIN9 - serial data input - PORTC 0
PIN10 - master reset (active LOW) - trvale pripojit na Vcc
PIN11 - shift registr clock input - PORTC 1
PIN12 - storage registr clock input - PORTC 2
PIN13 - output enable (active LOW) - trvale pripojit na GND

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


=======================

Domaci ukol 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%1. home work - JELINEK Martin, KOUBA Zdenek%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
po startu dojde k inicializaci HW
==nastaveni vstupu a vystupu
==vystupy vynulovany
==nastaveni PWM a AD prevodniku
==nastaveni a nasledne pouziti SLEEP MODu


.INCLUDE "m88def.inc" ; info pro prekladac


.EQU OSV_INT = 1 ; prirazeni pinu pro osvetleni interieru .EQU DOOR_IN = 2 ; prirazeni pinu pro dverni kontakt

.EQU OSV_PALUB = 5 ; prirazeni pinu pro osvetleni interieru (PD5) .EQU AD_IN = 4 ; prirazeni pinu pro potenciometr

.EQU STMIVANI = 100 ; pocet stmivacich kroku interniho osvetleni (0-255) .EQU PWM_PULSES = 50 ; pocet pulzu osvetleni palubni desky za 1 periodu PWM

.ORG 0x0000 rjmp START ; Resetovaci vektor/skok na hl. program

.ORG 0x0008 rjmp PALUBKA ; vektor preruseni/skok na obsluhu nastaveni palubniho osvetleni



Main program start

START:


%%%%NASTAVENI%%%%
nastaveni IN/OUT ; zvolit port podle pouziteho procesoru!!!!!!!!!!!!!!!!

SBI DDRB, OSV_INT ; nastaveni vystupniho pinu pro osvetleni interieru CBI DDRB, DOOR_IN ; nastaveni vstupniho pinu pro dverni kontakt

SBI DDRD, OSV_PALUB ; nastaveni vystupniho pinu pro osvetleni interieru CBI DDRB, AD_IN ; nastaveni vstupniho pinu pro potenciometr


nastaveni SLEEP modu
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


nastaveni A/D converteru

ldi R16,0b10001111 ; ADC enable (8.bit), enable interrupt (4.bit), prescaler 128 out ADCSRA,R16 ; vystup: ; 2. a 1. bit ADCL (ADC9, ADC8) ; 8. - 1. bit ADCH (ADC7 - ADC0)

nastaveni hardwarove PWM

PWM_PALUB_INIT: ; timer 0 ldi R17, 0x00 ; disable all interrupts out TIMSK0, R17 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, OSV_PALUB ; PWM output - pin pro osvetleni palubni desky

-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
==========ovladani osvetleni palubni desky potenciometrem
(popis funkce)
AD prevod nastaven pravidelne
dokonceni AD prevodu vyvolava preruseni
v preruseni je cislo z AD prevodniku predano do PWM
%%%%OBSLUHA_PRERUSENI%%%%


PALUBKA: MOV R20, ADCH out OCR0B, R20 ; output pulse "high" lenght to PWM compare unit reti  ; return from Interrupt


==========ovladani vnitrniho osvetleni dvernim kontaktem
(popis funkce)
kontakt pripojen na pin (DOOR_IN)
zmena na pinu probudi mikrokontroler ze sleep modu
rozhodne se, zda se jedna o sepnuti 1), ci rozepnuti kontaktu 2)
1) nastaveni vystupu do log. 1
1) sleep mode
2) cekaci smycka 5 sec
2) softwarova klesajici PWM
2) sleep mode
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
%%%%PROCEDURY%%%%


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

LOOP: sleep ; prechod do sleep modu nop ; return from Interrupt to this instruction SBIC DDRB, DOOR_IN ; je kontakt sepnut? NE-pokracuje ZHASINEJ1 rjmp TURN_ON ; skok na sepnuti svetla


ZHASINEJ1: ldi R16, 0x0 cpse R16, R17 ; je R17 nula? NE-pokracuje CEKEJ5SEC rjmp CEKEJ5SEC ; skok na CEKEJ5SEC rjmp LOOP ; skok na LOOP, R17 bylo=0, je zhasnuto


CEKEJ5SEC: ; cekaci smycka pred zapocetim stmivani LDI R3, 20 ; doba je nastavovana cislem ukladanym do registru R3 WAIT1: INC R1 BRNE WAIT1 INC R2 BRNE WAIT1 DEC R3 BRNE WAIT1

LDI R17, STMIVANI ; ulozi pocet stmivacich kroku(=x) do R17=x´ LDI R19, STMIVANI ; R19=y

ZHASINEJ2: MOV R18, R17 ; R18=x´´ DEC R17 SUB R19, R17

PWM_PIN_ON: SBI PORTB, OSV_INT ; zapne interni svetlo

WAIT2: INC R1 ; po zapnuti ceka BRNE WAIT2 DEC R18 BRNE WAIT2


PWM_PIN_OFF: CBI PORTB, OSV_INT ; vypne interni svetlo

WAIT3: INC R1 ; po vypnuti ceka BRNE WAIT3 DEC R19 BRNE WAIT3

cpse R16, R17 ; je R17 nula? NE-pokracuje ZHASINEJ2: rjmp ZHASINEJ2: ; skok na ZHASINEJ2: rjmp LOOP ; skok na LOOP, R17 bylo=0, je zhasnuto definitivne


TURN_ON: SBI PORTB, OSV_INT ; zapne interni svetlo rjmp LOOP ; skok na LOOP


TURN_OFF: CBI PORTB, OSV_INT ; vypne interni svetlo rjmp LOOP ; skok na LOOP

Osobní nástroje