123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
-
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <stdlib.h>
-
- #include "uart.h"
- #include "cube.h"
-
- #ifndef F_CPU
- #define F_CPU 16000000L
- #endif
-
- volatile uint8_t _isrCounter = 0;
- volatile uint8_t **imgBuffer = NULL;
- volatile uint8_t changedFlag = 0;
- volatile uint8_t imgFlag = 0;
- volatile uint8_t layer = 0;
-
- inline void delay_ns(int16_t ns);
- inline void isrCall(void);
-
- void setImage(uint8_t **img) {
- uint8_t i = 0, j;
-
- changedFlag = 1;
- for (; i < 8; i++) {
- for (j = 0; j < 8; j++) {
- imgBuffer[i][j] = img[i][j];
- }
- }
- }
-
- uint8_t isFinished(void) {
- return imgFlag;
- }
-
- void init(void) {
- uint8_t ctr = 0;
- DDRD = 0xFF;
- DDRB = 0xFF;
- DDRC = 0xFF;
- DDRA = 0xFF;
-
- TCCR1A |= (1 << WGM12);
- TCCR1B |= (1 << CS10);
- OCR1A = 3968;
- TIMSK = (1 << OCIE1A);
-
-
-
- imgBuffer = malloc(8 * sizeof(uint8_t*));
-
- for(ctr = 0; ctr < 8; ctr++) {
-
- imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
- }
-
- sei();
- }
-
- void close(void) {
- uint8_t ctr = 0;
- for (; ctr < 8; ctr++) {
- free((uint8_t *)imgBuffer[ctr]);
- }
- free(imgBuffer);
- TIMSK &= ~(1 << OCIE1A);
- }
-
-
- ISR(TIMER1_COMPA_vect) {
- if (_isrCounter < 20) {
- _isrCounter++;
- } else {
- _isrCounter = 0;
- isrCall();
- }
- }
-
-
- inline void setFet(uint8_t data) {
- PORTD = (data & ~(3));
- PORTB &= ~(24);
- PORTB |= ((data << 3) & 24);
- }
-
-
- inline void selectLatch(uint8_t latchNr) {
- PORTC = 0;
- if (latchNr < 8) {
- PORTC = 1 << latchNr;
- }
- }
-
- inline void setLatch(uint8_t latchNr, uint8_t data) {
- setFet(0);
- selectLatch(latchNr);
- PORTA = data;
- delay_ns(LATCHDELAY);
- selectLatch(8);
- setFet(1 << latchNr);
- }
-
- inline void isrCall(void) {
- uint8_t latchCtr = 0;
-
- if (changedFlag != 0) {
-
- layer = 0;
- imgFlag = 0;
- changedFlag = 0;
- }
-
- for (; latchCtr < 8; latchCtr++) {
- setLatch(latchCtr, imgBuffer[layer][latchCtr]);
- }
-
-
- if (layer < 7) {
- layer++;
- } else {
- layer = 0;
- imgFlag = 1;
- }
- }
-
- inline void delay_ns(int16_t ns) {
-
- for (;ns > 0; ns -= 63)
- asm volatile("nop"::);
- }
|