|

楼主 |
发表于 2021-4-4 07:48
|
显示全部楼层
- /*
- 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
- NE555频率可调脉冲发生器模块(方波占空比50%简版)系列实验
- 程序二:实时测量脉冲的频率、占空比、周期、高电平时间与低电平时间
- */
- int divider[6] = {0, 1, 8, 64, 256, 1024};
- int prescaler = 5;
- double count = 0;
- double middle = 0;
- char x = 0;
- ISR(TIMER1_OVF_vect)
- {
- if (prescaler < 4)
- {
- prescaler++;
- }
- }
- void interrupt()
- {
- if (!x)
- {
- count = TCNT1;
- TCNT1 = 0x000;
- TCCR1B = prescaler;
- attachInterrupt(0, interrupt, FALLING);
- }
- else
- {
- middle = TCNT1;
- attachInterrupt(0, interrupt, RISING);
- }
- x = ~x;
- }
- void setup()
- {
- Serial.begin(57600);
- TIMSK1 = 0x01;
- TCCR1A = 0x00;
- attachInterrupt(0, interrupt, RISING);
- }
- void loop()
- {
- Serial.print("频 率: ");
- Serial.print(16000000.0 / divider[prescaler] / count);
- Serial.print(" Hz\t\占空比: ");
- Serial.print(middle / count * 100);
- Serial.print(" %\t\周 期: ");
- Serial.print(0.0000625 * divider[prescaler]*count);
- Serial.print(" ms\t\高电平时间: ");
- Serial.print(0.0000625 * divider[prescaler]*middle);
- Serial.print(" ms\t\低电平时间: ");
- Serial.print(0.0000625 * divider[prescaler] * (count - middle));
- Serial.println(" ms");
- if (prescaler > 1)
- {
- prescaler--;
- delay(200);
- }
- delay(1000);
- }
复制代码
|
|