在做一个恒流源,希望使用AD采集信号达到反馈,从而调整我的PWM输出的占空比,使用的是STM32F107的板子,但是出来的效果并不好,总是出现一小段PWM波然后就是一长段的持续高电平输出,(大致有30ms到70ms之间的持续时间吧),然后再接一小段PWM波的输出,并不断循环,不知道是什么原因
用的是ADC1和TIM1_CH1,不知道是可能存在冲突还是什么原因,后来又尝试了使用DMA传输,但是依然出不来效果。
麻烦各位了,能不能帮忙看一看。
下面是程序:
#include <stdio.h>
#include "stm32f10x.h"
//u32 DMAToBuf[50];
//u16 Data;
#define ADC1_DR_Address ((u32)0x4001244C)
//void Delay(void)
//{
// int x,y;
// for(x=100;x>0;x--)
// for(y=1000;y>0;y--);
//}
//void Delay(vu16 cnt)
//{
//u16 i,j;
//for (i=0;i<cnt;i++)
//{ for (j=0;j<100;j++)
// { }
// }
//}
//void RCC_Configuration(void)
//{
// ErrorStatus HSEStartUpStatus;
// RCC_HSEConfig(RCC_HSE_ON);
// HSEStartUpStatus = RCC_WaitForHSEStartUp();
// if(HSEStartUpStatus==SUCCESS)
// {
// RCC_HCLKConfig(RCC_SYSCLK_Div1);
// //PCLK1(APB1) = HCLK/2
// RCC_PCLK1Config(RCC_HCLK_Div2);
// //PCLK2(APB2) = HCLK
// RCC_PCLK2Config(RCC_HCLK_Div1);
//
//
// RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_4);
// RCC_PLLCmd(ENABLE);
// while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
// RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
// while(RCC_GetSYSCLKSource()!=0x08);
// }
// /* RCC system reset(for debug purpose) */
//}
void Init_TIMER(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1);
TIM_InternalClockConfig(TIM1);
TIM_BaseInitStructure.TIM_Period = 1440-1;
TIM_BaseInitStructure.TIM_Prescaler = 0;
TIM_BaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
void Init_PWM(uint16_t Dutyfactor)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = Dutyfactor;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
void PWM_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void key_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD ,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
//void DMA_Config()
//{
// DMA_InitTypeDef DMA_InitStructure;
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
//
// DMA_DeInit(DMA1_Channel1);
// DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;
// DMA_InitStructure.DMA_MemoryBaseAddr=(u32)DMAToBuf;
// DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
// DMA_InitStructure.DMA_BufferSize=50;
// DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
// DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
// DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
// DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
// 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);
// DMA_Cmd(DMA1_Channel1,ENABLE);
//
//}
void ADC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
//ADC_DMACmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
// void NVIC_Configuration(void)
//{
// NVIC_InitTypeDef NVIC_InitStructure;
//#ifdef VECT_TAB_RAM
// /* Set the Vector Table base location at 0x20000000 */
// NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
//#else /* VECT_TAB_FLASH */
// /* Set the Vector Table base location at 0x08000000 */
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//#endif
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//}
int main(void)
{
u16 Data;
u16 cha_0;
u16 cha_1;
u16 Pulse=749;
//RCC_Configuration();
PWM_GPIO_Init();
key_Configuration();
Init_TIMER();
Init_PWM(Pulse);
ADC_Configuration();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_11)==0)
{
//NVIC_Configuration();
// ADC_Configuration();
//DMA_Config();
while(1)
{
Data=ADC_GetConversionValue(ADC1);
if(Data>3102)
{
cha_0=(int)((Data-3102)/74.5);
Pulse=(int)(Pulse+(cha_0*14.4));
TIM_SetCompare1(TIM1,Pulse);
}
if(Data<3102)
{
cha_1=(int)((3102-Data)/74.5);
Pulse=(int)(Pulse-(cha_1*14.4));
TIM_SetCompare1(TIM1,Pulse);
}
if(Data==3102)
{
Pulse=749;
TIM_SetCompare1(TIM1,Pulse);
}
// Delay(205);
}
}
}
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
//void DMAChannel1_IRQHandler(void)
//{
// vu16 i;
// vu32 dat1=0;
// if(DMA_GetITStatus(DMA1_IT_TC1)==SET)
// {
//
// for(i=0;i<50;i++)
// {
// dat1=dat1+DMAToBuf;
// }
// Data=dat1/50;
// DMA_ClearITPendingBit(DMA1_IT_TC1);
// }
// else
// if(DMA_GetITStatus(DMA1_IT_HT1)==SET)
// {
// DMA_ClearITPendingBit(DMA1_IT_HT1);
// }
//}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>