Hex-current-measurements
Dependencies: mbed LoRaWAN-lib SX1276Lib
app/main.cpp@12:630be38ec1ed, 2019-05-01 (annotated)
- Committer:
- amirchaudhary
- Date:
- Wed May 01 17:17:56 2019 +0000
- Revision:
- 12:630be38ec1ed
- Parent:
- 11:9e35ddff7ed8
Hex-current-measurements
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:92bca02df485 | 1 | #include "mbed.h" |
mluis | 0:92bca02df485 | 2 | #include "board.h" |
mluis | 0:92bca02df485 | 3 | #include "SerialDisplay.h" |
mluis | 0:92bca02df485 | 4 | |
mluis | 1:352f608c3337 | 5 | |
amirchaudhary | 12:630be38ec1ed | 6 | AnalogIn Vbat(PA_4); |
amirchaudhary | 12:630be38ec1ed | 7 | AnalogIn Led1(PA_1); |
amirchaudhary | 12:630be38ec1ed | 8 | AnalogIn Led2(PC_0); |
amirchaudhary | 11:9e35ddff7ed8 | 9 | AnalogIn RM(PC_2); |
amirchaudhary | 11:9e35ddff7ed8 | 10 | AnalogIn Vce(PB_1); |
amirchaudhary | 12:630be38ec1ed | 11 | DigitalOut Relay(D6); |
uss1994 | 10:9a4efdd07a77 | 12 | |
uss1994 | 10:9a4efdd07a77 | 13 | |
mluis | 0:92bca02df485 | 14 | /** |
mluis | 0:92bca02df485 | 15 | * Main application entry point. |
mluis | 0:92bca02df485 | 16 | */ |
amirchaudhary | 11:9e35ddff7ed8 | 17 | Serial pc(SERIAL_TX, SERIAL_RX,115200); |
amirchaudhary | 11:9e35ddff7ed8 | 18 | int MY_SetSysClock_PLL_HSE(void) |
mluis | 0:92bca02df485 | 19 | { |
amirchaudhary | 11:9e35ddff7ed8 | 20 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
amirchaudhary | 11:9e35ddff7ed8 | 21 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mluis | 7:3173f0508a98 | 22 | |
amirchaudhary | 11:9e35ddff7ed8 | 23 | /* Enable HSE and activate PLL with HSE as source */ |
amirchaudhary | 11:9e35ddff7ed8 | 24 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
amirchaudhary | 11:9e35ddff7ed8 | 25 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
mluis | 7:3173f0508a98 | 26 | |
amirchaudhary | 11:9e35ddff7ed8 | 27 | // PLLCLK = (8 MHz * 8)/2 = 32 MHz |
amirchaudhary | 11:9e35ddff7ed8 | 28 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
amirchaudhary | 11:9e35ddff7ed8 | 29 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
amirchaudhary | 11:9e35ddff7ed8 | 30 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8; |
amirchaudhary | 11:9e35ddff7ed8 | 31 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2; |
amirchaudhary | 11:9e35ddff7ed8 | 32 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
amirchaudhary | 11:9e35ddff7ed8 | 33 | return (-1); // FAIL |
amirchaudhary | 11:9e35ddff7ed8 | 34 | } |
mluis | 0:92bca02df485 | 35 | |
amirchaudhary | 11:9e35ddff7ed8 | 36 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
amirchaudhary | 11:9e35ddff7ed8 | 37 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
amirchaudhary | 11:9e35ddff7ed8 | 38 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz |
amirchaudhary | 11:9e35ddff7ed8 | 39 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz |
amirchaudhary | 11:9e35ddff7ed8 | 40 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
amirchaudhary | 11:9e35ddff7ed8 | 41 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
amirchaudhary | 11:9e35ddff7ed8 | 42 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
amirchaudhary | 11:9e35ddff7ed8 | 43 | return (-2); // FAIL |
amirchaudhary | 11:9e35ddff7ed8 | 44 | } |
mluis | 3:9c6f7f082151 | 45 | |
amirchaudhary | 11:9e35ddff7ed8 | 46 | /* Enable HSE and activate PLL with HSE as source */ |
amirchaudhary | 11:9e35ddff7ed8 | 47 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI; |
amirchaudhary | 11:9e35ddff7ed8 | 48 | RCC_OscInitStruct.HSIState = RCC_HSI_OFF; |
amirchaudhary | 11:9e35ddff7ed8 | 49 | RCC_OscInitStruct.MSIState = RCC_MSI_OFF; |
amirchaudhary | 11:9e35ddff7ed8 | 50 | RCC_OscInitStruct.HSI48State = RCC_HSI48_OFF; |
amirchaudhary | 11:9e35ddff7ed8 | 51 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
amirchaudhary | 11:9e35ddff7ed8 | 52 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
amirchaudhary | 11:9e35ddff7ed8 | 53 | return (-3); // FAIL |
amirchaudhary | 11:9e35ddff7ed8 | 54 | } |
amirchaudhary | 11:9e35ddff7ed8 | 55 | |
amirchaudhary | 11:9e35ddff7ed8 | 56 | return 0; // OK |
amirchaudhary | 11:9e35ddff7ed8 | 57 | } |
mluis | 3:9c6f7f082151 | 58 | |
amirchaudhary | 11:9e35ddff7ed8 | 59 | void my_patch(void) |
amirchaudhary | 11:9e35ddff7ed8 | 60 | { |
amirchaudhary | 11:9e35ddff7ed8 | 61 | int retVal; |
amirchaudhary | 11:9e35ddff7ed8 | 62 | |
amirchaudhary | 11:9e35ddff7ed8 | 63 | // Put device into default clock, i.e using MSI = 2MHz |
amirchaudhary | 11:9e35ddff7ed8 | 64 | HAL_RCC_DeInit(); |
amirchaudhary | 11:9e35ddff7ed8 | 65 | |
amirchaudhary | 11:9e35ddff7ed8 | 66 | // Enable HSE clock |
amirchaudhary | 11:9e35ddff7ed8 | 67 | retVal = MY_SetSysClock_PLL_HSE(); |
amirchaudhary | 11:9e35ddff7ed8 | 68 | if(retVal< 0) |
amirchaudhary | 11:9e35ddff7ed8 | 69 | { |
amirchaudhary | 11:9e35ddff7ed8 | 70 | // fail |
amirchaudhary | 11:9e35ddff7ed8 | 71 | //pc.printf("Failed to start HSE, ERR= %d\r\n", retVal); |
amirchaudhary | 11:9e35ddff7ed8 | 72 | |
amirchaudhary | 11:9e35ddff7ed8 | 73 | // indicate error |
amirchaudhary | 11:9e35ddff7ed8 | 74 | while(1) |
amirchaudhary | 11:9e35ddff7ed8 | 75 | { |
mluis | 3:9c6f7f082151 | 76 | |
amirchaudhary | 11:9e35ddff7ed8 | 77 | } |
amirchaudhary | 11:9e35ddff7ed8 | 78 | } |
amirchaudhary | 11:9e35ddff7ed8 | 79 | } |
mluis | 5:1e9f6a365854 | 80 | |
amirchaudhary | 11:9e35ddff7ed8 | 81 | int main() |
amirchaudhary | 12:630be38ec1ed | 82 | { |
amirchaudhary | 11:9e35ddff7ed8 | 83 | |
amirchaudhary | 11:9e35ddff7ed8 | 84 | pc.printf("mbed-os-rev: %d.%d.%d lib-rev: %d\r\n", \ |
amirchaudhary | 11:9e35ddff7ed8 | 85 | MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION); |
amirchaudhary | 11:9e35ddff7ed8 | 86 | pc.printf("BUILD= %s, SysClock= %d, RCC= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR); |
amirchaudhary | 11:9e35ddff7ed8 | 87 | my_patch(); |
amirchaudhary | 11:9e35ddff7ed8 | 88 | pc.printf("NEW SysClock= %d, NEW RCC= %0X\r\n", SystemCoreClock, RCC->CR); |
amirchaudhary | 12:630be38ec1ed | 89 | wait(1); |
amirchaudhary | 11:9e35ddff7ed8 | 90 | |
amirchaudhary | 11:9e35ddff7ed8 | 91 | printf("\n"); |
amirchaudhary | 11:9e35ddff7ed8 | 92 | printf("\n"); |
amirchaudhary | 12:630be38ec1ed | 93 | int min=0,count=0; |
amirchaudhary | 12:630be38ec1ed | 94 | |
amirchaudhary | 11:9e35ddff7ed8 | 95 | float meas_Vbat,meas_Led1,meas_Led2,meas_RM,meas_Vce; |
amirchaudhary | 11:9e35ddff7ed8 | 96 | float v_Vbat,v_Led1,v_Led2,v_RM,v_Vce; |
amirchaudhary | 11:9e35ddff7ed8 | 97 | // float meas_v; |
amirchaudhary | 12:630be38ec1ed | 98 | |
amirchaudhary | 12:630be38ec1ed | 99 | |
amirchaudhary | 12:630be38ec1ed | 100 | |
amirchaudhary | 11:9e35ddff7ed8 | 101 | // printf("\tAnalogIn example\n"); |
amirchaudhary | 11:9e35ddff7ed8 | 102 | printf("min"); |
amirchaudhary | 11:9e35ddff7ed8 | 103 | printf("\tVbat"); |
amirchaudhary | 11:9e35ddff7ed8 | 104 | printf("\tLED1"); |
amirchaudhary | 11:9e35ddff7ed8 | 105 | printf("\tLED2"); |
amirchaudhary | 11:9e35ddff7ed8 | 106 | printf("\tRM"); |
amirchaudhary | 12:630be38ec1ed | 107 | // printf("\tVce"); |
amirchaudhary | 11:9e35ddff7ed8 | 108 | printf("\n"); |
amirchaudhary | 11:9e35ddff7ed8 | 109 | |
amirchaudhary | 11:9e35ddff7ed8 | 110 | while(1) { |
amirchaudhary | 12:630be38ec1ed | 111 | Relay= 1; // Start the test Relay =1 |
amirchaudhary | 12:630be38ec1ed | 112 | |
amirchaudhary | 11:9e35ddff7ed8 | 113 | meas_Vbat = Vbat.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
amirchaudhary | 12:630be38ec1ed | 114 | meas_Led1 = Led1.read() - Vce.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
amirchaudhary | 12:630be38ec1ed | 115 | meas_Led2 = Led2.read() - Vce.read(); |
amirchaudhary | 12:630be38ec1ed | 116 | meas_RM = RM.read() - Vce.read(); |
amirchaudhary | 12:630be38ec1ed | 117 | // meas_Vce = Vce.read(); |
amirchaudhary | 11:9e35ddff7ed8 | 118 | |
amirchaudhary | 12:630be38ec1ed | 119 | /* |
amirchaudhary | 11:9e35ddff7ed8 | 120 | // Display readings |
amirchaudhary | 11:9e35ddff7ed8 | 121 | v_Vbat = meas_Vbat*3300*2; |
amirchaudhary | 11:9e35ddff7ed8 | 122 | v_Led1 = meas_Led1*3300; |
amirchaudhary | 11:9e35ddff7ed8 | 123 | v_Led2 = meas_Led2*3300; |
amirchaudhary | 11:9e35ddff7ed8 | 124 | v_RM = meas_RM*3300; |
amirchaudhary | 11:9e35ddff7ed8 | 125 | v_Vce = meas_Vce*3300; |
amirchaudhary | 11:9e35ddff7ed8 | 126 | |
amirchaudhary | 12:630be38ec1ed | 127 | */ |
amirchaudhary | 12:630be38ec1ed | 128 | |
amirchaudhary | 12:630be38ec1ed | 129 | // Display readings |
amirchaudhary | 12:630be38ec1ed | 130 | v_Vbat = meas_Vbat * 3.350*2; |
amirchaudhary | 12:630be38ec1ed | 131 | v_Led1 = (meas_Led1 * 3.300)/2.2; |
amirchaudhary | 12:630be38ec1ed | 132 | v_Led2 = (meas_Led2 * 3.300)/2.2; |
amirchaudhary | 12:630be38ec1ed | 133 | v_RM = (meas_RM * 3.300)/2.2; |
amirchaudhary | 12:630be38ec1ed | 134 | // v_Vce = meas_Vce * 3.300; |
amirchaudhary | 12:630be38ec1ed | 135 | |
amirchaudhary | 11:9e35ddff7ed8 | 136 | |
amirchaudhary | 11:9e35ddff7ed8 | 137 | printf("%d\t", min); |
amirchaudhary | 12:630be38ec1ed | 138 | printf("%.03f\t", v_Vbat); |
amirchaudhary | 12:630be38ec1ed | 139 | printf("%.03f\t", v_Led1); |
amirchaudhary | 12:630be38ec1ed | 140 | printf("%.03f\t", v_Led2); |
amirchaudhary | 12:630be38ec1ed | 141 | printf("%.03f\t", v_RM); |
amirchaudhary | 11:9e35ddff7ed8 | 142 | |
amirchaudhary | 12:630be38ec1ed | 143 | printf("\n"); |
amirchaudhary | 11:9e35ddff7ed8 | 144 | |
amirchaudhary | 12:630be38ec1ed | 145 | wait(10.0); // 10 second |
amirchaudhary | 11:9e35ddff7ed8 | 146 | min++; |
mluis | 0:92bca02df485 | 147 | } |
amirchaudhary | 12:630be38ec1ed | 148 | |
mluis | 0:92bca02df485 | 149 | } |