Things2Do

Dependencies:   DebounceIn PinDetect SI570 Si5351A mbed

Fork of t2d by David Cordova

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "si5351a.h"
00003 #include "PinDetect.h"
00004 #include "DebounceIn.h"
00005 
00006 #define MIN_FREQ 20000000
00007 #define MAX_FREQ 120000000
00008 #define STEP_FREQ 100000
00009 #define RESET_FREQ 35000000
00010    
00011 // Interrupt on digital pushbutton
00012 PinDetect buttonReset(p13);
00013 PinDetect buttonData(p11);
00014 PinDetect buttonIref(p12);
00015 PinDetect buttonStep(p23);
00016 
00017     
00018 // periodic interrupt routines
00019 Ticker data_out;            
00020 Ticker clock_out;
00021     
00022 // data, clk, iref switch output
00023 DigitalOut data(p15);
00024 DigitalOut clk(p14);    
00025 DigitalOut iref_sw(p16);
00026 
00027 //Encoder
00028 DigitalIn A(p22);
00029 DigitalIn B(p21);
00030 
00031 
00032 // Leds
00033 DigitalOut led1(LED1);
00034 DigitalOut led2(LED2);
00035 DigitalOut led3(LED3);
00036 DigitalOut led4(LED4);
00037  
00038 
00039 // setup terminal link
00040 Serial pc(USBTX,USBRX); 
00041     
00042 // Comparateur Threshold
00043 AnalogOut Vth(p18);
00044     
00045 // communication with Si5351A I2C_SDA=p9 & I2C_SCL=p10
00046 I2C i2c(p9, p10);       
00047     
00048 // Base clock = 25MHz
00049 SI5351A ckref(i2c, 25000000UL,SI5351_CRYSTAL_LOAD_8PF,SI5351_CLK_DRIVE_STRENGTH_8MA);
00050     
00051 // define local file system
00052 LocalFileSystem local("local"); 
00053 
00054 //Interruption function
00055 void toggle(void);
00056 void initialisation(void);
00057 void stepChange(void);
00058 void reset(void);
00059 
00060 
00061 //read the data from the .txt file
00062 uint16_t read_data (int);
00063 
00064 //convert decemal to binary
00065 uint16_t * set_dec2bin (uint16_t);
00066 
00067 //send the data to programm the divider
00068 void send_data (void);
00069 
00070 //send the clock to programm the divider
00071 void send_clk (void);
00072 
00073 // turn all the leds  off
00074 void allLedsOff(void);
00075 
00076 // turn all the leds  on
00077 void allLedsOn(void);
00078 
00079 //Variabeles
00080 int divider, mode, resetFreq(RESET_FREQ), stepFreq(STEP_FREQ);
00081 uint16_t *div_bin; // pointer
00082 int count_data,count_clk,counter(0),stepCount(0),thresholdVoltage(0);
00083 int aLastState(0);
00084 
00085 int main (){
00086     
00087     //Config the interrupt button with the debouncing
00088     buttonReset.mode(PullUp);
00089     wait(0.01);
00090     buttonReset.attach_asserted(&reset);
00091     
00092     buttonStep.mode(PullUp);
00093     wait(0.01);
00094     buttonStep.attach_asserted(&stepChange);
00095     
00096     buttonData.mode(PullUp);
00097     wait(0.01);
00098     buttonData.attach_asserted(&toggle);
00099     
00100     buttonIref.mode(PullUp);
00101     wait(0.01);
00102     buttonIref.attach_asserted(&initialisation);
00103     
00104     buttonReset.setSampleFrequency();
00105     buttonIref.setSampleFrequency();
00106     buttonData.setSampleFrequency();
00107     buttonStep.setSampleFrequency();
00108     
00109     //Init the variables
00110     ckref.set_frequency(SI5351_CLK0, RESET_FREQ);   // CLK0=48MHz
00111     data       = 0;
00112     clk        = 0;
00113     aLastState = A;
00114     
00115     //Getting the data from the file
00116     divider          = read_data(0);
00117     div_bin          = set_dec2bin(divider);
00118     mode             = read_data(1);
00119     resetFreq        = read_data(2)*1000000;
00120     stepFreq         = read_data(3);
00121     counter          = resetFreq;
00122     thresholdVoltage = read_data(4);
00123     Vth              = ((double)thresholdVoltage/1000) * 0.3f;
00124     // Loop
00125     while(1){
00126         if (A != aLastState){
00127             if(B != A)
00128                 counter = counter + stepFreq;
00129             else
00130                 counter = counter - stepFreq;
00131         }
00132         aLastState = A;
00133         if(stepCount == 0){
00134             allLedsOff();
00135             led1 = 1;
00136         }
00137         else if (stepCount == 1){
00138             allLedsOff();
00139             led2 = 1;
00140         }
00141         else if (stepCount == 2){
00142             allLedsOff();
00143             led3 = 1;
00144         }
00145         else if (stepCount == 3){
00146             allLedsOff();
00147             led4 = 1;
00148         }        
00149         ckref.set_frequency(SI5351_CLK0, counter);
00150         wait(0.2);
00151         printf("frequence : %d threshold voltage: %1.2f volts\n\r", counter, Vth.read() * 3.3f);        
00152     }     
00153 }
00154 
00155 void allLedsOff(){
00156     led1      = 0;
00157     led2      = 0;
00158     led3      = 0;
00159     led4      = 0;
00160 }
00161 
00162 void allLedsOn(){
00163     led1      = 1;
00164     led2      = 1;
00165     led3      = 1;
00166     led4      = 1;
00167 }
00168 
00169 //Interrupt fonction
00170 void reset(){
00171     counter  = resetFreq;
00172     allLedsOn();
00173     wait(0.5);
00174     allLedsOff();
00175     wait(0.2);
00176     allLedsOn();
00177     wait(0.5);
00178     allLedsOff();    
00179 }
00180 
00181 void initialisation(){
00182     iref_sw   = 1;
00183     allLedsOn();
00184     wait(5);
00185     iref_sw   = 0;
00186     allLedsOff();
00187 }
00188     
00189 void toggle() {
00190     data_out.attach(&send_data, 0.01); 
00191     wait(0.005);
00192     clock_out.attach(&send_clk, 0.005);
00193     count_data  = 0;
00194     count_clk   = 0;
00195     allLedsOff();
00196     led1        = 1;
00197     wait(0.2);
00198     led1        = 0;
00199     led2        = 1;
00200     wait(0.2);
00201     led2        = 0;
00202     led3        = 1;
00203     wait(0.2);
00204     led3        = 0;
00205     led4        = 1;
00206     wait(0.2);
00207     led4        = 0;
00208 }   
00209 
00210 void stepChange(){
00211     if(stepCount == 3)
00212         stepCount = 0;
00213     else
00214         stepCount++;
00215     if(stepCount == 0){
00216         allLedsOff();
00217         stepFreq = stepFreq*1000;
00218         led1 = 1;
00219     }
00220     else if (stepCount == 1){
00221         allLedsOff();
00222         stepFreq = stepFreq*10000;
00223         led2 = 1;
00224     }
00225     else if (stepCount == 2){
00226         allLedsOff();
00227         stepFreq = stepFreq*100000;
00228         led3 = 1;
00229     }
00230     else{
00231         allLedsOff();
00232         stepFreq = stepFreq*1000000;
00233         led4 = 1;
00234     }      
00235 }
00236 
00237 
00238 void send_data (void){    
00239     if(count_data<12){
00240         if(mode==0){
00241             data= *(div_bin+count_data);
00242             pc.printf("%d",*(div_bin+count_data));
00243             count_data=count_data+1;
00244         }
00245         if(mode==1) {
00246             data= *(div_bin+11-count_data);
00247             pc.printf("%d",*(div_bin+11-count_data));
00248             count_data=count_data+1;
00249         }
00250     }
00251     else
00252         data=0;
00253 }
00254     
00255 void send_clk (void){
00256     if(count_clk<24) {
00257         clk=!clk;
00258         count_clk=count_clk+1;
00259     }
00260     else {
00261         clk=0;;
00262     }        
00263 }  
00264 
00265 uint16_t read_data (int  mode) 
00266 {              
00267     int value;
00268     int i,j;
00269     char text[10][10]; // when the file exist and have data
00270     char line[10];     // it reads line by line
00271 
00272     for(i=0; i<10; i++)
00273         for(j=0; j<10; j++)
00274             text[i][j] = '\0';  // initializing the vector with null
00275     for(i=0; i<10; i++)
00276         line[i] = '\0';
00277                
00278     FILE* fp = fopen ("/local/div_prog.txt","r"); // open file for reading               
00279         if (fp!=NULL){
00280             fseek (fp, 0, SEEK_END);
00281             int  size = ftell(fp);
00282             if (size==0){
00283                 pc.printf("File is empty\n\r");
00284             }
00285             else{
00286                 rewind (fp); // restore pointer to the begin of the file
00287                 i=0;
00288                 while ( fgets ( line, sizeof line, fp ) != NULL ){ /* read a line */
00289                     strcpy(text[i], line);
00290                     printf("array ----> %s", text[i]);
00291                     i++;
00292                 }
00293                 fclose(fp); // close file
00294                 if(mode==0){// Get the divider value
00295                     value=atoi(text[mode]);
00296                         pc.printf("Divider value = %d \n\r",value); // display read data value
00297                 }
00298                 if(mode==1){// Get the Mode (LSB/MSB)
00299                     value=atoi(text[mode]);
00300                     if (value==0)
00301                         pc.printf("LSB Mode \n"); //
00302                     if (value==1)
00303                         pc.printf("MSB Mode \n"); //        
00304                 }
00305                 if(mode==2){// Get the Reset frequency in MHz
00306                     value=atoi(text[mode]);
00307                     pc.printf("Reset Frequency = %d \n\r", value);    
00308                 }
00309                 if(mode==3){// Get the frequency step
00310                     value=atoi(text[mode]);
00311                     pc.printf("Frequency Step = %d \n\r", value);    
00312                 }
00313                 if(mode==4){
00314                     value=atoi(text[mode]);
00315                     pc.printf("Threshold voltage = %d \n\r",value);   
00316                 }                             
00317             }
00318         }
00319                 
00320         else{
00321             int num;    
00322             pc.printf("File does no exist \n\r");
00323             pc.printf("Enter the divider value \n: \r");
00324             pc.printf("Byte to send LSB(0) or MSB(1)\n: \r");
00325             for(i=0; i<2; i++){   //just read to save to lines
00326                 pc.scanf("%s\r", text[i]);
00327                 while (!(sscanf(text[i], "%d\r", &num))){
00328                     printf("Invalid input '%s'\n\r", text[i]);
00329                     pc.scanf("%s\r", text[i]);
00330                 }     
00331             }                
00332                    
00333         }
00334         return value;
00335 }
00336     
00337 uint16_t * set_dec2bin (uint16_t read_div) {
00338     int i;
00339     static uint16_t  r[12];
00340     for( i = 0; i < 12; i++ ){
00341         r[i]= read_div >> i & 1;
00342         pc.printf("%d",r[i]); // display read data value
00343    }
00344      pc.printf("\n\r"); 
00345      return r;
00346 }