新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 51單片機C語言演奏“童話”

        51單片機C語言演奏“童話”

        作者: 時間:2016-11-22 來源:網絡 收藏
        #include

        sbit sound=P2^4;
        sbit led1=P1^6;
        sbit led2=P1^7;
        unsigned char h,l,count,length;
        unsigned int t,f,i,j;
        unsigned int scale[22]={100,262,294,330,349,392,440,494,523,578,659,698,784,880,988,1046,1175,1318,1397,1568,1760,1976};
        bit flag=1;
        void setthl(unsigned int p)
        {
        t=p*2;
        f=1000000/t;
        h=(65536-f)/256;
        l=(65536-f)%256;
        TH1=h;
        TL1=l;
        flag=1;
        TR1=1;
        }
        time1() interrupt 3 using 1
        {
        TH1=h;
        TL1=l;
        sound=~sound;
        }

        void timer0(void) interrupt 1
        {
        TH0=0x9c;
        TL0=0x60;
        count++;
        if (count==length)
        {
        flag=0;
        count=0;
        }
        }

        本文引用地址:http://www.104case.com/article/201611/320043.htm

        delay()
        {
        length=10;
        flag=1;
        TR1=0;
        while(flag);
        TR1=1;
        }

        note(unsigned char n,unsigned char len)
        {
        setthl(scale[n]);
        length=len;
        while (flag);
        led1=!led1;
        led2=!led2;

        }

        song()
        {
        note(5,15);
        note(12,15);
        note(11,15);
        note(10,30);
        note(10,15);note(11,8);note(10,38);

        note(10,15);note(11,15);note(10,15);note(11,15);note(10,15);note(9,8);
        note(8,8); delay();

        note(8,15);note(10,15);note(12,15);note(13,30);note(13,15);note(13,8);note(12,23);note(9,15);note(9,15);note(11,8);note(10,38);
        delay();
        note(8,15);note(10,15);note(12,15);note(13,30);note(13,15);note(13,8);note(12,23);note(9,15);
        note(9,15);note(11,15);note(10,15);note(11,15);note(10,15);note(9,8);note(8,38);
        note(9,15);note(9,8);note(10,7);note(6,30);note(6,15);note(8,15);note(8,15);note(7,15);note(7,30);note(8,30);(8,30);
        }
        Delay(int i)
        { int j;
        for(;i>0;i--)
        {
        for(j=i;j>0;j--);
        }
        }
        main()
        {while(1)
        {count=0,length=0;
        TMOD=0x11;
        TH0=0x3c;
        TL0=0xb0;
        EA=1;
        ET1=1;
        ET0=1;
        TR1=1;
        TR0=1;
        led1=!led1;
        song();
        sound=1;
        EA=0;
        Delay(500);
        }

        }



        關鍵詞: 51單片機C語言童

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 县级市| 金堂县| 莎车县| 宁津县| 鹰潭市| 延川县| 漾濞| 湖口县| 泸州市| 从化市| 安龙县| 崇文区| 华阴市| 乌鲁木齐市| 酒泉市| 英吉沙县| 昌图县| 高邑县| 沽源县| 长春市| 商南县| 南阳市| 晋江市| 安仁县| 拜泉县| 建平县| 全州县| 阜康市| 靖安县| 紫金县| 灵寿县| 凌海市| 镇雄县| 中超| 兰溪市| 许昌市| 阿瓦提县| 梨树县| 满洲里市| 嵊泗县| 陈巴尔虎旗|