源程序是100例實訓里的,功能是P3^2口低電平發送一字符串(外部中斷0),因為外部中斷0有別的用處,不想占用。我現在想用一個IO口來控制發送字符串(比如P1^5口低電平條件),但fasong函數并不起作用,想知道原因或者解決的方法,在此先謝謝了。- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar Receive_Buffer[11]; //接收緩沖
- uchar Buf_Index=0; //緩沖空間索引
- sbit PIN1=P1^5;
- //數碼管編碼
- uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
- //延時
- void DelayMS(uint ms)
- {
- uchar i;
- while(ms--) for(i=0;i<120;i++);
- }
- void fasong(void);
- //主程序
- void main()
- {
- uchar i;
- P0=0x00;
- Receive_Buffer[0]=-1;
- SCON=0x50; //串口模式 1,允許接收
- TMOD=0x20; //T1 工作模式 2
- TH1=0xfd; //波特率 9600
- TL1=0xfd;
- PCON=0x00; //波特率不倍增
- EA=1;EX0=1;IT0=1;
- ES=1;IP=0x01;
- TR1=1;
- while(1)
- {
- for(i=0;i<100;i++)
- { //收到-1 為一次顯示結束
- if(Receive_Buffer[i]==-1) break;
- P0=DSY_CODE[Receive_Buffer[i]];
- DelayMS(500);
- }
- DelayMS(200);
- }
- if(!PIN1)
- {
-
- fasong();
- }
- }
- //串口接收中斷函數
- void Serial_INT() interrupt 4
- {
- uchar c;
- if(RI==0) return;
- ES=0; //關閉串口中斷
- RI=0; //清接收中斷標志
- c=SBUF;
- if(c>='0'&&c<='9')
- { //緩存新接收的每個字符,并在其后放-1 為結束標志
- Receive_Buffer[Buf_Index]=c-'0';
- Receive_Buffer[Buf_Index+1]=-1;
- Buf_Index=(Buf_Index+1)%10;
- }
- ES=1;
- }
- void fasong(void)interrupt 0//外部中斷 0
- {
- uchar *s="來自80C51!\r\n";
- uchar i=0;
- while(s[i]!='\0')
- {
- SBUF=s[i];
- while(TI==0);
- TI=0;
- i++;
- }
- }
復制代碼
|