请看程序,定时间隔10秒钟让led亮1秒钟

2020-02-05 08:45发布

本帖最后由 sunoracle 于 2012-7-20 09:57 编辑

晶振是11.0592的,LED灯珠是低电平触发的。下面有段程序不会写,烦请看看,指点一二。 或者请写出其他方法...

#include<reg52.h>         

#define uchar unsigned char
#define uint unsigned int

sbit P2_0 = P2^0;
uint i;
uint T_count = 0;

void led() interrupt 1
{
        if (  ++T_count == 200 )  //200个50ms,也就是10秒
        {
          P2_0 = 0;  // LED点亮
          T_count = 0;       
        }
       //?????????此处如何让P2_0在持续1秒之后,变为P2_0=1,使得led灯灭
}

void main()
{
        TMOD = 0x01;
        TH0 = 0x4C;         //50ms
        TL0 = 0x00;
        IE = 0x82;
        TR0 = 1;
        while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
17条回答
zenghui
2020-02-06 02:48
本帖最后由 zenghui 于 2012-7-20 11:24 编辑

7楼的代码中
        if (++count == 200)    //200个50ms,也就是10秒
        {
                LED = 0;           // LED点亮
        }
        else if (count >= 220) //P2_0在持续1秒之后,变为P2_0=1,使得led灯灭
        {
                LED = 1;
                count = 0;
        }

我觉得改为
        if ((count++)< 200)    LED = 1; //200个50ms,也就是10秒LED灯灭
        else if ((count++)< 220)   LED = 0;//LED点亮
        else {LED = 1;count = 0;}
更加严谨一些

一周热门 更多>