嘿嘿,我来水一把,说错了莫怪!
在嵌入式控制器上,用C语言来实现像PLC一样的上升沿/下降沿检测
volatile unsigned char pedge_flag;
volatile unsigned char nedge_flag;
int main (void)
{
unsigned char pedge_temp = 0;
unsigned char nedge_temp = 0;
unsigned char input_signal;
/*****************
add user's code
*****************/
pedge_flag = 0;
nedge_flag = 0;
while(1)
{
input_signal = input_signal_read();//读取当前输入状态
if( input_signal && (!pedge_temp) )//检查当前状态与上一周期状态
{
pedge_flag = 1;//上升沿置位
}
else
{
pedge_flag = 0;//上升沿复位
}
pedge_temp = input_signal;//当前输入状态并存储
if( (!input_signal) && nedge_temp )
{
nedge_flag = 1;//下降沿置位
}
else
{
nedge_flag = 0;//下降沿复位
}
nedge_temp = input_signal;//当前输入状态存储
/*********
add user's code
*********/
//delay(1);
}
}
|