用TIM2的CC2,触发ADC,频率是1HZ,能看到寄存器的CC2IF位变化,但是ADC的STRT位始终不变,ADC不能触发,折腾了两天了,请各位帮忙看看是啥问题.ADC是好的,用软件触发可以正确转换. /******************** (C) COPYRIGHT 2007 STMicroelectronics ******************** * File Name : main.c * Author : MCD Application Team * Version : V1.0 * Date : 10/08/2007 * Description : Main program body ******************************************************************************** * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. *******************************************************************************/
/* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h"
/* Private define ------------------------------------------------------------*/ #define ADC1_DR_Address ((u32)0x4001244C) #define BufferSize 3
/* Private typedef -----------------------------------------------------------*/ typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private variables ---------------------------------------------------------*/ ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; vu32 ADC_ConvertedValueTab[6]; SPI_InitTypeDef SPI_InitStructure; u8 SPI2_Buffer_Tx[BufferSize] = {0xF8, 0x00,0x0F}; u8 SPI1_Buffer_Rx[BufferSize], SPI2_Buffer_Rx[BufferSize]; u8 TxIdx = 0, RxIdx = 0, k = 0; volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED; volatile TestStatus TransferStatus3 = FAILED, TransferStatus4 = FAILED; ErrorStatus HSEStartUpStatus; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;
/*TIM2CLK = 36 MHz, Prescaler = 4, TIM2 counter clock = 7.2 MHz CC1 update rate = TIM2 counter clock / CCR1_Val = 146.48 Hz */ vu16 CCR1_Val = 15000; TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);
/* Private function prototypes ----------------------------------------------*/ void sys_init(void); void RCC_Configuration(void); void NVIC_Configuration(void); void GPIO_Configuration(void); void SysTick_Config(void); void SPI_Config(void); void timer_Config(void); void ADC_Config(void); void DMA_Config(void); void UART_Config(void); void I2C_Config(void);
/* Private functions ---------------------------------------------------------*/ void delay(void); void delay() { int i,j=0; for (i=0; i<0xff; i++) j++; }
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif
sys_init();
TIM_Cmd(TIM2, ENABLE); /* TIM2 enable counter */ while(1) { //delay(); if (TIM_GetFlagStatus(TIM2, TIM_FLAG_CC2) != RESET) { TIM_Cmd(TIM2, DISABLE); TIM_ClearFlag(TIM2, TIM_FLAG_CC2); GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6))); TIM_Cmd(TIM2, ENABLE); } //if (DMA_GetFlagStatus(DMA1_FLAG_TC1) != RESET) // { // DMA_ClearFlag(DMA1_FLAG_TC1); // GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6))); // } // } if (ADC_GetFlagStatus(ADC1, ADC_FLAG_STRT) != RESET) { TIM_Cmd(TIM2, DISABLE); ADC_ClearFlag(ADC1, ADC_FLAG_STRT); TIM_Cmd(TIM2, ENABLE); // GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6))); } } }
#ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert_param error has occurred. * Input : - file: pointer to the source file name * - line: assert_param error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ //while (1){} } #endif
/*--------------------- NEXT IS THE SYSTEM INIT ------------------------------*/
void sys_init(void) { RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); SysTick_Config(); SPI_Config(); timer_Config(); DMA_Config(); UART_Config(); I2C_Config(); ADC_Config(); }
void RCC_Configuration(void) { RCC_DeInit(); //RCC system reset(for debug purpose) RCC_HSEConfig(RCC_HSE_ON);// Enable HSE HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready if(HSEStartUpStatus == SUCCESS) { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/* Enable Prefetch Buffer */ FLASH_SetLatency(FLASH_Latency_2); /* Flash 2 wait state */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* HCLK = SYSCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PCLK1 = HCLK/2 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); /* ADCCLK = PCLK2/4 */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLCmd(ENABLE); /* Enable PLL */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}/* Wait till PLL is ready */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Select PLL as system clock source */ while(RCC_GetSYSCLKSource() != 0x08){}/* Wait till PLL is used as system clock source */ } RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);/* Enable DMA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);/* Enable ADC1,2 and GPIOC clock */ /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);/* TIM2 clocks enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); /* SPI2 Periph clock enable */ }
void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);/* Set the Vector Table base location at 0x20000000 */ #else /* VECT_TAB_FLASH */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); /* Set the Vector Table base location at 0x08000000 */ #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);/* Configure the Priority Group to 2 bits */ NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;/* enabling interrupt */ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0);/* Configure the SysTick handler priority */ }
void SysTick_Config(void) { SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);/* Configure HCLK clock as SysTick clock source */ SysTick_SetReload(720000);/* SysTick interrupt each 100 Hz with HCLK equal to 72MHz */ SysTick_CounterCmd(SysTick_Counter_Enable);/* Enable the SysTick Counter */ }
void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOC Configuration:Pin6, 7, 8 and 9 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PC.00 (ADC Channel10) as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); }
void SPI_Config(void) { SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE);/* Enable SPI2 */ }
void timer_Config(void) { TIM_TimeBaseStructure.TIM_Period = 65535;/* Time base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_PrescalerConfig(TIM2, 1024, TIM_PSCReloadMode_Immediate);/* Prescaler configuration */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;/* Output Compare Timing Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);/* TIM IT disable */ } void ADC_Config(void) { ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_Init(ADC2, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5); //ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 2, ADC_SampleTime_55Cycles5); //ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5); //ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 2, ADC_SampleTime_55Cycles5); //ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 3, ADC_SampleTime_55Cycles5); ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_ResetCalibration(ADC1); /* Enable ADC1 reset calibaration register */ while(ADC_GetResetCalibrationStatus(ADC1)){} /* Check the end of ADC1 reset calibration register */ ADC_StartCalibration(ADC1); /* Start ADC1 calibaration */ while(ADC_GetCalibrationStatus(ADC1)){} /* Check the end of ADC1 calibration */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 DMA */ ADC_Cmd(ADC2, ENABLE); /* Enable ADC2 */ ADC_ResetCalibration(ADC2); /* Enable ADC2 reset calibaration register */ while(ADC_GetResetCalibrationStatus(ADC2)){} /* Check the end of ADC2 reset calibration register */ ADC_StartCalibration(ADC2); /* Start ADC2 calibaration */ while(ADC_GetCalibrationStatus(ADC2)){} /* Check the end of ADC2 calibration */ ADC_ExternalTrigConvCmd(ADC2, ENABLE); /* Start ADC2 External Conversion */ ADC_ExternalTrigConvCmd(ADC1, ENABLE); /* Start ADC1 External Conversion */ }
void DMA_Config(void) { DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValueTab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 5; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel1, DMA_IT_TC,ENABLE); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); }
void UART_Config(void) {
}
void I2C_Config(void) {
}
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
|
|