- #include<xxxx.h> //頭文件未知,需要改
- #define uchar unsigned char
- #define uint unsigned int
- sbit BEEP = P3^7; //STC單片機P3內部有上拉電阻
- sbit guang = P1^0; //STC單片機P1內部有上拉電阻
- sbit jidianqi = P0^0; //STC單片機P0內部無上拉電阻,需要外部加
- uint c=0;
- //
- void exint0() interrupt 0
- {
- EX0 = 0; // 關閉中斷,防止誤動作
- c=0;
- jidianqi = 0;
- EX0 = 1; // 打開中斷
- }
- //
- void exint1() interrupt 2
- {
- EX1 = 0; // 關閉中斷,防止誤動作
- c++;
- if(c==3) c=0; // c值超出范圍值清0,也可以設置其它值,例如c=1,看需求,不用就刪除此行吧
- delay1ms(200);
- EX1 = 1; // 打開中斷
- }
- //
- void main()
- {
- IT0 = 1;
- EX0 = 1;
- IT1 = 1;
- EX1 = 1;
- EA = 1;
- BEEP = 1;
- jidianqi = 0; // 如果是繼電器,應該用PNP管驅動繼電器,觸發信號低電平,
- // 如果jidianqi = 0;是繼電器關閉,最好改一改電路,防止STC類單片機啟動復位IO高電平誤動作
- delay1ms(200); // 稍微延個時,穩定一下
-
- while(1)
- {
- switch(c)
- {
- case 1: jidianqi=1; break;
- case 2: jidianqi=1; P0=0; break;
- default: break; // 如果c不在范圍值之內,執行,可添加其它功能
- }
- }
- }
復制代碼
|