mirror of
https://github.com/rene-dev/stmbl.git
synced 2026-09-26 01:54:24 +08:00
adc config
This commit is contained in:
@@ -70,6 +70,8 @@ void TIM2_IRQHandler(void){//1KHz
|
||||
mag_pos+=pi/100;
|
||||
output_pwm();
|
||||
//GPIO_ResetBits(GPIOD,GPIO_Pin_11);
|
||||
//ADC_SoftwareStartConv(ADC1);
|
||||
//ADC_SoftwareStartConv(ADC2);
|
||||
}
|
||||
|
||||
void ADC_IRQHandler(void)
|
||||
@@ -81,6 +83,7 @@ void ADC_IRQHandler(void)
|
||||
|
||||
//t1 = ADC_GetConversionValue(ADC1);
|
||||
//t2 = ADC_GetConversionValue(ADC2);
|
||||
//atan2f(res1_pos, res2_pos);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
||||
+91
-92
@@ -15,17 +15,10 @@ void setup(){
|
||||
//TIM4:PWM
|
||||
//TIM2:int
|
||||
|
||||
/* TIM config */
|
||||
/* TIM4 clock enable */
|
||||
//PWM timer
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
||||
|
||||
//int timer
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
//DAC enable
|
||||
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
|
||||
/* LEDs are on GPIOD */
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
||||
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
||||
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3, ENABLE);
|
||||
|
||||
@@ -36,13 +29,7 @@ void setup(){
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||||
|
||||
//PWM
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
|
||||
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||||
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_14;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
@@ -51,59 +38,52 @@ void setup(){
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||||
|
||||
//PWM
|
||||
|
||||
/* int set up, TIM2*/
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseStructure.TIM_Period = 8400;//1kHz
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = 9;
|
||||
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
||||
//TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);//DAC?
|
||||
|
||||
/* int NVIC setup */
|
||||
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
setup_pwm();
|
||||
setup_adc();
|
||||
|
||||
// systick timer
|
||||
time = 0;
|
||||
RCC_ClocksTypeDef RCC_Clocks;
|
||||
RCC_GetClocksFreq(&RCC_Clocks);
|
||||
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
|
||||
}
|
||||
|
||||
//setup PWM using TIM4
|
||||
void setup_pwm(){
|
||||
/* TIM4 clock enable */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
||||
/* GPIOD clock enable */
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
|
||||
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||||
|
||||
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
|
||||
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
|
||||
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
|
||||
|
||||
//Analog pin configuration
|
||||
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;//The channel 10 is connected to PC0
|
||||
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //The PC0 pin is configured in analog mode
|
||||
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down
|
||||
//GPIO_Init(GPIOA,&GPIO_InitStructure);//Affecting the port with the initialization structure configuration
|
||||
|
||||
//ADC structure configuration
|
||||
/*
|
||||
ADC_DeInit();
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
|
||||
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once
|
||||
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
|
||||
ADC_InitStructure.ADC_NbrOfConversion = 1;//I think this one is clear :p
|
||||
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
|
||||
//ADC_InitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
|
||||
ADC_Init(ADC1,&ADC_InitStructure);//Initialize ADC with the previous configuration
|
||||
ADC_Init(ADC2,&ADC_InitStructure);//Initialize ADC with the previous configuration
|
||||
ADC_Init(ADC3,&ADC_InitStructure);//Initialize ADC with the previous configuration
|
||||
|
||||
|
||||
ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
||||
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
|
||||
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
|
||||
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
|
||||
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
|
||||
ADC_CommonInit(&ADC_CommonInitStructure);
|
||||
|
||||
|
||||
//Enable ADC conversion
|
||||
ADC_Cmd(ADC1,ENABLE);
|
||||
ADC_Cmd(ADC2,ENABLE);
|
||||
ADC_Cmd(ADC3,ENABLE);
|
||||
|
||||
//Select the channel to be read from
|
||||
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_84Cycles);
|
||||
ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
|
||||
ADC_EOCOnEachRegularChannelCmd(ADC1,ENABLE);
|
||||
|
||||
ADC_RegularChannelConfig(ADC2,ADC_Channel_2,1,ADC_SampleTime_84Cycles);
|
||||
ADC_ITConfig(ADC2,ADC_IT_EOC,DISABLE);
|
||||
ADC_EOCOnEachRegularChannelCmd(ADC2,DISABLE);
|
||||
|
||||
ADC_RegularChannelConfig(ADC3,ADC_Channel_3,1,ADC_SampleTime_84Cycles);
|
||||
ADC_ITConfig(ADC3,ADC_IT_EOC,DISABLE);
|
||||
ADC_EOCOnEachRegularChannelCmd(ADC3,DISABLE);
|
||||
*/
|
||||
|
||||
/* pwm set up, TIM4*/
|
||||
/* Compute the prescaler value */ // 168MHz/2 / pwm frq / pwm res - 1
|
||||
uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 10000 / mag_res) - 1; // = 4
|
||||
@@ -115,7 +95,6 @@ void setup(){
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
||||
|
||||
|
||||
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
||||
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
||||
TIM_OCInitStructure.TIM_Pulse = 0;
|
||||
@@ -132,42 +111,62 @@ void setup(){
|
||||
/* PWM1 Mode configuration: Channel4 */
|
||||
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
|
||||
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* int set up, TIM2*/
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseStructure.TIM_Period = 8400;//1kHz
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = 9;
|
||||
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
||||
//TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);//DAC?
|
||||
// Setup ADC
|
||||
void setup_adc(){
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
||||
|
||||
/* int NVIC setup */
|
||||
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
//Analog pin configuration
|
||||
GPIO_InitStructure.GPIO_Pin = RES_SIN_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(RES_SIN_PORT,&GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = RES_COS_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(RES_COS_PORT,&GPIO_InitStructure);
|
||||
|
||||
//ADC structure configuration
|
||||
ADC_DeInit();
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
|
||||
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once
|
||||
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
|
||||
ADC_InitStructure.ADC_NbrOfConversion = 1;//I think this one is clear :p
|
||||
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
|
||||
ADC_Init(ADC1,&ADC_InitStructure);//Initialize ADC with the previous configuration
|
||||
ADC_Init(ADC2,&ADC_InitStructure);//Initialize ADC with the previous configuration
|
||||
|
||||
ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
||||
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
|
||||
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
|
||||
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
|
||||
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
|
||||
ADC_CommonInit(&ADC_CommonInitStructure);
|
||||
|
||||
//Enable ADC conversion
|
||||
ADC_Cmd(ADC1,ENABLE);
|
||||
ADC_Cmd(ADC2,ENABLE);
|
||||
|
||||
//Select the channel to be read from
|
||||
ADC_RegularChannelConfig(ADC1,RES_SIN_CHANNEL,1,RES_SampleTime);
|
||||
ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
|
||||
ADC_EOCOnEachRegularChannelCmd(ADC1,ENABLE);
|
||||
|
||||
ADC_RegularChannelConfig(ADC2,RES_COS_CHANNEL,1,RES_SampleTime);
|
||||
ADC_ITConfig(ADC2,ADC_IT_EOC,DISABLE);
|
||||
ADC_EOCOnEachRegularChannelCmd(ADC2,DISABLE);
|
||||
|
||||
// analog NVIC
|
||||
/*
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
*/
|
||||
|
||||
// systick timer
|
||||
time = 0;
|
||||
RCC_ClocksTypeDef RCC_Clocks;
|
||||
RCC_GetClocksFreq(&RCC_Clocks);
|
||||
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
|
||||
}
|
||||
|
||||
void SysTick_Handler(void)
|
||||
|
||||
+18
@@ -17,7 +17,25 @@
|
||||
//#define mag_res 5250
|
||||
#define mag_res 8400
|
||||
|
||||
|
||||
// res in:
|
||||
// ADC1: SIN: PA3 ADC1 IN3, ADC2 IN3, ADC3 IN3
|
||||
// ADC2: COS: PB1 ADC1 IN9, ADC2 IN9
|
||||
// TODO: RCC_AHB1PeriphClockCmd hardcoded in setup.c
|
||||
#define RES_SIN_PIN GPIO_Pin_3
|
||||
#define RES_SIN_PORT GPIOA
|
||||
#define RES_SIN_CHANNEL ADC_Channel_3
|
||||
|
||||
#define RES_COS_PIN GPIO_Pin_1
|
||||
#define RES_COS_PORT GPIOB
|
||||
#define RES_COS_CHANNEL ADC_Channel_9
|
||||
|
||||
//sample times for F4: 3,15,28,56,84,112,144,480
|
||||
#define RES_SampleTime ADC_SampleTime_84Cycles
|
||||
|
||||
void setup();
|
||||
void setup_adc();
|
||||
void setup_pwm();
|
||||
void SysTick_Handler(void);
|
||||
|
||||
volatile int time;
|
||||
|
||||
Reference in New Issue
Block a user