Ahmet
S38>S85
- Katılım
- 31 Mayıs 2021
- Mesajlar
- 6,081
C++:
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#define AD9833_CS 10
unsigned long freq0 = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
/*
int bir = 0;
int onn = 0;
int yuz = 0;
int bin = 0;
int onbin = 0;
int yuzbin = 0;
int birm = 0;
int onm = 0;
*/
long bir = 0;
long onn = 0;
long yuz = 0;
long bin = 0;
long onbin = 0;
long yuzbin = 0;
long birm = 0;
long onm = 0;
double fin;
byte kare1[8] = {B00000, B00000, B01111, B01001, B01001, B01001, B01001, B11001};
byte kare2[8] = {B00000, B00000, B01111, B01001, B01001, B01001, B01001, B11001};
byte sin1[8] = {B00000, B00010, B00101, B00101, B01000, B01000, B10000, B10000};
byte sin2[8] = {B00000, B00000, B00001, B00001, B10010, B10010, B01100, B01000};
byte ucgen1[8] = {B00000, B00000, B00001, B00010, B00100, B01000, B10000, B10000};
byte ucgen2[8] = {B00000, B00000, B10000, B01000, B00100, B00010, B00001, B00001};
byte unlem[8] = {B11111, B10001, B10101, B10101, B10001, B10101, B10001, B11111};
byte bos[8] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};
bool flg = 0;
#define encA 2
#define encB 3
#define enb 4
int enbb;
bool enf = 0;
#define wave 5
int wsel = 0;
bool wfl = 0;
int type;
#define out 6
bool outfl = 0;
int ay = 0;
static unsigned int period = 0xffff;
int konum, enc, enc1, adimBayrak;
void encoder(void)
{
enc = digitalRead(encB) + (digitalRead(encA) << 1);
if (enc1 != enc)
{
switch (enc1)
{
case 0b00:
adimBayrak = 1; // debounce için bayrak değişken
break;
case 0b01:
if (enc == 0b11)
{
if (adimBayrak)
{
--konum;
flg = 0;
// ayar();
// lcd.clear();
adimBayrak = 0;
}
}
break;
case 0b10:
if (enc == 0b11)
{
if (adimBayrak)
{
++konum;
// ayar();
// lcd.clear();
flg = 0;
adimBayrak = 0;
}
}
break;
}
enc1 = enc;
}
}
void setup() {
lcd.begin();
lcd.clear();
lcd.createChar(0, kare1);
lcd.createChar(1, kare2);
lcd.createChar(2, sin1);
lcd.createChar(3, sin2);
lcd.createChar(4, ucgen1);
lcd.createChar(5, ucgen2);
lcd.createChar(6, unlem);
lcd.createChar(7, bos);
attachInterrupt(digitalPinToInterrupt(encA), encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encB), encoder, CHANGE);
pinMode(encA, INPUT);
pinMode(encB, INPUT);
pinMode(enb, INPUT);
pinMode(wave, INPUT_PULLUP);
pinMode(out, INPUT_PULLUP);
enc = enc1 = digitalRead(encB) + (digitalRead(encA) << 1);
pinMode(AD9833_CS, OUTPUT);
digitalWrite(AD9833_CS, HIGH);
SPI.begin();
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE2));
konum = 0;
lcd.setCursor(8, 0);
lcd.print(bir);
lcd.setCursor(7, 0);
lcd.print(onn);
lcd.setCursor(6, 0);
lcd.print(yuz);
lcd.setCursor(5, 0);
lcd.print(bin);
lcd.setCursor(4, 0);
lcd.print(onbin);
lcd.setCursor(3, 0);
lcd.print(yuzbin);
lcd.setCursor(2, 0);
lcd.print(birm);
lcd.setCursor(1, 0);
lcd.print(onm);
Serial.begin(9600);
}
void loop() {
if (digitalRead(enb) == LOW && enf == 0) {
enbb++;
konum = 0;
enf = 1;
}
if (digitalRead(enb) == HIGH && enf == 1) {
enf = 0;
}
if (digitalRead(wave) == LOW && wfl == 0) {
wsel++;
wfl = 1;
}
if (digitalRead(wave) == HIGH && wfl == 1) {
wfl = 0;
}
if(wsel > 3){
wsel = 0;
}
if(digitalRead(out) == LOW && outfl == 0){
ay++;
outfl = 1;
}
if(digitalRead(out) == HIGH &&outfl == 1){
outfl = 0;
}
if(ay == 1){
ayar();
ul();
ay++;
}
else{
ay = 0;
}
switch(wsel) {
case 1:
type = 0x2028;
kare();
break;
case 2:
type = 0x2002;
ucgen();
break;
case 3:
type = 0x2000;
sinus();
break;
}
if (enbb == 1) { //hz birler basamağ
bir = konum;
if (bir > 9) {
konum = 0;
bir = 0;
}
if (bir < 0) {
konum = 0;
bir = 0;
}
lcd.setCursor(8, 0);
lcd.cursor();
delay(2);
lcd.setCursor(8, 0);
lcd.print(bir);
}
if (enbb == 2) { //hz onlar basamağ
onn = konum;
if (onn > 9) {
konum = 0;
onn = 0;
}
if (onn < 0) {
konum = 0;
onn = 0;
}
lcd.setCursor(7, 0);
lcd.cursor();
delay(2);
lcd.setCursor(7, 0);
lcd.print(onn);
}
if (enbb == 3) { //hz birler basamağ
yuz = konum;
if (yuz > 9) {
konum = 0;
yuz = 0;
}
if (yuz < 0) {
konum = 0;
yuz = 0;
}
lcd.setCursor(6, 0);
lcd.cursor();
delay(2);
lcd.setCursor(6, 0);
lcd.print(yuz);
}
if (enbb == 4) { //hz birler basamağ
bin = konum;
if (bin > 9) {
konum = 0;
bin = 0;
}
if (bin < 0) {
konum = 0;
bin = 0;
}
lcd.setCursor(5, 0);
lcd.cursor();
delay(2);
lcd.setCursor(5, 0);
lcd.print(bin);
}
if (enbb == 5) { //hz birler basamağ
onbin = konum;
if (onbin > 9) {
konum = 0;
onbin = 0;
}
if (onbin < 0) {
konum = 0;
onbin = 0;
}
lcd.setCursor(4, 0);
lcd.cursor();
delay(2);
lcd.setCursor(4, 0);
lcd.print(onbin);
}
if (enbb == 6) { //hz birler basamağ
yuzbin = konum;
if (yuzbin > 9) {
konum = 0;
yuzbin = 0;
}
if (onbin < 0) {
konum = 0;
yuzbin = 0;
}
lcd.setCursor(3, 0);
lcd.cursor();
delay(2);
lcd.setCursor(3, 0);
lcd.print(yuzbin);
}
if (enbb == 7) { //hz birler basamağ
birm = konum;
if (birm > 9) {
konum = 0;
birm = 0;
}
if (birm < 0) {
konum = 0;
birm = 0;
}
lcd.setCursor(2, 0);
lcd.cursor();
delay(2);
lcd.setCursor(2, 0);
lcd.print(birm);
}
if (enbb == 8) { //hz birler basamağ
onm = konum;
if (onm > 9) {
konum = 0;
onm = 0;
}
if (onm < 0) {
konum = 0;
onm = 0;
}
lcd.setCursor(1, 0);
lcd.cursor();
delay(2);
lcd.setCursor(1, 0);
lcd.print(onm);
}
if (enbb == 9) {
enbb = 0;
}
if (period == 0)
{
period = 0xffff;
}
else
{
--period;
}
}
void ayar() {
fin = onm * 10000000 + birm * 1000000 + yuzbin * 100000 + onbin * 10000 + bin * 1000 + yuz * 100 + onn * 10 + bir + 0;
// fin = bir + 0;
Serial.println(fin);
freq0 = round(fin * 10.73741824);
unsigned int freq0_lo = freq0 & 0x3fff;
unsigned int freq0_hi = (freq0 >> 14) & 0x3fff;
digitalWrite(AD9833_CS, LOW);
SPI.transfer16(0x2000);
SPI.transfer16(0x4000 | freq0_lo);
SPI.transfer16(0x4000 | freq0_hi);
SPI.transfer16(type);
digitalWrite(AD9833_CS, HIGH);
}
void kare() {
//lcd.clear();
lcd.setCursor(12, 0);
lcd.write(byte(0));
lcd.setCursor(13, 0);
lcd.write(byte(1));
lcd.setCursor(14, 0);
lcd.write(byte(0));
lcd.setCursor(15, 0);
lcd.write(byte(1));
}
void sinus() {
// lcd.clear();
lcd.setCursor(12, 0);
lcd.write(byte(2));
lcd.setCursor(13, 0);
lcd.write(byte(3));
lcd.setCursor(14, 0);
lcd.write(byte(2));
lcd.setCursor(15, 0);
lcd.write(byte(3));
}
void ucgen() {
//lcd.clear();
lcd.setCursor(12, 0);
lcd.write(byte(4));
lcd.setCursor(13, 0);
lcd.write(byte(5));
lcd.setCursor(14, 0);
lcd.write(byte(4));
lcd.setCursor(15, 0);
lcd.write(byte(5));
}
void ul() {
// lcd.clear();
lcd.setCursor(15, 1);
lcd.write(byte(6));
}