-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADC.c
90 lines (79 loc) · 2.76 KB
/
ADC.c
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
84
85
86
87
88
89
90
#include "ADC.h"
//
uint32_t GPIOC15_gnd_connect(void){
if (!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15))// Catch from GND
{//slow, press and connect GPIOC 13 to GND
delay_ms(10);
return 1;
}
else
{//fast no press !!!
delay_ms(10);
return 0;
}
}
void GPIO_toggeld_13(void)
{
/* Read LED output (GPIOA PIN8) status */
uint8_t led_bit = GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13);
/* If LED output set, clear it */
if(led_bit == (uint8_t)Bit_SET)
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
/* If LED output clear, set it */
else
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
}
void GPIOC_init_13_o (void){
//to output for display
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); // (1)
GPIO_StructInit (& GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC , &GPIO_InitStructure);
}
//
void ADC_init(void)
{
__enable_irq();
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// input of ADC (it doesn't seem to be needed, as default GPIO state is floating input)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; // that's ADC2 (PA1 on STM32)
GPIO_Init(GPIOA, &GPIO_InitStructure);
//clock for ADC (max 14MHz --> 72/6=12MHz)
RCC_ADCCLKConfig (RCC_PCLK2_Div6);
// enable ADC system clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
// define ADC config
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // we work in continuous sampling mode
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_RegularChannelConfig(ADC2,ADC_Channel_1, 1,ADC_SampleTime_28Cycles5); // define regular conversion config
ADC_Init ( ADC2, &ADC_InitStructure); //set config of ADC2
// enable ADC
ADC_Cmd (ADC2,ENABLE); //enable ADC2
// ADC calibration (optional, but recommended at power on)
ADC_ResetCalibration(ADC2); // Reset previous calibration
while(ADC_GetResetCalibrationStatus(ADC2));
ADC_StartCalibration(ADC2); // Start new calibration (ADC must be off at that time)
while(ADC_GetCalibrationStatus(ADC2));
// start conversion
ADC_Cmd (ADC2,ENABLE); //enable ADC1
ADC_SoftwareStartConvCmd(ADC2, ENABLE);
}
unsigned int ADC_read()
{
delay_ms(10);
return ADC_GetConversionValue(ADC2);
delay_ms(10);
}