output text on a selfmade 6×5-led-matrix with arduino

here’s some code that I used to output text an my selfmade 6×5-led-matrix with the arduino. i will post schematics and more information on this project later.

#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
//the text that will be written on the matrix

char output[] = "Hello World! This is just a test! * "";

//define rows and columns on the matrix as uint8_t-arrays
uint8_t rows[6] = {
2,3,4,5,6,7};
uint8_t columns[5] = {
9,10,11,12,13};

// memory for the matrix
uint8_t matrix[5];

//current column for the Interupt
uint8_t matrix_col;

uint8_t so_rows, so_columns, so_output;

void setup (){
//calculate the number of rows and columns
so_rows = sizeof(rows);
so_columns = sizeof(columns);
so_output = sizeof(output)-1;

//set pins for the matrix as output and all leds to off
for (int i = 0; i < so_rows; i++){
pinMode (rows[i], OUTPUT);
digitalWrite (rows[i], LOW);
}
for (int i = 0; i < so_columns; i++){
pinMode (columns[i], OUTPUT);
digitalWrite (columns[i], HIGH);

//Set up Timer2 to create Interrupts
Matrix_SetupTimer2();
}
}

void loop(){
for (int w = 0; w < so_output;w++){
DisplayChar (output[w]);
}
}

// Configures the ATMega328 8-Bit Timer2 to generate an interrupt every 4us
unsigned char Matrix_SetupTimer2(){
//Timer2 Settings: Timer Prescaler /64, mode 0
//Timer clock = 16MHz/64 = 250Khz or 4us
//Possible alternative Settings:
// T2S/?  |CS22|CS21|CS20| Timer Clock
// T2S/128|  1 |  0 |  1 | 16Mhz/128=125Khz or 8us
// T2S/32 |  0 |  1 |  1 | 16Mhz/32 =500Khz or 2us
// T2S/8  |  0 |  1 |  0 | 16Mhz/8  =  2Mhz or 0.5us

TCCR2A = 0;
TCCR2B = 1<<CS22 | 0<<CS21 | 0<<CS20;

//Timer2 Overflow Interrupt Enable
TIMSK2 = 1<<TOIE2;
}

// Timer2 Interruptroutine (automatically executed every 4us
ISR(TIMER2_OVF_vect) {
//set current column to 1 (sets leds off)
digitalWrite(columns[matrix_col], 1);

//cycle through all columns
matrix_col++;
if (matrix_col >= 5) matrix_col = 0;

//PORTD writes directly to Arduino Ports 0-7 (much faster than digitalWrite)
PORTD = matrix[matrix_col];

// set current column to 0 (sets leds on)
digitalWrite(columns[matrix_col], 0);
}

void Scroll_up(){
//uint8_t tmp = matrix[0];
for(uint8_t z = 0; z < so_columns; z++){
for(uint8_t i = 0; i < so_columns-1; i++){
matrix[i] = matrix[i+1];
}
matrix[so_columns-1] = 0;
delay(100);
}
}

void clearMatrix(){
for (int i; i < so_columns; i++){
matrix[i] = 0;
}
}

