找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1791|回復(fù): 0
收起左側(cè)

1602 18b20 1302單片機(jī)驅(qū)動(dòng)程序

[復(fù)制鏈接]
ID:240358 發(fā)表于 2017-10-17 20:06 | 顯示全部樓層 |閱讀模式
分享一個(gè)1602 18b20 1302驅(qū)動(dòng)
單片機(jī)源程序如下:
  1. #include<reg52.h>
  2. #include<intrins.h>
  3. typedef unsigned char uchar;
  4. typedef unsigned int uint;
  5. sbit TSCIK=P1^0;
  6. sbit TI0=P1^1;
  7. sbit TRST=P1^2;
  8. sbit RS=P3^5;
  9. sbit RW=P3^6;
  10. sbit EN=P3^4;
  11. sbit DS=P2^2;
  12. void delay_us(uchar us)
  13. {
  14.         while(us--);
  15. }
  16. void read_1602busy()//判斷液晶忙不忙
  17. {
  18.         uchar busy;
  19.         P0=0xff;
  20.         RS=0;
  21.         RW=1;
  22.         do
  23.         {
  24.                  EN=1;
  25.                 busy=P0;
  26.                 EN=0;
  27.     }while(busy&0x80);

  28. }
  29. void write_1602cmd(uchar cmd)//對(duì)1602寫命令
  30. {
  31.         read_1602busy();
  32.         RS=0;
  33.         RW=0;
  34.         P0=cmd;
  35.         EN=1;
  36.         EN=0;
  37. }
  38. void write_1602dat(uchar dat)//對(duì)1602寫數(shù)據(jù)
  39. {
  40.          read_1602busy();
  41.          RS=1;
  42.          RW=0;
  43.          P0=dat;
  44.          EN=1;
  45.          EN=0;
  46. }
  47. /********************************************/
  48. /*函數(shù)名稱:顯示字符串*/
  49. /*函數(shù)功能:在指定位置顯示一個(gè)字符串*/
  50. /*輸入:x要顯示的橫坐標(biāo)取值0-40,y要顯示的縱坐標(biāo)取值0-1
  51. ,*str:需要顯示的字符串*/
  52. void dis_1602str(uchar x,uchar y,uchar *str)
  53. {
  54.         if(y)        x|=0x40;
  55.         x|=0x80;
  56.         while(*str!='\0')
  57.         {
  58.                 write_1602dat(*str++);
  59.         }
  60. }
  61. /*******************************************/
  62. /*函數(shù)名稱:顯示字符*/
  63. /*函數(shù)功能:在指定位置顯示一個(gè)字符*/
  64. /*輸入:x要顯示的橫坐標(biāo)取值0-40,y要顯示的縱坐標(biāo)取值0-1
  65. ,dat:需要顯示的數(shù)據(jù)已ASCLL形式*/
  66. void dis_1602onechar(uchar x,uchar y,uchar dat)
  67. {
  68.         if(y)        x|=0x40;
  69.         x|=0x80;
  70.         write_1602cmd(x);
  71.         write_1602dat(dat);
  72. }
  73. void init_1602()//1602初始化
  74. {
  75.         write_1602cmd(0x38);//16*2顯示
  76.         write_1602cmd(0x0c);//開顯示
  77.         write_1602cmd(0x06);//讀一個(gè)字節(jié)后指針加一
  78.         write_1602cmd(0x01);//清除顯示/
  79. }
  80. void write_ds1302_dat(uchar cmd,uchar dat)//寫ds1302數(shù)據(jù)
  81. {
  82.         uchar i;
  83.         TRST=0;                //拉低使能端
  84.         TSCIK=0;    //拉低數(shù)據(jù)總線
  85.         TRST=1;           //拉高使能端,產(chǎn)生上升沿
  86.         for(i=0;i<8;i++)  //每次寫一位,寫八次
  87.         {
  88.                 TSCIK=0;
  89.                 TI0=cmd&0x01;
  90.                 TSCIK=1;
  91.                 cmd>>=1;
  92.         }
  93.         for(i=0;i<8;i++)
  94.         {
  95.                 TSCIK=0;
  96.                 TI0=dat&0x01;
  97.                 TSCIK=1;
  98.                 dat>>=1;
  99.         }
  100. }
  101. uchar read_ds1302_dat(uchar cmd)//讀ds1302數(shù)據(jù)
  102. {
  103.         uchar i,dat;
  104.         TRST=0;
  105.         TSCIK=0;
  106.         TRST=1;
  107.         for(i=0;i<8;i++)
  108.         {
  109.                 TSCIK=0;
  110.                 TI0=cmd&0x01;
  111.                 TSCIK=1;
  112.                 cmd>>=1;
  113.         }
  114.         for(i=0;i<8;i++)
  115.         {
  116.                 TSCIK=0;
  117.                 dat>>=1;
  118.                 if(TI0)        dat|=0x80;
  119.                 TSCIK=1;
  120.         }
  121.         return dat;
  122. }
  123. uchar dat_to_bcd(uchar dat)//數(shù)據(jù)轉(zhuǎn)bcd碼
  124. {
  125.         uchar dat1,dat2;
  126.         dat1=dat/10;
  127.         dat2=dat%10;
  128.         dat2=dat2+dat1*16;
  129.         return dat2;
  130. }
  131. uchar bcd_to_dat(uchar dat)//bcd碼轉(zhuǎn)數(shù)據(jù)
  132. {
  133.         uchar dat1,dat2;
  134.         dat1=dat/16;
  135.         dat2=dat%16;
  136.         dat2=dat2+dat1*10;
  137.         return dat2;
  138. }
  139. bit ds18b20_init()//da18b20初始化
  140. {
  141.         bit i;
  142.         DS=1;
  143.         _nop_();
  144.         DS=0;
  145.         delay_us(75);
  146.         DS=1;
  147.         delay_us(4);
  148.         i=DS;
  149.         delay_us(20);
  150.         DS=1;
  151.         _nop_();
  152.         return(i);
  153. }
  154. void write_byte18b20(uchar dat)//ds18b20寫數(shù)據(jù)
  155. {
  156.         uchar i;
  157.         for(i=0;i<8;i++)
  158.         {
  159.                 DS=0;
  160.                 _nop_();
  161.                 DS=dat&0x01;
  162.                 delay_us(10);
  163.                 DS=1;
  164.                 _nop_();
  165.                 dat>>=1;
  166.         }
  167. }
  168. uchar read_byte18b20()//ds188b20讀數(shù)據(jù)
  169. {
  170.         uchar i,j,dat;
  171.         for(i=0;i<8;i++)
  172.         {
  173.                 DS=0;
  174.                 _nop_();
  175.                 DS=1;
  176.                 _nop_();
  177.                 j=DS;
  178.                 delay_us(10);
  179.                 DS=1;
  180.                 _nop_();
  181.                 dat=(j<<7|(dat>>1));
  182.         }
  183.         return(dat);
  184. }
  185. /**************************/
  186. /***函數(shù)功能:獲得溫度(三位數(shù)無小數(shù)點(diǎn))***/
  187. uint get_tmp()
  188. {
  189.         uchar L,M;
  190.     uint i;
  191.     ds18b20_init();
  192.         write_byte18b20(0xcc);//發(fā)送跳躍ROM指令
  193.         write_byte18b20(0x44);//發(fā)送溫度轉(zhuǎn)換指令
  194.         ds18b20_init();
  195.         write_byte18b20(0xcc);
  196.         write_byte18b20(0xbe);
  197.         L=read_byte18b20();
  198.         M=read_byte18b20();
  199.         i=M;
  200.         i<<=8;
  201.         i|=L;
  202.         i=i*0.0625*10+0.5;//讀取正溫度值
  203.         return(i);
  204. }
  205. void main()
  206. {
  207.         uchar sec,min,hour,secshi,secge,minshi,minge,hourshi,hourge,ge,shi,bai,k;
  208.         uint i;
  209.         write_ds1302_dat(0x8e,0);
  210.         write_ds1302_dat(0x80,dat_to_bcd(00));//對(duì)秒寫數(shù)據(jù)
  211.         write_ds1302_dat(0x82,dat_to_bcd(02));//對(duì)分寫數(shù)據(jù)
  212.         write_ds1302_dat(0x84,dat_to_bcd(17));//對(duì)時(shí)寫數(shù)據(jù)
  213.         write_ds1302_dat(0x8e,0x80);
  214.         init_1602();
  215.         while(1)
  216.         {
  217.                 write_ds1302_dat(0x8e,0);
  218.                 sec= bcd_to_dat(read_ds1302_dat(0x81));//秒
  219.                 min= bcd_to_dat(read_ds1302_dat(0x83));//分
  220.                 hour=bcd_to_dat(read_ds1302_dat(0x85));//時(shí)
  221. ……………………

  222. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
lcd1602,ds18b20,ds1302驅(qū)動(dòng).doc (4.41 KB, 下載次數(shù): 5)


回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表