Ein bisschen mehr der Atmega8 Lektüre

Benutzeravatar
davidvajda.de
Site Admin
Beiträge: 1488
Registriert: Di Jul 18, 2023 8:36 pm
Wohnort: D-72072, Tübingen
Kontaktdaten:

Re: Ein bisschen mehr der Atmega8 Lektüre

Beitrag von davidvajda.de »

Also, ich mache das jetzt so - ich schreibe erst Mal ein Quine Mc Cluskey für 3 Variablen

3 Variablen, weil - ich 8 Ausgabe LED Bits habe.

Wenn ich für Eingangsvariablen habe, habe ich 16 ausgabebits, bei 3 Eingangsvariablen, 8

Code: Alles auswählen

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {
    int x0, x1, x2;
    time_t t;
    int i;

    
    srand((unsigned) time(&t));
    

    for (x2 = 0;  x2 <= 1;  x2++) {
        for (x1 = 0;  x1 <= 1;  x1++) {
            for (x0 = 0;  x0 <= 1;  x0++, i++) {
                    printf( "%2i %i %i %i    %d\n", i, x2, x1, x0, rand () %2);
            }
        }
    }
}

Code: Alles auswählen

david@laptop-peaq:~$ gcc quine8.c -o quine8
david@laptop-peaq:~$ ./quine8 > quine
ich schreibe ein makefile dazu.

Code: Alles auswählen

compile:
 gcc quine8.c -o quine8
run:
 ./quine8 > quine00xx.tmp

Code: Alles auswählen

10 0 0 0    1
11 0 0 1    1
12 0 1 0    0
13 0 1 1    1
14 1 0 0    0
15 1 0 1    1
16 1 1 0    0
17 1 1 1    1
Da stimmt noch was nicht.

Code: Alles auswählen

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {
    int x0, x1, x2;
    time_t t;
    int i;

    
    srand((unsigned) time(&t));
    

    for (i = 0, x2 = 0;  x2 <= 1;  x2++) {
        for (x1 = 0;  x1 <= 1;  x1++) {
            for (x0 = 0;  x0 <= 1;  x0++, i++) {
                    printf( "%2i %i %i %i    %d\n", i, x2, x1, x0, rand () %2);
            }
        }
    }
}

i wurde nicht initialisiert.

Code: Alles auswählen

0 0 0 0    1
 1 0 0 1    0
 2 0 1 0    0
 3 0 1 1    1
 4 1 0 0    1
 5 1 0 1    0
 6 1 1 0    0
 7 1 1 1    1

 0 0 0 0    1
 3 0 1 1    1
 4 1 0 0    1
 7 1 1 1    1

Grupppe 0:
 0 0 0 0    1
Gruppe 1:
 4 1 0 0    1
Gruppe 2:
 3 0 1 1    1
Gruppe 3:
 7 1 1 1    1

0:4     - 0 0
3:7     - 1 1

y <= (not x1 and not x0) or (x2 and x0)
Na ja, das ist ein bisschen wenig, trotzdem schreibe ich mal das Programm dazu.

Code: Alles auswählen

0 0 0 0    1
 1 0 0 1    0
 2 0 1 0    0
 3 0 1 1    1
 4 1 0 0    1
 5 1 0 1    0
 6 1 1 0    0
 7 1 1 1    1

 0 0 0 0    1
 3 0 1 1    1
 4 1 0 0    1
 7 1 1 1    1

Grupppe 0:
 0 0 0 0    1
Gruppe 1:
 4 1 0 0    1
Gruppe 2:
 3 0 1 1    1
Gruppe 3:
 7 1 1 1    1

0:4     - 0 0
3:7     - 1 1

y <= (not x1 and not x0) or (x2 and x0)

; r2 ^= x2
; r1 ^= x1
; r0 ^= x0

Jetzt habe ich DNF und KNF

jeder Min oder Max term wird in ein Register geschrieben, mit der Verknüpfung der einzelnen Variablen
Diese Termen werden ins nächste Register verknüpft

Also, bei der DNF sind es Minterme

Das geht dann so

Zunächst muss ich die Variablen in einer Art Assembler Schleife ändern

ldi r0, 0
ldi r1, 0
ldi r2, 0

Code: Alles auswählen

ldi r0, 0
ldi r1, 0
ldi r2, 0

label1:

label2:

label3:

; Hier kommt die eigentliche Aktion und eigentlich Verknüpfungen

inc r0
cpi r0, 2
brle label3
ldi r0, 0
inc r1
cpi r1, 2
brle label2
ldi r1, 0
inc r2
cpi r2, 2
brle label1
ldi r0, 0
ldi r1, 0
ldi r2, 0
Der Befehl brle beim Atmega8 ist nicht richtig.
- Branch Less or Equal ist eh falsch. Es muss Branch Less heissen und der heisst beim Atmega8

Code: Alles auswählen

brlo
Ich muss die Register durch die +16 ersetzen

Code: Alles auswählen

; so ist richtig
.include "m8def.inc"

ldi r16, 0
ldi r17, 0
ldi r18, 0

label1:

label2:

label3:

; Hier kommt die eigentliche Aktion und eigentlich Verknüpfungen

inc r16
cpi r16, 2
brlo label3
ldi r16, 0
inc r17
cpi r17, 2
brlo label2
ldi r17, 0
inc r18
cpi r18, 2
brlo label1
ldi r16, 0
ldi r17, 0
ldi r18, 0

Code: Alles auswählen

Assembly complete with no errors.
Segment usage:
   Code      :        17 words (34 bytes)
   Data      :         0 bytes
   EEPROM    :         0 bytes
david@laptop-peaq:~$

