模塊化51庫程序溫度計18B20程序,保證好用,有需要的拿走吧!!!!!
單片機源程序如下:
- #include <REGX52.H>
- #include <intrins.h>
- //=====18B20位申明======//
- sbit cs=P2^2;
- sbit dula=P2^6;
- sbit wela=P2^7;
- //=====18B20位申明完成======//
- unsigned char code table[]=
- {
- 0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f
- };
- unsigned char temp,temp1; //18B20全局變量。
- //===============================================//
- //====11.0592Mhz晶振前提下,延時1毫秒左右=====//
- void delay1ms(unsigned int ms)
- {
- unsigned int x,y;
- for(x=ms;x>0;x--)
- for(y=112;y>0;y--);
- }
- //================延時1毫秒完畢==============//
- //=============================================//
- //====11.0592Mhz晶振前提下,延時110微秒左右====//
- void delay110us(unsigned int us)
- {
- unsigned int x,y;
- for(x=us;x>0;x--)
- for(y=11;y>0;y--);
- }
- //=============================================//
- //=====18B20函數(shù)申明======//
- void init_18B20(void);
- void write_18B20byte(unsigned char record);
- unsigned char read_18B20byte(void);
- unsigned char shift_18B20temp(void);
- void display_18B20(unsigned char temp,unsigned char temp1);
- //=====18B20函數(shù)申明======//
- //======18B20用到的函數(shù)=====//
- void init_18B20(void)
- {
- cs=1;
- cs=0;
- delay110us(8);
- cs=1;
- delay110us(4);
- cs=1;
- }
- void write_18B20byte(unsigned char record)
- {
- unsigned char a;
- cs=1;
- for(a=0;a<8;a++)
- {
- cs=0;
- _nop_();
- cs=record&0x01;
- delay110us(1);
- cs=1;
- record>>=1;
- }
- delay110us(2);
- }
- unsigned char read_18B20byte(void)
- {
- unsigned char a,value;
- cs=1;
- for(a=0;a<8;a++)
- {
- cs=0;
- _nop_();
- value>>=1;
- cs=1;
- if(cs)
- value|=0x80;
- delay110us(1);
- }
- return value;
- }
- unsigned char shift_18B20temp(void)
- {
- unsigned char low,high,temp=0;
- init_18B20();
- write_18B20byte(0xcc);
- write_18B20byte(0x44);
- init_18B20();
- write_18B20byte(0xcc);
- write_18B20byte(0xbe);
- low=read_18B20byte();
- high=read_18B20byte();
- temp1=(low&0x0f)*6.25;
- high<<=4;
- low>>=4;
- high=high+low;
- delay110us(5);
- return high;
- }
- //======18B20用到的函數(shù)完成=====//
- void display_18B20(unsigned char temp,unsigned char temp1)
- {
- wela=1;
- P0=0xfe;
- wela=0;
- dula=1;
- P0=table[temp/10];
- dula=0;
- delay1ms(2);
- wela=1;
- P0=0xfd;
- wela=0;
- dula=1;
- P0=table[temp%10]+0x80;
- dula=0;
- delay1ms(2);
- wela=1;
- P0=0xfb;
- wela=0;
- dula=1;
- P0=table[temp1/10];
- dula=0;
- delay1ms(2);
- wela=1;
- P0=0xf7;
- wela=0;
- dula=1;
- P0=table[temp1%10];
- dula=0;
- delay1ms(2);
- }
- void main()
- {
- while(1)
- {
- temp=shift_18B20temp();
- display_18B20(temp,temp1);
- }
- }
復制代碼
以上資料51hei提供下載:
18B20.rar
(20.92 KB, 下載次數(shù): 27)
2019-8-25 11:40 上傳
點擊文件名下載附件
模塊化51庫程序溫度計18B20程序,保證好用,有需要的拿走吧!!!!! 下載積分: 黑幣 -5
|