【发布时间】:2019-12-17 10:51:01
【问题描述】:
免责声明 - 在中断方面,我是个大菜鸟,所以如果您有一些错误要指出,我很高兴听到。
我正在编程一个数字霍尔传感器,只要有磁铁,它就像一个开关,我想为它使用一个计时器。 d10 引脚,PB2,由 5V 供电。 [Atmega328p]
我想使用oneround 作为稍后计算风速的单轮时间,无限循环。
磁铁以小半径旋转,因此 PIN [默认开启] 将在 0.1s-1.0s 之间的任何速度关闭,然后再打开。
当我尝试打印值 TCNT1 时出现问题,如果我使用 256 的预分频器,它会给出一个数字 - 仍然是随机的,但是数字。现在,如果我使用 1024 预分频器,每次出现磁铁时,它都会不断加 1。
你能告诉我我做错了什么吗?
我可以打印TCNT1 作为 PIN 转回 On 点(即 360 度)的时间差吗?
我需要以毫秒为单位的时间来计算风速。
//include user/defined libraries
#include "i2cmaster.h"
#include "lcd.h"
#include "ds1621.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <unistd.h>
#include <util/delay.h>
//void init_timer1();
volatile unsigned char run = 0;
volatile unsigned int oneround;
void init_timer1() {
//______________________________// Set the Timer Mode to CTC and start timer
TCCR1B |= (1 << WGM12);
OCR1A = 0x3D; // Set the value that you want to count to OCR1A = 0x3D;
PCICR |= (1 << PCIE0);
PCIFR |= (1 << PCIF0);
PCMSK0 |= (1 << PCINT2);
//________________________________________________________________________________________pray this 1010011 1001000 1001001 1010100 works
sei();
}
int main(void) {
// WINDSPEED"
DDRB &= ~(1 << DDB2); // DIRECTION REGISTER INOUT D10 PCINT2 OC1B PB2 SS PWM CTC
PORTB |= (1 << PORTB2); // OUTPUT REGISTER hIGH
//DDRB = 0b11111011; // DIRECTION REGISTER IN OUT
//PORTB = 0b00000100; // OUTPUT REGISTER
i2c_init(); //initialization of communication
LCD_init(); //initialization of the LCD
init_timer1(); // Lets count those laps baby
while (1) { /* O_O KAPUT! */
LCD_set_cursor(1, 1);
printf("%u ", oneround);
// rest of the sensors
}
return 0;
} // Main close
ISR(PCINT0_vect) {
if (run == 0) {
TCCR1B &= ~(1 << CS12); // clear timer and stop
oneround = TCNT1;
TIFR1 |= (1 << OCF1A) | (1 << OCF1B) | (1 << TOV1);
TCCR1B |= (1 << CS22) | (1 << CS21) | (1 << CS20); // set prescaler to 1024 and start timer 1sec
run = 1;
}
else {
run = 0;
}
}
【问题讨论】:
标签: c timer embedded interrupt