Created
January 7, 2014 13:39
-
-
Save niklasf/8299384 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
/* | |
* GccApplication1.c | |
* | |
* Created: 19.11.2013 13:22:54 | |
* Author: ese1-30 | |
*/ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#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++; | |
} | |
} | |
char read_key() { | |
uint8_t key; | |
PORTA = 0b11111101; | |
_delay_us(10); | |
key = (PINA >> 4) & (0b00001111); | |
switch (key) { | |
case 0x07: | |
return '3'; | |
case 0x0B: | |
return '6'; | |
case 0x0D: | |
return '9'; | |
case 0x0E: | |
return 'E'; | |
} | |
PORTA = 0b11111110; | |
_delay_us(10); | |
key = (PINA >> 4) & (0b00001111); | |
switch (key) { | |
case 0x07: | |
return 'A'; | |
case 0x0B: | |
return 'B'; | |
case 0x0D: | |
return 'C'; | |
case 0x0E: | |
return 'D'; | |
} | |
PORTA = 0b11111011; | |
_delay_us(10); | |
key = (PINA >> 4) & (0b00001111); | |
switch (key) { | |
case 0x07: | |
return '2'; | |
case 0x0B: | |
return '5'; | |
case 0x0D: | |
return '8'; | |
case 0x0E: | |
return 'F'; | |
} | |
PORTA = 0b11110111; | |
_delay_us(10); | |
key = (PINA >> 4) & (0b00001111); | |
switch (key) { | |
case 0x07: | |
return '1'; | |
case 0x0B: | |
return '4'; | |
case 0x0D: | |
return '7'; | |
case 0x0E: | |
return '0'; | |
} | |
return 0; | |
} | |
int main(void) | |
{ | |
// Init keyboard. | |
DDRA = 0b00001111; | |
SPI_MasterInit(); | |
SPI_MasterTransmitString("\e[j"); | |
char previous = 0; | |
int len = 0; | |
while (1) { | |
char key = read_key(); | |
if (key && key != previous) { | |
if (len == 16) { | |
SPI_MasterTransmitString("\e[1;0H"); | |
} else if (len == 32) { | |
SPI_MasterTransmitString("\e[j"); | |
len = 0; | |
} | |
SPI_MasterTransmit(key); | |
len++; | |
} | |
previous = key; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment