Stap 3: Code
De Arduino-code is als hieronder:Origineel van Arduino FM-ontvanger met TEA5767 http://www.electronicsblog.net
Aangepast door Jingfeng Liu
LinkSprite.com
//
#include < Wire.h >
#include < LiquidCrystal.h >
unsigned char search_mode = 0;
int b = 0;
int c = 0;
#define Button_next 6
#define Button_prev 7
unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
dubbele frequentie = 0;
dubbele freq_available = 0;
LiquidCrystal lcd (12, 11, 5, 4, 3, 2);
VOID Setup {}
Wire.begin();
LCD.begin (16, 2);
knoppen
pinMode (Button_next, INPUT);
digitalWrite (Button_next, hoge); optrekken van weerstand
pinMode (Button_prev, INPUT);
digitalWrite (Button_prev, hoge); optrekken van weerstand
frequentie = 87,5; beginnend frequentie
frequencyB = 4 *(frequency*1000000+225000)/32768; berekening van PLL woord
frequencyH = frequencyB >> 8;
frequencyL = frequencyB & 0XFF;
delay(100);
Wire.beginTransmission(0x60); TEA5767 schrijven
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0x10);
Wire.write(0x00);
Wire.endTransmission();
delay(100);
}
void loop
{
unsigned char buffer [5];
lcd.setCursor (0, 0);
Wire.requestFrom(0x60,5); lezing TEA5767
Als (Wire.available())
{
for (int i = 0; ik < 5; i ++) {}
buffer [i] = Wire.read();
}
freq_available = ((([0] buffer & 0x3F) << 8) + buffer [1]) * 32768/4-225000;
LCD.Print ("FM");
LCD.Print((freq_available/1000000));
frequencyH = ((buffer [0] & 0x3F));
frequencyL = buffer [1];
Als (search_mode) {}
Als (buffer [0] & 0x80) search_mode = 0;
}
Als (search_mode == 1) lcd.print ("SCAN");
else {}
LCD.Print("");
}
lcd.setCursor (0, 1);
LCD.Print ("niveau:");
LCD.Print ((buffer [3] >> 4));
LCD.Print ("/ 16");
Als (buffer [2] & 0x80) lcd.print ("STEREO");
anders lcd.print ("MONO");
}
knoppen lezen
Button_Next / / /
Als (! digitalRead(Button_next) & &! b) {}
frequentie =(freq_available/1000000) + 0,05;
frequencyB = 4 * (frequentie * 1000000 + 225000) / 32768 + 1;
frequencyH = frequencyB >> 8;
frequencyL = frequencyB & 0XFF;
Wire.beginTransmission(0x60);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0x1F);
Wire.write(0x00);
Wire.endTransmission();
//////////////////////
b = 100;
};
Als (! digitalRead(Button_next) & & b == 1) {}
scannnn omhoog
search_mode = 1;
Wire.beginTransmission(0x60);
Wire.write(frequencyH+0x40);
Wire.write(frequencyL);
Wire.write(0xD0);
Wire.write(0x1F);
Wire.write(0x00);
Wire.endTransmission();
/////////////////
b = 100;
};
Als (! b == 0) b--;
Button_Prev / / /
Als (! digitalRead(Button_prev) & &! c) {}
frequentie =(freq_available/1000000)-0,05;
frequencyB = 4 * (frequentie * 1000000 + 225000) / 32768 + 1;
frequencyH = frequencyB >> 8;
frequencyL = frequencyB & 0XFF;
Wire.beginTransmission(0x60);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0x1F);
Wire.write(0x00);
Wire.endTransmission();
c = 100;
};
Als (! digitalRead(Button_prev) & & c == 1) {}
scannnn omlaag
search_mode = 1;
Wire.beginTransmission(0x60);
Wire.write(frequencyH+0x40);
Wire.write(frequencyL);
Wire.write(0x50);
Wire.write(0x1F);
Wire.write(0x00);
Wire.endTransmission();
c = 100;
};
Als (! c == 0) c--;
////////////////////
}
Nadat we de code naar de Arduino hebt gedownload, kunnen we druk op de twee knoppen voor scannen omhoog en scannen naar beneden de kanalen om te zoeken naar stations.