1 | #include "stm32f4xx.h" /* STM32 registers */
|
2 | #include "stm32f4xx_conf.h" /* STM32 peripheral drivers */
|
3 |
|
4 | void gpioinit(void);
|
5 | void adcinit(void);
|
6 |
|
7 | void main(void)
|
8 | {
|
9 | SystemInit();
|
10 | gpioinit();
|
11 | adcinit();
|
12 | ADC_SoftwareStartConv(ADC1);
|
13 |
|
14 | while (1)
|
15 | {
|
16 | }
|
17 | }
|
18 |
|
19 | void adcinit(void)
|
20 | {
|
21 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
22 |
|
23 |
|
24 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructInit;
|
25 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
26 |
|
27 | /* Selects the internal clock for TIM1 */
|
28 | TIM_InternalClockConfig(TIM1);
|
29 |
|
30 | TIM_TimeBaseStructInit.TIM_Period = (68000000/200000)-1;
|
31 | TIM_TimeBaseStructInit.TIM_Prescaler = 0;
|
32 | TIM_TimeBaseStructInit.TIM_ClockDivision = TIM_CKD_DIV1;
|
33 | TIM_TimeBaseStructInit.TIM_CounterMode = TIM_CounterMode_Up;
|
34 | TIM_TimeBaseStructInit.TIM_RepetitionCounter = 0x00;
|
35 | TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructInit);
|
36 |
|
37 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
38 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
39 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
|
40 | TIM_OCInitStructure.TIM_Pulse = 1;
|
41 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
|
42 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
|
43 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
|
44 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
|
45 | TIM_OC1Init(TIM1, &TIM_OCInitStructure);
|
46 |
|
47 | /* Enables the TIM1 Preload on CC1 Register */
|
48 | TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //OCxPE bit in the TIMx_CCMRx register
|
49 |
|
50 | /* Enables the TIM1 Preload on ARR Register */
|
51 | TIM_ARRPreloadConfig(TIM1, ENABLE); //ARPE bit in the TIMx_CR1 register
|
52 |
|
53 | /* Sets the TIM1 new Autoreload value ARR */
|
54 | TIM_SetAutoreload(TIM1, (68000000/200000)-1); //Autoreload register sets the period
|
55 |
|
56 | /* Sets the new TIM1 Capture Compare 1 value */
|
57 | TIM_SetCompare1(TIM1, 1); //Capture Compare register sets the duty cycle
|
58 |
|
59 | /* Selects the OC1 event as TRGO for TIM1 */
|
60 | TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_OC1);
|
61 |
|
62 | /* Enables the TIM1 counter */
|
63 | TIM_Cmd(TIM1, ENABLE);
|
64 |
|
65 | /* Enables the TIM1 peripheral Main Outputs */
|
66 | TIM_CtrlPWMOutputs(TIM1, ENABLE);
|
67 |
|
68 |
|
69 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
|
70 | RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
71 | GPIO_InitTypeDef gpioInitStruct;
|
72 |
|
73 | gpioInitStruct.GPIO_Pin = GPIO_Pin_7;
|
74 | gpioInitStruct.GPIO_Mode = GPIO_Mode_AIN;
|
75 | gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
76 | GPIO_Init(GPIOA, &gpioInitStruct);
|
77 |
|
78 | ADC_InitTypeDef ADC_InitStructure;
|
79 | ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
80 | NVIC_InitTypeDef NVIC_adc;
|
81 |
|
82 | ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
|
83 | ADC_InitStructure.ADC_ScanConvMode = DISABLE;
|
84 | ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
85 | ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigInjecConvEdge_Rising;
|
86 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
|
87 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
88 | ADC_InitStructure.ADC_NbrOfConversion = 1;
|
89 |
|
90 | ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
|
91 | ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
|
92 | ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
|
93 | ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_15Cycles;
|
94 |
|
95 | NVIC_adc.NVIC_IRQChannel = ADC_IRQn;
|
96 | NVIC_adc.NVIC_IRQChannelCmd = ENABLE;
|
97 | NVIC_adc.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
98 | NVIC_adc.NVIC_IRQChannelSubPriority = 0x01;
|
99 | NVIC_Init(&NVIC_adc);
|
100 |
|
101 | ADC_CommonInit(&ADC_CommonInitStructure);
|
102 | ADC_Init(ADC1,&ADC_InitStructure);
|
103 |
|
104 | ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
|
105 |
|
106 | ADC_RegularChannelConfig(ADC1,ADC_Channel_7,1,ADC_SampleTime_3Cycles);
|
107 | ADC_Cmd(ADC1, ENABLE);
|
108 | }
|
109 |
|
110 | void gpioinit(void)
|
111 | {
|
112 | GPIO_InitTypeDef GPIO_InitStruct;
|
113 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
114 |
|
115 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12;
|
116 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
117 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
118 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
119 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
120 | GPIO_Init(GPIOD, &GPIO_InitStruct);
|
121 |
|
122 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
123 |
|
124 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
|
125 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
|
126 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
127 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
128 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
|
129 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
130 | }
|
131 |
|
132 | float a0[] = {1, -2.1621, 1.1690};
|
133 | float b0[] = {0.023975, -0.043383, 0.019626};
|
134 | float z[] = {0, 0, 0};
|
135 | float x = 0;
|
136 | volatile float y = 0;
|
137 |
|
138 | void ADC_IRQHandler()
|
139 | {
|
140 | GPIO_SetBits(GPIOD, GPIO_Pin_12);
|
141 | x = ADC_GetConversionValue(ADC1) - 2048;
|
142 | z[0] = x - a0[1] * z[1] - a0[2] * z[2];
|
143 | y = b0[0] * z[0] + b0[1] * z[1] + b0[2] * z[2];
|
144 | z[2] = z[1];
|
145 | z[1] = z[0];
|
146 | GPIO_ResetBits(GPIOD, GPIO_Pin_12);
|
147 | ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
|
148 | }
|