2011-12-20 22:36:04 +01:00
|
|
|
/*
|
|
|
|
* CTHN.de MiniLEDCube
|
2011-10-15 11:32:43 +02:00
|
|
|
*
|
2011-12-20 22:36:04 +01:00
|
|
|
* By Kai Lauterbach (klaute at web dot de) 11/2011
|
2011-12-09 23:15:06 +01:00
|
|
|
*
|
|
|
|
* Based on http://mosfetkiller.de/?s=miniledcube
|
|
|
|
*
|
|
|
|
* License: General Public License (GPL v3)
|
2011-10-15 11:32:43 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
// Main loop
|
2011-12-10 00:49:39 +01:00
|
|
|
int __attribute__((OS_main))
|
|
|
|
main()
|
2011-08-20 15:22:47 +02:00
|
|
|
{
|
2011-12-20 22:36:04 +01:00
|
|
|
// Initialize the AVR and the USB connection.
|
2011-10-15 11:32:43 +02:00
|
|
|
init();
|
|
|
|
init_usb();
|
|
|
|
|
2011-10-19 00:04:59 +02:00
|
|
|
// Hauptschleife
|
|
|
|
for (;;)
|
2011-10-15 11:32:43 +02:00
|
|
|
{
|
|
|
|
usbPoll(); // keep connected
|
|
|
|
}
|
2011-08-20 15:22:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-19 00:04:59 +02:00
|
|
|
// Initialisierung
|
2011-08-20 15:22:47 +02:00
|
|
|
void init()
|
|
|
|
{
|
2011-12-20 22:36:04 +01:00
|
|
|
// Init ports
|
|
|
|
DDRB = 0b11111111; // PB0-PB7: LED 1-8 (Kathode)
|
2011-12-18 22:16:32 +01:00
|
|
|
PORTB = 0b11111111; // HIGH
|
2011-08-20 15:22:47 +02:00
|
|
|
|
2011-12-18 22:16:32 +01:00
|
|
|
DDRD = 0b01111000; // PD6: LED 9 (Kathode); PD5-PD3: A-C (Anoden)
|
|
|
|
PORTD = 0b01000000;
|
2011-08-20 15:22:47 +02:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
// Setup Timer-Interrupt "TIMER1"
|
|
|
|
TIMSK |= (1 << OCIE1A);
|
2011-08-20 15:22:47 +02:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
// Refreshrate is 100Hz
|
|
|
|
// Set the compare value
|
2011-11-30 00:16:21 +01:00
|
|
|
// 625d = 0x271 = 0b00000010, 0b01110001
|
|
|
|
OCR1AH = 0b00000010;
|
|
|
|
OCR1AL = 0b01110001;
|
2011-08-20 15:22:47 +02:00
|
|
|
|
2011-11-05 16:52:02 +01:00
|
|
|
// anpassen auf reihenweise ausgabe
|
2011-12-20 22:36:04 +01:00
|
|
|
// prescale is 64 (0x011) ----> CS12=0, CS11=1, CS10=1
|
2011-12-02 14:47:59 +01:00
|
|
|
TCCR1B |= (1 << CS11) | (1 << CS10) | (1 << WGM12);
|
2011-11-30 00:16:21 +01:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
sei(); // Enable interrupts global
|
2011-08-20 15:22:47 +02:00
|
|
|
}
|
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
// TIMER1 Interruptvector
|
2011-11-05 16:52:02 +01:00
|
|
|
ISR (TIMER1_COMPA_vect)
|
2011-08-20 15:22:47 +02:00
|
|
|
{
|
2011-12-02 14:47:59 +01:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
delay--; // decrease the delay counter
|
|
|
|
if ( !delay ) // check if we are done with waiting
|
2011-12-18 19:35:47 +01:00
|
|
|
{
|
2011-12-20 22:36:04 +01:00
|
|
|
if (frmnum == MAX_EEPROM_FRAMES)
|
2011-12-18 19:35:47 +01:00
|
|
|
{
|
2011-12-20 22:36:04 +01:00
|
|
|
if (mode == MODE_ANIMATION_SINGLE)
|
|
|
|
mode = MODE_ANIMATION_STOP; // stop the animation after we have reached the 31th frame
|
|
|
|
frmnum = 0; // always start at the first frame
|
2011-12-18 22:16:32 +01:00
|
|
|
} else {
|
|
|
|
if (mode)
|
|
|
|
{
|
2011-12-20 22:36:04 +01:00
|
|
|
// if we are in an animation mode we have to load a frame out of the eeprom
|
|
|
|
// and increase the counter
|
2011-12-18 22:16:32 +01:00
|
|
|
loadEEPROMFrame(frmnum);
|
|
|
|
frmnum++;
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 22:36:04 +01:00
|
|
|
delay = delay_max; // start counting from the top of the maximum delay
|
2011-12-18 19:35:47 +01:00
|
|
|
}
|
|
|
|
|
2011-12-02 14:47:59 +01:00
|
|
|
// PORTD = __, 9, C, B, A,D+,D-,__
|
2011-12-20 22:36:04 +01:00
|
|
|
PORTD &= 0b10000111; // delete bit 3 to 6 (bit 3 to 5 = layer 0 to 2; bit 6 = wire 9 to the LEDs)
|
|
|
|
PORTD |= (1 << 6) | ((1 << level) << 3); // set the current level and pin 6 to high (9th LEDs off)
|
2011-11-30 20:45:22 +01:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
uint8_t tmp = level * 9; // calculate the position in the frame
|
2011-11-30 00:16:21 +01:00
|
|
|
|
|
|
|
// PORTB = 1..8
|
2011-12-20 22:36:04 +01:00
|
|
|
// 0 = led is on, 1 = led is off
|
2011-12-17 22:03:57 +01:00
|
|
|
PORTB = ~((frame >> tmp) & 0xff);
|
2011-11-30 20:45:22 +01:00
|
|
|
|
2011-12-17 22:03:57 +01:00
|
|
|
if ( (((frame >> tmp) >> 8) & 0x01) )
|
2011-12-20 22:36:04 +01:00
|
|
|
PORTD &= ~(1 << 6); // turn the 9th LED on if required
|
2011-11-30 20:45:22 +01:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
// rotate through the 3 level
|
2011-12-17 22:03:57 +01:00
|
|
|
level++;
|
|
|
|
if (level > 2) level = 0;
|
2011-12-02 14:47:59 +01:00
|
|
|
|
2011-08-20 15:22:47 +02:00
|
|
|
}
|
2011-10-19 00:04:59 +02:00
|
|
|
|
2011-12-20 22:36:04 +01:00
|
|
|
/**
|
|
|
|
* Simple function to load a single frame out of the EEPROM.
|
|
|
|
*/
|
2011-12-18 22:16:32 +01:00
|
|
|
void loadEEPROMFrame(uint8_t f)
|
2011-12-18 19:35:47 +01:00
|
|
|
{
|
|
|
|
frame = eeprom_read_dword( &eep_anim[f] );
|
|
|
|
}
|
|
|
|
|