这是根据输入捕获试验改的,可以实现方波测频并且根据测量值输出100倍频的pwm波,按键可控制*1,*3,*5的关系。
项目用来做电赛的滤波芯片的时钟信号的,测量基波频率并用单片机输出芯片的时钟来测量基波,3次谐波和5次谐波的幅值。
源代码注释很详细,电赛2017年自适应滤波器的发挥部分。
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "timer.h"
- #include "usart.h"
- #include "lcd.h"
- #include "adc.h"
- #include "key.h"
- extern u8 TIM2CH1_CAPTURE_STA;
- extern u16 TIM2CH1_CAPTURE_VAL
- int main(void)
- {
- static int t=1;
- u32 temp=0;
- u32 freq,cop,freq1,Ccr,psc1;
- float templ;
- u16 adcx,adcx1,adcdip,adccop=0;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)
- delay_init();
- uart_init(9600);
- LCD_Init();
- Adc_Init();
- KEY_Init();
- TIM1_PWM_Init(65535,71);
- TIM2_Cap_Init(0xFFFF,72-1);
- POINT_COLOR=RED
- while(1)
- {
-
- if(TIM2CH1_CAPTURE_STA&0X80
- {
- temp=TIM2CH1_CAPTURE_STA&0X3F;
- temp*=65536;
- temp+=TIM2CH1_CAPTURE_VAL;
- freq=1000000/temp;
- TIM2CH1_CAPTURE_STA=0;
- }
-
- if((freq>=49)&&(freq<=200))
- {
- cop=freq;
- }
-
- if(KEY_Scan(1)==WKUP_PRES)
- {
- t=1;
- adcx=Get_Adc_Average(ADC_Channel_1,10);
- if(adccop<=adcx)
- {
- adccop=adcx;
- adcdip=adccop;
- }else
- adcdip=adccop;
- templ=(float)adcdip*(3.3/4096);
- adcx1=templ;
- templ-=adcx1;
- templ*=1000;
- delay_ms(250);
- }
- if(KEY_Scan(1)==KEY1_PRES)
- {
- t=3;
- adcx=Get_Adc_Average(ADC_Channel_1,10);
- if(adccop<=adcx)
- {
- adccop=adcx;
- adcdip=adccop;
- }else
- adcdip=adccop;
- templ=(float)adcdip*(3.3/4096);
- adcx1=templ;
- templ-=adcx1;
- templ*=1000;
- delay_ms(250);
- }
- if(KEY_Scan(1)==KEY0_PRES)
- {
- t=5;
- adcx=Get_Adc_Average(ADC_Channel_1,10);
- if(adccop<=adcx)
- {
- adccop=adcx;
- adcdip=adccop;
- }else
- adcdip=adccop;
- templ=(float)adcdip*(3.3/4096);
- adcx1=templ;
- templ-=adcx1;
- templ*=1000;
- delay_ms(250);
- }
- delay_ms(300);¨
- freq1=(10000/cop/t)-1;
- Ccr=(5000/cop/t)-1;
- TIM_SetAutoreload(TIM1,freq1)
- TIM_SetCompare1(TIM1,Ccr);
- delay_ms(200);
-
- printf("Frequency:%d HZ\r\n",cop);
- LCD_ShowString(60,60,200,16,16,"Frequency: HZ");
- LCD_ShowString(60,80,200,16,16,"Vrms:0.000 V");
- LCD_ShowString(60,100,200,16,16,"ARR: ");
- LCD_ShowxNum(140,100,freq1,4,16,0);
- LCD_ShowxNum(140,60,cop,4,16,0);
- LCD_ShowxNum(116,80,templ,3,16,0x80);
- LCD_ShowxNum(100,80,adcx1,1,16,0);
- delay_ms(500);
- LCD_Clear(WHITE);
-
- }
- }
复制代码
全部程序51hei下载地址:
测方波频率并输出100倍频.7z
(229.65 KB, 下载次数: 45)
|