|
程序和仿真
單片機(jī)源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LED1=P1^0;
- sbit LED2=P1^3;
- sbit K1=P1^7;
- uchar Operation_No=0; //操作代碼
- //數(shù)碼管代碼
- uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- //延時
- void DelayMS(uint ms)
- {
- uchar i;
- while(ms--) for(i=0;i<120;i++);
- }
- //向串口發(fā)送字符
- void Putc_to_SerialPort(uchar c)
- {
- SBUF=c;
- while(TI==0);
- TI=0;
- }
- //主程序
- void main()
- {
- LED1=LED2=1;
- P0=0x00;
- SCON=0x50; //
- TMOD=0x20; //T1 工作模式 2
- PCON=0x00; //
- TH1=0xfd;
- TL1=0xfd;
- TI=RI=0;
- TR1=1;
- IE=0x90; //允許串口中斷
- while(1)
- {
- DelayMS(100);
- if(K1==0) //按下 K1 時選擇操作代碼 0,1,2,3
- {
- while(K1==0);
- Operation_No=(Operation_No+1)%4;
-
- switch(Operation_No) //A/B/C
- {
- case 0: Putc_to_SerialPort('X');
- LED1=LED2=1;
- break;
- case 1: Putc_to_SerialPort('A');
- LED1=~LED1;LED2=1;
- break;
- case 2: Putc_to_SerialPort('B');
- LED2=~LED2;LED1=1;
- break;
- case 3: Putc_to_SerialPort('C');
- LED1=~LED1;LED2=LED1;
- break; }
- }
- }
- }
- //甲機(jī)串口接收中斷函數(shù)
- void Serial_INT() interrupt 4
- {
- if(RI)
- {
- RI=0;
- if(SBUF>=0&&SBUF<=9) P0=DSY_CODE[SBUF];
- else P0=0x00;
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
雙機(jī)通信.zip
(88.27 KB, 下載次數(shù): 30)
2019-12-25 20:05 上傳
點(diǎn)擊文件名下載附件
單片機(jī)雙機(jī)通信
|
|