2014年1月4日土曜日

16x8マトリクスLEDでbitman表示

前回のマトリクスLED回路をAtmega8で制御してbitmanを表示してみた。
  • bitmanのパターンはここを参考
  • runで巡回スクロールするのにかなり脳みそを使った
  • runのパターンとMSB,LSBが逆になっていてスクロール方向が右シフトになってる状況でさらに巡回とか
  • danceも同様にスクロールのシフト演算がややこしかった
  • 十数回はトライアンドエラーを繰り返してようやく思い通りのパターンに
  • プログラム変更して試すのに毎回4分かかるのがやはりキツイ
動作風景



プログラム
----
/*
atmega8 16pu
2 x 8x8 matrix led controller
ISP
PC6 RESET
PB5 SCK
PB4 MISO
PB3 MOSI
USART
PD0 RXD
PD1 TXD
INT
PD2 INT0
74HC138N
PD4 A0
PD5 A1
PD6 A2
74HC595
PD7 DS
PB0 ~OE
PB1 STCP/SHCP
*/
#define F_CPU 1000000UL // Clock Speed
#define BAUD 9600
#define MYUBRR (((F_CPU / (BAUD * 16UL))) - 1)
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define sbi(PORT,BIT) PORT|=_BV(BIT)
#define cbi(PORT,BIT) PORT&=~_BV(BIT)
#define set(p,d) \
if ((d)) { \
sbi(p);\
} else {\
cbi(p);\
}
#define DELAY 1600
#define INTERVAL 50000 // 0.05sec
#define LIMIT 30000000 // 30sec
volatile uint32_t e;
void
display(
uint16_t m[8]
)
{
uint8_t i,j;
for(i=0;i<8;i++) {
/* row */
switch(i) {
case 0:
cbi(PORTD,PD4);
cbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 1:
sbi(PORTD,PD4);
cbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 2:
cbi(PORTD,PD4);
sbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 3:
sbi(PORTD,PD4);
sbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 4:
cbi(PORTD,PD4);
cbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
case 5:
sbi(PORTD,PD4);
cbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
case 6:
cbi(PORTD,PD4);
sbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
case 7:
sbi(PORTD,PD4);
sbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
}
sbi(PORTB,PB1);
cbi(PORTB,PB0);
for(j=0;j<16;j++) {
if(m[i]&(1<<j)) {
sbi(PORTD,PD7);
} else {
cbi(PORTD,PD7);
}
sbi(PORTB,PB0);
cbi(PORTB,PB0);
}
sbi(PORTB,PB0);
cbi(PORTB,PB0);
cbi(PORTB,PB1);
_delay_us(DELAY);
}
}
void
dance()
{
uint8_t i,x;
uint16_t p,m[8];
uint32_t e1;
uint8_t d[9][8] = {
{
0b00011000,
0b00011000,
0b00111100,
0b01011010,
0b01011010,
0b00100100,
0b00100100,
0b01100110
},{
0b00000000,
0b00011000,
0b00111100,
0b01011010,
0b01011010,
0b00011000,
0b00011000,
0b00111100
},{
0b00000000,
0b00110000,
0b00111100,
0b01011010,
0b01011100,
0b00011000,
0b00011000,
0b00111100
},{
0b00000000,
0b00110000,
0b00111100,
0b11011010,
0b00111100,
0b00101000,
0b01101000,
0b00001100
},{
0b00000000,
0b00110000,
0b00111100,
0b01011010,
0b01011010,
0b00101000,
0b00101000,
0b01101100
},{
0b00000000,
0b00011000,
0b00111100,
0b01011010,
0b01011010,
0b00011000,
0b00011000,
0b00111100
},{
0b00001100,
0b00001100,
0b00111100,
0b01011011,
0b00111100,
0b00010100,
0b00010110,
0b00110000
},{
0b00000000,
0b00001100,
0b00111100,
0b01011010,
0b01011010,
0b00010100,
0b00010100,
0b00110110
},{
0b00011000,
0b00011000,
0b00111100,
0b01011010,
0b01011010,
0b00100100,
0b00100100,
0b01100110
}
};
x = 0;
p = 0;
e1 = 0;
while (e<LIMIT) {
if (e1 > INTERVAL) {
sbi(PORTB,PB1);
for(i=0;i<8;i++) {
if (x <= 8) {
m[i] = d[p][i] << (8-x) << 8;
m[i] |= d[p][i] >> x;
} else {
m[i] = d[p][i] << x;
m[i] |= d[p][i] << (x-8) >> 8;
}
}
p += 1;
if (p >= 9) {
p = 0;
}
x += 1;
if (x >= 16) {
x = 0;
}
e1 = 0;
}
display(m);
e += DELAY * 8;
e1 += DELAY * 8;
}
}
int
main(void)
{
// port b output setting
sbi(DDRB,PB0);
sbi(DDRB,PB1);
// port d output setting
sbi(DDRD,PD4);
sbi(DDRD,PD5);
sbi(DDRD,PD6);
sbi(DDRD,PD7);
e = 0;
srand(0);
while (1) {
dance();
e = 0;
}
return 0;
}
view raw dance.c hosted with ❤ by GitHub
/*
atmega8 16pu
2 x 8x8 matrix led controller
ISP
PC6 RESET
PB5 SCK
PB4 MISO
PB3 MOSI
USART
PD0 RXD
PD1 TXD
INT
PD2 INT0
74HC138N
PD4 A0
PD5 A1
PD6 A2
74HC595
PD7 DS
PB0 ~OE
PB1 STCP/SHCP
*/
#define F_CPU 1000000UL // Clock Speed
#define BAUD 9600
#define MYUBRR (((F_CPU / (BAUD * 16UL))) - 1)
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define sbi(PORT,BIT) PORT|=_BV(BIT)
#define cbi(PORT,BIT) PORT&=~_BV(BIT)
#define set(p,d) \
if ((d)) { \
sbi(p);\
} else {\
cbi(p);\
}
#define DELAY 1600
#define INTERVAL 50000 // 0.05sec
#define LIMIT 30000000 // 30sec
volatile uint32_t e;
void
display(
uint16_t m[8]
)
{
uint8_t i,j;
for(i=0;i<8;i++) {
/* row */
switch(i) {
case 0:
cbi(PORTD,PD4);
cbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 1:
sbi(PORTD,PD4);
cbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 2:
cbi(PORTD,PD4);
sbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 3:
sbi(PORTD,PD4);
sbi(PORTD,PD5);
cbi(PORTD,PD6);
break;
case 4:
cbi(PORTD,PD4);
cbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
case 5:
sbi(PORTD,PD4);
cbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
case 6:
cbi(PORTD,PD4);
sbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
case 7:
sbi(PORTD,PD4);
sbi(PORTD,PD5);
sbi(PORTD,PD6);
break;
}
sbi(PORTB,PB1);
cbi(PORTB,PB0);
for(j=0;j<16;j++) {
if(m[i]&(1<<j)) {
sbi(PORTD,PD7);
} else {
cbi(PORTD,PD7);
}
sbi(PORTB,PB0);
cbi(PORTB,PB0);
}
sbi(PORTB,PB0);
cbi(PORTB,PB0);
cbi(PORTB,PB1);
_delay_us(DELAY);
}
}
void
run()
{
uint8_t i,j,x;
uint16_t p,m[8];
uint32_t e1;
/* run */
uint8_t r[8][8] = {
{
0b00011000,
0b00011000,
0b00111100,
0b01011010,
0b01011010,
0b00100100,
0b00100100,
0b01100110
},
{
0b00000000,
0b00011000,
0b01111110,
0b10011001,
0b00011000,
0b00111100,
0b00100100,
0b01100110
},
{
0b00001100,
0b00001100,
0b00111000,
0b01011100,
0b01011000,
0b00100100,
0b00100110,
0b01100000
},
{
0b00001100,
0b00001100,
0b00111000,
0b01011100,
0b01011100,
0b00110100,
0b01110110,
0b10000000
},
{
0b00000000,
0b00101100,
0b01011101,
0b00011110,
0b11011000,
0b01100100,
0b00000100,
0b00000110
},
{
0b00000000,
0b00001100,
0b00011100,
0b00111000,
0b00011110,
0b00110000,
0b00010000,
0b00011000
},
{
0b00000110,
0b00000110,
0b00001100,
0b00001100,
0b00001110,
0b00001110,
0b00011000,
0b00010000
},
{
0b00000000,
0b00011000,
0b11111111,
0b00011000,
0b00111000,
0b00100100,
0b01100100,
0b00000110
}
};
x = 0;
p = 0;
e1 = 0;
for(j=0;j<2;j++) {
for(i=0;i<8;i++) {
m[i] = r[j][i];
}
for(i=0;i<3;i++) {
display(m);
}
}
while (e<LIMIT) {
if (e1 > INTERVAL) {
sbi(PORTB,PB1);
for(i=0;i<8;i++) {
if (x < 8) {
m[i] = r[p][i] << (8-x) << 8;
m[i] |= r[p][i] >> x;
} else {
m[i] = r[p][i] << (16-x);
}
}
p += 1;
if (p >= 7) {
p = 2;
}
x += 1;
if (x >= 16) {
x = 0;
}
e1 = 0;
}
display(m);
e += DELAY * 8;
e1 += DELAY * 8;
}
}
int
main(void)
{
// port b output setting
sbi(DDRB,PB0);
sbi(DDRB,PB1);
// port d output setting
sbi(DDRD,PD4);
sbi(DDRD,PD5);
sbi(DDRD,PD6);
sbi(DDRD,PD7);
e = 0;
srand(0);
while (1) {
run();
e = 0;
}
return 0;
}
view raw run.c hosted with ❤ by GitHub
----

0 件のコメント:

amazonアソシエイト