一乐电子

一乐电子百科

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

快捷登录

手机号码,快捷登录

搜索
12
返回列表 发新帖
楼主: huojunan
收起左侧

求一个电平转脉冲的电路

[复制链接]
发表于 2018-3-3 23:30 | 显示全部楼层
huojunan 发表于 2018-3-3 19:22
就是不断把pinstate写入到last pinstate , 然后当不一致时,再用判断是何种情况?

是这么个意思,您试试
 楼主| 发表于 2018-3-4 19:21 | 显示全部楼层
BG4RFF 发表于 2018-3-3 23:30
是这么个意思,您试试

const int  buttonPin = 22;    // the pin that the pushbutton is attached to
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void POWERdown()
{

  Serial.println("POWERdown");

}

void setup()
{
  //digitalWrite(22, HIGH);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

}

void loop(){

  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState)
  {
       if (lastButtonState==HIGH)
    { POWERdown();}

    delay(1000);
   }
else  { Serial.println("WAIT");  }
    delay(1000);

lastButtonState = HIGH;


}


        用mega2560 测试,22脚接+5V,串口监视器不断刷出WAIT,  接到GND就不断刷出 POWERdown,实际上POWERdown()只要在检测到断电后执行一次就行,不需要重复执行,不知道一个怎么写这个程序



 楼主| 发表于 2018-3-5 09:21 | 显示全部楼层
zhen_gj 发表于 2018-2-27 17:05
用555设计个图,不知能不能满足你的需要

监测的是高电平转为低电平,断电后一直保持低电平,但是我想了下,电断了,应该更接近于悬空,还是说要用个高阻值的电阻把GND和监测脚接起来,有电时是监测电阻上的压降,断电就被拉到GND

发表于 2018-3-5 23:36 | 显示全部楼层
const int  buttonPin = 22;    // the pin that the pushbutton is attached to
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void POWERdown()
{
        Serial.println("POWERdown");
}
void POWERON()
{
        Serial.println("POWERON");
}

void setup()
{
  //digitalWrite(22, HIGH);
        pinMode(buttonPin, INPUT);
        Serial.begin(9600);
        lastButtonState=digitalRead(buttonPin);//这里注意,第一次直接读
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    if (lastButtonState==HIGH)
     POWERdown();//对应掉电时刻
        if (lastButtonState==LOW) //对应上电时刻
     POWERON();
    delay(1000);
   }
else
{ Serial.println("WAIT");  }
    delay(1000);
lastButtonState = buttonState;//这里注意
}


这个试试
发表于 2018-3-5 23:38 | 显示全部楼层
22脚接1k到5v,10k到GND,然后接个键试试
发表于 2018-3-24 14:59 | 显示全部楼层
高手云集

本版积分规则

QQ|一淘宝店|手机版|商店|电子DIY套件|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2024-5-19 10:53 , Processed in 0.052333 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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