單片機驅(qū)動DS1302時間DS18B20溫度12864液晶顯示完整程序設計
//從 1-wire 總線上讀取一個字節(jié)
byte read_byte(void)
{
byte i;
byte value = 0;
for (i=8;i>0;i--)
{
value>>=1;
KEY = 0; // pull DQ low to start timeslot
KEY = 1; // then return high
delay1(1); //for (i=0; i3; i++);
if(KEY)value|=0x80;
delay1(6); // wait for rest of timeslot
}
return(value);
}
//向 1-WIRE 總線上寫一個字節(jié)
void write_byte(char val)
{
byte i;
for (i=8; i>0; i--) // writes byte, one bit at a time
{
KEY = 0; // pull DQ low to start timeslot
KEY = val0x01;
delay1(5); // hold value for remainder of timeslot
KEY = 1;
val=val/2;
}
delay1(5);
}
//讀取溫度
float wendu(void)
{
float tem;
union{
byte c[2];
int x;
}temp;
ow_reset();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
temp.c[1]=read_byte(); //lsd
temp.c[0]=read_byte(); //msd
ow_reset();
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
tem=0.0;
if(temp.x 0x01) tem+=0.0625;temp.x>>=1;
if(temp.x 0x01) tem+=0.125;temp.x>>=1;
if(temp.x 0x01) tem+=0.25;temp.x>>=1;
if(temp.x 0x01) tem+=0.5;temp.x>>=1;
tem+=temp.x;
return tem;
}
void cs1_lcd(){ ////////選擇左邊
bit a=1;
cs1=a;
cs2=!a;
}
void cs2_lcd(){ ////////選擇右邊
bit a=1;
cs1=!a;
cs2=a;
}
void busy_lcd(){//////判斷忙
bit a=1;
di=!a;
rw=a;
e=a;
while(P07==a);
}
w_lcd(unsigned b){ ///寫a到12864
bit a=1;
busy_lcd();
rw =!a;
di =a;
P0=b;
e=a;
e=!a;
}
void dis_lcd(){//打開顯示選擇第一行顯示
bit a=1;
busy_lcd();
rw=!a;
di=!a;
P0=0x3f;
e=a;
e=!a;
P0=0xc0;
e=a;
e=!a;
}
y_lcd(unsigned a){///由A確定y軸
bit b=1;
busy_lcd();
rw=!b;
di=!b;
P0=a;
P07=b;
P06=!b;
P05=b;
P04=b;
P03=b;
e=b;
e=!b;
}
x_lcd(unsigned a){//由a確定X軸
bit b=1;
busy_lcd();
rw=!b;
di=!b;
P0=a;
P07=!b;
P06=b;
e=b;
e=!b;
}
void clr_lcd(){///清屏
bit a=0;
unsigned x,y,date,i;
cs1_lcd();
date=0x00;
for(i=0;i2;i++){
for(y=0;y8;y++)
for(x=0;x64;x++){
x_lcd(x);
y_lcd(y);
w_lcd(date);
}
cs2_lcd();}
}
unsigned char code shu[18][16]={///0到9數(shù)字
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},//0
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},//1
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},//2
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},//3
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00},///4
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00},//5
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00},//6
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00},//7
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},//8
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00},//9
{0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00},// :
{0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00},// /
};
unsigned char code shu1[9][32]={///
{0x02,0x42,0x42,0xFE,0x42,0x42,0xFE,0x02,0x02,0xFA,0x02,0x02,0xFE,0x00,0x00,0x00,0x08,0x08,0x08,0x07,0x84,0x44,0x23,0x18,0x06,0x01,0x3E,0x40,0x43,0x40,0x78,0x00},//現(xiàn)12
{0x00,0x04,0x04,0xC4,0x64,0x9C,0x87,0x84,0x84,0xE4,0x84,0x84,0x84,0x84,0x04,0x00,0x04,0x02,0x01,0x7F,0x00,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x00},//在 13
{0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00},//時 14
{0x00,0xF8,0x01,0x06,0x00,0xF0,0x92,0x92,0x92,0x92,0xF2,0x02,0x02,0xFE,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x07,0x04,0x04,0x04,0x04,0x07,0x40,0x80,0x7F,0x00,0x00},//間 15
{0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,0x2A,0x2A,0x2A,0x2A,0x3E,0x00,0x00,0x00,0x00,0x48,0x46,0x41,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x49,0x49,0x41,0x40,0x00},//星 16
{0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00},//期 17
{0x10,0x21,0x86,0x70,0x00,0x7E,0x4A,0x4A,0x4A,0x4A,0x4A,0x7E,0x00,0x00,0x00,0x00,0x02,0xFE,0x01,0x40,0x7F,0x41,0x41,0x7F,0x41,0x41,0x7F,0x41,0x41,0x7F,0x40,0x00},//溫18
{0x00,0x00,0xFC,0x04,0x24,0x24,0xFC,0xA5,0xA6,0xA4,0xFC,0x24,0x24,0x24,0x04,0x00,0x80,0x60,0x1F,0x80,0x80,0x42,0x46,0x2A,0x12,0x12,0x2A,0x26,0x42,0xC0,0x40,0x00},//度19
};
unsigned char time1[7]={0x30,0x00,0x16,8,7,2,8};//分別是秒,分,時,日,月,星期,年 次子程序出自http://www.51hei.com hkcd斑竹,已通過測試無誤。
delay()
{
unsigned char i;
i=10;
i=10;
i=10;
i=10;
i=10;
i=10;
i=10;}
w_1302(unsigned char dat,unsigned char shu){//寫一個八位的數(shù)去DS1302 //上升效 ;;;選寫地址再寫數(shù)據(jù)
ACC=dat;//地址
sclk=0;
rst=1;//
io=acc0;///
sclk=0;
delay();
sclk=1;
io=acc1;
sclk=0;
delay();
sclk=1;
io=acc2;
sclk=0;
delay();
sclk=1;
io=acc3;
sclk=0;
delay();
sclk=1;
io=acc4;
sclk=0;
delay();
sclk=1;
io=acc5;
sclk=0;
delay();
sclk=1;
io=acc6;
sclk=0;
delay();
sclk=1;
io=acc7;
sclk=0;
delay();
sclk=1;///
//sclk=0;
ACC=shu; //數(shù)據(jù)
io=acc0;///
sclk=0;
delay();
sclk=1;
io=acc1;
sclk=0;
delay();
sclk=1;
io=acc2;
sclk=0;
delay();
sclk=1;
io=acc3;
sclk=0;
delay();
sclk=1;
io=acc4;
sclk=0;
delay();
sclk=1;
io=acc5;
sclk=0;
delay();
sclk=1;
io=acc6;
sclk=0;
delay();
sclk=1;
io=acc7;
sclk=0;
delay();
sclk=1;///
rst=0;
sclk=0;
} ///寫完畢
unsigned char r_1302(unsigned char ADD){
sclk=0;
rst=1;
ACC=ADD; //地址
io=acc0;///
sclk=0;
delay();
sclk=1;
io=acc1;
sclk=0;
delay();
sclk=1;
io=acc2;
sclk=0;
delay();
sclk=1;
io=acc3;
sclk=0;
delay();
sclk=1;
io=acc4;
sclk=0;
delay();
sclk=1;
io=acc5;
sclk=0;
delay();
sclk=1;
io=acc6;
sclk=0;
delay();
sclk=1;
io=acc7;
sclk=0;/////
delay();
sclk=1;
delay();
sclk=0;
acc0=io;
delay();
sclk=1;
delay();
sclk=0;
acc1=io;
delay();
sclk=1;
delay();
sclk=0;
acc2=io;
delay();
sclk=1;
delay();
sclk=0;
acc3=io;
delay();
sclk=1;
delay();
sclk=0;
acc4=io;
delay();
sclk=1;
delay();
sclk=0;
acc5=io;
delay();
sclk=1;
delay();
sclk=0;
acc6=io;
delay();
sclk=1;
delay();
sclk=0;
acc7=io;
delay();
rst=0;
sclk=0;
return(ACC);
}
ds1302_c()
{
w_1302(0x80,0x80);
w_1302(0x8e,0x00);
w_1302(0x80,time1[0]); //;秒
w_1302(0x82,time1[1]); // 分=0
w_1302(0x84,time1[2]); //時 =0
w_1302(0x86,time1[3]); // 日
w_1302(0x88,time1[4]);// 月
w_1302(0x8a,time1[5]);//星期
w_1302(0x8c,time1[6]);//年
w_1302(0x8e,0x80);
w_1302(0x90,0xb8);
w_1302(0x80,0x00);
}
//x_lcd(unsigned a){//由a確定X軸 //y_lcd(unsigned a){///由A確定y軸
//void dis_lcd(){//打開顯示選擇第一行顯示 //w_lcd(unsigned b){ ///寫a到12864
//void busy_lcd(){//////判斷忙 //void cs1_lcd(){ ////////選擇左邊
//void cs2_lcd(){ ////////選擇右邊
d12864_lcd(unsigned char x,y,k){
unsigned char ix,iy,i;
dis_lcd();//打開顯示
if(x>63){
x=x-64;
cs2_lcd();
}
else cs1_lcd();
i=0;
if(k12){
for(iy=0;iy2;iy++)
for(ix=0;ix8;ix++){
y_lcd(iy+y);
x_lcd(ix+x);
w_lcd(shu[k][i]);
i++;
}
}
else{k-=12;
for(iy=0;iy2;iy++)
for(ix=0;ix16;ix++){
y_lcd(iy+y);
x_lcd(ix+x);
w_lcd(shu1[k][i]);
i++;
}
}
}
51單片機相關文章:51單片機教程
單片機相關文章:單片機教程
單片機相關文章:單片機視頻教程
單片機相關文章:單片機工作原理
評論