t2d board
Dependencies: DebounceIn PinDetect SI570 Si5351A mbed
Fork of mbed_blinky by
main.cpp
00001 00002 #include "mbed.h" 00003 #include "si5351a.h" 00004 00005 InterruptIn button(p18); // Interrupt on digital pushbutton input p18 00006 Timer debounce; // define debounce timer 00007 Ticker data_out; // periodic interrupt routines 00008 Ticker clock_out; 00009 DigitalOut data(p16); 00010 DigitalOut clk(p17); 00011 DigitalOut swon(p21); 00012 DigitalOut swled(LED1); 00013 00014 uint16_t divider, mode; 00015 uint32_t ref0,ref1,ref2; 00016 uint16_t *div_bin; // pointer 00017 int count_data,count_clk; 00018 00019 I2C i2c(p28, p27); // communication with Si5351A I2C_SDA=p28 & I2C_SCL=p27 00020 SI5351A ckref(i2c, 25000000UL); // Base clock = 25MHz 00021 Serial pc(USBTX,USBRX); // setup terminal link 00022 LocalFileSystem local("local"); // define local file system 00023 00024 uint16_t read_data (int ); 00025 uint16_t * set_dec2bin (uint16_t); 00026 void send_data (void); 00027 void send_clk (void); 00028 void toggle(void); // function prototype 00029 00030 uint16_t read_data (int mode) 00031 { 00032 // char bitmode[10]; 00033 // char div[10]; 00034 // char reads[10], readc[10]; 00035 // char writes[10]; 00036 // char writec[10]; 00037 uint16_t value; 00038 int i,j; 00039 char text[10][10]; // when the file exist and have data 00040 char line[10]; // it reads line by line 00041 00042 for(i=0; i<10; i++) 00043 for(j=0; j<10; j++) 00044 text[i][j] = '\0'; // initializing the vector with null 00045 for(i=0; i<10; i++) 00046 line[i] = '\0'; 00047 00048 FILE* fp = fopen ("/local/div_prog.txt","r"); // open file for reading 00049 if (fp!=NULL) 00050 { 00051 fseek (fp, 0, SEEK_END); 00052 int size = ftell(fp); 00053 if (size==0) 00054 { 00055 pc.printf("File is empty\n"); 00056 } 00057 else 00058 { 00059 rewind (fp); // restore pointer to the begin of the file 00060 i=0; 00061 while ( fgets ( line, sizeof line, fp ) != NULL ) /* read a line */ 00062 { 00063 strcpy(text[i], line); 00064 printf("array ----> %s ", text[i]); 00065 i++; 00066 } 00067 fclose(fp); // close file 00068 00069 if(mode==0) 00070 { 00071 value=atoi(text[mode]); 00072 pc.printf("Divider value = %d \n",value); // display read data value 00073 } 00074 if(mode==1) 00075 { 00076 value=atoi(text[mode]); 00077 if (value==0) 00078 pc.printf("LSB Mode \n"); // 00079 if (value==1) 00080 pc.printf("MSB Mode \n"); // 00081 } 00082 } 00083 } 00084 00085 else 00086 { 00087 int num; 00088 pc.printf("File does no exist \n"); 00089 pc.printf("Enter the divider value \n: "); 00090 pc.printf("Byte to send LSB(0) or MSB(1)\n: "); 00091 00092 for(i=0; i<2; i++) //just read to save to lines 00093 { 00094 pc.scanf("%s", text[i]); 00095 while (!(sscanf(text[i], "%d", &num))) 00096 { 00097 printf("Invalid input '%s'\n", text[i]); 00098 pc.scanf("%s", text[i]); 00099 } 00100 } 00101 00102 00103 } 00104 00105 // FILE* fp2 = fopen("/local/div_prog.txt","w"); // open file 00106 // memcpy(writes, div, sizeof writes); 00107 // fputs(writes, fp2); // put char (data value) into file 00108 // fclose(fp2); // close file 00109 // value=atoi(writes); 00110 // pc.printf("Divider valuec = %d \n",value); // display read data value 00111 00112 return value; 00113 } 00114 00115 uint16_t * set_dec2bin (uint16_t read_div) { 00116 int i; 00117 static uint16_t r[12]; 00118 /* for loop execution */ 00119 for( i = 0; i < 12; i++ ){ 00120 r[i]= read_div >> i & 1; 00121 pc.printf("%d",r[i]); // display read data value 00122 } 00123 pc.printf("\n"); 00124 return r; 00125 } 00126 00127 00128 void send_data (void) { 00129 if(count_data<12) { 00130 if(mode==0){ 00131 data= *(div_bin+count_data); 00132 pc.printf("%d",*(div_bin+count_data)); 00133 count_data=count_data+1; 00134 } 00135 if(mode==1) { 00136 data= *(div_bin+11-count_data); 00137 pc.printf("%d",*(div_bin+11-count_data)); 00138 count_data=count_data+1; 00139 } 00140 00141 } 00142 else { 00143 data=0; 00144 // count_data=0; 00145 // pc.printf("\n"); 00146 } 00147 00148 } 00149 00150 void send_clk (void){ 00151 if(count_clk<24) { 00152 clk=!clk; 00153 // pc.printf("d%d",clk); 00154 count_clk=count_clk+1; 00155 } 00156 else { 00157 clk=0; 00158 // count_clk=0; 00159 // pc.printf("\n"); 00160 } 00161 00162 } 00163 00164 int main () 00165 { 00166 ref0=read_data(2); 00167 ref1=read_data(3); 00168 ref2=read_data(4); 00169 //ckref.set_frequency(SI5351_CLK0, ref0*1000000); // CLK0=48MHz 00170 //ckref.set_frequency(SI5351_CLK1, ref1*1000000); // CLK0=50MHz 00171 //ckref.set_frequency(SI5351_CLK2, ref2*1000000); // CLK0=45MHz 00172 // ckref.set_frequency(SI5351_CLK0, 40000000); // CLK0=48MHz 00173 ckref.set_frequency(SI5351_CLK1, 4800000); // CLK0=50MHz 00174 // ckref.set_frequency(SI5351_CLK2, 49000000); // CLK0=45MHz 00175 00176 data=0; 00177 clk=0; 00178 divider= read_data(0); 00179 div_bin= set_dec2bin(divider); 00180 mode= read_data(1); 00181 00182 debounce.start(); 00183 button.rise(&toggle); // attach the address of the toggle 00184 swon=1; 00185 swled=1; 00186 wait(1); 00187 swon=0; 00188 swled=0; 00189 while(1) { //begin while 00190 wait(1); 00191 } // end while 00192 00193 } 00194 00195 void toggle() { 00196 if (debounce.read_ms()>200) // only allow toggle if debounce timer 00197 data_out.attach(&send_data, 0.01); 00198 wait(0.005); 00199 clock_out.attach(&send_clk, 0.005); 00200 debounce.reset(); // restart timer when the toggle is performed 00201 count_data=0; 00202 count_clk=0; 00203 swon=1; 00204 swled=1; 00205 wait(1); 00206 swon=0; 00207 swled=0; 00208 } 00209
Generated on Thu Jul 14 2022 17:13:58 by
1.7.2
