?? 080630.c
字號(hào):
//*********************************************************************/*
/*DS12CR887高精度萬(wàn)年歷 C51程序
編寫(xiě):wsb
日期:2008-7-7
QQ:576515317
功能:電路采用DS12CR887高精度時(shí)鐘芯片,用74HC595驅(qū)動(dòng)15個(gè)碼管
采用模擬串口的方式進(jìn)行數(shù)據(jù)的發(fā)送。三個(gè)獨(dú)立按鍵,一個(gè)功能
鍵,一個(gè)時(shí)間加,一個(gè)時(shí)間減,組成時(shí)間調(diào)整功能。
一個(gè)DS18B20數(shù)字溫度傳感器實(shí)現(xiàn)環(huán)境溫度的讀取。
能夠顯示的內(nèi)容有年、月、日、時(shí)、分、秒、星期和溫度。
*/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,0xbf};
uchar shi,fen,miao,nian,yue,ri,xq,s1num,tt;
uint temp;
bit flag;
//DS12CR887的四個(gè)位聲明
sbit dscs=P2^5;
sbit dsas=P2^4;
sbit dswr=P2^3;
sbit dsds=P2^2;
sbit tempDS=P2^1; //DS18B20 I/0口定義
sbit s1=P3^0; //功能鍵
sbit s2=P3^1; //時(shí)間加
sbit s3=P3^2; //時(shí)間減
sbit rd=P3^7;
sbit SH_CP=P1^4; //移位寄存器時(shí)鐘脈沖
sbit ST_CP=P1^5; //存儲(chǔ)寄存器時(shí)鐘脈沖輸出鎖存器控制
sbit d1=P1^3;//雙二極管閃爍
sbit DS=P1^7; //串行數(shù)據(jù)輸入
void delay(uint count) //延時(shí)子函數(shù)
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
/************************
函數(shù)名:dsreset()
功能 :DS18B20初始化復(fù)位
*************************/
void dsreset(void)
{
uint i;
tempDS=0;
i=103;
while(i>0)i--;
tempDS=1;
i=4;
while(i>0)i--;
}
/************************
函數(shù)名:tmpreadbit()
功能:從DS18B20里讀一位
*************************/
bit tmpreadbit(void)
{
uint i;
bit dat;
tempDS=0;i++; //i++ for delay 小延時(shí)一下
tempDS=1;i++;i++;
dat=tempDS;
i=8;while(i>0)i--;
return (dat);
}
/************************
函數(shù)名:tmpread()
功能:從DS18B20里讀一個(gè)字節(jié)
*************************/
uchar tmpread(void)
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //讀出的數(shù)據(jù)最低位在最前面,這樣剛好//一個(gè)字節(jié)在DAT里
}
return(dat); //將一個(gè)字節(jié)數(shù)據(jù)返回
}
/************************
函數(shù)名:tmpwritebyte()
功能:寫(xiě)一個(gè)字節(jié)到DS18B20里
*************************/
void tmpwritebyte(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1 寫(xiě)1部分
{
tempDS=0;
i++;i++;
tempDS=1;
i=8;while(i>0)i--;
}
else
{
tempDS=0; //write 0 寫(xiě)0部分
i=8;while(i>0)i--;
tempDS=1;
i++;i++;
}
}
}
/************************
函數(shù)名:tmpchange()
功能:給18B20發(fā)送溫度轉(zhuǎn)換命令
*************************/
void tmpchange(void)
{
dsreset(); //初始化DS18B20
delay(1); //延時(shí)
tmpwritebyte(0xcc); // 跳過(guò)序列號(hào)命令
tmpwritebyte(0x44); //開(kāi)始轉(zhuǎn)換
}
/************************
函數(shù)名:tmp()
功能:DS18B20獲取溫度
*************************/
void tmp()
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe); //發(fā)送讀取數(shù)據(jù)命令
a=tmpread(); //連續(xù)讀兩個(gè)字節(jié)數(shù)據(jù)
b=tmpread();
temp=b;
temp=temp<<8;
temp=temp|a; //兩字節(jié)合成一個(gè)整型變量。
tt=temp*0.0625; //得到真實(shí)十進(jìn)制溫度值
temp=tt*10+0.5;
}
////DS12CR887/////
////數(shù)據(jù)讀取/////
uchar read_ds(uchar add)
{
uchar date;
//dscs=0;
dsas=1;
dsds=1;
dswr=1;
dscs=0;
P0=add;
dsas=0;
dsds=0;
P0=0xff;//讀取P0口先給P0口全賦個(gè)一
date=P0;
dsds=1;
dsas=1;
dscs=1;
return date;
}
///DS12CR887數(shù)據(jù)寫(xiě)入/////
void write_ds(uchar add,uchar date)
{
//dscs=0;
dsas=1;
dsds=1;
dswr=1;
dscs=0;
P0=add;
dsas=0;
dswr=0;
P0=date;
dswr=1;
dsas=1;
dscs=1;
}
/************************
函數(shù)名:write_595byte()
功能:模擬串口給595送數(shù)據(jù)
*************************/
void write_595byte(uchar shi_s,uchar fen_s,uchar miao_s,nian_s,yue_s,ri_s,xq_s,temp_s)
{
uchar k,shi_ge,shi_shi,fen_ge,fen_shi,miao_ge,
miao_shi,nian_ge,nian_shi,yue_ge,yue_shi,
ri_ge,ri_shi,xq,temp_shi,temp_ge;
shi_ge=table[shi_s%10]; //先把所有數(shù)據(jù)進(jìn)行分離
shi_shi=table[shi_s/10];
fen_ge=table[fen_s%10];
fen_shi=table[fen_s/10];
miao_ge=table[miao_s%10];
miao_shi=table[miao_s/10];
nian_ge=table[nian_s%10];
nian_shi=table[nian_s/10];
yue_ge=table[yue_s%10];
yue_shi=table[yue_s/10];
ri_ge=table[ri_s%10];
ri_shi=table[ri_s/10];
xq=table[xq_s];
temp_ge=table[temp_s%100/10];
temp_shi=table[temp_s/100];
for(k=0;k<8;k++)
{
SH_CP=0;
miao_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
miao_shi<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
fen_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
fen_shi<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
shi_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
shi_shi<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
ri_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
ri_shi<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
yue_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
yue_shi<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
nian_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
nian_shi<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
xq<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
for(k=0;k<8;k++)
{
SH_CP=0;
temp_ge<<=1; //將數(shù)據(jù)左移把最高位移入到了CY當(dāng)中
DS=CY;
SH_CP=1; //上升沿發(fā)生移位
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -