DA轉(zhuǎn)換-15~15v電壓源設(shè)計(jì)
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
float num=51;
float temp;
uchar val;
uchar aa[4];
unsigned char code disp_code[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf,0xff};//數(shù)碼管顯示
/**********************************************************
延時(shí)子函數(shù)
**********************************************************/
void delayms(uint ms)
{
uchar t;
while(ms--)
{
for(t = 0; t < 120; t++);
}
}
/******************************************************************
函數(shù)功能:顯示計(jì)數(shù)次數(shù)的子程序
入口參數(shù):x
********************************************************************/
void Display(void)
{ uchar i;
P2=0xfe ;
for(i=0;i<4;i++)
{ P1=disp_code[aa[i]];
if(P2==0xfb)
P1=disp_code[aa[i]]&0x7f;
delayms(5);
P2=_crol_(P2,1);
}
}
/*******************************************
函數(shù)功能:主函數(shù)
******************************************/
void main(void)
{
EA=1; //開放總中斷
EX0=EX1=1; //允許使用外中斷
IT0=IT1=1; //選擇負(fù)跳變來(lái)觸發(fā)外中斷
temp=num/8.5;
val=(uchar)temp;
if(val<15)
{aa[0]=16;val=15-val;}
else {aa[0]=17;val=val-15;}
aa[1]=val/10;
if (aa[1]==0)
aa[1]=17;
aa[2]=val%10;
aa[3]=0;
P0=(uchar)num;
while(1)
Display();
}
void int0(void) interrupt 0 //外中斷0的中斷編號(hào)為0
{
if(num==0)
num=num+8.5;
num=num-8.5;
temp=num/8.5;
val=(uchar)temp;
if(val<15)
{aa[0]=16;val=15-val;}
else {aa[0]=17;val=val-15;}
aa[1]=val/10;
if (aa[1]==0)
aa[1]=17;
aa[2]=val%10;
aa[3]=0;
}
void int1(void) interrupt 2 //外中斷1的中斷編號(hào)為0
{
if(num==255)
num=num-8.5;
num=num+8.5;
temp=num/8.5;
val=(uchar)temp;
if(val<15)
{aa[0]=16;val=15-val;}
else {aa[0]=17;val=val-15;}
aa[1]=val/10;
if (aa[1]==0)
aa[1]=17;
aa[2]=val%10;
aa[3]=0;
P0=(uchar)num;
}