#include "defs.h"
#include "funcs.c"
#include <math.h>
uint8_t tempHolder;
void SPI_Modes_Setting(int mode)
{
if (mode == 0)
{
// CPOL and CPHA
SPCR &= ~(1 << CPOL) & ~(1 << CPHA);
} else if (mode == 1)
{
SPCR &= ~(1 << CPOL);
SPCR |= (1 << CPHA);
} else if (mode == 2)
{
SPCR |= (1 << CPOL);
SPCR &= ~(1 << CPHA);
} else if (mode == 3)
{
SPCR |= (1 << CPOL) | (1 << CPHA);
}
}
void SPI_MasterInit(void)
{
SREG |= (1 << 7);
// SPI mode configuration for CPOL and CPHA
SPI_Modes_Setting(0);
//fosc/128 selected
//SPCR |= (1 << SPR0) | (1 << SPR1);
//SPSR &= ~(1 << SPI2X);
// DDB5 --> SCK , DDB3 --> MOSI , DDB2 --> SS , DDB1 --> Output Enable ,
DDRB |= (1 << DDB5) | (1 << DDB3) | (1 << DDB2) | (1 << DDB1) ;
// DDB4 --> MISO
DDRB &= ~(1 << DDB4);
PORTB |= (1 << DDB1);
//portD settings
// DDD2 --> pbBlk , DDD3 --> pbBlu , DDD4 --> pbRed , DDD5 --> pbGrn
DDRD &= ~(1 << DDD2) | (1 << DDD3) | (1 << DDD4) | (1 << DDD5);
// Arduino as SPI Master
SPCR |= (1 << DORD) | (1 << MSTR);
//SPI Enable
SPCR |= (1 << SPE); // enable right after Data registers are ready
// SPI Interrupt Enable
SPCR |= (1 << SPIE);
}
uint8_t dummyRead, i = 0;
// spi communication global control variable
struct spiVarStruct spiVar;
// spi interrupt function
ISR(SPI_STC_vect) {
//read data register
dummyRead = SPDR;
if (!(spiVar.len > spiVar.ind + 1)) {
spiVar.ind = 0;
spiVar.inProc = false;
} else {
spiVar.ind++;
SPDR = (uint8_t) * (spiVar.sendBuff + spiVar.ind);
}
}
// spi Master send data with interrupt
bool spi_SendDataInt(struct spiVarStruct* spiVar)
{
if (spiVar->inProc) {
return false;
} else {
spiVar->inProc = true;
SPDR = (uint8_t) * (spiVar->sendBuff);
return true;
}
}
void updtLEDMatrices() {
for (uint8_t y = 0; y < 8; y++) {
PORTB &= ~(1 << DDB2); //ss low
for (uint8_t x = 0; x < 8; x++) {
tempHolder = y + 1;
spiVar.sendBuff = &tempHolder;
spiVar.len = 1;
spiVar.ind = 0;
spi_SendDataInt(&spiVar);
delay(0.1);
while (spiVar.inProc);
spiVar.sendBuff = leds[x] + y;
spi_SendDataInt(&spiVar);
delay(0.1);
while (spiVar.inProc);
}
PORTB |= (1 << DDB2); //ss high
}
}
void clrLEDMatrices() {
clearData(leds);
updtLEDMatrices();
}
void setup() {
Serial.begin(9600);
SPI_MasterInit();
PORTB |= (1 << DDB2);
clearData(leds);
pinMode(5, INPUT);
}
void buildDigits(int val, int noOfdigs, int strtDig) {
int ans;
int rem;
for (int x = 0; x < noOfdigs; x++)
{
if (x == 0)
{
ans = val / pow(10, (noOfdigs - (x + 1)));
rem = val % (int)pow(10, (noOfdigs - (x + 1)));
} else {
ans = rem / pow(10, (noOfdigs - (x + 1)));
rem = rem % (int)pow(10, (noOfdigs - (x + 1)));
}
if (ans == 0 && strtDig == 0 && x == 0)
{
makeNumber(leds, 6 * (x + strtDig), 12);
} else
{
makeNumber(leds, 6 * (x + strtDig), ans);
}
}
}
void wrtFloatToLed(float val) {
clrLEDMatrices();
buildDigits((int)val, 4, 0);
makeNumber(leds, 6 * 4, 11);
buildDigits((int)(10000 * (val - (int)val)), 4, 5);
updtLEDMatrices();
}
float test01 = 0;
void loop()
{
if (digitalRead(4)) {
if (test01 > 0)
{
test01 -= 1.125;
}
}
if (digitalRead(5)) {
if (test01 < 9999)
{
test01 += 1.125;
}
}
wrtFloatToLed(test01);
delay(100);
}