脉冲信号测量stm32单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "timer.h"
- #include "lcd.h"
- #include "usart.h"
- #include "adc.h"
- u32 Cnt=0;
- u32 fre=0;
- u8 TIM3CH1_CAPTURE_STA=0; //输入捕获状态
- u16 TIM3CH1_CAPTURE_VAL; //输入捕获值
- u16 IC2Value,DutyCycle; //输入捕获值
- void show(void);
- void dis_fre(void);
- void dis_zkb(void);
- int main(void)
- {
- u16 adcx;
- float v;
- u8 dat[7];
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- delay_init();
- uart_init(9600);
- Adc_Init();
- LCD_Init();
- POINT_COLOR=BLUE;
- LED_Init();
- TIM1_PWM_Init(899,0); //不分频。PWM频率=72000/(899+1)=80Khz
- TIM2_Cap_Init(0xFFFF,0x00);
- TIM3_Cap_Init();
- show();
- while(1)
- {
- TIM2->CR1 |= 1 << 0;
- delay_ms(1000);
- TIM2->CR1 &= ~(1<<0);
- fre = Cnt*10*65535+TIM2->CNT*10;
- Cnt=0;
- TIM2->CNT=0; //PA0频率输入
- dis_fre();
-
- dis_zkb();
- adcx=Get_Adc(ADC_Channel_1); //幅度采样PA1
- v=(float)adcx*(3.3/4096);
- adcx=(u16)(v*100);
- if(adcx>0)
- {
- dat[0]=adcx/100+0x30;
- dat[1]='.';
- dat[2]=adcx%100/10+0x30;
- dat[3]=adcx%10+0x30;
- dat[4]='V';
- dat[5]='\0';
- }
- LCD_ShowString(54,145,200,16,16,dat);
- }
- }
- void TIM2_IRQHandler(void) //测频率,PA0
- {
- if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- Cnt++;
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- }
- }
- void TIM3_IRQHandler(void) //占空比,PA7
- {
- if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
- {
- if(TIM_GetCapture1(TIM3)==65535)//已经捕获到高电平了
- {
- TIM3CH1_CAPTURE_VAL=65535;
- }
-
- }
- if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
- {
- IC2Value = TIM_GetCapture2(TIM3);
- if(IC2Value!=0)
- {
- DutyCycle=(TIM_GetCapture1(TIM3)*10000)/IC2Value;
- }
- else
- {
- DutyCycle = 0;
- }
- }
- TIM_ClearITPendingBit(TIM3, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
- }
- void show(void)
- {
- Test_Show_CH_Font16(15,80,6,BLACK);
- Test_Show_CH_Font16(31,80,7,BLACK);
- LCD_ShowChar(47,80,':',16,0);
- Test_Show_CH_Font16(15,100,3,BLACK);
- Test_Show_CH_Font16(31,100,4,BLACK);
- Test_Show_CH_Font16(47,100,5,BLACK);
- LCD_ShowChar(63,100,':',16,0);
- }
- void dis_fre(void)
- { u8 dat1[12];
- dat1[0]=fre/10000000+0x30;
- if(dat1[0]==0x30)dat1[0]=' ';
- dat1[1]=fre%10000000/1000000+0x30;
- if(dat1[0]==' '&&dat1[1]==0x30)dat1[1]=' ';
- dat1[2]=fre%1000000/100000+0x30;
- if(dat1[2]==0x30&&dat1[1]==' ')dat1[2]=' ';
- dat1[3]=fre%100000/10000+0x30;
- if(dat1[3]==0x30&&dat1[2]==' ')dat1[3]=' ';
- dat1[4]=fre%10000/1000+0x30;
- if(dat1[4]==0x30&&dat1[3]==' ')dat1[4]=' ';
- dat1[5]=fre%1000/100+0x30;
- if(dat1[5]==0x30&&dat1[4]==' ')dat1[5]=' ';
- dat1[6]=fre%100/10+0x30;
- dat1[7]='.';
- dat1[8]=fre%10+0x30;
- dat1[9]='H';
- dat1[10]='z';
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
脉冲信号测量仪.rar
(342.33 KB, 下载次数: 50)
|