#include <pic.h>
__CONFIG(HS&WDTDIS&LVPDIS);
#define _XTAL_FREQ 40000000
#define E RB3
#define RW RB4
#define RS RB5
const unsigned char table[16]={0,6,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char temp1,temp2;
void delay(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void test(void)
{
TRISD7=1;
RS=0;
RW=1;
E=1;
while(RD7==1);
E=0;
TRISD7=0;
}
void write_data(unsigned char data)
{
RS=1;
RW=0;
PORTD=data;
E=1;
E=0;
}
void write_data_test(unsigned char data)
{
test();
RS=1;
RW=0;
PORTD=data;
E=1;
E=0;
}
void write_command(unsigned char command)
{
RS=0;
RW=0;
PORTD=command;
E=1;
E=0;
}
void write_command_test(unsigned char command)
{
test();
RS=0;
RW=0;
PORTD=command;
E=1;
E=0;
}
void write_address(unsigned char x,unsigned char y)
{
x&=0X0F;
y&=0X01;
if(y==0)
write_command_test(0X80|x);
if(y==1)
write_command_test((0X40+x)|0X80);
}
void display(unsigned char x,unsigned char y,unsigned char data)
{
write_address(x,y);
write_data_test(data);
}
void init(void)
{
delay(1500);
write_command(0X38);
delay(500);
write_command(0X38);
delay(500);
write_command(0X38);
write_command_test(0X38);
write_command_test(0X08);
write_command_test(0X01);
write_command_test(0X06);
write_command_test(0X0C);
}
unsigned char reset(void)
{
unsigned int i;
unsigned char k;
TRISA4=0;
RA4=0;
i=67;
while(i>0)
i--;
TRISA4=1;
i=6;
while(i>0)
i--;
if(RA4==0)
k=0;
else
k=1;
i=31;
while(i>0)
i--;
return k;
}
void writebyte(unsigned char data)
{
unsigned char i;
unsigned char j;
for(j=0;j<8;j++)
{
if(data&0x01)
{
TRISA4=0;
RA4=0;
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
TRISA4=1;
i=5;
while(i>0)
i--;
}
else
{
TRISA4=0;
RA4=0;
i=5;
while(i>0)
i--;
TRISA4=1;
NOP();
NOP();
}
data=data>>1;
}
}
unsigned char readbyte(void)
{
unsigned char buf=0;
unsigned char i,j;
for(j=1;j<=8;j++)
{
buf=buf>>1;
TRISA4=0;
RA4=0;
NOP();
NOP();
TRISA4=1;
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
if(RA4==1)
buf|=0x80;
i=4;
while(i>0)
i--;
}
return buf;
}
unsigned char convert(void)
{
unsigned int i;
if(reset()==0)
{
i=77;
while(i>0)
i--;
writebyte(0xcc);
writebyte(0x44);
return 1;
}
else
{
return 0;
}
}
void readflash(void)
{
unsigned int i;
unsigned char low,high;
if(reset()==0)
{
i=77;
while(i>0)
i--;
writebyte(0xcc);
writebyte(0xbe);
low=readbyte();
high=readbyte();
temp2=low&0x0f;
temp1=(low>>4)|(high<<4);
}
else
{
temp1=0;
temp2=0;
}
}
void main(void)
{
unsigned char buf;
double temperature;
TRISA=0B11101111;
PORTA=0B00000000;
TRISB=0B11000111;
TRISD=0B00000000;
PORTB=0B00000000;
PORTD=0B00000000;
TRISC=0B11111011;
PORTC=0B00000000;
init();
display(2,0,'T');
display(3,0,'E');
display(4,0,'M');
display(5,0,'P');
display(6,0,'E');
display(7,0,'R');
display(8,0,'A');
display(9,0,'T');
display(10,0,'U');
display(11,0,'R');
display(12,0,'E');
while(1)
{
if(convert()==1)
{
readflash();
if(temp1>99)
temp1=99;
if(temp2>15)
temp2=0;
display(5,1,temp1/10+'0');
display(6,1,temp1%10+'0');
display(7,1,'.');
display(8,1,table[temp2]/10+'0');
display(9,1,table[temp2]%10+'0');
temperature=temp1+(double)table[temp2]*0.01;
if(temperature>30)
{
PORTC=0B00000100;
__delay_ms(40);
PORTC=0B00000000;
__delay_ms(15);
}
}
}
}
PIC单片机使用1602显示DS18B20温度数据
需积分: 11 190 浏览量
2018-10-08
14:06:11
上传
评论 6
收藏 108KB RAR 举报
blinksss
- 粉丝: 0
- 资源: 1
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第39题组合总和.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第38题外观数列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第37题解数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第36题有效的数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第35题搜索插入位置.zip
- index.wxml
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