#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/timer.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/pwm.h"
#include "utils/uartstdio.h"
unsigned int interval;
unsigned int TimerAVal,TimerBVal;
unsigned int distance;
unsigned int PFlevel;
void InitUART(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE,GPIO_PIN_0|GPIO_PIN_1);
UARTClockSourceSet(UART0_BASE,UART_CLOCK_PIOSC);
UARTStdioConfig(0,115200,16000000);
}
void InitPWM()
{
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);//使能pwm1外设
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);//使能PF
GPIOPinConfigure(GPIO_PF1_M1PWM5 );//设置端口复用,
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1);//设置端口的pwm输出功能
PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);//下降计数,非同步
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, 320);//0.05MHz
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 160);//占空比50%,脉冲宽度
PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true);
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
}
/*
* pf1 used to trigger the hc-sr04
* pb6 used to calculate the time of high level on echo
*PB7 used to detect rising edge then enter the Interrupt handler in which i enable
* the timerA(PB6)
* */
void PortB7IntHandler(void)
{
GPIOIntClear(GPIO_PORTB_BASE,GPIO_INT_PIN_7);
TimerLoadSet(TIMER0_BASE,TIMER_A,60000);
IntEnable(INT_TIMER0A);
TimerEnable(TIMER0_BASE,TIMER_A);
//UARTprintf("Enter PB7 interrupt");
}
void Timer0AIntHandler(void)
{
TimerIntClear(TIMER0_BASE,TIMER_CAPA_EVENT);
TimerAVal=TimerValueGet(TIMER0_BASE,TIMER_A);
TimerDisable(TIMER0_BASE,TIMER_A);
//UARTprintf("enter timerA interrupt");
}
void main()
{
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_XTAL_16MHZ|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN);
InitUART();
InitPWM();
UARTprintf("UART configured properly\n");
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE,GPIO_PIN_7);
//使用PF1产生周期为100us,占空比为50%的方波,触发会hc-sr04。Pb7在echo检测到上升沿进入GPIO中断
//在中断处理函数里面,开启PB6的定时器,设置为捕获计时模式,捕获下降沿时触发中断,在中断处理函数里读取定时器的值
GPIOIntTypeSet(GPIO_PORTB_BASE,GPIO_PIN_7,GPIO_RISING_EDGE);//PB7配置为上升沿中断
GPIOIntEnable(GPIO_PORTB_BASE,GPIO_INT_PIN_7);
GPIOIntRegister(GPIO_PORTB_BASE,PortB7IntHandler);
IntEnable(INT_GPIOP7);
GPIOPinConfigure(GPIO_PB6_T0CCP0);
GPIOPinTypeTimer(GPIO_PORTB_BASE,GPIO_PIN_6);
TimerConfigure(TIMER0_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_CAP_TIME);//捕获计时模式
IntMasterEnable();
TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_NEG_EDGE);//下降沿触发定时器timer0A中断
TimerIntEnable(TIMER0_BASE,TIMER_CAPA_EVENT);//定时器捕捉下降沿产生中断
TimerIntRegister(TIMER0_BASE,TIMER_A,Timer0AIntHandler) ;
while(1)
{
interval=60000-TimerAVal;
UARTprintf("the interval is%d\n",interval);
}
}
很困惑,为什么定时器的值只会在59990和59989之间变化