晶振11.0592M,波特率9600,用了虛擬終端,打算加上 虛擬串口,嘿嘿
單片機源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- ///共陽數碼管 0-9 P 滅
- uchar code smdm[]=
- {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xFF}; //
- #define sm_dm P0
- sbit Led=P3^7;
- sbit Key_set=P3^2;
- uchar code hello_world[]="Hello World ";
- uchar Dat_rxd;
- uchar work_mode=0;
- /******************************************************************/
- void DelayUs(unsigned char us)//delay us
- {
- unsigned char uscnt;
- uscnt=us>>1; /*12MHz頻率*/
- while(--uscnt);
- }
- /******************************************************************/
- /* 毫秒函數聲明 */
- /******************************************************************/
- void DelayMs(unsigned int ms)
- {
- while(--ms)
- {
- DelayUs(250);
- DelayUs(250);
- DelayUs(250);
- DelayUs(250);
- }
- }
- //******************串口初始化函數****************************//
- void Com0_Init(void)
- {
- EA=1; //開總中斷
- ES=1; //允許串口中斷
- TMOD=0x20; //定時器T1,在方式3中斷產生波特率
- SCON=0x50;
- TH1=0xfd; //波特率設置為9600(晶振12M)
- TL1=0xfd;
- TR1=1; //開定時器T1運行控制位
- }
- ///////////////////////
- void Com0(void) interrupt 4 using 1
- {
- if(RI)
- {
- Led=~Led;
- Dat_rxd=SBUF; //串口接收
- Dat_rxd=Dat_rxd-0x30;
- sm_dm=smdm[Dat_rxd];
- RI=0;
- }
- }
- void send_string(uchar *p)
- {
- while(*p!= '\0')
- {
- SBUF=*p;
- p++;
- while(TI==0);
- TI=0;
- }
- }
- //////////////////////
- void main(void)
- {
- ////////////
- Com0_Init();
- while(1)
- {
- if(~Key_set)
- {
- DelayMs(3);
- if(~Key_set)
- {
- work_mode=1;
- while(~Key_set);
- }
- }
- switch(work_mode)
- {
- case 0: break;
- case 1: Led=~Led;
- send_string(hello_world);
- work_mode=0;
- break;
- }
- }
- }
復制代碼
仿真代碼51hei附件下載:
仿真代碼.7z
(308.84 KB, 下載次數: 131)
2021-7-18 15:42 上傳
點擊文件名下載附件
|