Skip to content

Instantly share code, notes, and snippets.

@hsinghao
Last active March 8, 2018 06:25
創意組合:數位FM收音機
// 2018/3/6
// Faya-Nugget 範例程式 (FMRadio_1.ino)
// 單元: 創意組合-數位FM收音機
// 網址: https://fayalab.blogspot.com/2018/03/FMradio.html
// 目標 (1) 利用faya電子積塊,製作出FM收音機
// (2) 利用編碼器的旋轉功能進行電台搜尋
// (3) 利用編碼器的按鈕功能直接選擇喜愛的電台
// (4) 接收到的電台於雙聲道喇叭模組撥放
// 接線: Arduino ==> faya模組
// D2 ==> RST (FM模組)
// A4 ==> SDIO (FM模組)
// A5 ==> SCLK (FM模組)
// D12 ==> PB (編碼器模組)
// D11 ==> A (編碼器模組)
// D10 ==> B (編碼器模組)
// D3 ==> DIN (串列七段顯示器)
// D4 ==> CLK (串列七段顯示器)
// D5 ==> LOAD (串列七段顯示器)
#include <Si4703_Breakout.h>
#include <Wire.h>
#include "LedControl.h"
//定義Arduino與七字節模組連接腳位與參數
LedControl faya7seg=LedControl(3,4,5,1); // DIN, CLK, LOAD, 串列數量
//定義Arduino與FM模組連接腳位
Si4703_Breakout radio(2, A4, A5); // reset, SDIO, SCLK
//定義編碼器腳位
int encoder_PB = 12; // 按鍵
int encoder_A = 11; // 相位A
int encoder_B = 10; // 相位B
int encoder_A_Last = HIGH; // 相位A的最後狀態
int state_A; //用來儲存相位A狀態
int state_PB; //用來儲存按鍵PB狀態
//FM電台相關變數
int channel_min = 875; // 最低頻率的FM電台 = 87.5 MHz
int channel_max = 1080; // 最高頻率的FM電台 = 108.0 MHz
int channel; // 用來儲存目前電台
//電台列表 共22台
int station[22]= {897, 901, 905, 909, 913, 917, 921, 931, 943, 963, 977, 981, 989, 997, 1007, 1017, 1033, 1041, 1049, 1059, 1065, 1077};
int list = 17; // 初始電台 [list+1] = 第18台 = 104.1
void setup()
{
pinMode (encoder_PB,INPUT);
pinMode (encoder_A,INPUT);
pinMode (encoder_B,INPUT);
// 初始化FM模組
radio.powerOn(); // 啟動模組功能
radio.setVolume(10); // 設定音量 (0~15)
channel = station[list]; // 設定第[list+1]為預設撥放台
radio.setChannel(channel); // 撥放頻道 - 第[scan+1]台的頻道
// 初始化七字節顯示器
faya7seg.shutdown(0,false); // 第0個元件不要進入省電模式
faya7seg.setIntensity(0,8); // 第0個的元件亮度8 (0~15)
faya7seg.clearDisplay(0); // 清除第0個元件
displayFM(channel); // 顯示頻道 - 第[scan+1]台的頻道
delay(2000);
}
void loop()
{
state_A = digitalRead(encoder_A); //讀取相位A狀態
state_PB = digitalRead(encoder_PB); //讀去按鈕PB狀態
if ((encoder_A_Last == LOW) && (state_A == HIGH)) // 相位A從LOW跳到HIGH時
{
if (digitalRead(encoder_B) == LOW) // **當相位B = LOW ==> 順時針旋轉**
{
if(channel > channel_max) // 每順時轉一格電台+2
channel = channel_min; // 超過最大頻率時
else // 跳到最小頻率
channel = channel + 2;
radio.setChannel(channel); // 設定頻道並撥放
displayFM(channel); // 將目前頻道顯示於七字節
}
else // **當相位B = HIGH ==> 逆時針旋轉**
{
if(channel < channel_min) // 每逆時轉一格電台-2
channel = channel_max; // 低於最小頻率時
else // 跳到最大頻率
channel = channel - 2;
radio.setChannel(channel); // 設定頻道並撥放
displayFM(channel); // 將目前頻道顯示於七字節
}
}
else if (state_PB == HIGH) //偵測到按下編碼器按鍵時
{
list++; //電台跳到下一台,到最大台數時,歸回第1台
if(list == 22)
list = 0;
channel = station[list]; // 設定頻道並撥放
radio.setChannel(channel); // 將目前頻道顯示於七字節
displayFM(channel);
delay(200);
}
encoder_A_Last = state_A; // 把A的目前狀態存到最後狀態
}
void displayFM(int number) // 七字節顯示電台號碼
{
int num1,num2,num3,num4;
num1=number/1000; // 千位數
num2=(number%1000)/100; // 百位數
num3=(number%100)/10; // 十位數
num4=number%10; // 個位數
faya7seg.setDigit(0,3,num1,false);
faya7seg.setDigit(0,2,num2,false);
faya7seg.setDigit(0,1,num3,true); // 十位數位置要有小數點
faya7seg.setDigit(0,0,num4,false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment