按鍵控制彩燈的仿真電路圖:
0.png (30.84 KB, 下載次數: 74)
下載附件
2017-4-2 01:25 上傳
0.png (54.88 KB, 下載次數: 91)
下載附件
2017-4-2 01:24 上傳
單片機源程序:
- /**********************劉金森 1440302103 電子信息工程*********************/
- #include<reg51.h> //51單片機頭文件
- #include<intrins.h> //包含crol(循環左移)函數所在的頭文件
- #define uint unsigned int
- void delay(int z); //聲明子函數
- unsigned char temp; //定義一個變量,用來給P1口賦值
- int i,j;
- sbit Key0=P0^0;
- sbit Key1=P0^1;
- sbit Key2=P0^2; //定義三個按鍵
- /**************按鍵控制函數************/
- uint key()
- {
- uint temp;
- Key0=0;
- Key1=0;
- Key2=0;
- if(Key0==1)
- {
- delay(500); //延時500毫秒,去按鍵時的抖動
- if(Key0==1)
- {
- temp=0xfc; //1111 1100B 只有兩盞燈亮
- P1=temp;
- while(Key0)
- {
- delay(500);
- temp=_crol_(temp,1); //將temp循環左移一位后再賦給temp
- P1=temp; //將移位后的值賦給P1口,從低位到高位逐個點亮發光二極管
- }
- }
-
- }
- else if(Key1==1)
- {
- delay(500); //延時500毫秒,去按鍵時的抖動
- if(Key1==1)
- {
- temp=0x10; //0001 0000B 只有一盞燈不亮
- P1=temp;
- while(Key1)
- {
- delay(500);
- temp=_cror_(temp,1); //將temp循環左移一位后再賦給temp
- P1=temp; //將移位后的值賦給P1口,從低位到高位逐個點亮發光二極管
- }
- }
-
- }
- else if(Key2==1)
- {
- delay(500); //延時500毫秒,去按鍵時的抖動
- if(Key2==1)
- {
- temp=0xfd; //1111 1101B 只有一盞燈亮
- P1=temp;
- while(Key2)
- {
- delay(500);
- temp=_cror_(temp,1); //將temp循環左移一位后再賦給temp
- P1=temp; //將移位后的值賦給P1口,從低位到高位逐個點亮發光二極管
- }
- }
-
- }
-
-
- return temp;
- }
- void delay(int z) //延時z毫秒
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*****************主函數*************/
- void main()
- {
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
下載:
test1.zip
(73.8 KB, 下載次數: 63)
2017-3-27 01:20 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|