分享一个1602 18b20 1302驱动
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- sbit TSCIK=P1^0;
- sbit TI0=P1^1;
- sbit TRST=P1^2;
- sbit RS=P3^5;
- sbit RW=P3^6;
- sbit EN=P3^4;
- sbit DS=P2^2;
- void delay_us(uchar us)
- {
- while(us--);
- }
- void read_1602busy()//判断液晶忙不忙
- {
- uchar busy;
- P0=0xff;
- RS=0;
- RW=1;
- do
- {
- EN=1;
- busy=P0;
- EN=0;
- }while(busy&0x80);
- }
- void write_1602cmd(uchar cmd)//对1602写命令
- {
- read_1602busy();
- RS=0;
- RW=0;
- P0=cmd;
- EN=1;
- EN=0;
- }
- void write_1602dat(uchar dat)//对1602写数据
- {
- read_1602busy();
- RS=1;
- RW=0;
- P0=dat;
- EN=1;
- EN=0;
- }
- /********************************************/
- /*函数名称:显示字符串*/
- /*函数功能:在指定位置显示一个字符串*/
- /*输入:x要显示的横坐标取值0-40,y要显示的纵坐标取值0-1
- ,*str:需要显示的字符串*/
- void dis_1602str(uchar x,uchar y,uchar *str)
- {
- if(y) x|=0x40;
- x|=0x80;
- while(*str!='\0')
- {
- write_1602dat(*str++);
- }
- }
- /*******************************************/
- /*函数名称:显示字符*/
- /*函数功能:在指定位置显示一个字符*/
- /*输入:x要显示的横坐标取值0-40,y要显示的纵坐标取值0-1
- ,dat:需要显示的数据已ASCLL形式*/
- void dis_1602onechar(uchar x,uchar y,uchar dat)
- {
- if(y) x|=0x40;
- x|=0x80;
- write_1602cmd(x);
- write_1602dat(dat);
- }
- void init_1602()//1602初始化
- {
- write_1602cmd(0x38);//16*2显示
- write_1602cmd(0x0c);//开显示
- write_1602cmd(0x06);//读一个字节后指针加一
- write_1602cmd(0x01);//清除显示/
- }
- void write_ds1302_dat(uchar cmd,uchar dat)//写ds1302数据
- {
- uchar i;
- TRST=0; //拉低使能端
- TSCIK=0; //拉低数据总线
- TRST=1; //拉高使能端,产生上升沿
- for(i=0;i<8;i++) //每次写一位,写八次
- {
- TSCIK=0;
- TI0=cmd&0x01;
- TSCIK=1;
- cmd>>=1;
- }
- for(i=0;i<8;i++)
- {
- TSCIK=0;
- TI0=dat&0x01;
- TSCIK=1;
- dat>>=1;
- }
- }
- uchar read_ds1302_dat(uchar cmd)//读ds1302数据
- {
- uchar i,dat;
- TRST=0;
- TSCIK=0;
- TRST=1;
- for(i=0;i<8;i++)
- {
- TSCIK=0;
- TI0=cmd&0x01;
- TSCIK=1;
- cmd>>=1;
- }
- for(i=0;i<8;i++)
- {
- TSCIK=0;
- dat>>=1;
- if(TI0) dat|=0x80;
- TSCIK=1;
- }
- return dat;
- }
- uchar dat_to_bcd(uchar dat)//数据转bcd码
- {
- uchar dat1,dat2;
- dat1=dat/10;
- dat2=dat%10;
- dat2=dat2+dat1*16;
- return dat2;
- }
- uchar bcd_to_dat(uchar dat)//bcd码转数据
- {
- uchar dat1,dat2;
- dat1=dat/16;
- dat2=dat%16;
- dat2=dat2+dat1*10;
- return dat2;
- }
- bit ds18b20_init()//da18b20初始化
- {
- bit i;
- DS=1;
- _nop_();
- DS=0;
- delay_us(75);
- DS=1;
- delay_us(4);
- i=DS;
- delay_us(20);
- DS=1;
- _nop_();
- return(i);
- }
- void write_byte18b20(uchar dat)//ds18b20写数据
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- DS=0;
- _nop_();
- DS=dat&0x01;
- delay_us(10);
- DS=1;
- _nop_();
- dat>>=1;
- }
- }
- uchar read_byte18b20()//ds188b20读数据
- {
- uchar i,j,dat;
- for(i=0;i<8;i++)
- {
- DS=0;
- _nop_();
- DS=1;
- _nop_();
- j=DS;
- delay_us(10);
- DS=1;
- _nop_();
- dat=(j<<7|(dat>>1));
- }
- return(dat);
- }
- /**************************/
- /***函数功能:获得温度(三位数无小数点)***/
- uint get_tmp()
- {
- uchar L,M;
- uint i;
- ds18b20_init();
- write_byte18b20(0xcc);//发送跳跃ROM指令
- write_byte18b20(0x44);//发送温度转换指令
- ds18b20_init();
- write_byte18b20(0xcc);
- write_byte18b20(0xbe);
- L=read_byte18b20();
- M=read_byte18b20();
- i=M;
- i<<=8;
- i|=L;
- i=i*0.0625*10+0.5;//读取正温度值
- return(i);
- }
- void main()
- {
- uchar sec,min,hour,secshi,secge,minshi,minge,hourshi,hourge,ge,shi,bai,k;
- uint i;
- write_ds1302_dat(0x8e,0);
- write_ds1302_dat(0x80,dat_to_bcd(00));//对秒写数据
- write_ds1302_dat(0x82,dat_to_bcd(02));//对分写数据
- write_ds1302_dat(0x84,dat_to_bcd(17));//对时写数据
- write_ds1302_dat(0x8e,0x80);
- init_1602();
- while(1)
- {
- write_ds1302_dat(0x8e,0);
- sec= bcd_to_dat(read_ds1302_dat(0x81));//秒
- min= bcd_to_dat(read_ds1302_dat(0x83));//分
- hour=bcd_to_dat(read_ds1302_dat(0x85));//时
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
lcd1602,ds18b20,ds1302驱动.doc
(4.41 KB, 下载次数: 5)
|