单片机计数程序

来源:百度知道 编辑:UC知道 时间:2024/05/22 05:45:00
单片机型号atmel s52 12M晶振
要求对P2.0口的开关计数(开关按下高电平,放开低电平)。
计数10次。当达到十次时候P3.0口由高电平转为低电平,P3.1口也由高电平转为低电平(P3.0转换为低电平时间比P3.1转换位低电平时间晚1秒钟)。

#include <reg52.h>
sbit Switch = P2^0;
sbit P3_0 = P3^0;
sbit P3_1 = P3^1;
#define uchar unsigned char
#define uint unsigned int
bit key_state;
//***********************************************************
void mDelay(uint temp)
{uint i;
while(temp!=0)
{temp--;
for(i=0;i<5000;i++);
}
}
//************************************************************
//************************主程序******************************
void main()
{ uchar count=0;
P3_0=1;
P3_1=1;
while(1)
{
if(Switch==1) key_state=1; else key_state=0;
if((Switch==0)&&(key_state==1))
{key_state=0;
count+=1; if(count==10)
{
P3_1=0;mDelay(200);//调括号的数来完成1S。
P3_0=0;
}
}
}
}
//此程序没有经过实际验证.给你一个参考吧.是按你的功能写的.你可以下载进试验,有问题请告知.应该能达到你的要求.不行加我的百度HI啊

还是自己写吧!这东西简单!
再说了,网上很多这样的例程,考一个被!