Created
January 28, 2014 14:02
-
-
Save niklasf/8668211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define C2 | |
#define DDR_SPI DDRB | |
#define PORT_SPI PORTB | |
#define DD_MOSI DDB2 | |
#define DD_SCK DDB1 | |
#define DD_SS DDB0 | |
void SPI_MasterInit() | |
{ | |
DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS); | |
PORT_SPI = 0b1; | |
SPCR = (1 << SPE) | (1 << MSTR) | (1<<SPR0); | |
} | |
void SPI_MasterTransmit(char ch) | |
{ | |
PORT_SPI = 0b0; | |
SPDR = ch; | |
while(!(SPSR & (1<<SPIF))); | |
PORT_SPI = 0b1; | |
} | |
void SPI_MasterTransmitString(char *str) | |
{ | |
while (*str) { | |
SPI_MasterTransmit(*str); | |
str++; | |
} | |
} | |
void sleep(const int milliseconds) { | |
for (int j = 0; j < milliseconds; j++) { | |
for (int i = 0; i < (8000 / 18 - 28); i++); | |
} | |
} | |
void sleep_using_timer(const int milliseconds) { | |
TCCR1B |= (1 << CS10); | |
for (int c = 0; c < milliseconds * 0.160; c++) { | |
TCNT1 = 0; | |
while (TCNT1 < 50000); | |
} | |
} | |
volatile int millis = 0; | |
volatile int seconds = 0; | |
volatile int minutes = 0; | |
int main() | |
{ | |
DDRA = 0xFF; | |
PORTA = 0x00; | |
#ifdef A1 | |
while (1) { | |
sleep(10000); | |
PORTA = ~PORTA; | |
} | |
#endif | |
#ifdef B1 | |
while (1) { | |
sleep_using_timer(10000); | |
PORTA = ~PORTA; | |
} | |
#endif | |
#ifdef C1 | |
while (1) { | |
sleep_using_timer(500); | |
PORTA = ~PORTA; | |
} | |
#endif | |
#ifdef A2 | |
TCCR0B |= (1 << CS01); | |
TIMSK0 |= (1 << TOIE0); | |
sei(); | |
while (1); | |
#endif | |
#ifdef B2 | |
SPI_MasterInit(); | |
TCCR0B |= (1 << CS01); | |
TIMSK0 |= (1 << TOIE0); | |
sei(); | |
while (1); | |
#endif | |
#ifdef C2 | |
DDRC = 0x00; | |
SPI_MasterInit(); | |
SPI_MasterTransmitString("\e[j 0: 0: 0"); | |
TCCR0B |= (1 << CS01); | |
TIMSK0 |= (1 << TOIE0); | |
uint8_t previous_keys = 0; | |
while (1) { | |
uint8_t keys = PINC; | |
uint8_t changed_and_pressed = keys & ~previous_keys; | |
if (changed_and_pressed & 0b0001) { | |
sei(); | |
} | |
if (changed_and_pressed & 0b0010) { | |
cli(); | |
char buffer[20]; | |
sprintf(buffer, "\e[j%2d:%2d:%3d (s)", minutes, seconds, millis); | |
SPI_MasterTransmitString(buffer); | |
} | |
if (changed_and_pressed & 0b0100) { | |
cli(); | |
millis = seconds = minutes = 0; | |
SPI_MasterTransmitString("\e[j 0: 0: 0"); | |
} | |
previous_keys = keys; | |
} | |
#endif | |
} | |
ISR (TIMER0_OVF_vect) | |
{ | |
static int c = 0; | |
c++; | |
#ifdef A2 | |
if (c == 4000) { | |
PORTA++; | |
c = 0; | |
} | |
#endif | |
#ifdef B2 | |
if (c == 4) { | |
millis++; | |
if (millis >= 1000) { | |
seconds++; | |
millis = millis % 1000; | |
} | |
if (millis % 13 == 0) { | |
char buffer[20]; | |
sprintf(buffer, "\e[j%2d:%2d:%3d", minutes, seconds, millis); | |
SPI_MasterTransmitString(buffer); | |
} | |
if (seconds >= 60) { | |
minutes++; | |
seconds = seconds % 60; | |
} | |
c = 0; | |
} | |
#endif | |
#ifdef C2 | |
if (c == 4) { | |
millis++; | |
if (millis >= 1000) { | |
seconds++; | |
millis = millis % 1000; | |
} | |
if (millis % 13 == 0) { | |
char buffer[20]; | |
sprintf(buffer, "\e[j%2d:%2d:%3d", minutes, seconds, millis); | |
SPI_MasterTransmitString(buffer); | |
} | |
if (seconds >= 60) { | |
minutes++; | |
seconds = seconds % 60; | |
} | |
c = 0; | |
} | |
#endif | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment