|
源代碼:
#include <reg52.H>
#define uchar unsigned char
#define uint unsigned int
unsigned char dispcount;
sbit a=P1^0;//段控制
sbit b=P1^1;//位控制
sbit LeDen=P1^2;//LED燈控制端
sbit Line=P1^3;//點(diǎn)陣行控制端
sbit lcd_en=P1^7;//1602液晶使能端
sbit rst=P3^4;//DS1302復(fù)位端,低電平關(guān)閉
sbit SDA=P3^2;
sbit SCL=P3^3;
unsigned char Duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Weima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar temp[8];
uchar tempdata[8];
//bit ack;
///////延時(shí)函數(shù)//////
void Delay(uint z)
{
uchar x;
for(;z>0;z--)
for(x=123;x>0;x--);
}
void Delayus()
{;;}
//////////啟動(dòng)總線////////
void Start()
{
SDA=1;
SCL=1;
Delayus();
SDA=0;
Delayus();
SCL=0;
}
//////////結(jié)束總線///////////
void Stop()
{
SDA=0;
SCL=1;
Delayus();
SDA=1;
Delayus();
}
//////非應(yīng)答函數(shù)/////////
void Noack()
{
SDA=1;
Delayus();
SCL=1;
Delayus();
SCL=0;
}
void Ack()
{
SDA=0;
Delayus();
SCL=1;
Delayus();
SCL=0;
}
/////////數(shù)據(jù)發(fā)送函數(shù)//////
void Send(uchar c)
{
uchar bite;
for(bite=0;bite<8;bite++)
{
if((c<<bite)&0x80)SDA=1;
else SDA=0;
SCL=1;
Delayus();
SCL=0;
}
SDA=1;
Delayus();
Delayus();
Delayus();
}
///////接收函數(shù)///////////
uchar Rec()
{
uchar byte,bite;
SDA=1;
for(bite=0;bite<8;bite++)
{
SCL=0;
Delayus();
SCL=1;
Delayus();
byte<<=1;
if(SDA==1)byte+=1;
}
SCL=0;
Delayus();
return(byte);
}
//////////顯示函數(shù)//////////////
void Display(unsigned char FirstBit,unsigned char Num)
{
static uchar i=0;
a=1;
P0=0;
Delayus();
a=0;
b=1;
P0=Weima[i+FirstBit];
Delayus();
b=0;
a=1;
P0=tempdata[i];
Delayus();
a=0;
i++;
if(i==Num)
i=0;
}
///////定時(shí)器初始化//////////
void Init_Timer0(void)
{
TMOD |= 0x01; //使用模式1,16位定時(shí)器,使用"|"符號(hào)可以在使用多個(gè)定時(shí)器時(shí)不受影響
EA=1; //總中斷打開(kāi)
ET0=1; //定時(shí)器中斷打開(kāi)
TR0=1; //定時(shí)器開(kāi)關(guān)打開(kāi)
}
///////////讀取數(shù)值////////
uchar Read(uchar AI)
{
uchar z;
Start();
Send(0x90);
Ack();
Send(0x40|AI); //括號(hào)中的參數(shù)AI數(shù)值從0--3為四路AD的地址,通過(guò)改變?cè)搮?shù)可以改變輸入通道
Ack();
Start();
Send(0x91);
Ack();
z=Rec();
Noack();
Stop();
return(z);
}
////////主函數(shù)////////////
void Main()
{
uchar num=0;
rst=0;//關(guān)閉1302時(shí)鐘
lcd_en=0;//關(guān)閉1602液晶
P0=0X00; //關(guān)閉點(diǎn)陣
Line=0;
P0=0XFF; //關(guān)閉LED燈
LeDen=0;
Init_Timer0();
while(1)
{
num=Read(0);//括號(hào)中的參數(shù)0--3為四路AD的地址,通過(guò)改變?cè)搮?shù)可以改變輸入通道
tempdata[0]=Duanma[num/1000];
tempdata[1]=Duanma[num/100];
tempdata[2]=Duanma[(num%100)/10];
tempdata[3]=Duanma[(num%100)%10];
Delay(100);
}
}
void Timer0(void) interrupt 1
{
TH0=(65536-2000)/256; //重新賦值
TL0=(65536-2000)%256;
Display(0,8); //顯示函數(shù),使用中斷顯示
}
|
|