Z MAM wiki

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

This programm counts how many times a button connected to the MC was pushed. The number of pushing is represented in binary by three LEDs each represents one digit. So with 3 LEDs we can count to 8 pushings of the button. The scheme of the project is represented on figure below:

Soubor:Sem sch new.jpg

The button is connected to the second pin of the port D and LEDs are connected to the pins 0-2 of the port B. When the button is pushed a delay cycle starts to run. On the first step some numbers are writed to three registers. They represent the number of clocks we need to skip before doing decision about button pushing. If the MC is working on 4 MHz frequency it will be 4000000 clocks for 1 s delay. This number, that consists of three registers, is decremented by one each clock. This is used because of the jitter of the signal level on the moment when the button contacts connect. After the delay and stilling of the signal it is chacked if the button was released. If yes, the counter of pushing increments and writes to the port B.

The programm in assembly is represented below:

 ; Programm for counting of pushing in binary
 .device AT90S2313
 .include "2313def.inc"
 ; freq = 4 MHz
 .def temp = r16 ; working variable
 .def Reg0 = r17 ; registers of delay
 .def Reg1 = r18
 .def Reg2 = r19
 .def Counter = r20 ; counter
 .org 0
 ; =======Programm=======
 ;ldi temp, 0x02
 ;out OSCCAL, temp
 ldi temp, 0b00000100 ; for second registr of port D
 out PORTD, temp      ; pull-up resistor
 ldi temp, 0b11111111 ; all pins port B are in out state
 out DDRB, temp
 clr Counter
 Pincykle:       ; cykle for tracking button
   sbis PinD, 2  ; skip if it's pushed
   rjmp Pincykle ; return if it's not
   ; button is pushed - pause for 0.2s, N = $027100
   ldi Reg2, $02
   ldi Reg1, $71
   ldi Reg0, $00
   rcall Delay
   Pin_release:       ; tracking the button release
   sbic PinD, 2       ; skip if the button is released
   rjmp Pincykle      ; return if it is pushed
   inc Counter        ; if the button is released, increment the counter
   out PORTB, Counter ; lead out the counter to port B
   ; pause 0.5s, N = $061A80h
   ldi Reg2, $06
   ldi Reg1, $1A
   ldi Reg0, $80
   rcall Delay
 rjmp Pincykle   ; return to tracking
 Delay:   ; the delay procedure
   subi Reg0, 1     ; decrement register by 1
   sbci Reg1, 0     ; decrement reg. by 0 and Carry Constant
   sbci Reg2, 0
   brcc Delay
 ret      ; return from the procedure
Osobní nástroje