2014年1月5日日曜日

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

前回のプログラムを組み合わせてさらにUSARTによるビットパターン更新を加えたものを作成した。



  • 待機モードとUSARTモード
  • 待機モードではbitman run,dance,ランダムパターンスクロールの3種類を10秒ずつランダムに切り替え
  • USART読み込みがあるとUSARTモードに移行し転送されたビットパターンを表示
  • 最後の転送から10秒経過すると待機モードに移行
動作風景



プログラム
----
/*
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 <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.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 10000000 // 10sec
#define SCROLL 0
#define RUN 1
#define DANCE 2
#define USART__ 3
volatile uint8_t mode,mi;
volatile uint32_t e;
volatile uint16_t ma[8],mb[8];
void
display(
volatile 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
scroll()
{
uint8_t i;
uint16_t m[8];
uint32_t e1;
e1 = 0;
while (e<LIMIT) {
if (mode != SCROLL) {
return;
}
if (e1 > INTERVAL) {
sbi(PORTB,PB1);
for(i=7;i>0;i--) {
m[i] = m[i-1];
}
m[0] = random() % 65546;
e1 = 0;
}
display(m);
e += DELAY * 8;
e1 += DELAY * 8;
}
mode = random() % USART__;
}
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 (mode != RUN) {
return;
}
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;
}
mode = random() % USART__;
}
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 (mode != DANCE) {
return;
}
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;
}
mode = random() % USART__;
}
void
usart__()
{
uint8_t i;
while (e<LIMIT && mode == USART__) {
display(ma);
e += DELAY * 8;
}
for(i=0;i<8;i++) {
ma[i] = 0;
mb[i] = 0;
}
mi = 0;
e = 0;
mode = random() % USART__;
}
int
main()
{
// 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);
// usart setting
/* Set baud rate */
UBRRH = (MYUBRR>>8);
UBRRL = MYUBRR;
/* Enable receiver and transmitter */
UCSRB = (1 << RXEN) | (1 << RXCIE);
/* Set frame format: 8 data bits, 1 stop bit */
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
/* all interrupt allow */
sei();
e = 0;
mode = SCROLL;
mi = 0;
srand(0);
while (1) {
switch(mode) {
case SCROLL:
scroll();
break;
case RUN:
run();
break;
case DANCE:
dance();
break;
case USART__:
usart__();
break;
}
e = 0;
}
return 0;
}
ISR(USART_RXC_vect)
{
uint8_t d,i;
mode = USART__;
d = UDR;
i = mi >> 1;
if (mi % 2 == 0) {
mb[i] = d << 8;
} else {
mb[i] |= d;
}
if (mi >= 15) {
for(i=0;i<8;i++) {
ma[i] = mb[i];
}
mi = 0;
} else {
mi++;
}
e = 0;
}
view raw usart3.c hosted with ❤ by GitHub

----

0 件のコメント:

amazonアソシエイト