#define ACK_KEY 0x01
#define RIGHT_KEY 0x02
#define UP_KEY 0x04
#define SET_KEY 0x08
#define PASSWD_X 32
#define PASSWD_Y 188
#define PASSWD_BEG 0 //密码起始地址
//#define DTE_FOR_VOID 0 //有效天数计数RAM地址
#define NULL 0
#define FALSE 0
#define TRUE 1
#define StatInputCom 0
#define StatExeCom 1
#define MaxLenWord 20 /*20*/ //单词最大长度
#define MaxLenWordTable 10 /*10*/ //词表最大长度
#define MaxLenComBuf 256 /*256*/ //命令缓冲区最大长度
#define MaxComNum 10 //最大命令种类数
#define MaxLenCom 10 //命令最大长度
#define MaxSensors 20 //DS18B20最多个数
#include <REG51xD2.h>
#include <absacc.h>
#include <intrins.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uchar;
typedef unsigned int uint;
bit OWFirst();
bit OWNext();
bit OWVerify();
void OWTargetSetup(unsigned char family_code);
void OWFamilySkipSetup();
bit OWReset();
void OWWriteByte(unsigned char byte_value);
void OWWriteBit(bit bit_value);
bit OWReadBit();
bit OWSearch();
uchar docrc8(uchar value);
// global search state
idata uchar ROM_NO[9];
xdata uchar ROM_NO_P0[MaxSensors][9];
xdata uchar SENSORS_NO[MaxSensors];
//xdata int LastDiscrepancy;
//xdata int LastFamilyDiscrepancy;
idata uchar LastDiscrepancy;
idata uchar LastFamilyDiscrepancy;
bit LastDeviceFlag;
idata uchar crc8, cnt;
//void printf(void);
bit resetpulse(void);
void writecommandtods18b20(uchar);
uchar readdatafromds18b20(void);
//void ReadTemperature(void);
void GetTemperature(void);
void delay(unsigned int);
uchar ReadOneChar(void);
void WriteOneChar(unsigned char);
void search_rom(void);
void shell(void);
uchar rd_eeprom_byte(unsigned int);
void wr_eeprom_byte(unsigned int, uchar);
void DisplaySensors(bit);
bit time_tick, time_tick1, ram_req, com_flag, display = 0;
bit set_flag, val_time, tick_200mS, sflag, ComMatchFlag = 0;
//sbit DQ = P0^1;
sbit DQ = P1^4;
idata uchar recv_cnt, send_cmd, State;
xdata unsigned char local_ram[256];
//uchar rom[8];
xdata uchar ComBuf[MaxLenComBuf+1]; /*store '\0'*/
sbit IO = P1^0;
sbit SCLK = P1^1;
sbit RSTB = P1^7;
/***************************************************************************/
/* Prototypes */
/***************************************************************************/
/* ----------------------------------------------------------------------- */
typedef struct{
int Num;
int LeftCurveNum, RightCurveNum;
struct{
int Length;
unsigned char Str[MaxLenWord+1]; /*for '\0'*/
} wt[MaxLenWordTable];
} WORDTABLE;
code uchar ComTable[MaxComNum][MaxLenCom+1]={"ds","select","connect","config","host","save","clr","exit","dis","help"};
bit GetWord(unsigned char *ComBuf,WORDTABLE *WordTable);
xdata WORDTABLE WordTable;
xdata unsigned char readdata[MaxSensors][9];
/* Time0中断服务程序,使用寄存器组2 */
void mTime0Interrupt( ) interrupt 1 //using 2
{
static unsigned char t_100ms = 10;
static unsigned char t_1000ms = 10;
static unsigned char t_min = 60;
TH0 = 0xDC; /* TH TL = 0xDC00 在11.0592MHz 周期是 10ms*/
//TL0 = 0x00; 不需要,这样可以得到更精确定时
if(!(--t_100ms))
{
t_100ms = 10;
if(t_1000ms%2)tick_200mS = 1;
if(!(--t_1000ms))
{
time_tick = 1;
t_1000ms = 20;
ram_req = 1; //请求通讯RAM到时标志
/*if(ram_req && (!com_flag))
{
ACC = 0x72;
TB8 = P;
SBUF = ACC;
recv_cnt = 0; //串口命令
com_flag = 1;
length = 4;
ram_req = 0;
}*/
if(!(--t_min))
{
t_min = 60;
time_tick1 = 1;
}
}
}
}
/* 串行口中断服务程序,使用寄存器组3 */
void mSelInterrupt( ) interrupt 4 using 1 // using 2
{
//uchar ctemp;
if(_testbit_(RI))
{
//PBYTE[recv_cnt] = SBUF;
local_ram[recv_cnt] = SBUF;
recv_cnt++;
}
/*if(_testbit_(TI))
{
if(send_cmd < length - 1 )
{
ACC = printf_buf[++send_cmd];
SBUF = ACC;
TB8 = P;
}
else
{
send_cmd = 0;
com_flag = 0;
}
}// End of if(_testbit_(TI)) */
}
main( ) {
uchar j, i;
unsigned int temp;
recv_cnt = 0; //串口命令
send_cmd = 0;
com_flag = 0;
//SCON = 0xD0;/* SM0=1 SM1=1 SM2=0 REN=1 TB8 = RB8 = 0 TI = 0 RI = 0 */
SCON = 0xD2;/* SM0=1 SM1=1 SM2=0 REN=1 TB8 = RB8 = 0 TI = 1 RI = 0 */
RCAP2H = 0xFF;
RCAP2L = 0xEE; //19200 Baud Rate
TH2 = 0xFF;
TL2 = 0xEE;
// DB 0FDH,0FAH,0F7H,0EEH,0DCH,0B8H,070H
// BAUD 115200,57600,38400,19200,9600,4800,2400
T2CON = 0x34; /*TF2=0 EXF2=0 RCLK=1 TCLK=1 EXEN2=0 TR2=1 C/T2=0 CP/RL2=0*/
TMOD = 0x11;/* GATE=0 C/T-=0 M1 M0=2 GATE C/T- M1 M0 */
TH0 = 0xDC; /* TH TL = 0xDC00 在11.0592MHz 周期是 10ms*/
TL0 = 0x00;
TCON = 0x10;/* 01101001 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 */
/*EA Null ET2 ES ET1 EX1 ET0 EX0*/
//REN = 1;
ET2 = 0;
//TR2 = 1;
//ES = 1; //允许串口中断
ET1 = 0;
//EX1 = 1;
EX0 = 0;
AUXR = 0x10; //使用片内XRAM
EA = 1; /* 允许中断 */
//for(i = 0; i < MaxSensors; i++)
// SENSORS_NO[i] = rd_eeprom_byte(S_NO_FIR + i);
search_rom();
DisplaySensors(0);
//resetpulse();
//writecommandtods18b20(0xcc);
//writecommandtods18b20(0x44);
printf("\t\t***********************************************\n\r");
printf("\t\t* Welcom to use this program *\n\r");
printf("\t\t* Set Sth. for Temperature... *\n\r");
printf("\t\t* Author:Lin 2006.07.15 *\n\r");
printf("\t\t***********************************************\n\r\n\r\n\r");
/*Login & Password*/
printf("\# ");
ET0 = 1;
while(1)
{
if(time_tick1)
{
time_tick1 = 0;
} //End of if(time_tick1)
if(time_tick)
{
time_tick = 0;
}//End of if(time_tick)
if(ram_req && (!com_flag))
{
GetTemperature();
if(display)
{
for(j = 0; j < cnt; j++)
{
/*for(i = 0; i < 9; i++)
{
printf("%02X ", (unsigned)readdata[j][i]);
}*/
printf("%d:", (unsigned)j+1);
i = readdata[j][1] & 0xf8;
if(i)
{
if(i == 0xf8)
{
printf("-");
readdata[j][0] = ~readdata[j][0];
readdata[j][1] = ~readdata[j][1];
readdata[j][0]++;
if(!readdata[j][0])readdata[j][1]++;
i = 0;
}
else if((i == 0x80)||(i == 0x7f))
printf("CRC err.");
}
if(!i)
{
printf("%d", (unsigned)((readdata[j][1] & 0x7)<<4)|(readdata[j][0]>>4));
printf(".");
//temp = (readdata[0] << 4)&0xff;
temp = (uchar)(readdata[j][0] << 4);
do
{
temp *= 10;
printf("%d", (unsigned)(temp>>8));
temp &= 0xff;
}while(temp);
printf(" ");
}
}
printf("\n\r\n\r");
ram_req = 0;
}
}
if(tick_200mS)
{
tick_200mS = 0;
} //End of if(tick_200mS)
shell();
}
}
bit resetpulse(void)
{
uchar i, j;
EA = 0;
DQ=1;
_nop_();
_nop_();
DQ=0;
j = 0x6b;
do{
}while(--j);
DQ=1;
i = 0x25;
do{
if(!DQ)break;
}while(--i);
j = 0x6b;
do{}while(--j);
DQ=1;
EA = 1;
return(i == 0);
}
void writecommandtods18b20(uchar val)
{
unsigned char i, j;
i = 8;
do{
DQ = 0;
j = 6;
do{
}while(--j);
DQ = val & 0x01;
val >>= 1;
j = 23;
do{
}while(--j);
DQ = 1;
_nop_();
_nop_();
}while(--i);
DQ = 1;
}
/*
unsigned char readdatafromds18b20(void)
{
unsigned char i, j;
unsigned char value=0;
i = 8;
do{
DQ = 1;
_nop_();
_nop_();
DQ = 0;
_nop_();
_nop_();
_nop_();
DQ = 1;
j = 7;
do{
}while(--j);
value >>= 1;
if(DQ)
value |= 0x80;
j = 2;
do{
}while(--j);
}while(--i);
return (value);
}
*/
//*****************************
/*
//初始化程序
Init_DS18B20(void)
{ unsigned char x=0;
DQ=1; //DQ先置高
delay(8); //稍延时
DQ=0; //发送复位脉冲
delay(85); //延时(>480ms)
DQ=1
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
此是Keil C下的18B20测温程序,并且带Shell。可以通过串口直接显示测温结果和18B20的ID。串口可是用中断实现的。网上见许多人都问如何使用18B20,其实读ID和校验部分是从厂家网站下载的,建议第一次无论接触一个器件,都先到厂家网站上看看。我从CSDN网站下载了能用的东西,很受感到。决定把两年前写的程序传上,希望对大家有所帮助。
资源推荐
资源详情
资源评论
收起资源包目录
cewen.rar (1个子文件)
cewen.c 31KB
共 1 条
- 1
资源评论
- oqqxx123456789a2013-06-25一直想做一个,现在总算是找到了,还有仿真。。。
- jakemt32012-07-17源码排得有些乱,不够直观
xuzixin
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功