双外部中断应用 对LED的控制
#include<reg51.h>
#define uchar unsigned char
void Delay(unsigned int i)
{
uchar j;
for(;i>0;i--)
for(j=0;j<125;j++)
{;}
}
void main()
{
uchar display[9]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned int a;
for(;;)
{
EA=1;
EX0=1;
EX1=1;
IT0=1;
IT1=1;
IP=0;
for(a=0;a<9;a++)
{
Delay(500);
P2=display[a];
}
}
}
void int0_isr(void) interrupt 0 using 1
{
uchar n;
for(n=0;n<10;n++)
{
P2=0x0f;
Delay(500);
P2=0xf0;
Delay(500);
}
}
void int1_isr(void) interrupt 2 using 2
{
uchar m;
for(m=0;m<10;m++)
{
P2=0xff;
Delay(500);
P2=0;
Delay(500);
}
}
数字电压表的设计
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define unit unsigned int
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unit ADresult[11];
sbit DATOUT=P1^0;
sbit DATIN=P1^1;
sbit CS=P1^2;
sbit IOCLK=P1^3;
sbit EOC=P1^4;
sbit weil=P3^0;
sbit wei2=P3^1;
sbit wei3=P3^2;
sbit wei4=P3^3;
void delay_ms(unit i)
{
int j;
for(;i>0;i--)
for(j=0; j<123;j++);
}
unit getdata(uchar channel)
{
uchar i,temp;
unit read_ad_data=0;
channel=channel<<4;
IOCLK=0;
CS=0;
temp=channel;
for(i=0;i<12;i++)
{
if(DATOUT) read_ad_data=read_ad_data|0x01;
DATIN=(bit)(temp&0x80);
IOCLK=1;
_nop_();_nop_();_nop_();
IOCLK=0;
_nop_();_nop_();_nop_();
temp=temp<<1;
read_ad_data<<=1;
}
CS=1;
read_ad_data>>=1;
return(read_ad_data);
}
void dispaly(void)
{
uchar qian,bai,shi,ge;
unit value;
value=ADresult[2]*1.221;
qian=value%10000/1000;
bai=value%1000/100;
shi=value%100/10;
ge=value%10;
weil=1;
P2=table[qian]-128;
delay_ms(1);
weil=0;
wei2=1;
P2=table[bai];
delay_ms(1);
wei2=0;
wei3=1;
P2=table[shi];
delay_ms(1);
wei3=0;
wei4=1;
P2=table[ge];
delay_ms(1);
wei4=0;
}
main(void)
{
ADresult[2]=getdata(2);
while(1)
{
_nop_();_nop_();_nop_();
ADresult[2]=getdata(2);
while(!EOC);
dispaly();
}
}
附件里面是仿真图,不含代码(自行生成hex文件)仅供参考,请自行测试,如有错误请告知:
Proteus8.8仿真下载:
两个外部中断的应用.zip
(31.95 KB, 下载次数: 15)
|