北邮PIC32单片机声控音乐播放器代码.docx
- 文档编号:28357586
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:20
- 大小:18.32KB
北邮PIC32单片机声控音乐播放器代码.docx
《北邮PIC32单片机声控音乐播放器代码.docx》由会员分享,可在线阅读,更多相关《北邮PIC32单片机声控音乐播放器代码.docx(20页珍藏版)》请在冰豆网上搜索。
北邮PIC32单片机声控音乐播放器代码
/***************************系统头文件*****************************/
#include
#include
#include
/*********************管脚,变量,函数定义***********************/
#defineucharunsignedchar
#defineuintunsignedint
#definersPORTAbits.RA7
#definerwPORTAbits.RA6
#defineEPORTGbits.RG0
#defineDB0PORTEbits.RE1
#defineDB1PORTEbits.RE2
#defineDB2PORTGbits.RG14
#defineDB3PORTEbits.RE4
#defineDB4PORTEbits.RE3
#defineDB5PORTDbits.RD12
#defineDB6PORTGbits.RG1
#defineDB7PORTEbits.RE0
#defineLEDPORTGbits.RG13
#defineLED1PORTDbits.RD1
#defineLED2PORTDbits.RD2
#definey4PORTDbits.RD5
#definey3PORTDbits.RD4
#definey2PORTFbits.RF5
#definey1PORTFbits.RF4
#definex4PORTBbits.RB14
#definex3PORTBbits.RB13
#definex2PORTBbits.RB12
#definex1PORTBbits.RB9
#defineSYS_FREQ(80000000)//主频80MHz
#definebofangPORTDbits.RD13
#definezantingPORTDbits.RD7
#definequyiPORTFbits.RF0
#definequerPORTFbits.RF1
#defineLED0_ON()LATDSET=(1<<0)
#defineCONFIG(CN_ON)//系统管道打开(全局中断开启)
#definePINS(CN13_ENABLE|CN14_ENABLE|CN17_ENABLE|CN18_ENABLE)//指示灯管道使能
#definePULLUPS(CN13_PULLUP_ENABLE|CN14_PULLUP_ENABLE|CN17_PULLUP_ENABLE|CN18_PULLUP_ENABLE)//两个指示灯的弱上拉电阻使能
voiddelay1(uintz);//延时函数1ms
voidset_port(uchardata);//fenjie
voidwrite_com(ucharcom);
voidwrite_data(uchardata);
voidInitscreen();
voidInitPort();
ucharkbscan();
voidWrite_ListChar(ucharcY,ucharcX,uchar*cStr);
voidwrite_pic(ucharadd,uchar*pic_num);
intm=0;
intn=0;
intm1=0,n1=0;
intp;
intdelay;
intts,time,savetime;
intled1,led2;
intq=0;
intfreq[]={1911,1703,1516,1332,1275,1136,1012,
915,851,758,670,638,568,506,
458,426,379,333,319,284,253};
intdetime[]={50,56,63,72,75,84,95,100,112,126,143,150,168,189,201,225,253,287,299,336,378};
inta[]={512,522,422,324,322,421,325,322,422,322,422,322,221,125,2,122,322,522,623,622,521,522,222,222,421,325,2,122,322,522,624,622,621,523,222,222,422,322,422,322,222,125,2,222,322,614,612,122,123,714,126,4,4,0};
intd[]={623,521,322,522,132,621,521,624,322,521,621,522,322,121,611,521,321,224,223,321,522,521,621,322,222,124,523,321,221,121,611,121,516,2,0};
intb[]={1,2,1,0,0,0};
intc[]={2,3,3,0,0,0};
/***************************系统配置*****************************/
#pragmaconfigFNOSC=PRIPLL//OscillatorSelection
#pragmaconfigFPLLIDIV=DIV_2//PLLInputDivider(PIC32StarterKit:
usedivideby2only)
#pragmaconfigFPLLMUL=MUL_20//PLLMultiplier
#pragmaconfigFPLLODIV=DIV_1//PLLOutputDivider
#pragmaconfigFPBDIV=DIV_1//PeripheralClockdivisor
#pragmaconfigFWDTEN=OFF//WatchdogTimer
#pragmaconfigWDTPS=PS1//WatchdogTimerPostscale
#pragmaconfigFCKSM=CSDCMD//ClockSwitching&FailSafeClockMonitor
#pragmaconfigOSCIOFNC=OFF//CLKOEnable
#pragmaconfigPOSCMOD=XT//PrimaryOscillator
#pragmaconfigIESO=OFF//Internal/ExternalSwitch-over
#pragmaconfigFSOSCEN=OFF//SecondaryOscillatorEnable
//调试模式选择,通过配置寄存器DEV_CONFIG0
#pragmaconfigCP=OFF//CodeProtect
#pragmaconfigBWP=OFF//BootFlashWriteProtect
#pragmaconfigPWP=OFF//ProgramFlashWriteProtect
#pragmaconfigICESEL=ICS_PGx2//ICE/ICDCommChannelSelect
#pragmaconfigDEBUG=ON//DebuggerDisabledforStarterKit
/***************************延时函数*****************************/
voiddelay1(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=112;y>0;y--);
}
/************************分解命令、数值的函数**************************/
voidset_port(uchardata)
{
ucharDB=data;
DB0=(DB)%2;
DB1=(DB/2)%2;
DB2=((DB/2)/2)%2;
DB3=(((DB/2)/2)/2)%2;
DB4=((((DB/2)/2)/2)/2)%2;
DB5=(((((DB/2)/2)/2)/2)/2)%2;
DB6=((((((DB/2)/2)/2)/2)/2)/2)%2;
DB7=(((((((DB/2)/2)/2)/2)/2)/2)/2)%2;
}
/*********************传命令函数定义**************************************/
voidwrite_com(ucharcom)//命令函数
{
PORTClearBits(IOPORT_A,BIT_7);//RS接为0,低电平,写入命令
delay1(20);
set_port(com);//将命令传给端口
delay1(200);
PORTSetBits(IOPORT_G,BIT_0);//使能端写入1
delay1(20);
PORTClearBits(IOPORT_G,BIT_0);//使能端写入0
delay1(20);
}
/************************传送数据函数的定义******************************/
voidwrite_data(uchardata)//数据函数
{
PORTSetBits(IOPORT_A,BIT_7);//RS接为1,高电平,写入数据
set_port(data);
delay1(200);
PORTSetBits(IOPORT_G,BIT_0);//使能端写入1
delay1(20);
PORTClearBits(IOPORT_G,BIT_0);//使能端写入0
delay1(20);
}
/*********************自定义字符函数*************************************/
ucharpic[3][8]=
{
{0x00,0x00,0x04,0x0A,0x11,0x00,0x00,0x00},
{0x00,0x00,0x00,0x11,0x0A,0x04,0x00,0x00},
{0x00,0x00,0x04,0x0A,0x11,0x00,0x00,0x00}/*,
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}*/
};
voidwrite_pic(ucharadd,uchar*pic_num)
{
uchari;
add=add<<3;
for(i=0;i<8;i++)
{
write_com(0x40|add+i);
write_data(*pic_num++);
}
}
/*********************液晶初始化函数定义*************************************/
voidInitscreen()//初始化函数
{
write_com(0x38);//初始化液晶
delay1(20);
write_com(0x0c);//不显示光标也不闪烁
delay1(20);
write_com(0x06);//光标向右移
delay1(20);
write_com(0x01);//屏幕清零
delay1(20);
write_com(0x80);//第一行光标定位
}
/*********************写字符串函数*************************************/
voidWrite_ListChar(ucharcY,ucharcX,uchar*cStr)
{
uchari=0;//字符串长度
if(cY==0)write_com(cX|=0x80);else
if(cY==1)write_com(cX|=0xc0);
//输出字符串
for(i=0;i<17&&cStr[i]!
='\0';i++)
{write_data(cStr[i]);}
}
/*********************初始化管脚中断函数*************************************/
voidInitPort()
{
mCNOpen(CONFIG,PINS,PULLUPS);
PORTSetPinsDigitalIn(IOPORT_F,BIT_5|BIT_4);
PORTSetPinsDigitalIn(IOPORT_D,BIT_4|BIT_5);
PORTSetPinsDigitalOut(IOPORT_B,BIT_9|BIT_12|BIT_13|BIT_14);
PORTClearBits(IOPORT_B,BIT_9|BIT_12|BIT_13|BIT_14);
PORTSetPinsDigitalOut(IOPORT_D,BIT_0|BIT_1|BIT_2);
PORTSetPinsDigitalOut(IOPORT_G,BIT_13);
PORTSetPinsDigitalOut(IOPORT_A,BIT_7|BIT_6);//(系统函数,设置它们为输出)
PORTSetPinsDigitalOut(IOPORT_D,BIT_12);//(系统函数,设置它们为输出)
//设置端口G的0\1为数字输出
PORTSetPinsDigitalOut(IOPORT_G,BIT_0|BIT_1|BIT_14);//(系统函数,设置它们为输出)
//设置端口E为数字输出
PORTSetPinsDigitalOut(IOPORT_E,BIT_0|BIT_4|BIT_3|BIT_1|BIT_2);//(系统函数,设置它们为输出)
PORTClearBits(IOPORT_A,BIT_7|BIT_6);//端口初始化为零
PORTClearBits(IOPORT_D,BIT_12);//端口初始化为零
PORTClearBits(IOPORT_G,BIT_0|BIT_1|BIT_14);
PORTClearBits(IOPORT_E,BIT_0|BIT_3|BIT_1|BIT_4|BIT_2);
PORTClearBits(IOPORT_D,BIT_0|BIT_1|BIT_2);
PORTClearBits(IOPORT_G,BIT_13);
mCNOpen(CONFIG,PINS,PULLUPS);
}
/*********************键盘函数*************************************/
ucharskscan()
{PORTClearBits(IOPORT_D,BIT_7|BIT_13);
PORTClearBits(IOPORT_F,BIT_0|BIT_1);
ucharkey1;
if(bofang==1)key1='1';
if(zanting==1)key1='2';
if(quyi==1)key1='3';
if(quer==1)key1='4';
else
key1='P';
returnkey1;
}
ucharkbscan()
{
unsignedintrow,col;
uintflag=0,a=1,b=1,c=1,d=1;
ucharkey;
PORTClearBits(IOPORT_B,BIT_9|BIT_12|BIT_13|BIT_14);
PORTSetBits(IOPORT_B,BIT_12|BIT_13|BIT_14);
a=y1;b=y2;c=y3;d=y4;
if((a&b&c&d)!
=1)
{
delay1(1000);
if((a&b&c&d)!
=1)
{
flag=1;
row=1;
if(a==0)col=1;
if(b==0)col=2;
if(c==0)col=3;
if(d==0)col=4;
}
}
if(flag!
=1)
{
PORTClearBits(IOPORT_B,BIT_9|BIT_12|BIT_13|BIT_14);
PORTSetBits(IOPORT_B,BIT_9|BIT_13|BIT_14);
a=y1;b=y2;c=y3;d=y4;
if((a&b&c&d)!
=1)
{
delay1(1000);
if((a&b&c&d)!
=1)
{
flag=1;
row=2;
if(a==0)col=1;
if(b==0)col=2;
if(c==0)col=3;
if(d==0)col=4;
}
}
}
if(flag!
=1)
{
PORTClearBits(IOPORT_B,BIT_9|BIT_12|BIT_13|BIT_14);
PORTSetBits(IOPORT_B,BIT_9|BIT_12|BIT_14);
a=y1;b=y2;c=y3;d=y4;
if((a&b&c&d)!
=1)
{
delay1(1000);
if((a&b&c&d)!
=1)
{
flag=1;
row=3;
if(a==0)col=1;
if(b==0)col=2;
if(c==0)col=3;
if(d==0)col=4;
}
}
}
if(flag!
=1)
{
PORTClearBits(IOPORT_B,BIT_9|BIT_12|BIT_13|BIT_14);
PORTSetBits(IOPORT_B,BIT_9|BIT_12|BIT_13);
a=y1;b=y2;c=y3;d=y4;
if((a&b&c&d)!
=1)
{
delay1(1000);
if((a&b&c&d)!
=1)
{
flag=1;
row=4;
if(a==0)col=1;
if(b==0)col=2;
if(c==0)col=3;
if(d==0)col=4;
}
}
}
if(flag!
=0)
{
if(row==1&&col==1)key='1';
if(row==1&&col==2)key='2';
if(row==1&&col==3)key='3';
if(row==1&&col==4)key='4';
if(row==2&&col==1)key='5';
if(row==2&&col==2)key='6';
if(row==2&&col==3)key='7';
if(row==2&&col==4)key='8';
if(row==3&&col==1)key='9';
if(row==3&&col==2)key='0';
if(row==3&&col==3)key='A';
if(row==3&&col==4)key='B';
if(row==4&&col==1)key='C';
if(row==4&&col==2)key='D';
if(row==4&&col==3)key='E';
if(row==4&&col==4)key='F';
}
else
key='P';
returnkey;
}
/*********************暂停函数*************************************/
voidstop()
{
ucharkey1;
key1=kbscan();
if(key1=='3')
{
while
(1)
{key1=kbscan();
if(key1=='4')
break;
}
}
}
voidstop1()
{
ucharkey3;
while
(1)
{
key3=skscan();
if(key3=='1')
break;
}
}
}
/*********************旋律分解函数*************************************/
intread(inta[],inti)
{
intk=a[i]/100;
intj=(a[i]-100*k)/10;
p=k+7*j-7;
if(p<0)
ts=0;
else
ts=floor(freq[p]*80/256);
time=a[i]-100*k-10*j;
returnts,time;
}
/*********************音乐播放函数*************************************/
intM[];
voidplay_music(M)
{intcount1=0;
ucharkey4;
while
(1)
{
read(M,q);
if(time!
=0)
{if(ts!
=0)
{
for(m=0;m
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 北邮 PIC32 单片机 声控 音乐 播放 代码