为恒流源提供PWM的输入,通过分压采进来一个2.5V左右的电压,根据与2.5V的差值经过简单的计算来调节一个50KHz的PWM波形的占空比,应该是个挺简单的程序,但是我出来的效果并不好
效果大致就是,出来一段PWM波,然后出现一段时间的持续高电平,然后继续输出PWM波,也不知道是什么原因导致的,是因为PWM和我的AD部分存在冲突么?
下面是程序:
#include <stdio.h>
#include "
STM32f10x.h"
void Delay(void)
{
int x,y;
for(x=100;x>0;x--)
for(y=1000;y>0;y--);
}
void Init_
tiMER(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM4);
TIM_InternalClockConfig(TIM4);
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(TIM4, &TIM_BaseInitStructure);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, 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_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
void PWM_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | 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(GPIOB, &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 ADC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | RCC_APB2Periph_ADC1,ENABLE);
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 = ENABLE;
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_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
int main(void)
{
u16 Temp_Value;
u16 cha_0;
u16 cha_1;
uint16_t Pulse=749;
PWM_GPIO_Init();
key_Configuration();
ADC_Configuration();
Init_TIMER();
Init_PWM(Pulse);
while(1)
{
if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_11)==0)
{
while(1)
{
Temp_Value = ADC_GetConversionValue(ADC1);
if(Temp_Value>=3102)
{
cha_0=(int)((Temp_Value-3102)/74.5);
Pulse=(int)(Pulse+(cha_0*14.4));
TIM_SetCompare3(TIM4,Pulse);
}
if(Temp_Value<=3102)
{
cha_1=(int)((3102-Temp_Value)/74.5);
Pulse=(int)(Pulse-(cha_1*14.4));
TIM_SetCompare3(TIM4,Pulse);
}
}
}
}
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
强!
我用的是STM32F107它的AD部分时钟和103增强型相同,用的是默认时钟配置,默认应该是APB2是72Mhz,在ADC那里是2分频,我查的是给56Mhz时钟时速度最快,但是我不太清楚这个56M是怎么设置得到的,程序里用的采样时间是55.5cycles。
AD采集和PWM同步应该怎么做呢?是用DMA中断么?
控制频率应该怎么设置呢?
ADC最大时钟14M,分频系数分别为2468(复位值是二分频,也就是你AD超频了。转换值还准不准没试过)
应该调用一下RCC_ADCCLKConfig(RCC_PCLK2_Div6),给ADC的时钟变为12M。
同步就用定时器触发,控制频率也是用定时器实现
8M9倍频是72M,7倍频不就是56M了。。。ADC转换一次最少需要1.5+12.5个周期,最大时钟14M,所以是1uS。因为56M4分频刚好是14M,而72M要6分频是12M,所以56M时是1uS,72M反而是1.17uS了
大致明白了,我来试试,
谢谢啦
手动:滑稽
一周热门 更多>