极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 902|回复: 0

8F328-U Signal Reverser

[复制链接]
发表于 2019-4-18 16:14:41 | 显示全部楼层 |阅读模式
本帖最后由 eddiewwm 于 2019-4-18 16:15 编辑

8F328-U Signal Reverser
  1. /*
  2.    Signal Reverser (Use Interrupt pin2)
  3.    Date: 20190418
  4.    Rev: 01
  5.    By: Eddie
  6. */

  7. #define SCLK 2    // Signal input pin
  8. #define REVOUT 13 // Reversed output waveform as pin2 SCLK input

  9. volatile unsigned long pulseWidthStart, pulseWidthEnd, pulseWidth, cycleStart, cycleTime;

  10. void setup() {
  11.   pinMode(SCLK, INPUT_PULLUP);
  12.   pinMode(REVOUT, OUTPUT);
  13.   digitalWrite(REVOUT, LOW);
  14.   attachInterrupt(digitalPinToInterrupt(SCLK), ISRrisingEdge, RISING);
  15. //  Serial.begin(115200);
  16. }

  17. void loop() {

  18. }

  19. void reverseSignal() {
  20.   unsigned long pulseTime = 1.31*(3000 - pulseWidth); //reverse signal
  21.   //1.31 is used to compensate the delayMicroseconds() error of LGT8F328P chip
  22. //  Serial.println(pulseTime);
  23.   digitalWrite(REVOUT, HIGH);
  24.   delayMicroseconds(pulseTime);
  25.   digitalWrite(REVOUT, LOW);
  26. }

  27. void ISRrisingEdge() {
  28.   pulseWidthStart = micros();
  29.   pulseWidth = 0;
  30.   cycleTime = pulseWidthStart - cycleStart;
  31.   //  Serial.println(cycleTime);
  32.   cycleStart = pulseWidthStart;
  33.   attachInterrupt(digitalPinToInterrupt(SCLK), ISRfallingEdge, FALLING);
  34. }

  35. void ISRfallingEdge() {
  36.   pulseWidthEnd = micros();
  37.   pulseWidth = pulseWidthEnd - pulseWidthStart;
  38. //  Serial.println(pulseWidth);
  39.   reverseSignal();
  40.   attachInterrupt(digitalPinToInterrupt(SCLK), ISRrisingEdge, RISING);
  41. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2019-8-22 07:29 , Processed in 0.044776 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表