Skip to content

Instantly share code, notes, and snippets.

@izumogeiger
Created February 11, 2014 06:45
Show Gist options
  • Save izumogeiger/8930300 to your computer and use it in GitHub Desktop.
Save izumogeiger/8930300 to your computer and use it in GitHub Desktop.
/*
matrix led board
ISP
PC6 RESET
PB5 SCK
PB4 MISO
PB3 MOSI
74HC138N
PD4 A0
PD5 A1
PD6 A2
NJU3711
PD7 DATA
PB1 ~STB
PB0 CLK
pB7 ~CLEAR
*/
#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 100000 // 1sec
volatile uint32_t e;
volatile uint8_t m[8];
void
display()
{
uint8_t i,j;
for(i=0;i<8;i++) {
/* row */
sbi(PORTB,PB1);
cbi(PORTB,PB7);
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;
}
for(j=0;j<8;j++) {
if(m[i]&(1<<j)) {
sbi(PORTD,PD7);
} else {
cbi(PORTD,PD7);
}
cbi(PORTB,PB0);
sbi(PORTB,PB0);
}
cbi(PORTB,PB1);
sbi(PORTB,PB7);
_delay_us(DELAY);
}
}
void
_display()
{
e = 0;
while (e < LIMIT) {
display();
e += DELAY * 8;
}
}
#define TIMEOUT (254)
void
measure()
{
int i;
#if 1
for(i=7;i>0;i--) {
m[i] = m[i-1];
}
m[0] = random() % 256;
#else
for(i=0;i<8;i++) {
m[i] = i;
}
#endif
}
int
main()
{
uint8_t i;
// port b output setting
sbi(DDRB,PB0);
sbi(DDRB,PB1);
sbi(DDRB,PB2);
sbi(DDRB,PB7);
// port d output setting
sbi(DDRD,PD4);
sbi(DDRD,PD5);
sbi(DDRD,PD6);
sbi(DDRD,PD7);
// portc pullup
PORTC = 0xFF;
_delay_ms(1500);
#if 0
// 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);
#endif
// timer0
TCNT0 = 0x00;
TCCR0 = (0<<CS02) | (0<<CS01) | (1<<CS00); //ck/1
sbi(SFIOR,PSR10);
/* all interrupt allow */
for(i=0;i<8;i++) {
m[i] =0;
}
while (1) {
measure();
_display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment