代碼是這個
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar receive[5];
uchar address=5;
uchar i,j;uint ii;uchar ij=0;
uchar effect_flag=0;//有效資料標志
sbit P31=P3^1;sbit P32=P3^2;sbit P33=P3^3;sbit P34=P3^4;sbit P35=P3^5;sbit P37=P3^7;
uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x77,0x40}; // 所需的數碼管編碼
void delay(uint x)
{
for(i=x;i>0;i--)
for(j=115;j>0;j--);
}
void main()
{
P31=0; //讀取地址
delay(6000);
address=P1;
P31=1;
SCON=0x50; //串口方式1,允許接受
TMOD=0x20; //T1作業于方式2
PCON=0x00; //波特率不倍增
TH1=TL1=0xfd;
ES=1; //允許串口中斷
TR1=1; //啟動定時器1
receive[0]=11; //地址處理,準備顯示
receive[1]=12;
receive[2]=address/100;
receive[3]=address/10%10;
receive[4]=address%10;
for(ii=0;ii<500;ii++) //顯示地址
{
P37=0;P35=1;P34=1;P33=1;P32=1;P1=DSY_CODE[receive[0]];delay(3);
P37=1;P35=0;P34=1;P33=1;P32=1;P1=DSY_CODE[receive[1]];delay(3);
P37=1;P35=1;P34=0;P33=1;P32=1;P1=DSY_CODE[receive[2]];delay(3);
P37=1;P35=1;P34=1;P33=0;P32=1;P1=DSY_CODE[receive[3]];delay(3);
P37=1;P35=1;P34=1;P33=1;P32=0;P1=DSY_CODE[receive[4]];delay(3);
}
for(i=0;i<5;i++){receive[i]=0;}//清除地址
EA=1; //開總中斷
RI=0;
while(1)
{
P37=0;P35=1;P34=1;P33=1;P32=1;P1=DSY_CODE[receive[0]];delay(3);
P37=1;P35=0;P34=1;P33=1;P32=1;P1=DSY_CODE[receive[1]];delay(3);
P37=1;P35=1;P34=0;P33=1;P32=1;P1=DSY_CODE[receive[2]];delay(3);
P37=1;P35=1;P34=1;P33=0;P32=1;P1=DSY_CODE[receive[3]];delay(3);
P37=1;P35=1;P34=1;P33=1;P32=0;P1=DSY_CODE[receive[4]];delay(3);
}
}
//-----------------------------------------------------------------------
//串口接受中斷函式
//-----------------------------------------------------------------------
void Serial_INT() interrupt 4
{
if(RI==0) return;
ES=0; //關閉串口中斷
if(effect_flag==1)
{
receive[ij]=SBUF; //讀取字符
ij++;if(ij==5){ij=0;effect_flag=0;}
}
if(SBUF==address)effect_flag=1;//有效資料標志置位
RI=0;
ES=1; //開串口中斷
}
仿真圖
uj5u.com熱心網友回復:
是串口中斷,不是定時中斷,別人不發送串口,你的單片機不會產生接收中斷。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/37448.html
標籤:單片機/工控
下一篇:K60芯片用JLink下載的時候錯誤Error:Flash Download failed - "Cortex-M4",希望能幫助我解決一下,急急急
