Created
May 14, 2019 11:14
-
-
Save avr-programmierung/f02d5f96e0e15d9c45d03a5bd81aa738 to your computer and use it in GitHub Desktop.
ATmega88 @ 8MHz Lauflicht 01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* lauflicht_sr_01.c ATmega88 @ 8MHz */ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
void soft_serial (uint8_t data) // Funktion soft_serial | |
{ | |
for (uint8_t i=0; i<=7; i++) | |
{ | |
if (data & 1) // Wenn data an Stelle 0 = 1 | |
PORTD |= (1<<PD0); // Data serial = 1 | |
else | |
PORTD &= ~(1<<PD0); // Data serial = 0 | |
PORTD |= (1<<PD1); // PD1 = 1 (CLK = low) | |
PORTD &= ~(1<<PD1); // PD1 = 0 (CLK = high) | |
data = data >> 1; // data um 1 Stelle nach rechts schieben | |
} | |
} | |
void rck (void) | |
{ | |
PORTD &= ~(1<<PD2); // PD2 = 0 (Takt = LOW für RCK) | |
PORTD |= (1<<PD2); // PD2 = 1 (Takt = HIGH für RCK -> Ausgabe Schieberegister) | |
} | |
int main(void) | |
{ | |
DDRD = 0xFF; // Richtungsregister PORTD = Ausgang | |
PORTD = 0x04; // PD2 (RCK) = High, 0x04 = 0000 0100 | |
uint8_t k, shift, data = 0x01; // data = 0000 0001 | |
while(1) | |
{ | |
_delay_ms(100); // 100ms warten | |
soft_serial(data); // Funktionsaufruf und Übergabe von | |
rck(); // Funktionsaufruf rck -> Takt für RCK generieren | |
if (data & 0x80) // Wenn 1000 000, dann nach rechts schieben | |
{ | |
shift=1; // Setze das flag shift auf 1 für Rechtslauf | |
k=0; // k zurücksetzen | |
} | |
else if (data & 0x01) // Wenn 0000 0001, dann nach links schieben | |
{ | |
shift=0; // Setze das flag shift auf 0 für Linkslauf | |
k=0; // k zurücksetzen | |
} | |
if (k <= 7 && shift == 0) // Wenn Linkslauf | |
{ | |
data = data << 1; // nach links schieben | |
k++; | |
} | |
else if (k <= 7 && shift == 1) // Wenn Rechtslauf | |
{ | |
data = data >> 1; // nach rechts schieben | |
k++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment