用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****/
图
关闭TIM2后,可以清晰https://bbs.eeworld.com.cn/upfiles/img/20079/2007911171350994.gif的看到CC2IF的变化,但是ADC的始终不变
顶顶!
LZ解决的咋样了?
我仿照版主给的例子用TIM2总也不能触发ADC转换!
谁做过TIM2触发ADC啊????
如果在TIM2比较中断里面用 ADC_SoftwareStartConvCmd(ADC1, ENABLE);
则可以转换。
如果关闭TIM2中断,
同时
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;//定时器2 捕获事件触发转换
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
则无论如何不能触发ADC中断。
哪位大侠做过给提个醒
又测试了一下,
改成
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
只能完成一次启动转换,
这应该是比较事件启动ADC,和输不输出到管脚有啥关系??
奇怪啊????????
void ADC1_Init(void)
{
//ADC1时钟使能(第9位为1)
RCC->APB2ENR |= (1<<9);
//定义ADC1引脚PC0、PC1、PC2,即ADC1_10、ADC1_11、ADC1_12,设置为模拟输入模式
GPIOC->CRL &= 0xFFFFF000;
ADC1->CR2 = ADON; //唤醒ADC1
ADC1->CR1 = SCAN|JAUTO; //扫描模式
ADC1->CR2 |= (3<<12)|JEXTTRIG|CONT; //连续转换
//ADC采样时间设置,通道10,11。采样时间为7.5周期(0-2为001,3-5为001)
// ADC1->SMPR1 = (1<<0) | (1<<3);
// ADC1->SQR1 = (2<<20);
ADC1->SQR3 = (12<<0); //规则通道只有通道12
//AD注入序列寄存器:长度为3(20-21为10),第1个转换为10(10-14为01010),第2个转换为11(15-19为01011),第3个转换为12(15-19为01011)
ADC1->JSQR = (10<<10)|(11<<15)|(1<<20);
// ADC1->CR2 |= ADON; //启动ADC1
ADC1->CR2 |= (1<<3);
//等待校准复位完成
while(ADC1->CR2 & (1<<3))
{}
//开始校准,第2位设置为1
ADC1->CR2 |= (1<<2);
//等待校准完成
while(ADC1->CR2 & (1<<2))
{}
ADC1->CR2 |= (1<<15);
//开启ADC1和ADC2全局中断(第18位)
NVIC->ISER[0] |= (1<<18);
}
void TIM2_Init(void)
{
RCC->APB1ENR |= (1<<0);//定时器3时钟使能(第1位为1)
//TIM3的重映射设置:定时器3没有重映像(第10、11位为00)
// AFIO->MAPR &= ~(3<<10);
//设置NVIC
// NVIC->ISER[0] |= (1<<28);//开启TIM3全局中断(第29位为1)
//定义TIME3控制寄存器1:第5-6位设置为00,表示边沿对齐模式;第7位为0,表示TIMx_ARR寄存器没有缓冲;第8-9位设置为00表示时钟分频因子为1
//第1位为1,禁止UEV,不产生更新事件;第2位为0;第4位设置为0表示计数器向上计数;
TIM2->ARR = 10;//自动重装载寄存器周期的值(定时时间)
TIM2->PSC = 71;//时钟频率 = 72/(时钟预分频+1)
// TIM2->CCMR1 = 1<<12;
// TIM2->CCER = (1<<4);
TIM2->CR1 = (1<<0);
//定义TIME3自动重装载寄存器,预分频器;
}