为恒流源提供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;
}
强!
一周热门 更多>