forked from skiffich/3D_Cube444_Attiny2313
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
172 lines (160 loc) · 4.19 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <avr/io.h>
#include <util/delay.h>
/*
//Write to register
void DigitalWrite(unsigned int val)
{
PORTD = 0x00; //start writing
for (unsigned int i = 0; i < 16; i++)
{
PORTD = (val>>i)&0x01;
PORTD = ((val>>i)&0x01)|0x04;
_delay_us(900);
PORTD = (val>>i)&0x01;
_delay_us(900);
}
PORTD = 0x02; //end writing
}
//*/
// Serial out <numb> bits from datapin using clockpin as /clk in direction (Most Significant Bit First, or, Least Significant Bit First)
void ShiftOutB(int val, int datapin, int clockpin, int direction, int numb)
{
unsigned char byte = 0;
if(direction == 1)
{
for (int i = 0; i < numb; i++)
{
PORTB = PORTB|(((((val >> i)) & 0x01)<< datapin)|(0x01 << clockpin));
PORTB = 0x00;
}
}
else
{
for (int i = numb - 1; i >= 0; i--)
{
PORTB = PORTB|(((((val >> i)) & 0x01)<< datapin)|(0x01 << clockpin));
PORTB = 0x00;
}
}
}
void ShiftOutD(int val, int datapin, int clockpin, int direction, int numb)
{
if(direction == 1)
{
for (int i = 0; i < numb; i++)
{
PORTD = PORTD|(((((val >> i)) & 0x01)<< datapin)|(0x01 << clockpin));
PORTD = 0x00;
}
}
else
{
for (int i = numb - 1; i >= 0; i--)
{
PORTD = PORTD|(((((val >> i)) & 0x01)<< datapin)|(0x01 << clockpin));
PORTD = 0x00;
}
}
}
//same as ShiftOut with adding PortD or PortB. Each port needs own ShiftOut function. Also added MR pin
void RegWrite(int port, int val, int MR, int CLK, int DS, int direction, int numb)
{
if (port)
{
PORTB = 0x00;
ShiftOutB(val,DS,CLK,direction, numb);
PORTB = 0x01<<MR;
}
else
{
PORTD = 0x00;
ShiftOutD(val,DS,CLK,direction, numb);
PORTD = 0x01<<MR;
}
}
// Switch on one LED in 4*4*4 LED matrix on x,y,z coordinates
void MatWrite(unsigned int x, unsigned int y, unsigned int z)
{
/* 0,0,0
/-----------------------------------/
/ / |
Z/ / |
/ / |
/ X / |
|-----------------------------------| |
| | |
| | |
| | |
| | |
Y| | |
| | /
| | /
| | /
| | /
|-----------------------------------| /
*/
x = 3 - x; //change x
y = 3 - y; // and y direction
int xx = x;
x = y; //Swap x and y
y = xx;
x = 0x01 << x; // Write 1 in same bit as x value
y = ((~(0x01 << y)) & 0x0F); //Write 1 in same bit as y value and invert last 4 bits
unsigned int byte;
byte = ((x<<4) + y) & 0xFF; //Write x as High bits and y as Low bits
//Write to Register
if (z == 0) {RegWrite(1, byte << 8, 0, 2, 1, 1, 16); RegWrite(1, 0, 3, 5, 4, 1, 16); }
if (z == 1) {RegWrite(1, byte, 0, 2, 1, 1, 16); RegWrite(1, 0, 3, 5, 4, 1, 16); }
if (z == 2) {RegWrite(1, byte << 8, 3, 5, 4, 1, 16); RegWrite(1, 0, 0, 2, 1, 1, 16); }
if (z == 3) {RegWrite(1, byte, 3, 5, 4, 1, 16); RegWrite(1, 0, 0, 2, 1, 1, 16); }
}
int main(void)
{
DDRB = 0b00111111;
while(1)
{
/* //Uncomment here. Serial change. Correct
unsigned int x;
for(int z = 0; z < 2; z++)
{
for (unsigned int i = 128; i >= 16; i /= 2)
{
for (unsigned int j = 8; j >= 1; j /= 2)
{
x = ((~j)&0x0F) + i;
x = x<<(z*8);
RegWrite(1, x, 0, 2, 1, 1, 16);
_delay_ms(10);
}
}
}
for(int z = 0; z < 2; z++)
{
for (unsigned int i = 128; i >= 16; i /= 2)
{
for (unsigned int j = 8; j >= 1; j /= 2)
{
x = ((~j)&0x0F) + i;
x = x<<(z*8);
RegWrite(1, x, 3, 5, 4, 1, 16);
_delay_ms(10);
}
}
}
//*/
//* //Uncomment here. Send xyz. Correct
for (int z = 0; z < 4; z++)
{
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
MatWrite(x,y,z);
_delay_ms(10);
}
_delay_ms(100);
}
}
//*/
}
}