论坛风格切换切换到宽版
  • 2295阅读
  • 8回复

圣诞礼物~ [复制链接]

上一主题 下一主题
离线BG2BHC
 
发帖
5338
只看楼主 倒序阅读 0楼 发表于: 2010-12-24
昨晚复习不动了 写了个程序玩玩~

#include <reg52.h>

sbit spk = p0^0;
#define halfnotetime 600

struct note
{
     int name;
     int time;
};

struct note code text[] = {{5, 1}, {10, 1}, {9, 1}, {8, 1}, {5, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {6, 4}, {6, 1}, {11, 1}, {10, 1}, {9, 1}, {7,3}, {5, 1}, {12, 1}, {12, 1}, {11, 1}, {9, 1}, {10, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {5, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {6, 4}, {6, 1}, {11, 1}, {10, 1}, {9, 1}, {12,1}, {12, 1}, {12, 2}, {13, 1}, {12, 1}, {11, 1}, {9, 1}, {8, 4}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {12, 1}, {8, 1}, {9, 1}, {10, 4}, {11, 1}, {11, 1}, {11, 2}, {11, 1}, {10, 1}, {10, 2}, {10, 1}, {9, 1}, {9, 1}, {8, 1}, {9, 2}, {12, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {12, 1}, {8, 1}, {9, 1}, {10, 4}, {11, 1}, {11, 1}, {11, 2}, {11, 1}, {10, 1}, {10, 2}, {12, 1}, {12, 1}, {11, 1}, {9, 1}, {8, 2}, {15, 2}, {0,2}, {-1,0}};

unsigned char code noteh[]={0, 249, 249, 250, 250, 251, 251, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 255};
unsigned char code notel[]={0, 34, 225,140, 216, 105, 233, 91, 143, 226, 68, 108, 179, 243, 46, 71, 120, 162, 182, 218, 250, 23};
unsigned char timerh0, timerl0;

void delay(int t)
{
     int i;
     char j;

     for(i=0; i<t; i++)
     {
           for(j=0; j<110; j++);
     }
}

void osc() interrupt 1
{
     spk = ~spk;
     th0 = timerh0;
     tl0 = timerl0;
}

void play(struct note text[])
{
     int i=0;
     
     while(text.name != -1)
     {
           if(text.name == 0)
           {
                 delay(halfnotetime*text.time);
           }
           else
           {
                 timerh0 = noteh[text.name];
                 timerl0 = notel[text.name];
                 tr0 = 1;
                 delay(halfnotetime*0.9*text.time);
                 tr0 = 0;
                 delay(halfnotetime*0.1*text.time);
           }
           i++;
     }
}

void main()
{
     tmod = 0x01;
     ea = 1;
     et0 = 1;

     while(1)
     {
           play(text);
     }
}
离线永远的FM
发帖
12236
只看该作者 1楼 发表于: 2010-12-24
!!!!!!!!!52信号源驱动扬声器!!可惜我只有arm。。
离线BG2BHC
发帖
5338
只看该作者 2楼 发表于: 2010-12-24
[quote=永远的fm]!!!!!!!!!52信号源驱动扬声器!!可惜我只有arm。。[/quote]
咱俩换吧。。。
离线BD4XR
发帖
8747
只看该作者 3楼 发表于: 2010-12-24
是《jingle bell》吗?
离线BG2BHC
发帖
5338
只看该作者 4楼 发表于: 2010-12-24
'
是《jingle bell》吗?
'
是的…………
离线BG2BHC
发帖
5338
只看该作者 5楼 发表于: 2010-12-24
[quote=小小ham]昨晚复习不动了 写了个程序玩玩~

#include <reg52.h>

sbit spk = p0^0;
#define halfnotetime 600

struct note
{
     int name;
     int time;
};

struct note code text[] = {{5, 1}, {10, 1}, {9, 1}, {8, 1}, {5, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {6, 4}, {6, 1}, {11, 1}, {10, 1}, {9, 1}, {7,3}, {5, 1}, {12, 1}, {12, 1}, {11, 1}, {9, 1}, {10, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {5, 4}, {5, 1}, {10, 1}, {9, 1}, {8, 1}, {6, 4}, {6, 1}, {11, 1}, {10, 1}, {9, 1}, {12,1}, {12, 1}, {12, 2}, {13, 1}, {12, 1}, {11, 1}, {9, 1}, {8, 4}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {12, 1}, {8, 1}, {9, 1}, {10, 4}, {11, 1}, {11, 1}, {11, 2}, {11, 1}, {10, 1}, {10, 2}, {10, 1}, {9, 1}, {9, 1}, {8, 1}, {9, 2}, {12, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {10, 1}, {10, 2}, {10, 1}, {12, 1}, {8, 1}, {9, 1}, {10, 4}, {11, 1}, {11, 1}, {11, 2}, {11, 1}, {10, 1}, {10, 2}, {12, 1}, {12, 1}, {11, 1}, {9, 1}, {8, 2}, {15, 2}, {0,2}, {-1,0}};

unsigned char code noteh[]={0, 249, 249, 250, 250, 251, 251, 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 255};
unsigned char code notel[]={0, 34, 225,140, 216, 105, 233, 91, 143, 226, 68, 108, 179, 243, 46, 71, 120, 162, 182, 218, 250, 23};
unsigned char timerh0, timerl0;

void delay(int t)
{
     int i;
     char j;

     for(i=0; i<t; i++)
     {
           for(j=0; j<110; j++);
     }
}

void osc() interrupt 1
{
     spk = ~spk;
     th0 = timerh0;
     tl0 = timerl0;
}

void play(struct note text[])
{
     int i=0;
     
     while(text.name != -1)
     {
           if(text.name == 0)
           {
                 delay(halfnotetime*text.time);
           }
           else
           {
                 timerh0 = noteh[text.name];
                 timerl0 = notel[text.name];
                 tr0 = 1;
                 delay(halfnotetime*0.9*text.time);
                 tr0 = 0;
                 delay(halfnotetime*0.1*text.time);
           }
           i++;
     }
}

void main()
{
     tmod = 0x01;
     ea = 1;
     et0 = 1;

     while(1)
     {
           play(text);
     }
}
离线BH7KQK
发帖
3425
只看该作者 6楼 发表于: 2010-12-24
楼主有意思。。
离线小比尔/5
发帖
2582
只看该作者 7楼 发表于: 2010-12-24
这事儿我也玩过~~~happy 呵呵
离线洮尔根
发帖
476
只看该作者 8楼 发表于: 2010-12-24
唉,我对单片机有种莫名的恐惧感