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.
ACS712.cpp
00001 #include "ACS712.h" 00002 #include "mbed.h" 00003 00004 // Constructor/Destructor 00005 ACS712::ACS712(PinName const outpin) 00006 00007 : 00008 00009 _aInPin(new AnalogIn(outpin)) 00010 00011 {} 00012 00013 ACS712::~ACS712() 00014 { 00015 delete _aInPin; 00016 } 00017 00018 // ----- Public Methods ----- 00019 00020 void ACS712::init() 00021 { 00022 00023 } 00024 double ACS712::read_current_sensor() 00025 { 00026 double val; 00027 val = _aInPin->read(); 00028 val *= val; 00029 val = sqrt(val); 00030 return val; 00031 } 00032 00033 int ACS712::read_ain_uint_16() 00034 { 00035 int val = 0; 00036 00037 val = _aInPin->read_u16(); 00038 00039 return val; 00040 } 00041 00042 void ACS712::convert_to_amps(float acs_offset, float gain, int type) 00043 { 00044 00045 double _current_in = read_current_sensor()*3.3; 00046 00047 switch(type) { 00048 00049 case 5: 00050 _current = (acs_offset*gain - _current_in*gain)/.185*gain; 00051 break; 00052 case 20: 00053 _current = (acs_offset*gain - _current_in*gain)/.100*gain; 00054 break; 00055 case 30: 00056 _current = (_current_in*gain - acs_offset*gain)/.066*gain; 00057 break; 00058 default: 00059 _current = 999.9; 00060 break; 00061 00062 } 00063 00064 } 00065 00066 00067 bool ACS712::over_current_detection(double operating_current) 00068 { 00069 00070 bool current_spike_flag = false; 00071 00072 if(_current > (1.1*operating_current)) { 00073 00074 current_spike_flag = true; 00075 00076 } 00077 else { 00078 00079 current_spike_flag = false; 00080 00081 } 00082 00083 return current_spike_flag; 00084 00085 } 00086 00087 float ACS712::get_current_amps() 00088 { 00089 00090 return _current; 00091 00092 }
Generated on Wed Jul 13 2022 00:00:33 by
1.7.2