SimpleFOClibrary  2.1
current_sense/hardware_specific/generic_mcu.cpp
Go to the documentation of this file.
1 #include "../hardware_api.h"
2 
3 
4 
5 #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega2560__) // if mcu is atmega328 or atmega2560
6  #define _ADC_VOLTAGE 5.0
7  #define _ADC_RESOLUTION 1024.0
8  #ifndef cbi
9  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
10  #endif
11  #ifndef sbi
12  #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
13  #endif
14 #elif defined(__arm__) && defined(CORE_TEENSY) // or teensy
15  #define _ADC_VOLTAGE 3.3
16  #define _ADC_RESOLUTION 1024.0
17 #elif defined(__arm__) && defined(__SAM3X8E__) // or due
18  #define _ADC_VOLTAGE 3.3
19  #define _ADC_RESOLUTION 1024.0
20 #elif defined(ESP_H) // or esp32
21  #define _ADC_VOLTAGE 3.3
22  #define _ADC_RESOLUTION 4095.0
23 #elif defined(_STM32_DEF_) // or stm32
24  #define _ADC_VOLTAGE 3.3
25  #define _ADC_RESOLUTION 1024.0
26 #else
27  #define _ADC_VOLTAGE 5.0
28  #define _ADC_RESOLUTION 1024.0
29 #endif
30 
31 // adc counts to voltage conversion ratio
32 // some optimizing for faster execution
33 #define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
34 
35 // function reading an ADC value and returning the read voltage
36 float _readADCVoltage(const int pinA){
37  uint32_t raw_adc = analogRead(pinA);
38  return raw_adc * _ADC_CONV;
39 }
40 
41 
42 // function reading an ADC value and returning the read voltage
43 void _configureADC(const int pinA,const int pinB,const int pinC){
44  pinMode(pinA, INPUT);
45  pinMode(pinB, INPUT);
46  if( _isset(pinC) ) pinMode(pinC, INPUT);
47 
48  #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega2560__) // if mcu is atmega328 or atmega2560
49  // set hight frequency adc - ADPS2,ADPS1,ADPS0 | 001 (16mhz/2) | 010 ( 16mhz/4 ) | 011 (16mhz/8) | 100(16mhz/16) | 101 (16mhz/32) | 110 (16mhz/64) | 111 (16mhz/128 default)
50  // set divisor to 8 - adc frequency 16mhz/8 = 2 mhz
51  // arduino takes 25 conversions per sample so - 2mhz/25 = 80k samples per second - 12.5us per sample
52  cbi(ADCSRA, ADPS2);
53  sbi(ADCSRA, ADPS1);
54  sbi(ADCSRA, ADPS0);
55  #endif
56 }
_ADC_CONV
#define _ADC_CONV
Definition: current_sense/hardware_specific/generic_mcu.cpp:33
_isset
#define _isset(a)
Definition: foc_utils.h:11
_configureADC
void _configureADC(const int pinA, const int pinB, const int pinC)
Definition: current_sense/hardware_specific/generic_mcu.cpp:43
_readADCVoltage
float _readADCVoltage(const int pinA)
Definition: current_sense/hardware_specific/generic_mcu.cpp:36