STM32芯片PWM驱动CP7121实现高分辫率4~20mA恒流输出
本帖最后由 chedg 于 2024-11-6 08:36 编辑#申请原创#
硬件示意图:
调试方法:TIM_TimeBaseInitStructure.TIM_Period = 21700;TIM_OCInitStructure.TIM_Pulse = 21700;把上面两代码的值设为相等,此时输出的PWM是高电平(即100%占空比)。再看电流表此时的值,如果21.715mA。那么,你就改变 TIM_TimeBaseInitStructure.TIM_Period = 21715;此时你会发现,执行PWM_SetCompare1(4000); 输出的电流就是4mA;执行PWM_SetCompare1(20000); 输出的电流就是20mA;TIM_TimeBaseInitStructure.TIM_Period = 21715; 细调此值,便能输出分辫率为1uA的较精准电流值。
软件代码:Main函数代码-------------------------------------------------------------------------------------------------------int main(void){ PWM_Init(); while (1) { PWM_SetCompare1(4000); }}PWM初始化代码-------------------------------------------------------------------------#include "stm32f10x.h" void PWM_Init(void){ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_InternalClockConfig(TIM2); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 21700; //ARR TIM_TimeBaseInitStructure.TIM_Prescaler = 3; //PSC TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 4000; //CCR TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);}
void PWM_SetCompare1(uint16_t Compare){ TIM_SetCompare1(TIM2, Compare);}
兄弟 这个不能带载 不能带载?怎么理解
页:
[1]