74HC595 Shift Register

74HC595 Shift Register
Çeşme: http://www.arduino.cc/en/tutorial/ShiftOut


Translate an If-then-else statement to AVR assembly code
https://www.youtube.com/watch?v=xEJtdMQMcrs


MIPS Example - Modulus Operation
https://www.youtube.com/watch?v=GGyKjBZGmZ0
//Pin connected to ST_CP of 74HC595 int latchPin = 8; //Pin connected to SH_CP of 74HC595 int clockPin = 12; //Pin connected to DS of 74HC595 int dataPin = 11; int coin = 0; void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } void loop() { digitalWrite(latchPin, 0); digitalWrite(dataPin, 0); digitalWrite(clockPin, 0); for (int i = 0; i < 16; i++) { digitalWrite(clockPin, 0); digitalWrite(dataPin, (i%2==0)==coin); digitalWrite(clockPin, 1); digitalWrite(dataPin, 0); } digitalWrite(clockPin, 0); digitalWrite(latchPin, 1); coin = coin==0?1:0; delay(500); } /* #define PIN_LATCH 8 #define PIN_CLOCK 12 #define PIN_DATA 11 #define pulse01(pin) digitalWrite(pin, LOW); digitalWrite(pin, HIGH); int c = 0; void setup() { pinMode(PIN_LATCH, OUTPUT); pinMode(PIN_CLOCK, OUTPUT); pinMode(PIN_DATA, OUTPUT); } void loop() { for (int i = 0; i < 8; i++) { digitalWrite(PIN_DATA, i!=c); pulse01(PIN_CLOCK); digitalWrite(PIN_DATA, LOW); } c = ++c % 8; pulse01(PIN_LATCH); delay(100); } */
#include <avr/io.h> #include <util/delay.h> #define PIN_LATCH PB0 #define PIN_DATA PB3 #define PIN_CLOCK PB4 int main(void) { DDRB |= (1<<PIN_LATCH); DDRB |= (1<<PIN_DATA); DDRB |= (1<<PIN_CLOCK); int c = 0; while (1) { for (int i = 0; i < 8; i++) { if (i!=c) { PORTB |= (1<<PIN_DATA); } else { PORTB &= ~(1<<PIN_DATA); } PORTB &= ~(1<<PIN_CLOCK); PORTB |= (1<<PIN_CLOCK); PORTB &= ~(1<<PIN_DATA); } c = ++c % 8; PORTB &= ~(1<<PIN_LATCH); PORTB |= (1<<PIN_LATCH); _delay_ms(100); } return 0; }
.nolist .include "./m328Pdef.inc" .list .equ latchPin = 0 ; Pin connected to ST_CP of 74HC595 .equ clockPin = 4 ; Pin connected to SH_CP of 74HC595 .equ dataPin = 3 ; Pin connected to DS of 74HC595 .def temp = r16 .def i = r17 .def coin = r18 .org 0x0000 rjmp start start: ldi temp, low(ramend) out spl, temp ldi temp, high(ramend) out sph, temp sbi DDRB, latchPin sbi DDRB, clockPin sbi DDRB, dataPin clr coin loop: cbi PORTB, latchPin cbi PORTB, dataPin cbi PORTB, clockPin clr i for: cbi PORTB, clockPin mov temp, i andi temp, 1 ; 2-1 cpi temp, 0 ; if mod%2 brne else cp coin, temp brne else ; then sbi PORTB, dataPin rjmp done else: cbi PORTB, dataPin done: sbi PORTB, clockPin cbi PORTB, dataPin inc i cpi i, 16 brne for cbi PORTB, clockPin sbi PORTB, latchPin rcall flip_coin rcall delay_500ms rjmp loop ret flip_coin: cpi coin, 0 brne coin_else ldi coin, 1 rjmp coin_done coin_else: ldi coin, 0 coin_done: ret delay_500ms: ; the subroutine: ldi r19, 31 ; load r18 with 31 OUTER_LOOP: ; outer loop label ldi r24, 0xfd ; load registers r24:r25 with 1021, our new ; init value ldi r25, 0x03 ; the loop label DELAY_LOOP: ; "add immediate to word": r24:r25 are ; incremented adiw r24, 1 ; if no overflow ("branch if not equal"), go ; back to "delay_loop" brne DELAY_LOOP dec r19 ; decrement r18 brne OUTER_LOOP ; and loop if outer loop not finished ret ; return from subroutine
#chip mega328p 'Pin connected to ST_CP of 74HC595 #define latchPin PORTB.0 'Pin connected to SH_CP of 74HC595 #define clockPin PORTB.4 'Pin connected to DS of 74HC595 #define dataPin PORTB.3 dir latchPin out dir clockPin out dir dataPin out Dim coin as Integer Do Forever latchPin = 0 dataPin = 0 clockPin = 0 For temp = 0 To 15 clockPin = 0 If (temp%2)=coin Then dataPin = 1 Else dataPin = 0 End If clockPin = 1 dataPin = 0 Next clockPin = 0 latchPin = 1 If coin = 0 Then coin = 1 Else coin = 0 End If Wait 500 ms Loop