public
Last active

Communicating with the MAX 6954 over an SPI bus using an ATMEGA328P.

  • Download Gist
spi_max6954_test.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.