This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

超声波测距 定时器TimerValueGet()问题



超声波测距,PF4采用外部中断双边沿触发方式,再外部中断函数中改变标志,在主函数读取定时器的两次value,目前问题,测试用的PF4接的一个按键,注释掉readValue = TimerValueGet(TIMER0_BASE, TIMER_A);按键按下松开都可以进入外部中断,但是加上那句后只能进一次,然后程序就死了,读出来有数据,有没有遇到类似问题的啊,或者有没有超声波例程 - -

lcd12864.zip
  • 程序附件在上边

  • /*
     * main.c
     */
    #include <stdio.h>
    #include <stdint.h>
    #include <stdbool.h>
    #include "driverlib/gpio.h"
    #include "driverlib/sysctl.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_ints.h"
    #include "LCDDriver.h"
    #include "inc/hw_gpio.h"
    #include "driverlib/timer.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"
    #include "driverlib/fpu.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom.h"
    
    //uint32_t g_ui32Flags;
    uint32_t g_ledFlag = 0;
    uint32_t g_uartFlag = 0;
    uint32_t ui32Period;
    uint32_t g_showFlag = 0;
    uint32_t readValue = 0;
    uint32_t g_readFlag = 0;
    
    void timer0Config();
    
    void timer0Config()
    {
    
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    	TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    	ui32Period = (SysCtlClockGet());
    	TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);
    	IntEnable(INT_TIMER0A);
    	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    	IntMasterEnable();
    	TimerEnable(TIMER0_BASE, TIMER_A);
    }
    void timer1Config()
    {
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
    	TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
    	ui32Period = (SysCtlClockGet()/10);
    	TimerLoadSet(TIMER1_BASE, TIMER_A, ui32Period -1);
    	IntEnable(INT_TIMER1A);
    	TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    	IntMasterEnable();
    	TimerEnable(TIMER1_BASE, TIMER_A);
    }
    
    void PortFIntHandler(void)
    {
    	//清除中断标志
    	GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_4);
    	//PF2低电平 关闭蓝色LED
    	//g_uartFlag = 1;
    
    	if(g_showFlag == 0)
    	{
    
    
    		g_showFlag = 1;
    	}
    	else if(g_showFlag == 1)
    	{
    		g_showFlag = 0;
    		g_readFlag = 1;
    
    	}
    
    }
    
    void
    Timer0IntHandler(void)
    {
    	// Clear the timer interrupt
    		TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    		// Read the current state of the GPIO pin and
    		// write back the opposite state
    		if(g_uartFlag == 0)
    		{
    			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
    			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
    			g_uartFlag = 1;
    		}
    		else if(g_uartFlag == 1)
    		{
    			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
    			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
    			g_uartFlag = 2;
    		}
    		else if(g_uartFlag == 2)
    		{
    			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
    			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
    			g_uartFlag = 0;
    		}
    
    }
    
    void
    Timer1IntHandler(void)
    {
    	// Clear the timer interrupt
    	TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    		// Read the current state of the GPIO pin and
    	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x01);
    	SysCtlDelay(160/3); //延时10微秒
    	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x00);
    	//LCD_Draw_String("OK123", 2, 1, false);
    }
    
    
    int main(void)
    {
    	char buf[10] = {0};
    
    	//配置时钟
    	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
    					SYSCTL_XTAL_16MHZ);
    	//	使能外设 所有外设操作前都要使能,否则会跳转到FaultISR()死循环
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    
    	ui32Period = (SysCtlClockGet());
    
    	//GPIO注册中断
    
    	GPIOIntRegister(GPIO_PORTF_BASE, PortFIntHandler);
    	//PF4作为中断输入源对应LaunchPad的按键1 SW1
    	GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    	//PF2配置为输出模式 对应LaunchPad的蓝色LED
    	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1);
    	//配置PF4为上拉电阻,输出电流能力2mA
    	GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
    	//终端类型为下降沿触发
    	GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4 , GPIO_BOTH_EDGES);
    	//使能PF4中断
    	GPIOIntEnable(GPIO_PORTF_BASE, GPIO_PIN_4);
    	//全局中断
    	IntMasterEnable();
    	timer0Config();
    	timer1Config();
    	//PF2高电平 点亮蓝色LED
    	//GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3,GPIO_PIN_3);
    
    
    
    
    	//timer配置
    
    
    	LCD_Enable();						// 使能LCD
    	LCD_Init();							// 初始化LCD
    	LCD_ScreenClr();					// 清屏
    
    	//sprintf(buf, "%d", ui32Period);
    	while(1)
    	{
    
    		if(g_showFlag == 0)
    		{
    			LCD_Draw_String("in", 4, 1, false);
    		}
    		else
    		{
    			LCD_Draw_String("out", 4, 1, false);
    		}
    
    
    		if(g_readFlag == 1)
    		{
    			g_readFlag = 0;
    			LCD_Draw_String("ab", 3, 1, false);
    			readValue = TimerValueGet(TIMER0_BASE, TIMER_A);
    
    			//sprintf(buf, "%d", readValue);
    		}
    
    		LCD_Draw_String("test", 1, 1, false);
    
    
    		//LCD_Draw_String(buf, 3, 3, false);
    
    	}
    }
    

    大概看了看你定时器的初始化,估计是你这里出错了。建议单独测试定时器部分。另外,应该是先初始化定时器,再开全局中断了吧。定时器中不要有延时。