代码补上:
#include "stm32f0xx.h"
#include "stdio.h"
#define ADC1_DR_Address 0x40012440
__IO uint16_t RegularConvData_Tab;
uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0;
void ADC1_DMA_Init(void);
void GPIOA_Init(void);
void TIM1_PWM_Init(void);
int main(void)
{
ADC1_DMA_Init();
GPIOA_Init();
TIM1_PWM_Init();
while (1)
{
Channel1Pulse = (uint16_t) (RegularConvData_Tab* (TimerPeriod - 1) / 4096);
Channel2Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
printf("%d",Channel1Pulse);
printf("%d",Channel2Pulse);
}
}
void ADC1_DMA_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ADC_DeInit(ADC1);
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* Configure PA.01 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure); // PC1,ê?è?ê±2?ó?éè???ù?ê
/* DMA1 Channel1 Config */
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; //????íaéèμ??·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab; // ?????ú′?ó3é?μ??·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // ???¨íaéèμ??′?ò????μ?μ?
DMA_InitStructure.DMA_BufferSize = 2; // DMA ?o3?éè?? //?o3??a2
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //éè??íaéèμ??·ê?·????ó?ò2????ó //1?íaéèμ??·??êy
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // éè???ú′?μ??·ê?·????ó?ò2????ó //1??ú′?μ??·??êy
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //íaéèêy?Y?í?èéè??
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //?ú′?êy?Y?í?èéè?? //°?×??ú
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // DMA ?£ê? //?-?·?£ê?
DMA_InitStructure.DMA_Priority = DMA_Priority_High; // DMA ó??è?? //??ó??è??
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // memory-to-memory ′?ê??£ê?éè?? //1??ú′?μ??ú′?
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE); //?????μμà //?μμàê1?ü
/* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); //????DMA ?-?·?£ê?
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Initialize ADC structure */ /* 3?ê?ADC ????*/
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // ????ADC μ?×a?ˉ·?±??ê /* ????ADC1 ?úá?D??£ê???·?±??ê?a12 bits */
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // ????????á?D?2é?ù?òμ¥′?2é?ù
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // ADC ?ú2?±??μ′¥·¢
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // éè??ADC ê?×ó?????ò??óò????
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward;
ADC_Init(ADC1, &ADC_InitStructure);
// /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */
// ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);
// ADC_TempSensorCmd(ENABLE);
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_55_5Cycles);
// ADC_VrefintCmd(ENABLE);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1); /* ADC ?ì?è*/
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); /* ê1?üADC1 */
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* μè′yADCEN ±ê??*/
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1); /* ADC1 ?¨?ú±???*/
}
void GPIOA_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* ê1?üGPIO ê±?ó*/
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
/* ????GPIO 8?¢9 1ü??2?êyéè??*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIO 1ü???′ó?éè??*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
}
void TIM1_PWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM1 ê±?óê1?ü*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
TimerPeriod = (SystemCoreClock / 17570 ) - 1;
/* Time ?¨ê±?ù′?éè??*/
TIM_TimeBaseStructure.TIM_Prescaler = 0; /*!???¨ó?à′??·?TIM ê±?ó?¤·??μ?μ*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /*!???¨μ???êy?÷?£ê?*/
/* Time ?¨ê±éè???aé?éy???????£ê?*/
TIM_TimeBaseStructure.TIM_Period = TimerPeriod; /*éè??ê±?ó?ü?ú*/
TIM_TimeBaseStructure.TIM_ClockDivision = 0; /*éè?¨ê±?ó·??μ*/
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; /*???¨???′??êy?÷?μ*/
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* ?μμà1μ?PWM ?£ê?éè??*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; /*!???¨μ?TIM ?£ê?*/
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /*???¨μ?TIM ê?3?±è??×′ì?*/
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; /*???¨TIM ?¥21μ?ê?3?±è??×′ì?. */
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; /*???¨μ???3??μ±?×°è?μ?2?×?±è????′??÷*/
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; /*???¨μ??¥21ê?3???D?*/
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; /*???¨?ú???D×′ì???μ?TIM ê?3?±è??òy??μ?×′ì?*/
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; /*???¨?ú???D×′ì???μ??¥21TIM ê?3?±è??òy??μ?×′ì?. */
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; //ê1?ü?μμà1 ???? /*???¨μ???3??μ±?×°è?μ?2???±è????′??÷*/
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //ê1?ü?μμà2 ???? /*???¨μ???3??μ±?×°è?μ?2???±è????′??÷*/
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/* TIM1 ?????÷ê1?ü*/
TIM_Cmd(TIM1, ENABLE);
/* TIM1 ?÷ê?3?ê1?ü*/
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t 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\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|