發一個源程序
已有 997 次閱讀2016-3-12 18:23
|
源程序
本來這個方案在去年就要完成的,沒想一拖再拖,沒有條件沒有時間,直到依附今天才完成,決定開源
#include <reg51.H>
#include <intrins.h>
sbit SDA=P3^7;/*定義數據線*/
sbit SCL=P3^6;/*定義時鐘線*/
bit ACK;
unsigned char Addwr=0xA0;/*器件地址及寫信號*/
unsigned char Addrd=0xA1;/*器件地址及讀信號*/
unsigned char code zhifu [15]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x92,0x0E,0x09,0xFE,0xF7};
/*LED字符定義數組:0,1,2,3,4,5,6,7,8,9,S,F,H,-,_*/
unsigned char *p1;
sbit P2_7=P2^7;
sbit P2_6=P2^6;
sbit P2_5=P2^5;
sbit P2_4=P2^4;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P1_4=P1^4;
unsigned char shi=0;/*做為設定時間的變量*/
unsigned char fen=0;
unsigned char miao=0;
unsigned char temp=20;
unsigned char key_a;
unsigned char shi_T;/*存儲LED顯示字符,時,分,秒高低各2位*/
unsigned char shi_T;
unsigned char shi_L;
unsigned char fen_T;
unsigned char fen_L;
unsigned char miao_T;
unsigned char miao_L;
bit panduan=1;
void delay(unsigned int t)/*延時子程序*/
{unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<600;j++);
}
void key()/*按鍵處理程序*/
{
if(P1_0==0)
{delay(20);
shi++;
while(P1_0==0)
{}
if(shi>23)
{shi=0;}
delay(20);
}
if(P1_1==0)
{delay(20);
fen++;
while(P1_1==0)
{}
if(fen>59)
{fen=0;}
delay(20);
}
if(P1_2==0)
{delay(20);
miao++;
while(P1_2==0)
{}
if(miao>59)
{miao=0;}
delay(5);
}
}
void table_data(unsigned char a1,unsigned char a2,unsigned char a3)
{unsigned char d;
p1=zhifu;
d=a1/10;
p1=p1+d;
shi_T=*p1;
p1=zhifu;
d=a1%10;
p1=p1+d;
shi_L=*p1;
p1=zhifu;
d=a2/10;
p1=p1+d;
fen_T=*p1;
p1=zhifu;
d=a2%10;
p1=p1+d;
fen_L=*p1;
p1=zhifu;
d=a3/10;
p1=p1+d;
miao_T=*p1;
p1=zhifu;
d=a3%10;
p1=p1+d;
miao_L=*p1;
}
void led6(unsigned char a,unsigned char b,unsigned char c,unsigned char d,unsigned char e,unsigned char f)
{P0=a;
P2=0x7F;
delay(1);
P0=b;
P2=0xBF;
delay(1);
P0=c;
P2=0xDF;
delay(1);
P0=d;
P2=0xEF;
delay(1);
P0=e;
P2=0xF7;
delay(1);
P0=f;
P2=0xFB;
delay(1);
}
void start_iic()/*開始信號函數*/
{
SDA=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;/*IIC總線起動*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;/*總線忙碌*/
}
void stop_iic()/*總線停止函數*/
{SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void send_data(unsigned char a)/*送字節函數*/
{ unsigned char b;
unsigned char conbit=8;/*送出8位數據的控制變量*/
b=a;
while(conbit)
{
if(b&0x80)
{SDA=1;}/*求出最高位*/
else
{SDA=0;}
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=1;/*將最高位數值送出*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;/*送完值*/
conbit--;
b=b<<1;/*向左移一位*/
}
}
unsigned char rcv_data()/*接收一個字節函數*/
{unsigned char b=0;
unsigned char i;
for(i=0;i<8;i++)
{SDA=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=b<<1;
if(SDA==1)
{b++;}
SCL=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
return(b);
}
bit send_ack()/*送出答應信號*/
{ bit ACKbit;
SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
bit rcv_ack()/*接收答應信號函數*/
{ bit ACKbit=1;
SDA=1;/*準備接收答應信號*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=1;/*接收答應位*/
if(SDA==1)
ACKbit=1;/*無效的答應信叼*/
else
ACKbit=0;/*有效的答應信號*/
SCL=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
return(ACKbit);
}
void iic_writer(unsigned char a1,unsigned char a2,unsigned char a3)/*第一個變量為器件地址寫,第二個變量為器件內單元地址,第三個變量為發送的數據*/
{ start_iic();
send_data(a1);
rcv_ack();
send_data(a2);
rcv_ack();
send_data(a3);
rcv_ack();
stop_iic();
}
unsigned char iic_read(unsigned char a1,unsigned char a2,unsigned char a3)/*第一個變量是器件地址寫,第二個變量為器件內單片地址,第三個變量為器件地址讀*/
{ unsigned char b;
start_iic();
send_data(a1);
rcv_ack();
send_data(a2);
rcv_ack();
start_iic();
send_data(a3);
rcv_ack();
b=rcv_data();
send_ack();
stop_iic();
return(b);
}
void sure()/*確認函數,按下確認鍵把設定的時間計下*/
{
if(P1_3==0)
{delay(20);
panduan=0;
delay(20);
}
iic_writer(Addwr,01,shi);
iic_writer(Addwr,02,fen);
iic_writer(Addwr,02,miao);
}
wait_key()
{if(P1_3==0)
{
delay(20);
TR0=!TR0;
delay(20);
}
}
main()
{
EA=1;/*開定時器T0中斷*/
ET0=1;
TMOD=0x01;/*定時器選擇T0工作在方式1*/
TH0=0xC3;
TL0=0x50;
panduan=1;
while(panduan)
{key();
table_data(shi,fen,miao);
led6(fen_T,fen_L,miao_T,miao_L,shi_T,shi_L);
sure();
}
panduan=1;
TR0=1;/*開定時器,正試工作*/
while(panduan)
{
table_data(shi,fen,miao);
led6(fen_T,fen_L,miao_T,miao_L,shi_T,shi_L);
wait_key();
}
}
time0() interrupt 1/*中斷程序*/
{
TR0=0;
TH0=0x3C;
TL0=0x50;
temp--;
if(temp==0xFF)
{temp=20;
miao--;
if(miao==0xFF)
{ miao=59;
fen--;
if(fen==0xFF)
{ fen=59;
shi--;
if(shi==0xFF)
{
P1_4=1;
}
}
}
}
TR0=1;
}