平时都是用T0,T1 这次两个都用完了 所以必须使用T2了 ,所以使用T2时就出现了不少的问题
我根据网上的资料 写了一个关于蜂鸣器的程序 但是实践中 蜂鸣器无法正常运行
单片机程序如下 , 希望大家能指导我一下 主要是关于T2定时器中断的问题 谢谢大家了
- #include <REG52.H>
- #define uchar unsigned char
-
- sfr T2MOD=0xC9;
- sbit beepIO=P2^7;
- uchar m,n;
- uchar code T[49][2]={{0,0},
- {0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},
- {0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},
- {0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},
- {0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}
- };
- uchar code music[][2]={{0,4},
- {22,6},{23,12},{43,6},{22,6},{23,12},{43,6},{22,6},{23,12},{43,6},{22,6},{23,12},{43,6},{22,6},{23,12},
- {0xFF,0xFF}};
- uchar s;
- void delay(uchar p) //延迟函数
- {
- uchar i,j;
- for(;p>0;p--)
- for(i=181;i>0;i--)
- for(j=181;j>0;j--);
- }
- void pause() //延迟函数
- {
- uchar i,j;
- for(i=150;i>0;i--)
- for(j=150;j>0;j--);
- }
- void T2_int() interrupt 5 //中断函数
- {
- TF2=0;
- beepIO=!beepIO;
- RCAP2H=T[m][0];
- RCAP2L=T[m][1];
- TH0=RCAP2H;
- TL0=RCAP2L;
- }
- void main()
- {
- uchar i=0;
- TCLK=0;
- RCLK=0;
- T2MOD=0X00;
- ET2=1;
- EA=1;
- while(1)
- {
- m=music[i][0];n=music[i][1];
- if(m==0x00)
- {TR2=0;delay(n);i++;}
- else if(m==0xFF)
- {TR2=0;delay(30);i=0;}
- else if(m==music[i+1][0])
- {TR2=1;delay(n);TR2=0;pause();i++;}
- else
- {TR2=1;delay(n);i++;}
- }
- }
复制代码
|