Skip to content

Instantly share code, notes, and snippets.

@unclewalter
Created July 5, 2015 09:22
Show Gist options
  • Save unclewalter/55c8d99513518cca864d to your computer and use it in GitHub Desktop.
Save unclewalter/55c8d99513518cca864d to your computer and use it in GitHub Desktop.
A very quick crude demonstration of Binary Code Modulation for Angus Gratton which I did on the way home from OpenLab. I haven't tested this, so no guarantees.
#ifndef GLOBALS_H
#define GLOBALS_H
#include <Arduino.h>
#include "nixie.h"
#define LATCH_PIN 8
#define CLOCK_PIN 12
#define DATA_PIN 11
#define DELAY 2
// max number of nixie tubes
#define MAX_TUBES 6
extern volatile byte bit;
extern volatile Nixie tubes[];
#endif
#include "nixie.h"
#include "globals.h"
// Zero all values
void clearNixie( uint16_t idx ) {
tubes[idx].c0 = 0;
tubes[idx].c1 = 0;
tubes[idx].c2 = 0;
tubes[idx].c3 = 0;
tubes[idx].c4 = 0;
tubes[idx].c5 = 0;
tubes[idx].c6 = 0;
tubes[idx].c7 = 0;
tubes[idx].c8 = 0;
tubes[idx].c9 = 0;
}
#ifndef Nixie_H
#define Nixie_H
#include <Arduino.h>
typedef struct {
byte c0;
byte c1;
byte c2;
byte c3;
byte c4;
byte c5;
byte c6;
byte c7;
byte c8;
byte c9;
} Nixie;
void clearNixie( uint16_t i );
#endif
// I've written this for nixie tubes since you already have them. This should be able to run this on a Arduino Uno.
// Pin assignments live in globals.h
#include <avr/io.h>
#include <avr/interrupt.h>
#include "globals.h"
volatile byte bit= 128;
volatile Nixie tubes[MAX_TUBES];
void setup() {
pinMode( LATCH_PIN, OUTPUT );
pinMode( CLOCK_PIN, OUTPUT );
pinMode( DATA_PIN, OUTPUT );
// init the nixie tube values to off
for( int i= 0; i < MAX_TUBES; i++ ) {
clearNixie( i );
}
cli();
TCCR1A = 0;
TCCR1B = 0;
OCR1A = DELAY;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
sei();
}
// BCM
ISR(TIMER1_COMPA_vect) {
bit <<= 1;
OCR1A <<= 1;
if( bit == 0 ) {
OCR1A = DELAY;
bit = 1;
}
// Latch LOW
PORTB &= ~B00000001;
int i= MAX_TUBES - 1;
while( i != -1 ) {
// Character: 0
if( tubes[ i ].c0 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 1
if( tubes[ i ].c1 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 2
if( tubes[ i ].c2 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 3
if( tubes[ i ].c3 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 4
if( tubes[ i ].c4 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 5
if( tubes[ i ].c5 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 6
if( tubes[ i ].c6 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 7
if( tubes[ i ].c7 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 8
if( tubes[ i ].c8 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
// Character: 9
if( tubes[ i ].c9 & bit )
PORTB &= ~B00001000;
else
PORTB |= B00001000;
// toggle clock
PORTB |= B00010000;
PORTB &= ~B00010000;
}
// Latch HIGH
PORTB |= B00000001;
}
void loop() {
// Iterate through each tube
for( int i= 0; i < MAX_TUBES; i++ ) {
// Count rapidly from 0 - 9
tubes[i].c0 = 255;
delay(100);
tubes[i].c0 = 0;
tubes[i].c1 = 255;
delay(100);
tubes[i].c1 = 0;
tubes[i].c2 = 255;
delay(100);
tubes[i].c2 = 0;
tubes[i].c3 = 255;
delay(100);
tubes[i].c3 = 0;
tubes[i].c4 = 255;
delay(100);
tubes[i].c4 = 0;
tubes[i].c5 = 255;
delay(100);
tubes[i].c5 = 0;
tubes[i].c6 = 255;
delay(100);
tubes[i].c6 = 0;
tubes[i].c7 = 255;
delay(100);
tubes[i].c7 = 0;
tubes[i].c8 = 255;
delay(100);
tubes[i].c8 = 0;
tubes[i].c9 = 255;
delay(100);
// Fade out 9
for (int j = 255; j>0; j--) {
tubes[i].c9 = j;
delay(4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment