Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // My AM/FM Radio, Lab 3 Main 00002 // Jammie Proctor 00003 // 03/02/2013 00004 00005 #include "mbed.h" 00006 #include "TextLCD.h" 00007 00008 I2C radio(p28, p27); // sda, scl 00009 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 00010 DigitalIn AM_FM(p22); // Radio mode AM/FM 00011 DigitalIn station(p23); // Station seek/scan 00012 DigitalOut reset(p21); // Radio reset 00013 DigitalOut lock(LED1); // Station active 00014 PwmOut rclock(p24); // Rclock generation 00015 00016 //====================================== Global Variables ============================================= 00017 const int write_addr=0x22; 00018 int fm_pwr_up[3]={0x01, 0x00, 0x05}; // FM power-up command 00019 int am_pwr_up[3]={0x01, 0x01, 0x05}; // AM power-up command 00020 int fm_station[4]={0x20, 0x00, 0x22, 0x60}; // Set FM station 00021 int am_station[6]={0x40, 0x00, 0x02, 0x17, 0x00, 0x00}; // Set AM station 00022 float lcd_station=88.0; 00023 int reg_station=8800, reg_am_station=535, i=0, Response, ack=0, pwr_down=0x11, tune=0x31; 00024 int* p_pwr_down = &pwr_down; 00025 int* p_tune = &tune; 00026 bool last_mode=0; 00027 00028 //===================================== Support Functions ============================================= 00029 void SendCommand(int* array, int length) { /* Send Command to Si4735 */ 00030 radio.start(); 00031 ack=radio.write(write_addr); 00032 for(i=0; i<length; i++) ack=radio.write(array[i]); 00033 Response=radio.read(ack); 00034 radio.stop(); 00035 wait(.2); 00036 } 00037 00038 void Reset(void) { /* Reset AM/FM Radio */ 00039 reset=0; 00040 wait(.2); 00041 reset=1; 00042 wait(.2); 00043 } 00044 00045 void AM_Restart(void) { /* Reinitialize AM receiver */ 00046 SendCommand(p_tune, 1); // Disable carrier 00047 wait(.2); 00048 SendCommand(p_pwr_down, 1); // Power down sequence 00049 wait(.2); 00050 Reset(); 00051 SendCommand(am_pwr_up, 3); // Launch AM 00052 SendCommand(am_station, 6); // Play current AM station 00053 } 00054 00055 void FM_Restart(void) { /* Reinitialize FM receiver */ 00056 SendCommand(p_tune, 1); // Disable carrier 00057 wait(.2); 00058 SendCommand(p_pwr_down, 1); // Power down sequence 00059 wait(.2); 00060 Reset(); 00061 SendCommand(fm_pwr_up, 3); // Launch FM 00062 SendCommand(fm_station, 4); // Play current FM station 00063 } 00064 //================================================ Main Code ======================================================= 00065 int main() { 00066 AM_FM.mode(PullUp); 00067 station.mode(PullUp); 00068 lock=0; 00069 rclock.period(.000030518); // Set rclock oscillator to 32.768kHz 00070 rclock=0.5; // 50% Duty Cycle for rclock 00071 radio.frequency(100000); // I^2C frequency setting 100kHz 00072 Reset(); 00073 SendCommand(fm_pwr_up, 3); // Initialize FM 00074 SendCommand(fm_station, 4); // Play default fm station 00075 last_mode=AM_FM; 00076 00077 while(1) { 00078 if(AM_FM) { // FM selected (SW1 in the off position) 00079 if(AM_FM!=last_mode) FM_Restart(); 00080 last_mode=AM_FM; 00081 if(!station) { // SW2 pressed? Go to next station 00082 lock=0; 00083 while(!station); 00084 if(reg_station<10800) reg_station+=10; 00085 else reg_station=8800; 00086 lcd_station=reg_station/100.0; 00087 fm_station[3]=reg_station&0x00FF; 00088 fm_station[2]=reg_station>>8; 00089 SendCommand(fm_station, 4); 00090 lock=1; 00091 } 00092 lcd.printf("Station:\n %3.1fMHz FM", lcd_station); 00093 wait(.1); 00094 lcd.cls(); 00095 } 00096 else { // AM selected (SW1 in the on position) 00097 if(AM_FM!=last_mode) AM_Restart(); 00098 last_mode=AM_FM; 00099 if(!station) { // SW2 pressed? Go to next station 00100 lock=0; 00101 while(!station); 00102 if(reg_am_station<1700) reg_am_station+=5; 00103 else reg_am_station=530; 00104 am_station[3]=reg_am_station&0x00FF; 00105 am_station[2]=reg_am_station>>8; 00106 SendCommand(am_station, 6); 00107 lock=1; 00108 } 00109 lcd.printf("Station:\n %ukHz AM", reg_am_station); 00110 wait(.1); 00111 lcd.cls(); 00112 } 00113 } 00114 }
Generated on Wed Jul 13 2022 17:16:32 by
1.7.2