求一基于12MGHZ的8051 ds18b20使用的C程序
答案:1 悬赏:50 手机版
解决时间 2021-12-26 23:08
- 提问者网友:且恨且铭记
- 2021-12-26 01:29
求一基于12MGHZ的8051 ds18b20使用的C程序
最佳答案
- 五星知识达人网友:第幾種人
- 2021-12-26 02:46
#include <reg51.h>
#include <intrins.h>
#define delay1us _nop_()
#define delay10us delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit BUSY=P0^7;
sbit BUS=P3^7;//1 wire bus
code uint8 rom_18b20_A[8]={0x28,0x7A,0x2A,0xCB,0x03,0x00,0x00,0x45};
uint16 tmp_data;
//LCD1602
void wait(void)
{
P0=0xff;
do
{
RS=0;
RW=1;
E=0;
E=1;
}while(BUSY);
E=0;
}
void w_dat(uint8 dat)
{
wait();
E=0;
P0=dat;
RS=1;
RW=0;
E=1;
E=0;
}
void w_cmd(uint8 cmd)
{
wait();
E=0;
P0=cmd;
RS=0;
RW=0;
E=1;
E=0;
}
void lcd_init(void)
{
w_cmd(0x38);
w_cmd(0x06);
w_cmd(0x0C);
w_cmd(0x01);
}
void printlcd(uint8 *str)
{
uint8 i;
w_cmd(0x80);
for(i=0;i<16&&*str;i++,w_dat(*str++));
w_cmd(0xC0);
for(i=0;i<16&&*str;i++,w_dat(*str++));
}
//18B20
void delay(uint16 del)
{
while(del--);
}
void reset_18b20(void)
{
BUS=1;
do
{
delay(50);
BUS=0;
delay(55);//500us
BUS=1;
delay(5);//70us
}while(BUS);
delay(50);
}
void write_byte_18b20(uint8 dat)
{
uint8 i;
BUS=1;
for(i=0;i<8;i++)
{
BUS=0;
BUS=dat%2;
delay(5);
BUS=1;
dat>>=1;
}
}
uint8 read_byte_18b20(void)
{
uint8 i,dat=0;
BUS=1;
for(i=0;i<8;i++)
{
BUS=0;
delay1us;
BUS=1;
delay10us;
dat>>=1;
if(BUS) dat|=0x80;
delay(4);
}
return dat;
}
void romcmd_match_18b20(uint8 *prom)
{
uint8 i;
write_byte_18b20(0x55);
for(i=0;i<8;write_byte_18b20(prom[i++]));
}
void fcmd_convT_18b20(void)
{
write_byte_18b20(0x44);
BUS=1;
do
{
delay(1000);
BUS=0;
delay1us;
BUS=1;
delay10us;
}while(!BUS);
delay(5);
}
void trace_tmpdata(uint16 dat) //Output Data: XXXX XXXX XXXX XXXX (16-bit)
{ // 1-15>|<>| || data |
uint16 outdat=0; // aftdotdata SIGN
uint8 trace_str[]="Temp: ",i=6;
if(dat&0x8000)
{
dat=~dat+1;
outdat|=dat%16*0x100;
outdat|=dat/16;
outdat|=0x80;
}else
{
outdat|=dat%16*0x100;
outdat|=dat/16;
}
if(outdat&0x80) trace_str[i++]='-';
if(outdat%0x80/100%10)
{
trace_str[i++]=outdat%0x80/100%10+'0';
trace_str[i++]=outdat%0x80/10%10+'0';
trace_str[i++]=outdat%0x80%10+'0';
}else if(outdat%0x80/10%10)
{
trace_str[i++]=outdat%0x80/10%10+'0';
trace_str[i++]=outdat%0x80%10+'0';
}else
{
trace_str[i++]=outdat%0x80%10+'0';
}
trace_str[i++]='.';
switch(outdat/0x100)
{
case 0: trace_str[i]='0';break;
case 1: trace_str[i]='1';break;
case 2: trace_str[i]='1';break;
case 3: trace_str[i]='2';break;
case 4: trace_str[i]='3';break;
case 5: trace_str[i]='3';break;
case 6: trace_str[i]='4';break;
case 7: trace_str[i]='4';break;
case 8: trace_str[i]='5';break;
case 9: trace_str[i]='6';break;
case 10: trace_str[i]='6';break;
case 11: trace_str[i]='7';break;
case 12: trace_str[i]='8';break;
case 13: trace_str[i]='8';break;
case 14: trace_str[i]='9';break;
case 15: trace_str[i]='9';break;
default:break;
}
printlcd(trace_str);
}
main()
{
lcd_init();
while(1)
{
reset_18b20();
romcmd_match_18b20(rom_18b20_A);
fcmd_convT_18b20();
reset_18b20();
romcmd_match_18b20(rom_18b20_A);
write_byte_18b20(0xBE);
tmp_data=0;
tmp_data+=read_byte_18b20();
tmp_data+=read_byte_18b20()*0x100;
reset_18b20();
trace_tmpdata(tmp_data);
}
}
#include <intrins.h>
#define delay1us _nop_()
#define delay10us delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us;delay1us
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit BUSY=P0^7;
sbit BUS=P3^7;//1 wire bus
code uint8 rom_18b20_A[8]={0x28,0x7A,0x2A,0xCB,0x03,0x00,0x00,0x45};
uint16 tmp_data;
//LCD1602
void wait(void)
{
P0=0xff;
do
{
RS=0;
RW=1;
E=0;
E=1;
}while(BUSY);
E=0;
}
void w_dat(uint8 dat)
{
wait();
E=0;
P0=dat;
RS=1;
RW=0;
E=1;
E=0;
}
void w_cmd(uint8 cmd)
{
wait();
E=0;
P0=cmd;
RS=0;
RW=0;
E=1;
E=0;
}
void lcd_init(void)
{
w_cmd(0x38);
w_cmd(0x06);
w_cmd(0x0C);
w_cmd(0x01);
}
void printlcd(uint8 *str)
{
uint8 i;
w_cmd(0x80);
for(i=0;i<16&&*str;i++,w_dat(*str++));
w_cmd(0xC0);
for(i=0;i<16&&*str;i++,w_dat(*str++));
}
//18B20
void delay(uint16 del)
{
while(del--);
}
void reset_18b20(void)
{
BUS=1;
do
{
delay(50);
BUS=0;
delay(55);//500us
BUS=1;
delay(5);//70us
}while(BUS);
delay(50);
}
void write_byte_18b20(uint8 dat)
{
uint8 i;
BUS=1;
for(i=0;i<8;i++)
{
BUS=0;
BUS=dat%2;
delay(5);
BUS=1;
dat>>=1;
}
}
uint8 read_byte_18b20(void)
{
uint8 i,dat=0;
BUS=1;
for(i=0;i<8;i++)
{
BUS=0;
delay1us;
BUS=1;
delay10us;
dat>>=1;
if(BUS) dat|=0x80;
delay(4);
}
return dat;
}
void romcmd_match_18b20(uint8 *prom)
{
uint8 i;
write_byte_18b20(0x55);
for(i=0;i<8;write_byte_18b20(prom[i++]));
}
void fcmd_convT_18b20(void)
{
write_byte_18b20(0x44);
BUS=1;
do
{
delay(1000);
BUS=0;
delay1us;
BUS=1;
delay10us;
}while(!BUS);
delay(5);
}
void trace_tmpdata(uint16 dat) //Output Data: XXXX XXXX XXXX XXXX (16-bit)
{ // 1-15>|<>| || data |
uint16 outdat=0; // aftdotdata SIGN
uint8 trace_str[]="Temp: ",i=6;
if(dat&0x8000)
{
dat=~dat+1;
outdat|=dat%16*0x100;
outdat|=dat/16;
outdat|=0x80;
}else
{
outdat|=dat%16*0x100;
outdat|=dat/16;
}
if(outdat&0x80) trace_str[i++]='-';
if(outdat%0x80/100%10)
{
trace_str[i++]=outdat%0x80/100%10+'0';
trace_str[i++]=outdat%0x80/10%10+'0';
trace_str[i++]=outdat%0x80%10+'0';
}else if(outdat%0x80/10%10)
{
trace_str[i++]=outdat%0x80/10%10+'0';
trace_str[i++]=outdat%0x80%10+'0';
}else
{
trace_str[i++]=outdat%0x80%10+'0';
}
trace_str[i++]='.';
switch(outdat/0x100)
{
case 0: trace_str[i]='0';break;
case 1: trace_str[i]='1';break;
case 2: trace_str[i]='1';break;
case 3: trace_str[i]='2';break;
case 4: trace_str[i]='3';break;
case 5: trace_str[i]='3';break;
case 6: trace_str[i]='4';break;
case 7: trace_str[i]='4';break;
case 8: trace_str[i]='5';break;
case 9: trace_str[i]='6';break;
case 10: trace_str[i]='6';break;
case 11: trace_str[i]='7';break;
case 12: trace_str[i]='8';break;
case 13: trace_str[i]='8';break;
case 14: trace_str[i]='9';break;
case 15: trace_str[i]='9';break;
default:break;
}
printlcd(trace_str);
}
main()
{
lcd_init();
while(1)
{
reset_18b20();
romcmd_match_18b20(rom_18b20_A);
fcmd_convT_18b20();
reset_18b20();
romcmd_match_18b20(rom_18b20_A);
write_byte_18b20(0xBE);
tmp_data=0;
tmp_data+=read_byte_18b20();
tmp_data+=read_byte_18b20()*0x100;
reset_18b20();
trace_tmpdata(tmp_data);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