|
#include "reg52.h"
//宏定義
#define uchar unsigned char
#define uint unsigned int
//按鍵定義
sbit KEY1= P2^0;
sbit KEY2= P2^1;
sbit KEY3= P2^2;
sbit KEY4= P2^3;
//數(shù)碼管定義
sbit W1= P2^4;
sbit W2= P2^5;
sbit W3= P2^6;
sbit W4= P2^7;
//DAC定義
sbit CS_5615=P3^2;
sbit CLK_5615=P3^3;
sbit DAT_5615=P3^4;
//報(bào)警指示定義
sbit led = P3^6;
sbit Duan= P3^7;
uint U;
uchar flag=0;
uint a=0;
uchar code table[]=
{
0x28,/*0*/
0xEB,/*1*/
0x32,/*2*/
0x62,/*3*/
0xE1,/*4*/
0x64,/*5*/
0x24,/*6*/
0xEA,/*7*/
0x20,/*8*/
0x60,/*9*/
}; //共陽(yáng)數(shù)碼管段碼表 沒有小數(shù)點(diǎn) 0~9
uchar code table1[]=
{
0x08,/*0*/
0xCB,/*1*/
0x12,/*2*/
0x42,/*3*/
0xC1,/*4*/
0x44,/*5*/
0x04,/*6*/
0xCA,/*7*/
0x00,/*8*/
0x40,/*9*/
0x29,/*U*/
};//帶小數(shù)點(diǎn)的編碼 0~9
void delay_ms(uint z) //延時(shí)函數(shù)
{
uint a,b;
for(a=z;a>0;a--)
for(b=5;b>0;b--);
}
void shuma(uint buf)
{
uchar a,b,c,d;
a=buf/1000;
b=buf%1000/100;
c=buf%100/10;
d=buf%10;
W1=0;W2=1;W3=1;W4=1;
P0=table[a];
delay_ms(60);
W1=1;W2=0;W3=1;W4=1;
P0=table1[b];
delay_ms(60);
W1=1;W2=1;W3=0;W4=1;
P0=table[c];
delay_ms(60);
W1=1;W2=1;W3=1;W4=0;
P0=table1[10];
delay_ms(60);
}
void tlc_5615(uint buf)
{
uint a,c;
c=buf;
CS_5615=0;
for(a=16;a>0;a--)
{
DAT_5615=c>>15;
c=c<<1;
CLK_5615=1;
CLK_5615=0;
}
CLK_5615=1;
CLK_5615=0;
CLK_5615=1;
CLK_5615=0;
CS_5615=1;
}
//主函數(shù)
void main(void)
{
uchar n,i;
n=5;
U=0;
led=0;
delay_ms(3000);
led=1;
while(1)
{
tlc_5615(U);
shuma(U/n*10);
if(flag==0)
{
if(KEY1==0)
{
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
if(KEY1==0)
{
if(U<120*n) //最大電壓
U=U+n;
else if(U>=120*n)
{
led=0;
for(i=0;i<50;i++)
shuma(U/n*10);
led=1;
}
}
}
if(KEY2==0)
{
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
if(KEY2==0)
{
if(U>=n)
U=U-n;
}
}
if(KEY3==0)
{
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
if(KEY3==0)
{
U=50*n;
}
}
if(KEY4==0)
{
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
shuma(U/n*10);
if(KEY4==0)
{
U=0;
}
}
}
if(Duan==0)
{
delay_ms(10);
if(Duan==0)
{
flag=1; //過流標(biāo)志置高
led=0;
U=0;
tlc_5615(0);
}
}
}
}
|
|