Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ADC.c
00001 #include "System.h" 00002 00003 volatile float BatteryVoltage = 0; 00004 volatile float RobotBusVoltageHR = 0; 00005 volatile float RobotBusCurrentHR = 0; 00006 00007 float HighResRobotBusVoltageScalingFactor; 00008 float HighResRobotBusVoltageOffset; 00009 float HighResRobotBusCurrentScalingFactor; 00010 00011 SIGNED_DWORD ExtADCRobotVoltage=0; 00012 SIGNED_DWORD ExtADCRobotCurrent=0; 00013 00014 AnalogIn BatteryChannel(p17); 00015 AnalogIn RobotBusCurrentChannel(p20); 00016 AnalogIn RobotBusVoltageChannel(p19); 00017 AnalogIn SelfCurrentChannel(p18); 00018 00019 Ticker BatteryCheck; 00020 Ticker InputPowerCheck; 00021 00022 SPI ADCSPI(p11, p12, p13); 00023 DigitalOut ConversionStart(p14); 00024 00025 void CheckBattery(); 00026 void CheckInputPower(); 00027 00028 SIGNED_DWORD VFilter[64]; 00029 SIGNED_DWORD IFilter[64]; 00030 WORD FilterIndex; 00031 00032 SIGNED_DWORD VBattFilter[8]; 00033 WORD VBattFilterIndex; 00034 00035 DigitalOut PING(LED3); 00036 DigitalOut PONG(LED4); 00037 00038 volatile BOOL ADCDataRdy; 00039 00040 00041 void InitADC() 00042 { 00043 HighResRobotBusVoltageScalingFactor = U1_VREF * ((R2 + R4)/(R4)) * VSCALE_ADJUST; 00044 HighResRobotBusVoltageOffset = INPUT_DIODE_DROP*2; 00045 HighResRobotBusCurrentScalingFactor = U1_VREF * ((R6+R7)/R7) ; 00046 00047 ACS576_ISCALE = ACS576_ISCALE * ACS576_ISCALE_ADJUST; 00048 00049 ADCSPI.format(16,0); 00050 00051 ConversionStart = 0; 00052 00053 BatteryCheck.attach(&CheckBattery,0.1); 00054 InputPowerCheck.attach(&CheckInputPower,(1.0/(SAMPLE_RATE*16))); 00055 } 00056 00057 void CheckBattery() 00058 { 00059 int i; 00060 SIGNED_DWORD TempSum = 0; 00061 00062 VBattFilter[VBattFilterIndex++] = BatteryChannel.read_u16(); 00063 VBattFilterIndex &= 0x7; 00064 00065 for(i=0;i<8;i++) 00066 { 00067 TempSum += VBattFilter[i]; 00068 } 00069 00070 BatteryVoltage = (float)((TempSum>>3))/65535.0f * MBED_VREF * ((R17+R15)/R15); 00071 } 00072 00073 void CheckInputPower() 00074 { 00075 SIGNED_DWORD TempSumV,TempSumI; 00076 int i; 00077 00078 ConversionStart = 1; 00079 VFilter[FilterIndex] = (SIGNED_WORD)(ADCSPI.write(0x0000)); 00080 IFilter[FilterIndex] = (SIGNED_WORD)(ADCSPI.write(0x0000)); 00081 FilterIndex++; 00082 ConversionStart = 0; 00083 00084 //oversample and average 00085 if(FilterIndex ==16) 00086 { 00087 ADCDataRdy = TRUE; 00088 FilterIndex = 0; 00089 TempSumV=0; 00090 TempSumI=0; 00091 for(i=0;i<32;i++) 00092 { 00093 TempSumV+=VFilter[i]; 00094 TempSumI+=IFilter[i]; 00095 } 00096 00097 ExtADCRobotCurrent = TempSumV>>4; 00098 ExtADCRobotVoltage = TempSumI>>4; 00099 00100 RobotBusVoltageHR = ((((float)(ExtADCRobotVoltage)/(32768.0f)) * HighResRobotBusVoltageScalingFactor + HighResRobotBusVoltageOffset))- VOFFSET_ADJUST; 00101 RobotBusCurrentHR = (((((float)(ExtADCRobotCurrent)/(32768.0f)) * HighResRobotBusCurrentScalingFactor) -ACS576_VOFFSET) * ACS576_ISCALE) - ACS576_IOFFSET_TRIM; 00102 00103 if(SystemState == SYSTEM_STATE_LOGGING) 00104 { 00105 00106 if (MyDataBlock.WriteOutPtr >MyDataBlock.ReadInPtr) 00107 { 00108 ReadWriteDifferential = (DATA_BLOCK_SIZE - MyDataBlock.WriteOutPtr + MyDataBlock.ReadInPtr); 00109 } 00110 else 00111 { 00112 ReadWriteDifferential = ( MyDataBlock.ReadInPtr - MyDataBlock.WriteOutPtr); 00113 } 00114 00115 if(ReadWriteDifferential >= DATA_BLOCK_SIZE - 1) 00116 { 00117 DataLogError = TRUE; 00118 ErrorMsg = "Overrun!"; 00119 } 00120 else 00121 { 00122 MyDataBlock.Voltage[MyDataBlock.ReadInPtr] = RobotBusVoltageHR; 00123 MyDataBlock.Current[MyDataBlock.ReadInPtr] = RobotBusCurrentHR; 00124 00125 MyDataBlock.ReadInPtr++; 00126 00127 if(MyDataBlock.ReadInPtr == DATA_BLOCK_SIZE) 00128 { 00129 MyDataBlock.ReadInPtr = 0; 00130 } 00131 } 00132 } 00133 else 00134 { 00135 ReadWriteDifferential = 0; 00136 } 00137 } 00138 } 00139 00140 00141
Generated on Thu Jul 14 2022 09:05:34 by
1.7.2