Amir Chaudhary / Mbed 2 deprecated PCB_Analog_values

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "board.h"
00003 #include "SerialDisplay.h"
00004 
00005 
00006 AnalogIn Vbat(PA_4);
00007 AnalogIn Led1(PA_1);
00008 AnalogIn Led2(PC_0);
00009 AnalogIn RM(PC_2);
00010 AnalogIn Vce(PB_1);
00011 DigitalOut Relay(D6);
00012 AnalogIn Exit(PC_4);
00013 //AnalogIn Exit2(PC_5);
00014 
00015 /**
00016  * Main application entry point.
00017  */
00018 Serial pc(SERIAL_TX, SERIAL_RX,115200);
00019 int MY_SetSysClock_PLL_HSE(void)
00020 {
00021     RCC_ClkInitTypeDef RCC_ClkInitStruct;
00022     RCC_OscInitTypeDef RCC_OscInitStruct;
00023 
00024     /* Enable HSE and activate PLL with HSE as source */
00025     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
00026     RCC_OscInitStruct.HSEState       = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
00027 
00028     // PLLCLK = (8 MHz * 8)/2 = 32 MHz
00029     RCC_OscInitStruct.PLL.PLLState        = RCC_PLL_ON;
00030     RCC_OscInitStruct.PLL.PLLSource       = RCC_PLLSOURCE_HSE;
00031     RCC_OscInitStruct.PLL.PLLMUL          = RCC_PLLMUL_8;
00032     RCC_OscInitStruct.PLL.PLLDIV          = RCC_PLLDIV_2;
00033     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
00034         return (-1); // FAIL
00035     }
00036 
00037     /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
00038     RCC_ClkInitStruct.ClockType      = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
00039     RCC_ClkInitStruct.SYSCLKSource   = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz
00040     RCC_ClkInitStruct.AHBCLKDivider  = RCC_SYSCLK_DIV1;         // 32 MHz
00041     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;           // 32 MHz
00042     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;           // 32 MHz
00043     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
00044         return (-2); // FAIL
00045     }
00046 
00047     /* Enable HSE and activate PLL with HSE as source */
00048     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
00049     RCC_OscInitStruct.HSIState       = RCC_HSI_OFF;
00050     RCC_OscInitStruct.MSIState       = RCC_MSI_OFF;
00051     RCC_OscInitStruct.HSI48State     = RCC_HSI48_OFF;
00052     RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE;
00053     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
00054         return (-3); // FAIL
00055     }
00056         
00057     return 0; // OK
00058 }
00059 
00060 void my_patch(void)
00061 {
00062     int retVal;
00063     
00064     // Put device into default clock, i.e using MSI = 2MHz
00065     HAL_RCC_DeInit();
00066     
00067     // Enable HSE clock
00068     retVal = MY_SetSysClock_PLL_HSE();
00069     if(retVal< 0)
00070     {
00071         // fail
00072         //pc.printf("Failed to start HSE, ERR= %d\r\n", retVal);
00073         
00074         // indicate error
00075         while(1)
00076         {
00077 
00078         }
00079     }    
00080 }
00081 
00082 int main()
00083 { 
00084     
00085     pc.printf("mbed-os-rev: %d.%d.%d   lib-rev: %d\r\n", \
00086             MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION);
00087     pc.printf("BUILD= %s, SysClock= %d, RCC= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR);   
00088     my_patch();
00089     pc.printf("NEW SysClock= %d, NEW RCC= %0X\r\n", SystemCoreClock, RCC->CR);
00090     wait(1);
00091     
00092     printf("\n");
00093     printf("\n");
00094     int min=0,count=0;
00095       
00096     float meas_Vbat,meas_Led1,meas_Led2,meas_RM,meas_Vce,meas_Exit,meas_Exit2;
00097     float v_Vbat,v_Led1,v_Led2,v_RM,v_Vce,v_Exit,v_Exit2;
00098  //   float meas_v;
00099 
00100 
00101 
00102    pc.printf("***Charging Mode***\n\n");
00103    Relay= 0; // Start the test Relay =1
00104  
00105 //    printf("\tAnalogIn example\n");    
00106     printf("count");
00107     printf("\tVbat");
00108     printf("\tLED1");
00109     printf("\tLED2");
00110     printf("\tRM");     
00111     printf("\tVce");
00112     printf("\tExit");
00113   //  printf("\tExit2");
00114     printf("\n");
00115 
00116 
00117  for(int j=0;j<=300;j++){
00118       meas_Vbat = Vbat.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00119       meas_Led1 = Led1.read() - Vce.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00120       meas_Led2 = Led2.read() - Vce.read();
00121       meas_RM = RM.read() - Vce.read();  
00122       meas_Vce = Vce.read(); 
00123       meas_Exit = Exit.read();
00124    //   meas_Exit2 = Exit2.read();
00125       
00126 
00127       
00128       // Display readings
00129       v_Vbat = meas_Vbat * 3300 *2;
00130       v_Led1 = (meas_Led1 * 3.300)/2.2;
00131       v_Led2 = (meas_Led2 * 3.300)/2.2;
00132       v_RM = (meas_RM * 3.300)/2.2;  
00133       v_Vce = meas_Vce * 3.300; 
00134       v_Exit = meas_Exit * 3.300/24;
00135     //  v_Exit2 = meas_Exit2 * 3.300/2.2;
00136       
00137 
00138       printf("%d\t", count);
00139       printf("%.0f\t", v_Vbat);
00140       printf("%.03f\t", v_Led1);
00141       printf("%.03f\t", v_Led2);
00142       printf("%.03f\t", v_RM);
00143       printf("%.03f\t", v_Vce);
00144       printf("%.03f\t",v_Exit);
00145    //   printf("%.03f\t",v_Exit2);
00146       printf("\n");
00147 
00148       count++;
00149      wait(0.1); // 10 second
00150 
00151 }
00152      count = 0;
00153 
00154   
00155    pc.printf("***Discharging Mode 1***\n\n");
00156    Relay= 0; // Start the test Relay =1
00157  
00158 //    printf("\tAnalogIn example\n");    
00159     printf("count");
00160     printf("\tVbat");
00161     printf("\tLED1");
00162     printf("\tLED2");
00163     printf("\tRM");     
00164     printf("\tVce");
00165     printf("\tExit");
00166    // printf("\tExit2");
00167     printf("\n");
00168 
00169 
00170  for(int j=0;j<=300;j++){
00171       meas_Vbat = Vbat.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00172       meas_Led1 = Led1.read() - Vce.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00173       meas_Led2 = Led2.read() - Vce.read();
00174       meas_RM = RM.read() - Vce.read();  
00175       meas_Vce = Vce.read(); 
00176       meas_Exit = Exit.read();
00177    //   meas_Exit2 = Exit2.read();
00178       
00179 
00180       
00181        // Display readings
00182       v_Vbat = meas_Vbat * 3300 *2;
00183       v_Led1 = (meas_Led1 * 3.300)/2.2;
00184       v_Led2 = (meas_Led2 * 3.300)/2.2;
00185       v_RM = (meas_RM * 3.300)/2.2;  
00186       v_Vce = meas_Vce * 3.300; 
00187       v_Exit = meas_Exit * 3.300/24;
00188     //  v_Exit2 = meas_Exit2 * 3.300/2.2;
00189       
00190 
00191       printf("%d\t", count);
00192       printf("%.0f\t", v_Vbat);
00193       printf("%.03f\t", v_Led1);
00194       printf("%.03f\t", v_Led2);
00195       printf("%.03f\t", v_RM);
00196       printf("%.03f\t", v_Vce);
00197       printf("%.03f\t",v_Exit);
00198    //   printf("%.03f\t",v_Exit2);
00199       printf("\n");
00200 
00201       count++;
00202    wait(0.1); // 10 second
00203 }
00204 
00205 
00206 
00207 
00208     printf("\n");
00209     printf("\n");
00210     pc.printf("***Discharging Mode 2***\n\n");
00211 //    printf("\tAnalogIn example\n");    
00212     printf("min");
00213     printf("\tVbat");
00214     printf("\tLED1");
00215     printf("\tLED2");
00216     printf("\tRM");     
00217     printf("\tVce");
00218     printf("\tExit");
00219    // printf("\tExit2");
00220     printf("\n");
00221 
00222     while(1) {
00223       Relay= 0; // Start the test Relay =1
00224       
00225       meas_Vbat = Vbat.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00226       meas_Led1 = Led1.read() - Vce.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00227       meas_Led2 = Led2.read() - Vce.read();
00228       meas_RM = RM.read() - Vce.read();  
00229       meas_Vce = Vce.read(); 
00230       meas_Exit = Exit.read();
00231      // meas_Exit2 = Exit2.read();
00232       
00233       
00234     // Display readings
00235       v_Vbat = meas_Vbat * 3300 *2;
00236       v_Led1 = (meas_Led1 * 3.300)/2.2;
00237       v_Led2 = (meas_Led2 * 3.300)/2.2;
00238       v_RM = (meas_RM * 3.300)/2.2;  
00239       v_Vce = meas_Vce * 3.300; 
00240       v_Exit = meas_Exit * 3.300/24;
00241     //  v_Exit2 = meas_Exit2 * 3.300/2.2;
00242       
00243 
00244       printf("%d\t", min);
00245       printf("%.0f\t", v_Vbat);
00246       printf("%.03f\t", v_Led1);
00247       printf("%.03f\t", v_Led2);
00248       printf("%.03f\t", v_RM);
00249       printf("%.03f\t", v_Vce);
00250       printf("%.03f\t",v_Exit);
00251    //   printf("%.03f\t",v_Exit2);
00252       printf("\n");
00253 
00254      
00255     wait(5.0); // 10 second
00256       min++;
00257     }
00258 
00259 }