static unsigned char __attribute__ ((progmem)) Font5x7[] = {

0x00, 0x00, 0x00, 0x00, 0x00,// (space)  0x20

B1111110,// !
B0111100,
B0011000,
B0000000,
B0011000,

B0100100,// "
B0100100,
B0000000,
B0000000,
B0000000,

B0100100,// #
B1111110,
B0100100,
B1111110,
B0100100,

B0111110,// $
B1001000,
B0111100,
B0001010,
B1111110,

B1000010,// %
B0001100,
B0110000,
B1000000,
B0000010,

B0111100,// &
B1000000,
B0111110,
B1000100,
B0111110,

B0001000,// '
B0001000,
B0000000,
B0000000,
B0000000,

B0100000,// (
B0100000,
B1000000,
B0100000,
B0100000,

B0000010,// )
B0000010,
B0000001,
B0000010,
B0000010,

B0011000,// *
B0011000,
B1111110,
B0011000,
B1000010,

B0010000,// +
B0010000,
B1111100,
B0010000,
B0010000,

B0000000,// ,
B0000000,
B0000000,
B0001000,
B0010000,

B0000000,// -
B0000000,
B1111110,
B0000000,
B0000000,

B0000000,// .
B0000000,
B0000000,
B0000000,
B0001000,

B0000100,// /
B0001000,
B0010000,
B0100000,
B1000000,

B0111110,// 0
B1000001,
B1000001,
B1000001,
B0111110,

B0011000,// 1
B0101000,
B1001000,
B0001000,
B0011100,

B0110100,// 2
B1000010,
B0000100,
B0001000,
B1111110,

B0111100,// 3
B1000010,
B0001100,
B1000010,
B0111100,

B0011000,// 4
B0101000,
B1111100,
B0001000,
B0011100,

B1111110,// 5
B1000000,
B1111100,
B0000010,
B1111100,

B0111110,// 6
B1000000,
B1111100,
B1000010,
B0111100,

B1111110,// 7
B0000100,
B0001000,
B0001000,
B0001000,

B0111100,// 8
B1000010,
B0111100,
B1000010,
B0111100,

B0111100,// 9
B1000010,
B0111110,
B0000010,
B0111100,

B0000000,// :
B0001000,
B0000000,
B0001000,
B0000000,

B0000000,// ;
B0001000,
B0000000,
B0001000,
B0010000,

B0000110,// <
B0011000,
B1100000,
B0011000,
B0000110,

B0000000,// =
B1111110,
B0000000,
B1111110,
B0000000,

B1100000,// >
B0011000,
B0000110,
B0011000,
B1100000,

B0111100,// ?
B1000010,
B0000100,
B0001000,
B0001000,

B0111100,// @
B1000010,
B1011110,
B1001110,
B0110000,

B0011000,// A
B0100100,
B0111100,
B1000010,
B1000010,

B1111100,// B
B0100010,
B0111100,
B0100010,
B1111100,

B0111110,// C
B1000000,
B1000000,
B1000000,
B0111110,

B1111100,// D
B0100010,
B0100010,
B0100010,
B1111100,

B1111110, // E
B1000000,
B1111100,
B1000000,
B1111110,

B1111110,// F
B0100000,
B0111100,
B0100000,
B0100000,

B0111100,// G
B1000000,
B1001110,
B1000010,
B0111100,

B1000010, // H
B1000010,
B1111110,
B1000010,
B1000010,

B0001000,// I
B0001000,
B0001000,
B0001000,
B0001000,

B0001000,// J
B0001000,
B0001000,
B1001000,
B0110000,

B1000010,// K
B1001100,
B1110000,
B1001100,
B1000010,

B1000000,// L
B1000000,
B1000000,
B1000000,
B1111110,

B1000010,// M
B1100110,
B1011010,
B1000010,
B1000010,

B1000100,// N
B1100100,
B1010100,
B1011100,
B1000100,

B0111100,// O
B1000010,
B1000010,
B1000010,
B0111100,

B1111000,// P
B1000100,
B1111000,
B1000000,
B1000000,

B0111100,// Q
B1000010,
B1010010,
B1001010,
B0111100,

B1111100,// R
B1000010,
B1111110,
B1000100,
B1000010,

B0111110,// S
B1000000,
B0111100,
B0000010,
B1111100,

B1111110,// T
B0010000,
B0010000,
B0010000,
B0010000,

B1000010,// U
B1000010,
B1000010,
B1000010,
B0111100,

B1000010,// V
B1000010,
B0100100,
B0100100,
B0011000,

B1000010,// W
B1000010,
B1011010,
B1011010,
B0100100,

B1000010,// X
B0100100,
B0011000,
B0100100,
B1000010,

B1000010,// Y
B0100100,
B0011000,
B0010000,
B0100000,

B1111110,// Z
B0000010,
B0001100,
B0110000,
B1111110,

B1110000,// [
B1000000,
B1000000,
B1000000,
B1110000,

B1000000,// "\"
B0100000,
B0010000,
B0001000,
B0000100,

B0001110,// ]
B0000010,
B0000010,
B0000010,
B0001110,

B0010000,// ^
B0101000,
B1000100,
B0000000,
B0000000,

B0000000,// _
B0000000,
B0000000,
B0000000,
B1111110,

B0010000,// `
B0001000,
B0000000,
B0000000,
B0000000,

B0110000,// {
B0100000,
B1000000,
B0100000,
B0110000,

B0001000,// |
B0001000,
B0001000,
B0001000,
B0001000,

B0001100,// }
B0000100,
B0000010,
B0000100,
B0001100,

B0000000,// ~
B0100000,
B1011010,
B0000100,
B0000000,

B1111000,// DEL
B1000100,
B1000010,
B1000100,
B1111000,
};

void DisplayChar(uint8_t c){
if (c >= 'a' && c <= 'z') c -= 32;
for(uint8_t i = 0; i <= 4; i++){
matrix[i] = pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i]) << 1;
}
if (c != '0x20'){
delay(800);
Scroll_up();
}
delay(200);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s