Κώδικας:
//=================================================================================
//ARDUINO FM RECEIVER v1.0
//SEPTEMBER 2012
//MANOS MAR.
//=================================================================================
#include <Wire.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include "frequencies.h"
//=================================================================================
//=================================================================================
#define Button_next 10
#define Button_prev 8
#define Button_store 9
#define backlight_pin 7
#define busenable_pin A3
#define BUZZER_PIN A2
#define TEA5768_ADDRESS 0x60
#define SIZE_BAR (5 * 5)
#define TRUE 1
#define FALSE 0
//=================================================================================
unsigned char search_mode=0;
int b=0;
int c=0;
unsigned char frequencyH=0;
unsigned char frequencyL=0;
unsigned int frequencyB;
double frequency=0;
double freq_available=0;
float roundedFrequency=0;
long h;
byte buffer[5];
byte buf[4];
char stationsBuffer[30];
boolean onetime=TRUE;
int index;
long z;
//=================================================================================
LiquidCrystal lcd(6, 5, 4, 3, 2, 1, 0);
//=================================================================================
byte *getChar(int n, byte newChar[])
{
int i;
byte code[5] =
{
B10000,
B11000,
B11100,
B11110,
B11111
};
for (i = 0; i < 8; i++)
newChar[i] = code[n - 1];
return newChar;
}
//=================================================================================
void setup()
{
int i;
byte newChar[8];
for (i = 1; i < 6; i++)
lcd.createChar(i, getChar(i, newChar));
pinMode(Button_next, INPUT);
pinMode(Button_prev, INPUT);
pinMode(Button_store, INPUT);
pinMode(backlight_pin, OUTPUT);
pinMode(busenable_pin, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(Button_next, HIGH); //pull up resistor
digitalWrite(Button_prev, HIGH); //pull up resistor
digitalWrite(Button_store, HIGH); //pull up resistor
digitalWrite(backlight_pin, LOW); //pull up resistor
digitalWrite(busenable_pin, HIGH); //pull up resistor
digitalWrite(BUZZER_PIN, LOW); //pull up resistor
Wire.begin();
lcd.begin(16, 2);
lcd.print(F(" ARDUINO FM "));
lcd.setCursor(0,1);
lcd.print(F(" RECEIVER v1.0 "));
delay(100);
lcd.clear();
/// buttons
h = EEPROM_read_long(4);
if (h == 4294967295)
{
frequency = 87.5;
}
else
{
frequency = floor(h)/1000000;//((unsigned long)(buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
}
frequencyB=4*(frequency*1000000+225000)/50000+1;
frequencyH=frequencyB>>8;
frequencyL=frequencyB&0xFF;
Wire.beginTransmission(TEA5768_ADDRESS);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0);//0x1F
Wire.write(0x00);
Wire.endTransmission();
/*
lcd.print(frequency);
delay(10);
lcd.clear();
*/
}
//=================================================================================
void loop()
{
int i, val;
double x;
lcd.setCursor(0, 0);
Wire.requestFrom(TEA5768_ADDRESS,5); //reading TEA5767
if (Wire.available())
{
for (int i=0; i<5; i++)
{
buffer[i]= Wire.read();
}
freq_available=floor(((buffer[0]&0x3F)<<8)+buffer[1])*50000/4-225000;
lcd.print("FM ");
roundedFrequency=freq_available/1000000;
printFloat(roundedFrequency,1);
lcd.print(" MHz ");
frequencyH=((buffer[0]&0x3F));
frequencyL=buffer[1];
if (search_mode)
{
if(buffer[0]&0x80) search_mode=0;
}
if ((search_mode==1)&&(val=digitalRead(Button_next)==0))
{
lcd.setCursor(13,0);
lcd.print(" ");
lcd.write(246); lcd.write(246);
}
if ((search_mode==1)&&(val=digitalRead(Button_prev)==0))
{
lcd.setCursor(13,0);
lcd.print(" ");
lcd.write(247); lcd.write(247);
}
else if(search_mode==0)
{
lcd.setCursor(13,0);
lcd.print(" ");
}
if(onetime == TRUE) identifyStation();
lcd.setCursor(0,1);
x = (buffer[3]>>4);
print_histogram(x,16);
}
lcd.setCursor(14,1);
if (buffer[2]&0x80) lcd.print("ST ");
else lcd.print(" ");
///// buttons read
//////////// button_next//////////
val=digitalRead(Button_next);
if ((val==0)&&!b)
{
beepBuzzer(12000,5);
// delay(50);
roundedFrequency=(freq_available/1000000)+0.1;
if(roundedFrequency > 108.0) roundedFrequency = 87.5;
frequencyB=4*(roundedFrequency*1000000+225000)/50000+1;
frequencyH=frequencyB>>8;
frequencyL=frequencyB&0xFF;
Wire.beginTransmission(TEA5768_ADDRESS);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0);//0x1F
Wire.write(0x00);
Wire.endTransmission();
b=5;
lcd.setCursor(14,0);
lcd.write(246);
delay(5);
};
val=digitalRead(Button_next);
if ((val==0)&&b==1)
{
///scannnn UP
search_mode=1;
Wire.beginTransmission(TEA5768_ADDRESS);
Wire.write(frequencyH+0x40);
Wire.write(frequencyL);
Wire.write(0xD0);
Wire.write(0);//0x1F
Wire.write(0x00);
Wire.endTransmission();
b=5;
};
if (!b==0) b--;
//////////// button_prev//////////
val=digitalRead(Button_prev);
if ((val==0)&&!c)
{
beepBuzzer(12000,5);
// delay(50);
roundedFrequency=(freq_available/1000000)-0.1;
if(roundedFrequency < 87.5) roundedFrequency = 108.0;
frequencyB=4*(roundedFrequency*1000000+225000)/50000+1;
frequencyH=frequencyB>>8;
frequencyL=frequencyB&0xFF;
Wire.beginTransmission(TEA5768_ADDRESS);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0);//0x1F
Wire.write(0x00);
Wire.endTransmission();
c=5;
lcd.setCursor(14,0);
lcd.write(247);
delay(5);
};
val=digitalRead(Button_prev);
if ((val==0)&&c==1)
{
///scannnn DOWN
search_mode=1;
Wire.beginTransmission(TEA5768_ADDRESS);
Wire.write(frequencyH+0x40);
Wire.write(frequencyL);
Wire.write(0x50);
Wire.write(0);//0x1F
Wire.write(0x00);
Wire.endTransmission();
c=5;
};
if (!c==0) c--;
val=digitalRead(Button_store);
if (val==0)
{
h=freq_available;
EEPROM_write_long(4,h);
beepBuzzer(12000,5);
lcd.setCursor(15,0);
lcd.write(248);
lcd.setCursor(0,1);
lcd.print("STORED! ");
delay(80);
onetime = TRUE;
}
}
// ===================================================================
void beepBuzzer(unsigned long hz, unsigned long ms)
{
unsigned long us = (750000 / hz);
unsigned long rep = (ms * 500L) / us;
for (int i = 0; i < rep; i++)
{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(us);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(us);
}
}
//=================================================================================
void printFloat(float value, int places)
{
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;
float d = 0.5;
if (value < 0) d *= -1.0;
for (i = 0; i < places; i++) d/= 10.0;
tempfloat += d;
if (value < 0) tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat)
{
tens *= 10.0;
tenscount += 1;
}
if (value < 0) lcd.print('-');
if (tenscount == 0) lcd.print(0, DEC);
for (i=0; i< tenscount; i++)
{
digit = (int) (tempfloat/tens);
lcd.print(digit, DEC);
tempfloat = tempfloat - ((float)digit * tens);
tens /= 10.0;
}
if (places <= 0) return;
lcd.print('.');
for (i = 0; i < places; i++)
{
tempfloat *= 10.0;
digit = (int) tempfloat;
lcd.print(digit,DEC);
tempfloat = tempfloat - (float) digit;
}
}
// ===================================================================
void print_histogram(double var, int divider)
{
int i;
int bloks;
float histogram;
histogram = (SIZE_BAR * var)/divider;
histogram = histogram + 0.5;
bloks = (int)histogram / 5;
for (i = 0; i < bloks; i++)
{
lcd.write(5);
}
if ((int)(histogram) % 5 > 0)
{
lcd.write((int)(histogram) % 5); lcd.print(" ");
}
}
// ===================================================================
void EEPROM_write_long (int offset, unsigned long val)
{
byte *p = (byte *) (void *) &val;
for (int i = 0; i < sizeof (val); i++)
EEPROM.write (offset++, *p++);
}
// ===================================================================
unsigned long EEPROM_read_long (int offset)
{
unsigned long value = 0;
byte *p = (byte *) (void *) &value;
for (int i = 0; i < sizeof (value); i++)
*p++ = EEPROM.read (offset++);
return value;
}
// ===================================================================
/*
long n;
byte buf[4];
//Long -> byte[4]:
buf[0] = (byte) n;
buf[1] = (byte) n >> 8;
buf[2] = (byte) n >> 16;
buf[3] = (byte) n >> 24;
//byte[4] -> int
long value = (unsigned long)(buf[4] << 24) | (buf[3] << 16) | (buf[2] << 8) | buf[1];
// ===================================================================
*/
void identifyStation(void)
{
z = roundedFrequency*100;
z = z/10;
switch(z)
{
case(station1):
index=0;
break;
case(station2):
index=1;
break;
case(station3):
index=2;
break;
case(station4):
index=3;
break;
case(station5):
index=4;
break;
case(station6):
index=5;
break;
case(station7):
index=6;
break;
case(station8):
index=7;
break;
case(station9):
index=8;
break;
case(station10):
index=9;
break;
case(station11):
index=10;
break;
case(station12):
index=11;
break;
case(station13):
index=12;
break;
case(station14):
index=13;
break;
case(station15):
index=14;
break;
}
chooseStation();
}
// ===================================================================
void chooseStation(void)
{
// Necessary casts and dereferencing, just copy.
strcpy_P(stationsBuffer, (char*)pgm_read_word(&(stationsTable[index])));
lcd.setCursor(0,1);
lcd.print(stationsBuffer);
delay(150);
onetime = FALSE;
}
Θα ήθελα να παρατηρήσω πως στο αρχείο "frequencies.h" δεν έχουν διαμορφωθεί όλα τα ονόματα