TI中文支持网
TI专业的中文技术问题搜集分享网站

想求一个输入捕获的程序,可以捕获到高电平时间,进而求得频率

下面是我写的程序,时间好像不对,请大佬们帮忙看看,我想测频率(方波+正弦)

main.c

#include "ALL.h"
#include "MY_timer.h"

char s[50];
extern double freq,temp;

int main(void)
{
ROM_FPULazyStackingEnable();
ROM_SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3);
ConfigureUART_0(115200,1);
Uartx_printf(UART0_BASE,"Hello, world!\n");
TIMER0_init(0xffff);
while(1)
{
sprintf(s,"freq:%.4f\r\n",freq);
Uartx_printf(UART0_BASE,s);
delay_ms(40);
}
}

MY_timer.c

#include "ALL.h"
//GPTM
#include "timer.h"
#include "MY_timer.h"
#include <math.h>

double time,freq;
void TIMER0_interrupt(void)
{
u32 str;// u8 flag=0;
// u32 t[12],i=0; str=ROM_TimerIntStatus(WTIMER0_BASE,true);//屏蔽中断状态 if(str==TIMER_CAPA_EVENT)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
time=TimerValueGet(WTIMER0_BASE,TIMER_A) ;
freq=1000000.0/time;
// t[i]=ROM_TimerValueGet64(WTIMER0_BASE);
// i++;
// time=labs(t[1]-t[0]);
// if(i==2) i=0;
// flag++;
// if(flag==3) f=40000000.0/time;
}
ROM_TimerIntClear(WTIMER0_BASE, TIMER_CAPA_EVENT);}

void TIMER0_init(uint32_t freq)
{
uint32_t Load;
Load=SysCtlClockGet()/freq – 1;
if(Load<=0xffffffff)
{
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER0);
ROM_TimerLoadSet(WTIMER0_BASE,TIMER_A,Load);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); ROM_GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4);
ROM_GPIOPinConfigure(GPIO_PC4_WT0CCP0); //#define GPIO_PC4_WT0CCP0 0x00021007
ROM_GPIOPinTypeTimer(GPIO_PORTC_BASE, GPIO_PIN_4);
ROM_TimerConfigure(WTIMER0_BASE,TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_SPLIT_PAIR);//计时捕获模式,上升沿捕获
ROM_TimerControlEvent(WTIMER0_BASE,TIMER_A,TIMER_EVENT_POS_EDGE);
ROM_TimerIntEnable(WTIMER0_BASE,TIMER_CAPA_EVENT); //定时器A捕获事件触发中断
IntRegister(INT_WTIMER0A ,TIMER0_interrupt);//注册中断函数
IntEnable(INT_WTIMER0A); //使能TIMER0A
IntMasterEnable(); //master interrupt enable API for all interrupts ROM_TimerEnable(WTIMER0_BASE, TIMER_A); //TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断
}
}

Susan Yang:

请问您现在具体使用的是哪个型号呢?

user6024197:

回复 Susan Yang:

TM4C123GH6PMI

赞(0)
未经允许不得转载:TI中文支持网 » 想求一个输入捕获的程序,可以捕获到高电平时间,进而求得频率
分享到: 更多 (0)