This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf103rb_adc.cpp
83 lines (67 loc) · 1.85 KB
/
f103rb_adc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @file f103rb_adc.cpp
* @author ZiTe <[email protected]>
*/
#include "f103rb_adc.hpp"
namespace F103RB
{
ADC::ADC(GPIO_PortPinTypeDef port_pin,
ADC_TypeDef *ADC,
uint8_t ADC_Channel)
{
// The clock of ADC con't over than 14MHz.
RCC_ADCCLKConfig(DEFAULT_ADC_CLK_CONFIG);
this->_PortPin = port_pin;
this->_ADCx = ADC;
this->_ADC_Channel = ADC_Channel;
}
void ADC::Init(void)
{
GPIO adc_pin(this->_PortPin, GPIO_Mode_AIN);
adc_pin.Init();
ADC_DeInit(this->_ADCx);
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
ADC_InitStruct.ADC_NbrOfChannel = 1;
ADC_InitStruct.ADC_ScanConvMode = DISABLE;
ADC_Init(this->_ADCx, &ADC_InitStruct);
this->Disable();
}
void ADC::Enable(void)
{
ADC_Cmd(this->_ADCx, ENABLE);
/* ADC Calibration */
// Reset calibration
ADC_ResetCalibration(this->_ADCx);
// Wait until reset calibration complete
while (ADC_GetResetCalibrationStatus(this->_ADCx) == 1)
{
/* Null */
}
// Start calibration
ADC_StartCalibration(this->_ADCx);
// Wait until calibration complete
while (ADC_GetCalibrationStatus(this->_ADCx) == 1)
{
/* Null */
}
}
void ADC::Disable(void)
{
ADC_Cmd(_ADCx, DISABLE);
}
uint16_t ADC::Get_Value(uint8_t Rank, uint8_t SampleTime)
{
ADC_RegularChannelConfig(this->_ADCx, this->_ADC_Channel, Rank, SampleTime);
ADC_SoftwareStartConvCmd(this->_ADCx, ENABLE);
// Wait for convert complete
while (ADC_GetFlagStatus(this->_ADCx, ADC_FLAG_EOC) == 0)
{
/* Null */
}
return ADC_GetConversionValue(this->_ADCx);
}
}