我对原子哥的历程中的PWM输出实验和ADC实验进行了改动和组合 首先不加反馈 是可以进行pwm输出和ADC测量显示的
[mw_shl_code=c,true]int main(void)
{
u16 adcx;
float led0pwmval=250;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//?è????????????????·?×é2
delay_init(168); //?????????±????
uart_init(115200); //???????®???¨??????115200
LED_Init(); //??????LED
LCD_Init(); //??????LCD????
Adc_Init(); //??????ADC
TIM14_PWM_Init (500-1,8-1);
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"ADC TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2014/5/6");
POINT_COLOR=BLUE;//?è??×?????????
LCD_ShowString(30,130,200,16,16,"ADC1_CH5_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH5_VOL:0.000V"); //???????¨??????????????
while(1)
{
adcx=Get_Adc_Average(ADC_Channel_5,20);//?????¨??5??×???????20???????ù
LCD_ShowxNum(134,130,adcx,4,16,0); //????ADCC???ù?ó????????
temp=(float)adcx*(3.3/4096); //?????????ó??????????????????????±???3.1111
adcx=temp; //??????????·???adcx±??????ò??adcx??u16????
LCD_ShowxNum(134,150,adcx,1,16,0); //??????????????????·???3.1111???°??????????????3
temp-=adcx; //°?????????????????·?????????????????·???±???3.1111-3=0.1111
temp*=1000; //??????·?????1000????????0.1111??×?????111.1???à?±??±?????????????
LCD_ShowxNum(150,150,temp,3,16,0X80); //??????????·??¨?°??×????????????????©????????????????111.
LED0=!LED0;
TIM_SetCompare1(TIM14,led0pwmval);
delay_ms(10);
}
}
[/mw_shl_code]
然后我的想法是 首先如上的代码是输出一个21KHZ,占空比50%的PWM波,然后我会在程序中设置一个我需要输出的电压值,根据ADC采集的当前输出电压来和当前这50%占空比来计算出当前的输入电压,之后根据当前的输入电压和理想输出电压来计算我需要的占空比来改变led0pwmval来改变PWM的占空比。 代码如下
[mw_shl_code=c,true]int main(void)
{
u16 adcx;
float led0pwmval=250;
float temp;
float Voutideal=3;
float zkb;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//?è????????????????·?×é2
delay_init(168); //?????????±????
uart_init(115200); //???????®???¨??????115200
LED_Init(); //??????LED
LCD_Init(); //??????LCD????
Adc_Init(); //??????ADC
TIM14_PWM_Init (500-1,8-1);
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"ADC TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2014/5/6");
POINT_COLOR=BLUE;//?è??×?????????
LCD_ShowString(30,130,200,16,16,"ADC1_CH5_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH5_VOL:0.000V"); //???????¨??????????????
while(1)
{
adcx=Get_Adc_Average(ADC_Channel_5,20);//?????¨??5??×???????20???????ù
LCD_ShowxNum(134,130,adcx,4,16,0); //????ADCC???ù?ó????????
temp=(float)adcx*(3.3/4096); //?????????ó??????????????????????±???3.1111
adcx=temp; //??????????·???adcx±??????ò??adcx??u16????
LCD_ShowxNum(134,150,adcx,1,16,0); //??????????????????·???3.1111???°??????????????3
temp-=adcx; //°?????????????????·?????????????????·???±???3.1111-3=0.1111
temp*=1000; //??????·?????1000????????0.1111??×?????111.1???à?±??±?????????????
LCD_ShowxNum(150,150,temp,3,16,0X80); //??????????·??¨?°??×????????????????©????????????????111.
LED0=!LED0;
TIM_SetCompare1(TIM14,led0pwmval);
zkb=(500-led0pwmval)/500;
led0pwmval=500-((500*Voutideal*zkb)/temp);
delay_ms(10);
}
}
[/mw_shl_code]
即为我想要的输出为3V,来改变PWM的占空比,但是改完之后可以读取ADC,但PWM不输出了,不知道错误出在哪里,希望大家可以帮助我一下。
我是为了做大学生电子竞赛来学习的这些知识,目前还是小白一个,所以希望大家能说的简单一些,让我这个小白尽量理解,谢谢了。 |