wulin 发表于 2021-5-13 21:25
开两个定时器,一个随键值控制喇叭发声频率,一个控制持续时间。不过要想声音好听就得添加DA转换和按键力 ...
请前辈帮我看看代码,虽然知道功能但是不太会用代码表达呀。
- #include <reg51.h>
- #define uchar unsigned char
- uchar DisBuf[8];
- sbit P3_7=P3^7;
- unsigned char table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //
- unsigned char BitCode[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //
- uchar code key_buf[16]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd, 0x7d,0xeb,
- 0xdb, 0xbb, 0x7b,0xe7, 0xd7,0xb7,0x77}; //
- unsigned int wu[]={64580,64684,64777,64820,64898,64968,65030,65058,65058,65111};
- uchar KeyValue;
- unsigned int num=0;
- unsigned char i;
- void Delay_Ms(uchar n) //
- {
- uchar k;
- while(n--)
- {
- for(k=0;k<113;k++);
- }
- }
-
- void Display()//
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- P0=DisBuf[i];
- P2=BitCode[i];
- Delay_Ms(1);
- P0=0xff;
- }
- }
- uchar KeyScan() //
- {
- uchar scan1,scan2,temp,j;
- P1 = 0x0f;
- scan1 = P1 & 0x0f;
- if (scan1 != 0x0f)
- {
- Delay_Ms(10);
- scan1 = P1 & 0x0f;
-
- if (scan1 != 0x0f)
- {
- P1 = 0xf0;
- scan2 = P1 & 0xf0;
- temp = scan1 | scan2 ;
- for (j=0;j<16;j++)
- {
- if (temp == key_buf[j])
- return j;
- }
-
- }
- return (16);
- }
- else
- return (16);
- }
- void main(void)
- {
-
- TMOD=0x01;
- ET0=1;
- EA=1;
- for(i=0;i<8;i++)
- {
- DisBuf[i]=0xff;
- }
- while(1)
- {
- Display();
- KeyValue = KeyScan();
- TR0=1;
- if (KeyValue<16)
- {
- while(KeyScan()<16);
-
- if(KeyValue<10)
- {
- num++;
- TR0=1;
- //if(num<=8)
- {
- for(i=num-1;i>=1;i--)
- {
- DisBuf[i]=DisBuf[i-1];//移位
-
- }
- DisBuf[0]=table[KeyValue];
- }
- }
- else
- {
- if(KeyValue==10)//清屏
- {
- for(i=0;i<=7;i++)
- {
- DisBuf[i]=0xff;
- }
- num = 0;
- }
- }
- }
- }
- }
-
- void time() interrupt 1 //中断发声
- {
- if(KeyValue<=9)
- {
- TH0=wu[KeyValue]/256;
- TL0=wu[KeyValue]%256;
- P3_7=!P3_7;
- }
- }
复制代码 |