简体   繁体   English

Arduino定时器中断无法正常工作

[英]Arduino timer interrupt not working correctly

I am programming Arduino to know the time between high to low or low to high. 我正在对Arduino进行编程,以了解从高到低或从低到高的时间。 But i am getting constant values from timer interrupt value. 但是我正在从计时器中断值中获取恒定值。 It was 1246*10ms= 12460. What is wrong with this code. 它是1246 * 10ms =12460。此代码出了什么问题。 I am tracking my signal with oscilloscope. 我正在用示波器跟踪信号。 Actual time was an approximately 250ms. 实际时间约为250毫秒。 Help me please. 请帮帮我。

#include <TimerOne.h>

void setup(void)
{
  pinMode(7, INPUT);
  Timer1.initialize(100);//1000000=1s
  Timer1.attachInterrupt(blinkLED);
  Serial.begin(9600);
}


int ledState = LOW,T=0,state0=0,state1=0;
volatile unsigned long blinkCount = 0;  variables

void blinkLED(void)
{
  T++;
}

void loop(void)
{
  state0=state1;
  state1=digitalRead(7);
  if(state0!=state1)
  {
    //Serial.print("state1=");
    //Serial.print(state1);
    //Serial.print("  T=");
    Serial.println(T);
    T=0;
  }
}

Firstly please make sure the code you post is valid. 首先,请确保您发布的代码有效。 I think the 'variables' should be a comment? 我认为“变量”应该发表评论吗? Also your code is not very readable (please add some comments). 另外,您的代码不是很可读(请添加一些注释)。

For measuring the length of pulses Arduino has a built in function called 'pusleIn()'. 为了测量脉冲的长度,Arduino具有一个称为“ pusleIn()”的内置函数。 Have a look at he documentation . 看一下他的文档

If you want to measure high and low pulses you should also have a look at 'attachInterrupt()' with the mode set to CHANGE. 如果要测量高脉冲和低脉冲,还应查看模式设置为CHANGE的“ attachInterrupt()”。

I hope this helps you out a bit. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM