int main(void)
{
u8 t=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
uart_init(9600);
TIM2_Configuration();
ADC_DMA_Config((u32)&AD_Value);
Adc_Init() ;
while(1){
volta =(float) AD_Value/4095*3.3;
printf("AD=%d
",AD_Value); printf("
volta1 = %f V
",volta);
delay_ms(8);
}
}
主程序
void ADC_DMA_Config(u32 cmar)
{
DMA_InitTypeDef DMA_InitStructure; //DMAμĽá11ìå
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//ê1ÄüDMAê±Öó
DMA_DeInit(DMA1_Channel1);//ê1Äüí¨μà1
DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t)0x4001244C);//DMA¶Ôó|μÄADC1μÄμØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //ÄúéèμØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMAμÄ·½Ïò£¬íaéèμ½Äúéè
DMA_InitStructure.DMA_BufferSize = 1;//DMA»o′æ
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //½óêÕoóμØÖ·2»μYÔö
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //μØÖ·μYÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//êy¾Y¿í¶èÎa16
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMAμÄêy¾Y3ß′çÎa16
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);//ê1Äü′«êäÖD¶Ï
}
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1)!=RESET)
{
DMA_ClearITPendingBit(DMA1_IT_TC1);
}
}
DMA的设置
void Adc_Init(void)
{
//
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //
ADC_InitStructure.ADC_ScanConvMode =DISABLE;//
ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;//
//òòÎaÿ′Î′¥·¢òaóDTIM2μÄÖD¶Ï′¥·¢
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA, ENABLE); // ê1ÄüADC1oíGPIOA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //
GPIO_Init(GPIOA, &GPIO_InitStructure); //GPIOA
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1,ADC_SampleTime_1Cycles5);
ADC_ExternalTrigConvCmd(ADC1, ENABLE);//
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE); //
ADC_ResetCalibration(ADC1); //
while(ADC_GetResetCalibrationStatus(ADC1)); //
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
TIM_Cmd(TIM2, ENABLE);//
DMA_Cmd(DMA1_Channel1, ENABLE);/
}
ADC的设置
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
TIM_TimeBaseStructure.TIM_Period = 7999;//éèÖñè½ÏÖüÆú
TIM_TimeBaseStructure.TIM_Prescaler = 71;//72MμÄÖ÷Æ죬Õaàï·ÖÆμÎa71
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse = 1000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//PWM1òaÎaLOW,PWM2ÔòÎaHIGH
TIM_OC2Init(TIM2, & TIM_OCInitStructure);
// TIM_InternalClockConfig(TIM2);
// TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
// TIM_UpdateDisableConfig(TIM2, DISABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel =DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
定时器的设置
就是串口助手那里始终为0 ,没有真正的电压
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>