微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机电子琴C代码

51单片机电子琴C代码

时间:11-27 来源:互联网 点击:
#include

#include

#include

#include

#include

#define uchar unsigned char

typedef unsigned char uint8; // 无符号8位整型变量

typedef signed char int8; // 有符号8位整型变量

typedef unsigned short uint16; // 无符号16位整型变量

typedef signed short int16; // 有符号16位整型变量

typedef unsigned int uint32; // 无符号32位整型变量

typedef signed int int32; // 有符号32位整型变量

typedef float fp32; // 单精度浮点数(32位长度)

typedef double fp64; // 双精度浮点数(64位长度)

sbit row1 = P1 ^ 0;

sbit row2 = P1 ^ 1;

sbit row3 = P1 ^ 2;

sbit col1 = P0 ^ 1;

sbit col2 = P0 ^ 2;

sbit col3 = P0 ^ 3;

sbit col4 = P0 ^ 4;

sbit col5 = P0 ^ 5;

sbit col6 = P0 ^ 6;

sbit col7 = P0 ^ 7;

#define uint unsigned int

uchar STH0; //定时器计数初值

uchar STL0;

bit FY=0; //放乐曲时FY=1, 电子琴弹奏时FY=0

uchar Song_Index=0,Tone_Index=0; //放音乐的参数

uchar k, key;

sbit SPK=P3^7;

sbit LED1=P1^0;

sbit LED2=P1^1;

sbit LED3=P3^4;

sbit LED4=P3^5;

sbit LED5=P3^6;

void Delay(uint16 count)

{

uint8 i;

while(--count != 0)

{

for(i = 0; i < 125; i++); // ";" 表示空语句,CPU空转。

} // i 从0加到125,在12M晶体下CPU大概耗时1毫秒

}

uint8 KeyDown(void)

{

col1=0; col2=0; col3=0; col4=0; col5=0; col6=0; col7=0; // 列线全部置低

if((row1==0) || (row2==0) || (row3==0)) // 若有任一行线读回状态为低

{

Delay(80); // 延时消抖

if((row1==0) || (row2==0) || (row3==0)) // 再次读行线状态,若有任一行线读回状态为低

return 1; // 返回1,表明有键盘按下

else

return 0; // 返回0,表明无键盘按下

}

else

return 0;

}

uint8 KeyUp(void)

{

col1=0; col2=0; col3=0; col4=0; col5=0; col6=0; col7=0;

if((row1==1) && (row2==1) && (row3==1))

{

Delay(80);

if((row1==1) && (row2==1) && (row3==1))

return 1;

else

return 0;

}

else

return 0;

}

uint8 KeyNum(void)

{

uint8 KeyTemp;

KeyTemp=0;

if(KeyDown()==1)

{

col1=0; col2=1; col3=1; col4=1; col5=1; col6=1; col7=1; // 将列线1置低,其他列线置高

if (row1==0) KeyTemp=1; // 若行线1读回状态为低,则表明按键1被按下

if (row2==0) KeyTemp=8; // 若行线2读回状态为低,则表明按键8被按下

if (row3==0) KeyTemp=15; // 若行线3读回状态为低,则表明按键15被按下

col1=1; col2=0; col3=1; col4=1; col5=1; col6=1; col7=1;

if (row1==0) KeyTemp=2;

if (row2==0) KeyTemp=9;

if (row3==0) KeyTemp=16;

col1=1; col2=1; col3=0; col4=1; col5=1; col6=1; col7=1;

if (row1==0) KeyTemp=3;

if (row2==0) KeyTemp=10;

if (row3==0) KeyTemp=17;

col1=1; col2=1; col3=1; col4=0; col5=1; col6=1; col7=1;

if (row1==0) KeyTemp=4;

if (row2==0) KeyTemp=11;

if (row3==0) KeyTemp=18;

col1=1; col2=1; col3=1; col4=1; col5=0; col6=1; col7=1;

if (row1==0) KeyTemp=5;

if (row2==0) KeyTemp=12;

if (row3==0) KeyTemp=19;

col1=1; col2=1; col3=1; col4=1; col5=1; col6=0; col7=1;

if (row1==0) KeyTemp=6;

if (row2==0) KeyTemp=13;

if (row3==0) KeyTemp=20;

col1=1; col2=1; col3=1; col4=1; col5=1; col6=1; col7=0;

if (row1==0) KeyTemp=7;

if (row2==0) KeyTemp=14;

if (row3==0) KeyTemp=21;

return KeyTemp;

}

else

return 0; //无按键按下

}

uchar code DSY_CODE[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x06};

uchar code GE_CODE[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};

uchar code Song[][100]= //任意选几首音乐的旋律

{

{5,3,5,3,5,3,1,2,4,3,2,5,5,3,5,3,5,3,1,2,4,3,2,1,2,2,4,4,3,1,5,2,4,3,2,5,5,3,5,3,5,3,1,2,4,3,2,1,-1}, //《粉刷匠》

{1,2,3,4,5,3,1,8,6,4,5,5,3,1,2,3,4,5,3,2,1,2,3,2,5,1,2,3,4,5,3,1,8,6,4,5,3,1,2,3,4,5,3,2,1,2,3,1,1,8,6,4,5,5,1,8,6,4,5,3,1,2,3,4,5,3,2,1,2,3,1,1,-1},//《小红帽》

{1,2,3,1,1,2,3,1,3,4,5,3,4,5,5,6,5,4,3,1,5,6,5,4,3,1,1,5,1,1,5,1,-1}, //《两只老虎》

{5,8,6,8,5,3,5,2,3,5,0,3,5,6,8,5,6,5,3,5,1,3,2,0,3,2,1,2,3,6,5,3,5,6,0,5,8,6,5,3,5,2,5,2,3,2,1,-1}, //《一分钱》

{5,3,5,3,5,3,2,3,5,5,5,3,6,5,3,5,3,2,1,2,3,5,3,2,1,2,3,6,5,6,5,2,3,5,6,5,6,5,

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top