Using the NS73M

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //  Experimentally derived Band Settings for VFO ...
00002 //      Band 0: 83.78 - 91.72
00003 //      Band 1: 88.74 - 98.28
00004 //      Band 2: 93.10 - 104.00
00005 //      Band 3: 99.50 - 112.72
00006 //      ref: http://cba.sakura.ne.jp/sub04/jisaku36.htm (translated)
00007 
00008 #include "mbed.h"
00009 #include "TextLCD.h"
00010 
00011 #define topFM  107900000            // Top of the FM Dial Range in USA
00012 #define botFM   87500000            // Bottom of the FM Dial Range in USA
00013 #define incrFM    200000            // FM Channel Increment in USA
00014 
00015 long frequency = 97300000;          // the default initial frequency in Hz
00016 long newFrequency = 0;
00017 bool mute = true;
00018 
00019 LocalFileSystem local("local");
00020 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
00021 Serial pc(USBTX, USBRX);
00022 I2C i2c(p9, p10);
00023 DigitalIn left(p23);
00024 DigitalIn center(p22);
00025 DigitalIn right(p21);
00026 FILE *fp;
00027 
00028 float storedF = 10.1;
00029 
00030 const int addr= 0xCC;
00031 
00032 void writeToReg(unsigned int, unsigned int, unsigned int);
00033 void initializeTransmitter(long);
00034 void setFrequency(long);
00035 
00036 int main() {
00037       lcd.printf("Starting up.\r\n");
00038       left.mode(PullUp);
00039       center.mode(PullUp);
00040       right.mode(PullUp);
00041       wait_ms(100);                                //Wait for VDD to settle
00042       
00043       fp = fopen("/local/out.txt", "r");
00044       fscanf(fp, "%f", &storedF);
00045       fclose(fp);
00046       
00047       frequency = ((long)(storedF*100.0))*10000;
00048       
00049       //lcd.cls();
00050       //lcd.printf("Transmitting on FM %d", frequency);
00051       
00052       initializeTransmitter(frequency);
00053 
00054       while(1) {
00055         if(left == 0)
00056         {
00057             if(frequency - incrFM > botFM)
00058                 frequency = frequency - incrFM;
00059             else
00060                 frequency = topFM;
00061                 
00062             lcd.cls();
00063             setFrequency(frequency);
00064             lcd.printf("Transmitting on FM %2.1f", float(frequency)/1000000.0);
00065             
00066             fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
00067             fprintf(fp, "%2.1f", float(frequency)/1000000.0);
00068             fclose(fp);
00069         }
00070         else if (right == 0)
00071         {
00072             if(frequency + incrFM < topFM)
00073                 frequency = frequency + incrFM;
00074             else
00075                 frequency = botFM;   
00076                  
00077             lcd.cls();
00078             setFrequency(frequency);
00079             lcd.printf("Transmitting on FM %2.1f", float(frequency)/1000000.0);
00080             
00081             fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
00082             fprintf(fp, "%2.1f", float(frequency)/1000000.0);
00083             fclose(fp);
00084         }
00085         else if (center == 0)
00086         {
00087             frequency = 97300000;
00088             lcd.cls();
00089             setFrequency(frequency);
00090             lcd.printf("Transmitting on FM %2.1f", float(frequency)/1000000.0);
00091             
00092             fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
00093             fprintf(fp, "%2.1f", float(frequency)/1000000.0);
00094             fclose(fp);
00095         }
00096         wait_ms(200); // lazy debounce
00097     }
00098 }
00099 
00100 void initializeTransmitter(long freq)
00101 {
00102     lcd.printf("Initializing...");
00103     
00104     writeToReg(addr, 0x0E, 0x05); // Software reset
00105     
00106     writeToReg(addr, 0x01, 0xB4); // Reg 1: forced subccarrier, pilot tone on
00107     
00108     writeToReg(addr, 0x02, 0x03); // Reg 2: Unlock detect off, 2mW Tx Power
00109     
00110     setFrequency(freq);
00111       
00112     //writeToReg(addr, 0x08, 0x1A); //Register 8: set Osc on band 2
00113     
00114     writeToReg(addr, 0x00, 0xA1); //Register 0: 200mV audio input, 75us pre-emphasis on, crystal off, power on
00115 
00116     writeToReg(addr, 0x0E, 0x05); // Software reset
00117     
00118     writeToReg(addr, 0x06, 0x1E); // Reg 6: chare pumps at 320uA and 80 uA
00119 
00120     lcd.cls();
00121     lcd.printf("Transmitting on FM %2.1f", float(freq)/1000000.0);  //for debugging
00122 }
00123 
00124 void setFrequency(long freq)
00125 {     
00126       int new_frequency;
00127 
00128       // New Range Checking... Implement the (experimentally determined) VFO bands:
00129       if (freq < 88500000) {        // Band 3
00130         writeToReg(addr, 0x08, 0x1B);
00131         //Serial.println("Band 3");
00132       }  
00133       else if (freq < 97900000) {   // Band 2
00134         writeToReg(addr, 0x08, 0x1A);
00135         //Serial.println("Band 2");
00136       }
00137       else if (freq < 103000000) {  // Band 1 
00138         writeToReg(addr, 0x08, 0x19);
00139         //Serial.println("Band 1");
00140       }
00141       else {                        // Band 0
00142         // Must be OVER 103.000.000,
00143         writeToReg(addr, 0x08, 0x18);
00144         //Serial.println("Band 0");
00145       }
00146       
00147       new_frequency = (freq + 304000) / 8192;
00148       
00149       unsigned char reg3 = new_frequency & 255;                  //extract low byte of frequency register
00150       unsigned char reg4 = new_frequency >> 8;                   //extract high byte of frequency register
00151       writeToReg(addr, 0x03, reg3);                             //send low byte
00152       writeToReg(addr, 0x04, reg4);                             //send high byte
00153       
00154       writeToReg(addr, 0x0E, 0x05);         // Software reset
00155 }
00156 
00157 void writeToReg(unsigned int addr, unsigned int reg, unsigned int data)
00158 {
00159     i2c.start();
00160     i2c.write(addr);
00161     i2c.write(reg);
00162     i2c.write(data);
00163     i2c.stop();
00164 }