主题中讨论的其他器件:LM393、
大家好、
使用 GPIO 输入 上升沿 中断时遇到问题。
工具: IAR Embedded Workbench。
应用: 使用 LM393 电机 速度 测量 传感器 获取 直流 电机的速度。
计划:
- PF3 被配置 为 具有 上升 沿 中断的输入。 它 与 速度 传感器的数字输出相连。 “ 1” 对应 于~1.25V, “0” <0.25V。
- 还有 一 个计时器中断: Timer0 、子计时 器 A; 带有 中断 的周期性中断、用于超时。 显示红色 LED。
代码如下:
- main.c:
#define PART_TM4C123GH6PM
//
#include
#include
#include
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
//#include "inc/tm4c123ghp6.h"
#define GPIO_PORTF_DATA_R 0x400253FC // (*(volatile UINT32_t *) 0x400073FC)
//
//
//! 此项目根据红外传感器信号计算电机速度。
//! 用于产生周期性中断的计时器定义采样时间。
//! 用于在 DO 为高电平时生成中断的 GPIO 端口计算 RPM
//! rpm = count/TS/20 (20条带)*60 (s/min)
//! 速度 v (mm/s)= pi*d (单位:mm)*RPM/60
//
//
//
//包含中断指示符当前值的标志。
//
//
uint32_t g_ui32Flags = 0;
uint32_t red_led = 0;
//
//
//包含传感器 GPIO 输入/执行中断值的标志。
//
//
uint32_t PortFIntFlags = 0;
INT 计数= 0;
int v;//速度;单位:mm/s;= pi*d*count/TS (20ms)/20*1000
浮点 PI = 3.14;// 3.14
int d = 66;//单位:mm
int T = 1000;//单位:mm;对应于1/s
int pulsesperturn = 20;
uint32_t 周期= 32000000;
//
//
//驱动程序库遇到错误时调用的错误例程。
//
//
#ifdef 调试
无效
_error__(char * pcFilename、uint32_t ui32Line)
{
}
#endif
//
//
//计时器中断的中断处理程序。
//
//
无效
Timer0IntHandler (空)
{
//
//清除计时器中断。
//
TimerIntClear (TIMER0_BASE、TIMER_TINA_TIMEOUT);
G_ui32Flags = 1;
//
//切换计时器的标志。
//
// HWREGBITW (&g_ui32Flags、0)^= 1;
red_led =~red_led;
GPIOPinWrite (GPIO_PORTF_BASE、GPIO_PIN_1、RED_LED);
}
void PortFIntHandler (void){
// PortFIntFlags++;
//
// uint32_t status=0;
//
// status = GPIOIntStatus (GPIO_PORTF_BASE、true);
//
// GPIOIntClear (GPIO_PORTF_BASE、STATUS);
//
// if (((status & GPIO_INT_PIN_3)== GPIO_INT_PIN_3)
// {
// count ++;
// }
if (GPIO_PORTF_DATA_R & 0x08!= 0)
{
count++;
}
GPIOIntClear (GPIO_PORTF_BASE、GPIO_INT_PIN_3);
}
//
//
//此 应用程序计算直流电机的速度
//通过红外传感器。
//
//
内部
main (空)
{
//将时钟设置为80MHz
SysCtlClockSet (SYSCTL_SYSDIV_1_SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHz);
//
// GPIO:PF3 -上升沿
SysCtlPeripheralEnable (SYSCTL_Periph_GPIOF);
SysCtlDelay(3);
GPIOPinTypeGPIOInput (GPIO_PORTF_BASE、GPIO_PIN_3);
// GPIOPadConfigSet (GPIO_PORTF_BASE、GPIO_PIN_3、GPIO_Strength _2mA、GPIO_PIN_TYPE_STD_WPD);
GPIOIntRegister (GPIO_PORTF_BASE、PortFIntHandler);
GPIOIntTypeSet (GPIO_PORTF_BASE、GPIO_PIN_3、GPIO_FALLING_EDGE);
GPIOIntEnable (GPIO_PORTF_BASE、GPIO_INT_PIN_3);
GPIOIntClear (GPIO_PORTF_BASE、GPIO_INT_PIN_3);
GPIOPinTypeGPIOOutput (GPIO_PORTF_BASE、GPIO_PIN_1);
//Timer0
SysCtlPeripheralEnable (SYSCTL_Periph_TIMER0);
SysCtlDelay(3);
TimerConfigure (TIMER0_BASE、TIMER_CFG_PERIODICASE);
TimerLoadSet (TIMER0_BASE、TIMER_A、PERIOD);
//设置计时器超时的中断。
TimerIntEnable (TIMER0_BASE、TIMER_TINA_TIMEOUT);
//启用计时器。
TimerEnable (TIMER0_BASE、TIMER_A);
IntEnable (INT_GPIOF);
IntEnable (INT_TIMER0A);
IntMasterEnable();
while (1)
{
如果(g_ui32Flags = 1)
{
//在计算过程中不处理中断
GPIOIntDisable (GPIO_PORTF_BASE、GPIO_INT_PIN_3);
v =(int)1000*pi*d*count/pulsesperturn;
printf ("count =%d\n"、count);
printf ("v =%d\n"、v);
计数= 0;
G_ui32Flags = 0;
GPIOIntEnable (GPIO_PORTF_BASE、GPIO_INT_PIN_3);
}
}
}
- startup_ewarm.c (部分):
//
//
//应用程序的 int 处理程序。
//
//
空 Timer0IntHandler (空);
void PortFIntHandler (void);
IntDefaultHandler、 //看门狗计时器
// IntDefaultHandler, //计时器0子计时器 A
Timer0IntHandler、 //计时器0子计时器 A
IntDefaultHandler、 // Timer 0子计时器 B
IntDefaultHandler、 //闪存控制
// IntDefaultHandler, // GPIO 端口 F
PortFIntHandler、 // GPIO 端口 F
IntDefaultHandler、 // GPIO 端口 G
问题:
变量 计数 始终为 但 绿色 LED 随着 旋转 的电机闪烁。
我 真的 不知道 问题是什么...
感谢 您的建议和 帮助。
此致。
Meidan