Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Communicating with the MAX 6954 over an SPI bus using an ATMEGA328P.
#include <string.h>
#include <SPI.h>
#include <PS2Keyboard.h>
#include <avr/sleep.h>
#define PS2DATA 2
#define PS2IRQ 3
PS2Keyboard keyboard;
#define CS1 10 //ss
#define CS2 9 //ss
#define DATAOUT 11 //MOSI DataIn bei max
#define DATAIN 12 //MISO
#define CLK 13 //sck
#define CHECKMALLOC(var) if((var) == NULL) {Serial.println("ERROR: malloc\n");abort();}
#define EXPRSIZE 128
#define LCDCOLS 8
char *expression;
int expr_size;
int lcd_c, lcd_r;
#define POUND 24
#define YEN 25
#define AE 26
#define DEG 27
#define HALFM 28
#define CROSS 29
#define ROCKETUP 30
#define ROCKETDOWN 31
#define DEL 127
char get_keypress() {
char c = keyboard.read();
/*
if (c == PS2_ENTER) {
Serial.println();
} else if (c == PS2_TAB) {
Serial.println("[Tab]");
} else if (c == PS2_ESC) {
Serial.println("[ESC]");
} else if (c == PS2_PAGEDOWN) {
Serial.println("[PgDn]");
} else if (c == PS2_PAGEUP) {
Serial.println("[PgUp]");
} else if (c == PS2_LEFTARROW) {
Serial.println("[Left]");
} else if (c == PS2_RIGHTARROW) {
Serial.println("[Right]");
} else if (c == PS2_UPARROW) {
Serial.println("[Up]");
} else if (c == PS2_DOWNARROW) {
Serial.println("[Down]");
} else if (c == PS2_DELETE) {
Serial.println("[Del]");
} else {
Serial.print(c);
}
*/
return c;
}
void read_register(byte address){
Serial.print(address, HEX);
Serial.print(" = ");
digitalWrite(CS1, LOW);
SPI.transfer(address | B10000000);
SPI.transfer(0x00);
digitalWrite(CS1, HIGH);
digitalWrite(CS1, LOW);
SPI.transfer(address);
Serial.println(SPI.transfer(0x00), BIN);
digitalWrite(CS1, HIGH);
}
void write_register(byte address, byte value) {
digitalWrite(CS1, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS1, HIGH);
digitalWrite(CS2, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS2, HIGH);
}
void write_register1(byte address, byte value) {
digitalWrite(CS1, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS1, HIGH);
}
void write_register2(byte address, byte value) {
digitalWrite(CS2, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS2, HIGH);
}
void displaySettings() {
read_register(0x01);
read_register(0x02);
read_register(0x03);
read_register(0x04);
}
void control_intensity(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) {
write_register(0x10, d1 << 4 | d0);
write_register(0x11, d3 << 4 | d2);
write_register(0x12, d5 << 4 | d4);
write_register(0x13, d7 << 4 | d6);
}
void write_chars(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) {
write_register(0x20, d0);
write_register(0x21, d1);
write_register(0x22, d2);
write_register(0x23, d3);
write_register(0x24, d4);
write_register(0x25, d5);
write_register(0x26, d6);
write_register(0x27, d7);
}
void write_string(char str[]){
int i=0;
for (int digit = 0x20; digit <= 0x27; digit++) {
if (str[i+1] == '.') {
write_register(digit, str[i] | B10000000);
i+=2;
}
else {
write_register(digit, str[i]);
i++;
}
}
/*
write_register(0x20, str[0]);
write_register(0x21, str[1]);
write_register(0x22, str[2]);
write_register(0x23, str[3]);
write_register(0x24, str[4]);
write_register(0x25, str[5]);
write_register(0x26, str[6]);
write_register(0x27, str[7]);
*/
}
void write_string1(char str[]){
write_register1(0x20, str[0]);
write_register1(0x21, str[1]);
write_register1(0x22, str[2]);
write_register1(0x23, str[3]);
write_register1(0x24, str[4]);
write_register1(0x25, str[5]);
write_register1(0x26, str[6]);
write_register1(0x27, str[7]);
}
void write_string2(char str[]){
write_register2(0x20, str[0]);
write_register2(0x21, str[1]);
write_register2(0x22, str[2]);
write_register2(0x23, str[3]);
write_register2(0x24, str[4]);
write_register2(0x25, str[5]);
write_register2(0x26, str[6]);
write_register2(0x27, str[7]);
}
/*void scroll_string(char str[], times){
while (times > 0) {
}
}*/
void setup() {
Serial.begin(9600);
keyboard.begin(PS2DATA, PS2IRQ);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(CLK, OUTPUT);
pinMode(CS1, OUTPUT);
pinMode(CS2, OUTPUT);
digitalWrite(CS1,HIGH); //disable device
digitalWrite(CS2,HIGH); //disable device
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV8); // 2,4,8,16,32,64,128
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
//SPCR = (1<<SPE)|(1<<MSTR); // 01000000 v 00010000 = 01010000
//clr=SPSR;
//clr=SPDR;
//delay(10);
// Decode mode enabled
write_register(0x01, B11111111);
// Global Intensity to zero
write_register(0x02, B00000000);
// Scan Limit - all digits
write_register(0x03, B00000111);
// Configuration - fast blinking and normal operation
// write_register(0x04, B01000101);
write_register(0x04, B01000101);
// Digit Type - all 16 segments
write_register(0x0C, B00000000);
delay(10);
// write_register(0x07, 0x01);
// delay(10);
// write_register(0x07, 0x01);
// delay(1000);
// write_register(0x07, 0x00);
// delay(10);
control_intensity(0, 0, 0, 0, 0, 0, 0, 0);
expression = (char*) malloc(EXPRSIZE * sizeof(char));
CHECKMALLOC(expression);
for (int i=0; i<EXPRSIZE; i++) {
expression[i] = ' ';
}
expression[EXPRSIZE-1]='\0';
lcd_c = 0;
lcd_r = 0;
expr_size = 0;
// Decode mode disabled
write_register(0x01, B00000000);
// Turn off all segments manually
for (int i=0x20; i<=0x2F; i++) {
write_register(i, B00000000);
}
// Turn on all segments manually (decode mode off)
// write_register(0x20, B11111111);
// write_register(0x28, B11111111);
// delay(5000);
// set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
// sleep_enable(); // enables the sleep bit in the mcucr register
// sleep_mode(); // here the device is actually put to sleep!!
int dl = 40;
for (int i=0; i<4; i++) {
write_register(0x20,B00000001);
delay(dl);
write_register(0x20,B00000000);
write_register(0x28,B00100000);
delay(dl);
write_register(0x28,B00010000);
delay(dl);
write_register(0x28,B00001000);
delay(dl);
write_register(0x28,B01000000);
delay(dl);
write_register(0x28,B00000100);
delay(dl);
write_register(0x28,B00000010);
delay(dl);
write_register(0x28,B00000001);
delay(dl);
}
for (int i=0x20; i<=0x2F; i++) {
write_register(i, B00000000);
}
/*
// Test each segment bit slowly
dl = 3000;
int segment = B10000000;
for (int i=0; i<8; i++) {
write_register(0x20,segment);
segment = segment >> 1;
delay(dl);
}
write_register(0x20,B00000000);
segment = B10000000;
for (int i=0; i<8; i++) {
write_register(0x28,segment);
segment = segment >> 1;
delay(dl);
}
write_register(0x28,B00000000);
*/
// Decode mode enabled
write_register(0x01, B11111111);
}
char c;
int b = 0;
int window_start, window_end;
int dl = 65;
void loop() {
/*control_intensity(0, 1, 2, 3, 4, 5, 6, 7);*/
control_intensity(0, 0, 0, 0, 0, 0, 0, 0);
/*write_string("#.#.#.#.#.#.#.#.");*/
/*delay(1000);*/
/*
for (int i = 48; i<=127; i++) {
for(int j=0x20; j<=0x27; j++) {
write_register(j, i);
}
delay(50);
}
for (int i=0; i<10; i++) {
for(int j=0x20; j<=0x27; j++)
write_register(j, '\\');
delay(dl);
for(int j=0x20; j<=0x27; j++)
write_register(j, '|');
delay(dl);
for(int j=0x20; j<=0x27; j++)
write_register(j, '/');
delay(dl);
for(int j=0x20; j<=0x27; j++)
write_register(j, '-');
delay(dl);
}
*/
/*
// larson scanning
for (int i=0; i<2; i++) {
control_intensity(0, 1, 2, 3, 0, 0, 0, 0);
write_string("**** ");
delay(dl);
control_intensity(0, 0, 1, 2, 3, 0, 0, 0);
write_string(" **** ");
delay(dl);
control_intensity(0, 0, 0, 1, 2, 3, 0, 0);
write_string(" **** ");
delay(dl);
control_intensity(0, 0, 0, 0, 1, 2, 3, 0);
write_string(" **** ");
delay(dl);
control_intensity(0, 0, 0, 0, 0, 1, 2, 3);
write_string(" ****");
delay(dl);
control_intensity(0, 0, 0, 0, 0, 0, 3, 2);
write_string(" ***");
delay(dl);
control_intensity(0, 0, 0, 0, 0, 3, 2, 1);
write_string(" ***");
delay(dl);
control_intensity(0, 0, 0, 0, 3, 2, 1, 0);
write_string(" ****");
delay(dl);
control_intensity(0, 0, 0, 3, 2, 1, 0, 0);
write_string(" **** ");
delay(dl);
control_intensity(0, 0, 3, 2, 1, 0, 0, 0);
write_string(" **** ");
delay(dl);
control_intensity(0, 3, 2, 1, 0, 0, 0, 0);
write_string(" **** ");
delay(dl);
control_intensity(3, 2, 1, 0, 0, 0, 0, 0);
write_string("**** ");
delay(dl);
control_intensity(2, 3, 0, 0, 0, 0, 0, 0);
write_string("*** ");
delay(dl);
control_intensity(1, 2, 3, 0, 0, 0, 0, 0);
write_string("*** ");
delay(dl);
}
*/
control_intensity(b,b,b,b,b,b,b,b);
// 0000000011111111222222223333333344444444555555556666666677777777888888889999999900000000
char e1[] = " This message to you How are doing miss ";
char e2[] = " is a from me you today Abigail?";
int elength = strlen(e1);
int x = 0;
while (1) {
write_string1(&e1[x]);
write_string2(&e2[x]);
for (int i=0; i<=9; i++){
control_intensity(i,i,i,i,i,i,i,i);
delay(65);
}
delay(2000);
for (int i=9; i>=0; i--){
control_intensity(i,i,i,i,i,i,i,i);
delay(65);
}
x+=8;
if (x >= elength) {
x = 0;
write_string(" ");
delay(2000);
}
}
/*
while (1) {
if (keyboard.available()) {
c = get_keypress();
if (c == PS2_BACKSPACE) {
expression[expr_size-1]= ' ';
expr_size--;
}
else if (c == PS2_UPARROW) {
b++;
if (b>15)
b = 15;
control_intensity(b,b,b,b,b,b,b,b);
}
else if (c == PS2_DOWNARROW) {
b--;
if (b<0)
b = 0;
control_intensity(b,b,b,b,b,b,b,b);
}
else if (expr_size < EXPRSIZE-1) {
expression[expr_size] = c;
expr_size++;
}
window_start = 0;
if (expr_size-LCDCOLS > 0){
window_start = expr_size-LCDCOLS;
}
window_end = window_start+16;
if (window_end > (EXPRSIZE-1)){
window_end = EXPRSIZE-1;
}
write_string1(&expression[0]);
write_string2(&expression[8]);
}
}
*/
}
// vim: ft=c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.