AM FM Radio Receiver

Dependencies:   TextLCD mbed

Committer:
peimmaj
Date:
Thu Mar 07 18:37:37 2013 +0000
Revision:
0:ad1dcc41c893
Final version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peimmaj 0:ad1dcc41c893 1 // My AM/FM Radio, Lab 3 Main
peimmaj 0:ad1dcc41c893 2 // Jammie Proctor
peimmaj 0:ad1dcc41c893 3 // 03/02/2013
peimmaj 0:ad1dcc41c893 4
peimmaj 0:ad1dcc41c893 5 #include "mbed.h"
peimmaj 0:ad1dcc41c893 6 #include "TextLCD.h"
peimmaj 0:ad1dcc41c893 7
peimmaj 0:ad1dcc41c893 8 I2C radio(p28, p27); // sda, scl
peimmaj 0:ad1dcc41c893 9 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
peimmaj 0:ad1dcc41c893 10 DigitalIn AM_FM(p22); // Radio mode AM/FM
peimmaj 0:ad1dcc41c893 11 DigitalIn station(p23); // Station seek/scan
peimmaj 0:ad1dcc41c893 12 DigitalOut reset(p21); // Radio reset
peimmaj 0:ad1dcc41c893 13 DigitalOut lock(LED1); // Station active
peimmaj 0:ad1dcc41c893 14 PwmOut rclock(p24); // Rclock generation
peimmaj 0:ad1dcc41c893 15
peimmaj 0:ad1dcc41c893 16 //====================================== Global Variables =============================================
peimmaj 0:ad1dcc41c893 17 const int write_addr=0x22;
peimmaj 0:ad1dcc41c893 18 int fm_pwr_up[3]={0x01, 0x00, 0x05}; // FM power-up command
peimmaj 0:ad1dcc41c893 19 int am_pwr_up[3]={0x01, 0x01, 0x05}; // AM power-up command
peimmaj 0:ad1dcc41c893 20 int fm_station[4]={0x20, 0x00, 0x22, 0x60}; // Set FM station
peimmaj 0:ad1dcc41c893 21 int am_station[6]={0x40, 0x00, 0x02, 0x17, 0x00, 0x00}; // Set AM station
peimmaj 0:ad1dcc41c893 22 float lcd_station=88.0;
peimmaj 0:ad1dcc41c893 23 int reg_station=8800, reg_am_station=535, i=0, Response, ack=0, pwr_down=0x11, tune=0x31;
peimmaj 0:ad1dcc41c893 24 int* p_pwr_down = &pwr_down;
peimmaj 0:ad1dcc41c893 25 int* p_tune = &tune;
peimmaj 0:ad1dcc41c893 26 bool last_mode=0;
peimmaj 0:ad1dcc41c893 27
peimmaj 0:ad1dcc41c893 28 //===================================== Support Functions =============================================
peimmaj 0:ad1dcc41c893 29 void SendCommand(int* array, int length) { /* Send Command to Si4735 */
peimmaj 0:ad1dcc41c893 30 radio.start();
peimmaj 0:ad1dcc41c893 31 ack=radio.write(write_addr);
peimmaj 0:ad1dcc41c893 32 for(i=0; i<length; i++) ack=radio.write(array[i]);
peimmaj 0:ad1dcc41c893 33 Response=radio.read(ack);
peimmaj 0:ad1dcc41c893 34 radio.stop();
peimmaj 0:ad1dcc41c893 35 wait(.2);
peimmaj 0:ad1dcc41c893 36 }
peimmaj 0:ad1dcc41c893 37
peimmaj 0:ad1dcc41c893 38 void Reset(void) { /* Reset AM/FM Radio */
peimmaj 0:ad1dcc41c893 39 reset=0;
peimmaj 0:ad1dcc41c893 40 wait(.2);
peimmaj 0:ad1dcc41c893 41 reset=1;
peimmaj 0:ad1dcc41c893 42 wait(.2);
peimmaj 0:ad1dcc41c893 43 }
peimmaj 0:ad1dcc41c893 44
peimmaj 0:ad1dcc41c893 45 void AM_Restart(void) { /* Reinitialize AM receiver */
peimmaj 0:ad1dcc41c893 46 SendCommand(p_tune, 1); // Disable carrier
peimmaj 0:ad1dcc41c893 47 wait(.2);
peimmaj 0:ad1dcc41c893 48 SendCommand(p_pwr_down, 1); // Power down sequence
peimmaj 0:ad1dcc41c893 49 wait(.2);
peimmaj 0:ad1dcc41c893 50 Reset();
peimmaj 0:ad1dcc41c893 51 SendCommand(am_pwr_up, 3); // Launch AM
peimmaj 0:ad1dcc41c893 52 SendCommand(am_station, 6); // Play current AM station
peimmaj 0:ad1dcc41c893 53 }
peimmaj 0:ad1dcc41c893 54
peimmaj 0:ad1dcc41c893 55 void FM_Restart(void) { /* Reinitialize FM receiver */
peimmaj 0:ad1dcc41c893 56 SendCommand(p_tune, 1); // Disable carrier
peimmaj 0:ad1dcc41c893 57 wait(.2);
peimmaj 0:ad1dcc41c893 58 SendCommand(p_pwr_down, 1); // Power down sequence
peimmaj 0:ad1dcc41c893 59 wait(.2);
peimmaj 0:ad1dcc41c893 60 Reset();
peimmaj 0:ad1dcc41c893 61 SendCommand(fm_pwr_up, 3); // Launch FM
peimmaj 0:ad1dcc41c893 62 SendCommand(fm_station, 4); // Play current FM station
peimmaj 0:ad1dcc41c893 63 }
peimmaj 0:ad1dcc41c893 64 //================================================ Main Code =======================================================
peimmaj 0:ad1dcc41c893 65 int main() {
peimmaj 0:ad1dcc41c893 66 AM_FM.mode(PullUp);
peimmaj 0:ad1dcc41c893 67 station.mode(PullUp);
peimmaj 0:ad1dcc41c893 68 lock=0;
peimmaj 0:ad1dcc41c893 69 rclock.period(.000030518); // Set rclock oscillator to 32.768kHz
peimmaj 0:ad1dcc41c893 70 rclock=0.5; // 50% Duty Cycle for rclock
peimmaj 0:ad1dcc41c893 71 radio.frequency(100000); // I^2C frequency setting 100kHz
peimmaj 0:ad1dcc41c893 72 Reset();
peimmaj 0:ad1dcc41c893 73 SendCommand(fm_pwr_up, 3); // Initialize FM
peimmaj 0:ad1dcc41c893 74 SendCommand(fm_station, 4); // Play default fm station
peimmaj 0:ad1dcc41c893 75 last_mode=AM_FM;
peimmaj 0:ad1dcc41c893 76
peimmaj 0:ad1dcc41c893 77 while(1) {
peimmaj 0:ad1dcc41c893 78 if(AM_FM) { // FM selected (SW1 in the off position)
peimmaj 0:ad1dcc41c893 79 if(AM_FM!=last_mode) FM_Restart();
peimmaj 0:ad1dcc41c893 80 last_mode=AM_FM;
peimmaj 0:ad1dcc41c893 81 if(!station) { // SW2 pressed? Go to next station
peimmaj 0:ad1dcc41c893 82 lock=0;
peimmaj 0:ad1dcc41c893 83 while(!station);
peimmaj 0:ad1dcc41c893 84 if(reg_station<10800) reg_station+=10;
peimmaj 0:ad1dcc41c893 85 else reg_station=8800;
peimmaj 0:ad1dcc41c893 86 lcd_station=reg_station/100.0;
peimmaj 0:ad1dcc41c893 87 fm_station[3]=reg_station&0x00FF;
peimmaj 0:ad1dcc41c893 88 fm_station[2]=reg_station>>8;
peimmaj 0:ad1dcc41c893 89 SendCommand(fm_station, 4);
peimmaj 0:ad1dcc41c893 90 lock=1;
peimmaj 0:ad1dcc41c893 91 }
peimmaj 0:ad1dcc41c893 92 lcd.printf("Station:\n %3.1fMHz FM", lcd_station);
peimmaj 0:ad1dcc41c893 93 wait(.1);
peimmaj 0:ad1dcc41c893 94 lcd.cls();
peimmaj 0:ad1dcc41c893 95 }
peimmaj 0:ad1dcc41c893 96 else { // AM selected (SW1 in the on position)
peimmaj 0:ad1dcc41c893 97 if(AM_FM!=last_mode) AM_Restart();
peimmaj 0:ad1dcc41c893 98 last_mode=AM_FM;
peimmaj 0:ad1dcc41c893 99 if(!station) { // SW2 pressed? Go to next station
peimmaj 0:ad1dcc41c893 100 lock=0;
peimmaj 0:ad1dcc41c893 101 while(!station);
peimmaj 0:ad1dcc41c893 102 if(reg_am_station<1700) reg_am_station+=5;
peimmaj 0:ad1dcc41c893 103 else reg_am_station=530;
peimmaj 0:ad1dcc41c893 104 am_station[3]=reg_am_station&0x00FF;
peimmaj 0:ad1dcc41c893 105 am_station[2]=reg_am_station>>8;
peimmaj 0:ad1dcc41c893 106 SendCommand(am_station, 6);
peimmaj 0:ad1dcc41c893 107 lock=1;
peimmaj 0:ad1dcc41c893 108 }
peimmaj 0:ad1dcc41c893 109 lcd.printf("Station:\n %ukHz AM", reg_am_station);
peimmaj 0:ad1dcc41c893 110 wait(.1);
peimmaj 0:ad1dcc41c893 111 lcd.cls();
peimmaj 0:ad1dcc41c893 112 }
peimmaj 0:ad1dcc41c893 113 }
peimmaj 0:ad1dcc41c893 114 }