void RCCInit (void)
{
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div16);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA |
RCC_AHBPeriph_GPIOB |
RCC_AHBPeriph_ADC12 ,
ENABLE
);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 |
RCC_APB1Periph_USART3 ,
ENABLE
);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 ,
ENABLE
);
return;
}
void GPIOInit (void)
{
GPIO_InitTypeDef s_gpio_init;
/** ADC1 GPIO Configuration
PA0 ------> ADC1_IN1
*/
s_gpio_init.GPIO_Pin = GPIO_Pin_0;
s_gpio_init.GPIO_Mode = GPIO_Mode_AN;
s_gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &s_gpio_init);
/** USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 , GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
s_gpio_init.GPIO_Pin = GPIO_Pin_9;
s_gpio_init.GPIO_Mode = GPIO_Mode_AF;
s_gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
s_gpio_init.GPIO_OType = GPIO_OType_PP;
s_gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &s_gpio_init);
s_gpio_init.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &s_gpio_init);
/** USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
s_gpio_init.GPIO_Pin = GPIO_Pin_2;
s_gpio_init.GPIO_Mode = GPIO_Mode_AF;
s_gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
s_gpio_init.GPIO_OType = GPIO_OType_PP;
s_gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &s_gpio_init);
s_gpio_init.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &s_gpio_init);
/** Configure pins as GPIO
PA8 ------> GPIO_Output
PA11 ------> GPIO_Output
PA12 ------> GPIO_Output
PB5 ------> GPIO_Output
PB6 ------> GPIO_Output
PB7 ------> GPIO_Output
*/
s_gpio_init.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_12;
s_gpio_init.GPIO_Mode = GPIO_Mode_OUT;
s_gpio_init.GPIO_OType = GPIO_OType_PP;
s_gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
s_gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &s_gpio_init);
s_gpio_init.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
s_gpio_init.GPIO_Mode = GPIO_Mode_OUT;
s_gpio_init.GPIO_OType = GPIO_OType_PP;
s_gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
s_gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &s_gpio_init);
return;
}
void USARTInit (void)
{
USART_InitTypeDef s_usart_init;
s_usart_init.USART_BaudRate = 9600;
s_usart_init.USART_WordLength = USART_WordLength_8b;
s_usart_init.USART_StopBits = USART_StopBits_1;
s_usart_init.USART_Parity = USART_Parity_No;
s_usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
s_usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &s_usart_init);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
s_usart_init.USART_BaudRate = 9600;
s_usart_init.USART_WordLength = USART_WordLength_8b;
s_usart_init.USART_StopBits = USART_StopBits_1;
s_usart_init.USART_Parity = USART_Parity_No;
s_usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
s_usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &s_usart_init);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
return;
}
void ADCInit (void)
{
ADC_InitTypeDef s_adc_init;
ADC_CommonInitTypeDef s_adc_common_init;
ADC_StructInit(&s_adc_init);
/* Calibration procedure */
ADC_VoltageRegulatorCmd(ADC1, ENABLE);
/* Insert delay equal to 10 us */
//DelayNUs(10);
DelayUs(10);
ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1) != RESET );
CalibrationValue = ADC_GetCalibrationValue(ADC1);
s_adc_common_init.ADC_Mode = ADC_Mode_Independent;
s_adc_common_init.ADC_Clock = ADC_Clock_AsynClkMode;
s_adc_common_init.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
s_adc_common_init.ADC_DMAMode = ADC_DMAMode_OneShot;
s_adc_common_init.ADC_TwoSamplingDelay = 0;
ADC_CommonInit(ADC1, &s_adc_common_init);
s_adc_init.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
s_adc_init.ADC_Resolution = ADC_Resolution_12b;
s_adc_init.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
s_adc_init.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
s_adc_init.ADC_DataAlign = ADC_DataAlign_Right;
s_adc_init.ADC_OverrunMode = ADC_OverrunMode_Disable;
s_adc_init.ADC_AutoInjMode = ADC_AutoInjec_Disable;
s_adc_init.ADC_NbrOfRegChannel = 1;
ADC_Init(ADC1, &s_adc_init);
/* ADC1 regular channel1 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
return;
}
void NVICInit (void)
{
NVIC_InitTypeDef s_nvic_init;
/* Enable the USART1 Interrupt */
s_nvic_init.NVIC_IRQChannel = USART1_IRQn;
s_nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
s_nvic_init.NVIC_IRQChannelSubPriority = 0;
s_nvic_init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&s_nvic_init);
/* Enable the USART1 Interrupt */
s_nvic_init.NVIC_IRQChannel = USART2_IRQn;
s_nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
s_nvic_init.NVIC_IRQChannelSubPriority = 0;
s_nvic_init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&s_nvic_init);
return;
}
void SYSInit (void)
{
RCCInit();
GPIOInit();
SysTickInit();
NVICInit();
USARTInit();
ADCInit();
return;
}
简直要疯掉了,STM32F103CB可以正常工作,STM32F303CC的GPIO和USART也可以正常工作,只是ADC初始化部分“红色代码行”总是死循环。
这是全部的初始化代码,请有经验的同仁帮俺看看,俺在这里作揖了。 |