Jetzt kommt die Verknüpfung, danach die Ausgabe auf dem Port - bei der Ausgabe auf dem Port, muss mit einer wandelnden Bitmaske vorgegangen werden.

Code: Alles auswählen

.include "m8def.inc"

ldi r16, 0
ldi r17, 0
ldi r18, 0
ldi r23, 1

label1:

label2:

label3:

; Hier kommt die eigentliche Aktion und eigentlich Verknüpfungen
ldi r19, 1
mov r20, r16
mov r21, r17
com r20
com r21
andi r20, 0x01
andi r21, 0x01
and r19, r20
and r19, r21
mov r22, r19
ldi r19, 1
and r19, r16
and r19, r17
or r22, r19

inc r16
cpi r16, 2
brlo label3
ldi r16, 0
inc r17
cpi r17, 2
brlo label2
ldi r17, 0
inc r18
cpi r18, 2
brlo label1
ldi r16, 0
ldi r17, 0
ldi r18, 0

Code: Alles auswählen

.include "m88def.inc"

ldi r16, 0
ldi r17, 0
ldi r18, 0
ldi r23, 1
ldi r25, 0

label1:

label2:

label3:

; Hier kommt die eigentliche Aktion und eigentlich Verknüpfungen
ldi r19, 1
mov r20, r16
mov r21, r17
com r20
com r21
andi r20, 0x01
andi r21, 0x01
and r19, r20
and r19, r21
mov r22, r19
ldi r19, 1
and r19, r16
and r19, r17
or r22, r19

ldi r24, 1

label4:
cp r24, r23
brge label5
lsl r22
lsl r24
rjmp label4
label5:
or r25, r22
lsl r23
inc r16
cpi r16, 2
brlo label3
ldi r16, 0
inc r17
cpi r17, 2
brlo label2
ldi r17, 0
inc r18
cpi r18, 2
brlo label1
ldi r16, 0
ldi r17, 0
ldi r18, 0

ldi r16, 0xff
out DDRD, r16

mov r16, r25
out PORTD, r16

end: rjmp end

Bild

Also, ich denke, ich habe es geschafft, das letzte Bit am Unteren Ende stimmt nicht, es kann sein, dass das ein Rechenfehler ist, eben beim letzten Bit, das erscheint, wahrscheinlich

Ansonsten müssen sie darauf achten, dass die Bits invertiert sind. Bei 1 ist die LED aus bei 0 ist die LED an.

Aber es entspricht nahezu der Wahrheitstabelle. Ich zeige Photo

Code: Alles auswählen

 0 0 0 0    1
 1 0 0 1    0
 2 0 1 0    0
 3 0 1 1    1
 4 1 0 0    1
 5 1 0 1    0
 6 1 1 0    0
 7 1 1 1    1
Wenn man das vergleicht. Die binär umgekehrte Darstellung lässt sich einfach lösen, indem wir ein 1er Komplement, auf das alles anwenden.

Code: Alles auswählen

0 0 0 0    0
 1 0 0 1    1
 2 0 1 0    0
 3 0 1 1    1
 4 1 0 0    1
 5 1 0 1    1
 6 1 1 0    1
 7 1 1 1    0

 1 0 0 1    1
 3 0 1 1    1
 4 1 0 0    1
 5 1 0 1    1
 6 1 1 0    1

Gruppe 1:
 1 0 0 1    1
 4 1 0 0    1
Gruppe 2:
 3 0 1 1    1
 5 1 0 1    1
 6 1 1 0    1

1:3         0 - 1
1:5         - 0 1
4:5         1 0 -
4:6         1 - 0

1:3         0 - 1
4:6         1 - 0
1:5         - 0 1
4:5         1 0 -

        1   3   4   5   6
1:3     *   *
4:6             *       *
1:5     *           *
4:5             *   *

        1   3   4   5   6
1:3     *   *
4:6             *       *
1:5     *           *

1:3         0 - 1
4:6         1 - 0
1:5         - 0 1

    y <= (not x2 and x0) or
        (x2 and not x0) or
        (not x1 and x0)
Nachdem wir das gemacht haben, können wir den Fehler für das letzte Bit finden und dann können wir auch andere Wahrheitstabellen darstellen

Code: Alles auswählen

Na ja, da ist ein Fehler drin, den suchen wir ein anderes mal, nicht immer alles auf ein Mal

.include "m88def.inc"

ldi r16, 0
ldi r17, 0
ldi r18, 0
ldi r23, 1
ldi r25, 0

label1:

label2:

label3:

; Hier kommt die eigentliche Aktion und eigentlich Verknüpfungen
ldi r19, 1
mov r20, r18
mov r21, r16
com r18
andi r20, 0x01
andi r21, 0x01
and r19, r20
and r19, r21
mov r22, r19

ldi r19, 1
mov r20, r18
mov r21, r16
com r16
andi r20, 0x01
andi r21, 0x01
and r19, r20
and r19, r21
or r22, r19

ldi r19, 1
mov r20, r17
mov r21, r16
com r17
andi r20, 0x01
andi r21, 0x01
and r19, r20
and r19, r21
or r22, r19

ldi r24, 1

label4:
cp r24, r23
brge label5
lsl r22
lsl r24
rjmp label4
label5:
or r25, r22
lsl r23
inc r16
cpi r16, 2
brlo label3
ldi r16, 0
inc r17
cpi r17, 2
brlo label2
ldi r17, 0
inc r18
cpi r18, 2
brlo label1
ldi r16, 0
ldi r17, 0
ldi r18, 0

ldi r16, 0xff
out DDRD, r16

mov r16, r25
out PORTD, r16

end: rjmp end
Antworten