⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsong.c

📁 this is source file song generate with mcs51 by keil
💻 C
字号:
/*  Filename     XSONG.C
    Description  PLAY SONG EXAMPLE
    Hardware     START-C51 
    Speed	     x2
    Compiler     Keil CA51 v5.0
    Engineer     Kriangsak b.
    Company      Sila Research Co.,Ltd. 

    Play 6 Songs ... 
    Active low by P1.0 - P1.5
*/

#include <reg51.h>
#include <absacc.h>
#include <assert.h>
#include <ctype.h>
#include <intrins.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/********** I/O PORT **********/

#define SEGM   XBYTE [0xd000]          // segment 
#define KEYI   XBYTE [0xe000]          // Key-input,Dip-sw
#define DIGT   XBYTE [0xf000]          // Digit,BL,485con,sound,user-led

/********** INT-RAM WORKING AREA **********/

unsigned char DIGMEM;                  // DIGT port buffer

/********** BASIC FUNCTION **********/

void dmsec (unsigned int count) {                // mSec Delay
    unsigned int i;                              // for Keil CA51 (Speed x2)
    while (count) {
        i = 230; while (i>0) i--;
        count--;
    }
}

void sound (unsigned char freq,unsigned int time) {   // Sound Generate (for song)
    unsigned char i;
    while (time>500) {
        DIGMEM = DIGMEM & 0xbf;                       // out signal low
        DIGT = DIGMEM;
        for (i=1;i<=freq;i++)
            time--;
        DIGMEM = DIGMEM | 0x40;                       // out signal high
        DIGT = DIGMEM;
        for (i=1;i<=freq;i++)
            time--;
    }
}

unsigned char code NOTETAB[] = {                                     // note 1-25
    0,0xfe,0xf0,0xe5,0xd4,0xca,0xc0,0xb5,0xaa,0xa2,0x97,0x8f,0x88,   // zero,d,d#,e,f,f#,g,g#,a,a#,b,c,c# (1)
    0x7f,0x78,0x70,0x6a,0x65,0x5f,0x59,0x54,0x50,0x4b,0x47,0x44,     //      d,d#,e,f,f#,g,g#,a,a#,b,c,c# (2)
    0x3f};                                                           //      d (3)

unsigned int code LENTAB[] = {         // length 1-4
    0,15500,30500,45500,60500};


void song (unsigned char *ptr) {
    unsigned char x,y,n;
    unsigned int l;
    while ((x = *ptr)!=0xff) {
        n = NOTETAB[x];        
        ptr++;
        y = *ptr;
        l = LENTAB[y];
        ptr++;
        if (n==0) dmsec (y*100);
        else sound (n,l);
        dmsec (50);
    }
}

/********** MAIN **********/

unsigned char code song1[] = {
    11,2,0,1,11,1,8,1,6,1,11,2,
         0,1,11,1,8,1,6,1,
         11,2,11,2,
         0,1,11,2,11,2,0,4,
    23,2,0,1,23,1,20,1,18,1,23,2,
         0,1,23,1,20,1,18,1,
         23,2,23,2,
         0,1,23,2,23,2,
    0xff};

unsigned char code song2[] = {
    10,2,11,2,0,2,13,2,14,2,0,2,
    10,2,11,2,13,2,14,2,19,2,18,3,
    8,2,9,2,10,2,11,2,
    0xff};


unsigned char code song3[] = {
    15,2,15,2,16,2,18,2,18,2,16,2,15,2,13,2,11,2,11,2,13,2,15,2,15,4,13,1,13,4,
    15,2,15,2,16,2,18,2,18,2,16,2,15,2,13,2,11,2,11,2,13,2,15,2,13,4,11,1,11,4, 
    0xff};

unsigned char code song4[] = {
    10,2,11,2,13,4,13,2,13,2,15,4,15,4,13,4,0,2,
    13,2,13,2,18,4,18,2,18,2,22,4,18,4,20,2,0,2,
    22,2,20,2,18,4,20,4,22,4,
    18,2,17,2,15,4,15,2,17,2,18,4,
    17,2,15,2,13,4,18,2,18,2,17,4,20,4,18,4,
    0xff};

unsigned char code song5[] = {
    4,2,8,2,11,2,15,4,
    6,2,10,2,13,2,18,4,
    4,1,8,1,11,1,15,4,
    6,1,10,1,13,1,18,4,
    0xff};

unsigned char code song6[] = {
    6,2,10,2,13,2,10,2,13,4,
    13,2,18,2,22,2,18,2,22,4,
    6,4,13,4,18,4,
    0xff};

void main (void) {
    DIGMEM = 0xff;                     // Digit buffer port (don't care rs485)
    while (1) { 
        switch (P1) {
            case 0xfe: song (song1); break;
            case 0xfd: song (song2); break;
            case 0xfb: song (song3); break;
            case 0xf7: song (song4); break;
            case 0xef: song (song5); break;
            case 0xdf: song (song6); break; 
        }
        dmsec (1000);
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -