📄 18b20lcd.c
字号:
#include<pic.h>
__CONFIG(0x3F3A);
# define DQ RA4 //定义18B20数据端口
# define DQ_DIR TRISA4 //定义18B20D口方向寄存器
# define W1_INPUT 1
# define W1_OUTPUT 0
# define FALSE 0
# define TRUE !FALSE
# define DQ_HIGH() DQ_DIR = W1_INPUT
# define DQ_LOW() DQ = 0; DQ_DIR = W1_OUTPUT
# include <pic.h>
# define uch unsigned char
# define unint unsigned int
unsigned int m=0;
char cc1[]="Temperature is :";
char cc2[]="+000.00 Degree";
//PIN define
#define LCDRS RB3
#define LCDRW RB2
#define LCDE RB1
#define LCDDATA PORTD
///////////////////////////////
////////////////////////////////////
void delay_1us(void) //1us延时函数
{
asm("nop");
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<142;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void LCD_en_write(void) //液晶使能
{
LCDE=1;
delay_nus(1);
LCDE=0;
}
void LCD_write_command(unsigned char command) //写指令
{
LCDRS=0; //RS=0
LCDDATA=command;//写低四位
LCD_en_write();
delay_nus(1);
}
void LCD_write_data(unsigned char data) //写数据
{
LCDRS=1; //RS=1
LCDDATA=data; //清高四位
LCD_en_write();
delay_nus(1);
}
void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command( address);
}
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
LCD_write_data( data);
}
void lcdinit()
{TRISD=0x00;
TRISB=0XF1;
RB2=0;
RD6=1;
LCD_en_write();
delay_nus(40);
LCD_write_command(0x38); //4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delay_nms(2);
}
void delay(unint x)
{
unint d;
d=x;
while(--d)
{;}
}
bit reset(void) //初始化18B20
{
static bit presence; //定义一个应答信号
DQ_LOW();
delay(70); //置总线为低电平并保持至少480us
DQ_HIGH(); //等电阻拉高总线并保持15-60us
// delay(5);
while(DQ);
presence=DQ; //接受应答信号
delay(20); //延时60-240us
return(presence); //返回应答信号
}
//*************** 读一位函数******************//
bit read_bit(void)
{
static bit i;
DQ_LOW();
DQ_LOW();
DQ_HIGH();
asm("nop");
asm("nop");
asm("nop");
i=DQ;
delay(3);
return(i);
}
//*********************写一位函数****************//
void write_bit(uch bitval)
{
DQ_LOW();
delay(1);
if (bitval==1)
{
DQ_HIGH();
}
delay(3);
DQ_HIGH();
}
//************** 从18B20中读一个字节**************//
uch read_byte(void)
{
uch i;
uch j;
uch value=0;
for (i=0;i<8;i++)
{
j=read_bit(); //调读位函数
if (j) //如果是 1 置1
{
value|=(0x01<<i); //先读低位,再读高位
asm("nop");
asm("nop");
asm("nop");
}
} //否则置 0
return(value);
}
//*********************向18B20中 写一个字节**************//
void write_byte(uch val)
{
uch i;
uch temp;
for (i=0;i<8;i++)
{
temp=val>>i;
temp&=0x01;
write_bit(temp); //调写位函数
}
asm("nop");
asm("nop");
asm("nop");
}
unsigned int read (void)
{
unsigned int temh,teml;
unsigned int value=0x550;
while (reset()) ; //复位等待从机应答
write_byte(0XCC); //忽略ROM匹配
write_byte(0X44); //发送温度转化命令
delay(25000); //延时100-300us
while( reset()); //再次复位,等待从机应答
write_byte(0XCC); //忽略ROM匹配
write_byte(0XBE); //发送读温度命令
teml =read_byte(); //读出温度低8
temh=read_byte(); //读出温度高8位
DQ_HIGH();
value =teml ; // 低字节
value += temh<<8; // 高字节
return(value);
}
void DS18B20_HEX_to_LCD(unsigned int x,char *p)
{
char temp;
unsigned int temp2;
char symbol='+';
temp=(unsigned char)((x&0x07ff)>>4);
if (x&0x800) symbol='-';
p[0]=symbol;
p[1]=temp/100+'0';
p[2]=temp%100/10+'0';
p[3]=temp%100%10+'0';
if (p[1]=='0') {
if (p[2]=='0') {p[2]=' ';}
p[1]=' ';
}
temp2=(unsigned int)((x&0x000f)*625);
p[5]=temp2/1000+'0';
temp2=temp2%1000;
p[6]=temp2/100+'0';
// temp2=temp2%100;
// p[7]=temp2/10+'0';
// temp2=temp2%10;
// p[8]=temp2+'0';
}
void main()
{
lcdinit();
LCD_write_string(0,0,cc1);
while(1)
{
m=read();
DS18B20_HEX_to_LCD(m,cc2);
delay_nus(50);
LCD_write_string(0,1,cc2);
delay_nus(50);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -