2021电子设计大赛周期信号检测和参数检测
自己用STM32写的
可以参考
单片机源程序如下:- #include "stdio.h"
- #include "main.h"
- #include "math.h"
- #include <ctype.h>
- #include "stm32f4xx.h"
- #include "input.h"
- #include "oscilloscope.h"
- #include "digital_multimeter.h"
- #include "stm32f4xx.h"
- #include "led.h"
- u16 buff[400];//存储ADC所采集到的波形数据
- float buf[400],buf1[400];
- float Vpp_p; //存储峰峰值
- long long indata=0;
- u8 Vpp_buff[20] = {0};//sprintf数据输出
- int str[20],c_1=0,str_max=0;
- //全局变量
- int key_val=0;
- int key_c=0;
- int key_d=0;
- float vpp;
- //函数声明
- //int flag=0;
- void Square_wave(void); //方波
- void Kai_display() //矩形
- {
- POINT_COLOR = RED;
- POINT_COLOR = 0x5510;
- LCD_Clear(BLACK);
- LCD_DrawRectangle(350,200,0,300);//矩形
-
- }
- void Kai_display_1() //矩形
- {
- POINT_COLOR = RED;
- POINT_COLOR = 0x5510;
- LCD_Clear(BLACK);
- LCD_DrawRectangle(350,0,0,200);//矩形
-
- }
- void Init(void){//初始化
- SysTick_Init(168);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组 分2组
- LED_Init();
- LCD_Init(); //LCD初始化
- KEY_Init();
- LCD_Clear(WHITE);//BLACK:黑
- Hwjs_Init();
- USART1_Init(115200);
- uart2_init(9600);
- Kai_display_1();//开机显示
- Adc_Init();//初始化ADC
- TIM3_Int_Init(10000-1,8400-1);
- extix_init();
- TIM5_CH1_Input_Init(0xffffffff,83); //以1M频率计数
- }
- int main()
- {
- int adcx,key=0,key_1=0,i=0;
- float temp;
- Kai_display_1();
- Init();//初始化
-
- BACK_COLOR= BLACK;
- POINT_COLOR = RED;
- LCD_DrawRectangle(350,200,0,350);//矩形
- while(1)
- {
- key = Hw_Scan();
- if(key == 1){key_val = 1;}
- if(key == 2){key_val = 2;}
- if(key == 3){key_val = 3;}
- if(key == 4){key_val = 4;}
- if(key == 5){key_val = 5;key_c =1;}
- if(key == 6){key_val = 6;}
- if(key == 7){key_val = 7;}
- if(key == 8){key_val = 8;}
- if(key == 9){key_val = 9;}
- if(key == 12){key_val = 3;key_d =2;}
- if(key == 13){key_val = 3;key_d =3;}
- if(key == 14){key_val = 3;key_d =4;}
- if(key == 15){key_val = 3;key_d =5;}
- if(key == 16){key_val = 3;key_d =6;}
- if(key == 17){key_val = 3;key_d =7;}
- if(key == 10){NVIC_SystemReset(); }
-
- if(key_val == 8)
- {
- adcx = Get_Adc_Average(ADC_Channel_5,200);
- temp = (float)adcx*(3.3/4096); //获取计算后的带小数的实际电压值,比如3.1111
- sprintf((char*)Vpp_buff,"V: %.2f V",temp);
- LCD_ShowString(10,290,210,16,16,Vpp_buff);
- delay_ms(500);
- }
- if(key_val == 3)
- {
- get_vpp_fb(buff);
- }
- if(key_val == 4)
- {
- get_vpp_fb(buff);
- sprintf((char*)Vpp_buff,"Vpp:%0.2f V",Vpp);
- LCD_ShowString(10,230,210,16,16,Vpp_buff);
- delay_ms(200);
- }
- if(key_val == 5)
- {
- get_vpp_fb(buff);
- str[c_1] = num1;
- c_1++;
- if(c_1 == 20)
- {
- // for(i=0;i<20;i++)
- // {
- // if(str[i] > str_max)
- // str_max = str[i];
- // }
- // printf("%d ",str_max);
- // if(0.8<(freq_1/1000) <1.2 )
- // {
- // if(str_max == 9){key_d =2;}
- // if( str_max>=7 && str_max<=8){key_d =4;}
- // if( str_max>=4 && str_max<=5){key_d =3;}
- // }
- c_1 = 0;
- str_max = 0;
- }
- }
- if(key_val == 7) //全部显示
- {
- key_c =1;
- LCD_ShowString(10,250,210,16,16,"FREQ:");
- LCD_ShowString(10,270,210,16,16,"Dcycle:");
- LCD_ShowString(120,270,210,16,16,"%");
- get_vpp_fb(buff);
- sprintf((char*)Vpp_buff,"Vpp:%0.2f V",Vpp);
- LCD_ShowString(10,230,210,16,16,Vpp_buff);
- delay_ms(200);
- }
- if(key_val == 9)
- {
- get_vpp_fb(buff);
- sprintf((char*)Vpp_buff,"fudu: %.2f V",fudu);
- LCD_ShowString(10,290,210,16,16,Vpp_buff);
- }
-
- if(TIM5_CH1_CAPTURE_STA&0x80) //成功捕获
- {
- indata=TIM5_CH1_CAPTURE_STA&0x3f;
- indata*=0xffffffff; //溢出次数乘以一次的计数次数时间 us
- indata+=TIM5_CH1_CAPTURE_VAL;//加上高电平捕获的时间
- TIM5_CH1_CAPTURE_STA=0; //开始下一次捕获
- }
- }
- }
- //----------------------------函数-----------------------//
复制代码
Keil代码下载:
周期波形信号识别和参数测量.7z
(484.61 KB, 下载次数: 51)
|