如何通过调节电位器,用来控制pwm占空比
时间:10-02
整理:3721RD
点击:
1、 RT1是色温调节3200K/5600K转换电位器外接PD5的AD口
2、 RT2是亮度调节电位器外接PD6的AD口
3、 PC6、PC7是PWM输出32khz的频率,根据检测到PD5/PD6的电压调节占空比,达到调光的效果.. 单颗MOS管驱动52颗3258的灯珠、两串26并,
刚开始学习单片,用电位器调节占空比,可是PC6、PC7之间无法输出占空比,以下是我的代码,麻烦各位大神帮忙看一下,
/*端口初始化*/
void Init_ADC_GPIO(void)
{
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);//PD5
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);//PD6
GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_OUT_PP_LOW_SLOW); //PC6低速推挽输出高电平
GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_OUT_PP_LOW_SLOW);//PC7低速推挽输出高电平
}
/*ADC初始化*/
void ADC_Init(void)
{
Init_ADC_GPIO();
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_5, ADC1_PRESSEL_FCPU_D18, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, DISABLE);
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D18, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, DISABLE);
/*开启ADC1控制器*/
ADC1_Cmd(ENABLE);
/*开始进行ADC1模数转换*/
ADC1_StartConversion();
}
/*定时器的设置*/
void TIM_Init(void)
{
TIM1_DeInit(); //定时器1复位
TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_UP, 1000, 0);//定时器1时基初始化
/* Channel 5 PWM configuration */
TIM1_OC1Init(TIM1_OCMODE_PWM1,TIM1_OUTPUTSTATE_ENABLE,TIM1_OUTPUTNSTATE_ENABLE, u16_adc1_value,TIM1_OCPOLARITY_HIGH,TIM1_OCNPOLARITY_HIGH,TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_SET);
TIM1_OC1PreloadConfig(ENABLE);
/* Channel 6 PWM configuration */
TIM1_OC2Init(TIM1_OCMODE_PWM1,TIM1_OUTPUTSTATE_ENABLE,TIM1_OUTPUTNSTATE_ENABLE, u16_adc2_value,TIM1_OCPOLARITY_HIGH,TIM1_OCNPOLARITY_HIGH,TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_SET);
TIM1_OC2PreloadConfig(ENABLE);
TIM1_PrescalerConfig(1, TIM1_PSCRELOADMODE_UPDATE);
TIM1_ARRPreloadConfig(ENABLE); //自动加载使能
TIM1_Cmd(ENABLE); //启动定时器1,开始计数
}
/*AD转换值的获取*/
uint16_t run(void)
{
int j=0;
uint16_t chanel;
// ADC on
ADC1_Cmd(ENABLE); //ADC1->CR1 |= 0x01;//ADC使能
// delay for ADC stabilization 7us
for (j = 40;j > 0;j--);//等待7us
// start conversion
ADC1_StartConversion(); //ADC1->CR1 |= 0x01; //开始转换
//设置TIM1捕捉比较寄存器1的值
TIM1_SetCompare1(u16_adc1_value);
//设置TIM1捕捉比较寄存器2的值
TIM1_SetCompare2(u16_adc2_value);
// wait for end of conversion
if(chanel==ADC1_CHANNEL_5)
{
while (!(ADC1->CSR & 0x80)); //等待转换结束
ADC1->CSR &= ~0x80; //清除转换结束标志
u16_adc1_value =((((unsigned int)ADC1->DRH)CCR1H =0x00;
TIM1->CCR2H =0x00;
TIM1->CCR1L =(unsigned char)(u16_adc1_value>>2);//结果由PD6口送出至LED
}
if(chanel==ADC1_CHANNEL_6)
{
while (!(ADC1->CSR & 0x80));//等待转换结束
ADC1->CSR &= ~0x80; //清除转换结束标志
u16_adc2_value =((((unsigned int)ADC1->DRH)CCR1H =0x00;
TIM1->CCR2H =0x00;
TIM1->CCR2L =(unsigned char)(u16_adc2_value>>2);//结果由PD7口送出至LED
}
ADC1_ClearITPendingBit((ADC1_IT_TypeDef)ADC1_IT_EOC);
return u16_adc1_value,u16_adc2_value;
}
*************************************************/
void CLK_init(void)/*时钟初始化,2分频*/
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIdiv2);
}
void init_devices(void)
{
_asm("sim"); //关闭全局中断
CLK_init(); //时钟初始化
//PWM_Init();
TIM_Init(); //定时器设置
Init_ADC_GPIO();//ADC端口初始化
ADC_Init(); //ADC初始化
run();
_asm("rim"); //开启全局中断
}
void main(void)
{
while(1)
{
run();
if(chanel==ADC1_CHANNEL_5)
{
// 下面的循环将占空比逐渐从0递增到50%
for(i=0;iCCR1H = 0;
TIM1->CCR1L = I;
DelayMs(5);
}
// 下面的循环将占空比逐渐从50%递减到0
for(i=128;i>0;i--)
{
//PC7输出占空比
TIM1->CCR2H = 0;
TIM1->CCR2L = I;
DelayMs(5);
}
}
if(chanel==ADC1_CHANNEL_6)
{
// 下面的循环将占空比逐渐从0%递减到50
for(i=0;iCCR2H = 0;
TIM1->CCR2L = I;
DelayMs(5);
}
// 下面的循环将占空比逐渐从50%递减到0
for(i=128;i>0;i--)
{
//PC6输出占空比
TIM1->CCR1H = 0;
TIM1->CCR1L = I;
DelayMs(5);
}
}
}
}
你的模式设置的不对把
不错
读ad设置,,,