// Copyright Sümer Yamaner 2015
// Devir pulse girişi PB3, PCI3, Attiny85 pin 2
// Attiny85 @ 8 MHz internal
// Timer1 Prescaler 16384 ==> 256 sayıp overflow olduğunda
// Period 524 mS oluyor
// 60 / 0.524 = 114.44 oluyor
// Yani 0.524 mS içinde kaç pulse geldiyse onu 114.44 ile çarparak
// Dakikadaki devir sayısını buluyoruz
// PCI ile inen ve çıkan darbe kenarları sayıldığı için iki kat fazla okuma yapılıyor
// O nedenle sayım sonucu 114.44 ile değil 57.22 ile çarpılıyor
#include "SSD1306_minimal.h"
SSD1306_Mini oled;
const byte bitmapUpper1[88] =
{
0, 240, 60, 15, 15, 15, 60, 240,
0, 0, 240, 252, 255, 0, 0, 0,
0, 240, 60, 15, 15, 15, 60, 240,
0, 240, 60, 15, 15, 15, 60, 240,
0, 0, 0, 0, 192, 252, 255, 0,
0, 255, 15, 15, 15, 15, 15, 15,
0, 240, 60, 15, 15, 15, 60, 240,
0, 15, 15, 15, 15, 15, 15, 255,
0, 240, 60, 15, 15, 15, 60, 240,
0, 240, 60, 15, 15, 15, 60, 240,
240, 12, 12, 48, 0, 0, 0, 0,
};
const byte bitmapUpper2[88] =
{
0, 255, 0, 0, 0, 0, 0, 255,
0, 0, 0, 0, 255, 0, 0, 0,
0, 0, 0, 192, 192, 192, 240, 63,
0, 0, 0, 192, 192, 192, 192, 63,
0, 0, 192, 252, 15, 0, 255, 0,
0, 63, 60, 60, 60, 252, 240, 192,
0, 255, 240, 240, 240, 240, 192, 0,
0, 0, 0, 0, 0, 240, 63, 3,
0, 15, 252, 240, 240, 240, 252, 15,
0, 63, 240, 192, 192, 192, 192, 255,
15, 48, 192, 0, 0, 252, 12, 12,
};
const byte bitmapLower1[88] =
{
0, 255, 0, 0, 0, 0, 0, 255,
0, 0, 0, 0, 255, 0, 0, 0,
0, 252, 15, 3, 3, 3, 0, 0,
0, 0, 0, 3, 3, 3, 3, 240,
0, 252, 255, 240, 240, 240, 255, 240,
0, 0, 0, 0, 0, 0, 3, 255,
0, 255, 0, 0, 0, 0, 3, 255,
0, 0, 0, 240, 63, 3, 0, 0,
0, 255, 3, 0, 0, 0, 3, 255,
0, 0, 0, 3, 3, 3, 3, 255,
192, 0, 0, 255, 0, 255, 48, 0,
};
const byte bitmapLower2[88] =
{
0, 15, 60, 240, 240, 240, 60, 15,
0, 0, 240, 240, 255, 240, 240, 0,
0, 255, 240, 240, 240, 240, 240, 240,
0, 15, 60, 240, 240, 240, 60, 63,
0, 0, 0, 0, 0, 0, 255, 0,
0, 63, 240, 240, 240, 252, 63, 15,
0, 15, 60, 240, 240, 240, 60, 15,
0, 0, 0, 255, 0, 0, 0, 0,
0, 15, 60, 240, 240, 240, 60, 15,
0, 15, 60, 240, 240, 240, 60, 15,
0, 3, 3, 0, 0, 255, 192, 192,
};
const byte rpmbmpUpper2[20] =
{ 0, 252, 48, 12, 12, 48, 0, 252, 48, 12, 12, 240, 0, 252, 48, 12, 252, 48, 12, 252};
const byte rpmbmpLower1[20] =
{ 0, 255, 0, 0, 0, 0, 0, 255, 12, 48, 48, 15, 0, 255, 0, 0, 255, 0, 0, 255};
const byte rpmbmpLower2[20] =
{ 0, 15, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 15, 0, 0, 15, 0, 0, 15};
int b[5];
volatile byte cntr; // pulse counter
volatile byte cntr_result; // tamamlanmış pulse count
volatile boolean flag; // sayım sonucu hazır
int rpm;
float frpm;
ISR(TIMER1_OVF_vect)
{
cntr_result = cntr;
cntr = 0;
flag = 1;
}
ISR(PCINT0_vect)
{
cntr++;
}
void setup()
{
// pinMode(4, OUTPUT);
oled.init(SlaveAddress);
oled.clear();
oled.sendCommand(0xA4); // Resume to RAM content
oled.sendCommand(0xA6); // Normal display
oled.sendCommand(0xAF); // Normal mode
oled.sendCommand(0x81); // Contrast
oled.sendCommand(0xFF); // Max
oled.sendCommand(0x20); // Memory addressing mode
oled.sendCommand(0x02); // Page addressing
oled.sendCommand(0x00); // Column Address Set Low Nibble
oled.sendCommand(0x10); // Column Address Set High Nibble
oled.sendCommand(0xB0); // Page Address Set
TCCR1 = 15; // Timer1 Prescaler 16384
TIMSK = 4; // Timer1 Overflow Interrupt Enable
GIMSK = 32; // Pin Change Interrupt Enable
PCMSK = 8; // PCI on PB3
}
void loop()
{
while(!flag);
frpm = cntr_result * 57.22;
rpm = frpm;
printoled(rpm);
flag = 0;
}
void printoled(int p)
{
// int p beş haneli string'e dönüştürülecek
// digit[5] leftmost, digit[0] rightmost
p = constrain(p, 0, 99999);
b[0] = p / 10000;
p = p - b[0] * 10000;
b[1] = p / 1000;
p = p - b[1] * 1000;
b[2] = p / 100;
p = p - b[2] * 100;
b[3] = p / 10;
// p = p - b[3] * 10;
b[4] = p % 10;
if (b[0] == 0) b[0] = 255;
if ((b[1] == 0) && (b[0] == 255)) b[1] = 255;
if ((b[2] == 0) && (b[1] == 255)) b[2] = 255;
if ((b[3] == 0) && (b[2] == 255)) b[3] = 255;
oled.sendCommand(0x04); // Column 4
oled.sendCommand(0x10); // Column 4
oled.sendCommand(0xB0); // Page 0
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if (b[m] != 255)
{
oled.sendData(bitmapUpper1[8 * b[m] + i]);
oled.sendData(bitmapUpper1[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x04); // Column 4
oled.sendCommand(0x10); // Column 4
oled.sendCommand(0xB1); // Page 1
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if (b[m] != 255)
{
oled.sendData(bitmapUpper2[8 * b[m] + i]);
oled.sendData(bitmapUpper2[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x06); // Column
oled.sendCommand(0x15); // Column
oled.sendCommand(0xB1); // Page 1
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 20; i++)
{
oled.sendData(rpmbmpUpper2[i]);
oled.sendData(rpmbmpUpper2[i]);
}
Wire.endTransmission();
oled.sendCommand(0x04); // Column 4
oled.sendCommand(0x10); // Column 4
oled.sendCommand(0xB2); // Page 2
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if (b[m] != 255)
{
oled.sendData(bitmapLower1[8 * b[m] + i]);
oled.sendData(bitmapLower1[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x06); // Column
oled.sendCommand(0x15); // Column
oled.sendCommand(0xB2); // Page 1
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 20; i++)
{
oled.sendData(rpmbmpLower1[i]);
oled.sendData(rpmbmpLower1[i]);
}
Wire.endTransmission();
oled.sendCommand(0x04); // Column 4
oled.sendCommand(0x10); // Column 4
oled.sendCommand(0xB3); // Page 3
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if (b[m] != 255)
{
oled.sendData(bitmapLower2 [8 * b[m] + i]);
oled.sendData(bitmapLower2[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x06); // Column
oled.sendCommand(0x15); // Column
oled.sendCommand(0xB3); // Page 3
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 20; i++)
{
oled.sendData(rpmbmpLower2[i]);
oled.sendData(rpmbmpLower2[i]);
}
Wire.endTransmission();
// delay(50);
// Aşağıya da logomuzu yazalım
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB4); // Page 4
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapUpper1[80 + i]);
oled.sendData(bitmapUpper1[80 + i]);
}
Wire.endTransmission();
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB5); // Page 5
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapUpper2[80 + i]);
oled.sendData(bitmapUpper2[80 + i]);
}
Wire.endTransmission();
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB6); // Page 6
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapLower1[80 + i]);
oled.sendData(bitmapLower1[80 + i]);
}
Wire.endTransmission();
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB7); // Page 7
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapLower2[80 + i]);
oled.sendData(bitmapLower2[80 + i]);
}
Wire.endTransmission();
}