本帖最后由 lixiang3 于 2015-6-19 19:43 编辑
使用keli uv2软件
原程序作者是杜洋,我是从《爱上单片机》第三版随附光盘中拿出来的源程序,使用STC12C4052AD。我移植到STC11F32上,按键数量由8个增加到16个。
BUG:暂无
电路连接:P0、P1连接16个电阻,为高阻输入,触摸通过用身体改变电平实现。操作时需要身体接触VCC。P2.0连接扬声器正极,扬声器负极连接GND,外接晶体频率12MHz
源代码:
#include <STC11Fxx.H>
sbit SPEAKER = P2^0;
#define KEY1 P1
#define KEY2 P0
unsigned char MUSIC;
unsigned char STH0;
unsigned char STL0;
unsigned int code tab[]=
{
64021,64103,64260,64400,//低音3开始
64524,64580,64684,64777,
64820,64898,64968,65030,
65058,65110,65157,65178
};
void main(void)
{
TMOD=0x01;
ET0=1;
EA=1;
P1M1 = 0xff;
P1M0 = 0x00;
P0M1 = 0xff;
P0M0 = 0x00;
P2M1 = 0x00;
P2M0 = 0x01;
KEY1 = 0x00;
KEY2 = 0x00;
while(1)
{
if(KEY1 != 0x00)
{
switch (KEY1)
{
case 0x01://
MUSIC = 7;
break;//
case 0x02://
MUSIC = 6;
break;//
case 0x04://
MUSIC = 5;
break;//
case 0x08://
MUSIC = 4;
break;//
case 0x10://
MUSIC = 3;
break;//
case 0x20://
MUSIC = 2;
break;//
case 0x40://
MUSIC = 1;
break;//
case 0x80://
MUSIC = 0;
break;//
default:
MUSIC = 0xff;
break;//
}
if(MUSIC != 0xff){
STH0=tab[MUSIC]/256;
STL0=tab[MUSIC]%256;
TR0=1;
}
}else{
SPEAKER = 1;
TR0=0;
}
}
if(KEY2 != 0x00){
switch (KEY2)
{
case 0x01://
MUSIC = 15;
break;//
case 0x02://
MUSIC = 14;
break;//
case 0x04://
MUSIC = 13;
break;//
case 0x08://
MUSIC = 12;
break;//
case 0x10://
MUSIC = 11;
break;//
case 0x20://
MUSIC = 10;
break;//
case 0x40://
MUSIC = 9;
break;//
case 0x80://
MUSIC = 8;
break;//
default:
MUSIC = 0xff;
break;//
}
if(MUSIC != 0xff){
STH0=tab[MUSIC]/256;
STL0=tab[MUSIC]%256;
TR0=1;
}
}else{
SPEAKER = 1;
TR0=0;
}
}
void t0(void) interrupt 1 using 0
{
TH0=STH0;
TL0=STL0;
SPEAKER=~SPEAKER;
}
顺便问下谁有24键对应的频率设置,谢谢!
|