此程序仅供大家参考
Stm32_template_输入捕获最终
- #include "stm32f10x.h"
- #include "delay.h"
- #include "12864.h"
- #include "stdio.h"
- #include "usart.h"
- #include "sys.h"
- u8 TIM3CH1_CAPTURE_STA=0; //输入捕获状态
- //[7]:捕获两个上升沿
- //[6]:捕获到第一个上升沿
- //[5:0]:溢出计时器
- u32 TIM3CH1_CAPTURE_VAL; //输入捕获值
-
- void TIM2_PWMconf(u16 arr,u16 psc);
- void TIM3_Cap_Init(u16 arr,u16 psc);
- void MY_GPIO(void);
- /***********************main***************************
- 函数:main
- 描述:
- ******************************************************/
- u8 flag,flag2;
- int main(void)
- {
- u32 temp=0,freqdisplay=0;
- double freq,temp1;
- GPIO_InitTypeDef mygpio;
- delay_init();
- NVIC_Configuration();
- uart_init(9600);
- Lcd12864_init();
- lcd_clr();
- MY_GPIO();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- mygpio.GPIO_Mode=GPIO_Mode_Out_PP;
- mygpio.GPIO_Pin=GPIO_Pin_0;
- mygpio.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&mygpio);
- Display();
- TIM2_PWMconf(899,5555); //不分频。PWM频率=72000/(899+1)=80Khz
- TIM3_Cap_Init(0xffff,72-1); //以1Mhz的频率计数
- while(1)
- {
- //delay_ms(10);
- if(TIM3CH1_CAPTURE_STA&0X80) //成功捕获到了一次上升沿
- {
- temp1=TIM3CH1_CAPTURE_STA&0X3F;
- printf("周期0:%f us\r\n",temp1);
- //freq=temp1;
- temp1*=65536; //溢出时间总和
- //printf("周期1:%f us\r\n",temp1);
- //temp1+=2.5;
- temp1+=TIM3CH1_CAPTURE_VAL; //得到总的高电平时间
- //printf("周期2:%d us\r\n",TIM3CH1_CAPTURE_VAL);
- //printf("周期3:%f us\r\n",temp1);
- //temp1*=1000;
- temp=temp1;
- freq=1000000.0/temp;
- addr_choose(2,0);
- Write_data_12864(0x30+temp/100000);
- Write_data_12864(0x30+temp%100000/10000);
- Write_data_12864(0x30+temp%10000/1000);
- Write_data_12864(0x2e);
- Write_data_12864(0x30+temp%1000/100);
- Write_data_12864(0x30+temp%100/10);
- // Write_data_12864(0x30+temp%10000/1000);
- // Write_data_12864(0x30+temp%100/10);
- Write_data_12864(0x30+temp%10);
- //printf("频率:%f us\r\n",freq); //打印总的高点平时间
- // freqdisplay=freq;
- // addr_choose(4,0);
- // Write_data_12864(0x30+freqdisplay/100000);
- // Write_data_12864(0x30+freqdisplay%100000/10000);
- // Write_data_12864(0x30+freqdisplay%10000/1000);
- // Write_data_12864(0x2e);
- // Write_data_12864(0x30+freqdisplay%1000/100);
- // Write_data_12864(0x30+freqdisplay%100/10);
- // Write_data_12864(0x30+freqdisplay%10);
- freqdisplay=freq*1000;
- addr_choose(4,0);
- Write_data_12864(0x30+freqdisplay/100000000);
- Write_data_12864(0x30+freqdisplay%100000000/10000000);
- Write_data_12864(0x30+freqdisplay%10000000/1000000);
- Write_data_12864(0x2e);
- Write_data_12864(0x30+freqdisplay%1000000/100000);
- Write_data_12864(0x30+freqdisplay%100000/10000);
- Write_data_12864(0x30+freqdisplay%10000/1000);
- Write_data_12864(0x30+freqdisplay%1000/100);
- Write_data_12864(0x30+freqdisplay%100/10);
- Write_data_12864(0x30+freqdisplay%10);
- TIM3CH1_CAPTURE_STA=0; //开启下一次捕获
- }
- }
- }
- void MY_GPIO(void)
- {
- GPIO_InitTypeDef MY_GPIO;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
-
- MY_GPIO.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_0;
- MY_GPIO.GPIO_Mode=GPIO_Mode_Out_PP;
- MY_GPIO.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&MY_GPIO);
- }
- void TIM2_PWMconf(u16 arr,u16 psc)
- {
- GPIO_InitTypeDef mygpio;
- TIM_TimeBaseInitTypeDef mytimebase;
- TIM_OCInitTypeDef mytimeOC;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器2时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //使能AFIO复用功能模块时钟
-
- mygpio.GPIO_Mode=GPIO_Mode_AF_PP;//复用输出
- mygpio.GPIO_Pin=GPIO_Pin_1;//CH2
- mygpio.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&mygpio);
-
- //初始化TIM2
- mytimebase.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
- mytimebase.TIM_Period=arr;
- mytimebase.TIM_Prescaler=psc;
- mytimebase.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInit(TIM2,&mytimebase);
- //初始化TIM2 Channel2 PWM模式
-
- mytimeOC.TIM_OCMode=TIM_OCMode_PWM1;//pwm1 模式
- mytimeOC.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性高
- mytimeOC.TIM_OutputState=TIM_OutputState_Enable; //比较输出使能
- TIM_OC2Init(TIM2,&mytimeOC);// TIM2 OC2初始化
-
- TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR2上的预装载寄存器
-
- TIM_Cmd(TIM2,ENABLE);//使能TIM2
-
- TIM_SetCompare2(TIM2,450);//CCR2值
- }
- void TIM3_Cap_Init(u16 arr,u16 psc)
- {
- GPIO_InitTypeDef mygpio;
- TIM_TimeBaseInitTypeDef mytimebase;
- TIM_ICInitTypeDef myic;
- NVIC_InitTypeDef mynvic;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
-
- mygpio.GPIO_Mode=GPIO_Mode_IPD; //设置成下拉输入 空闲时电平
- mygpio.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init (GPIOA,&mygpio);
- GPIO_ResetBits(GPIOA,GPIO_Pin_6);
- //初始化 定时器3 TIM3
- mytimebase.TIM_Period=arr;
- mytimebase.TIM_Prescaler=psc;
- mytimebase.TIM_CounterMode=TIM_CounterMode_Up;
- mytimebase.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInit(TIM3,&mytimebase);
-
- //初始化TIM3 输入捕获参数
- myic.TIM_Channel=TIM_Channel_1;
- myic.TIM_ICPolarity=TIM_ICPolarity_Rising;
- myic.TIM_ICSelection=TIM_ICSelection_DirectTI;
- myic.TIM_ICPrescaler=TIM_ICPSC_DIV1;
- myic.TIM_ICFilter=0x00;
- TIM_ICInit(TIM3,&myic);
-
- //中断分组初始化
- mynvic.NVIC_IRQChannel=TIM3_IRQn ;
- mynvic.NVIC_IRQChannelPreemptionPriority=2;
- mynvic.NVIC_IRQChannelSubPriority=0;
- mynvic.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&mynvic);
-
- TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM3,ENABLE);
-
-
- }
-
- void TIM3_IRQHandler(void)
- {
- if((TIM3CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
- {
- if(TIM3CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM3CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM3CH1_CAPTURE_VAL=0XFFFF;
- }
- else
- TIM3CH1_CAPTURE_STA++;
- }
- }
-
- if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
- {
- if(TIM3CH1_CAPTURE_STA&0X40) //捕获到一个上升沿
- {
- TIM3CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM3CH1_CAPTURE_VAL=TIM_GetCounter(TIM3);
- }
- else //还未开始,第一次捕获上升沿
- {
- TIM3CH1_CAPTURE_STA=0; //清空
- TIM3CH1_CAPTURE_VAL=0;
- TIM_SetCounter(TIM3,0);
- TIM3CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- }
- }
- }
- TIM_ClearITPendingBit(TIM3, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位
- }
复制代码
全部资料51hei下载地址:
测频率LCD12864.rar
(302.78 KB, 下载次数: 163)
|