📄 7290_2.c
字号:
}
}
/********定时1ms***********/
void delay3(uint Tm_count)
{
uint i;
while (Tm_count--)
{
Watch();
for (i=0; i<90; i++) {};
}
}
/******************/
/*产生输液量的定时*/
/******************/
void Time_0(void) interrupt 1
{
static uchar count;
TR0 = 0;
EA = 0;
TH0 = TH0_T;
TL0 = TL0_T;
if (count >= 13)
{
V_ml++;
count = 0;
_nop_();
_nop_();
}
else count++;
EA = 1;
TR0 = 1;
}
/****************************************/
/*************向计算机发数据*************/
/****************************************/
/*********************************************/
/*************Dalls(18B20)********************/
/*********************************************/
/* C_f=1 华氏,C_f=0 摄氏 */
void temperature(bit C_f)
{
tmstart();
delay3(100);
now_temper = read_temper(C_f);
}
/**************************/
bit tmreset(void)
{
uint i;
uchar j;
EA = 0;
TMDAT = 0;
i = 103; while (i>0) i--;
TMDAT =1;
i = 4; while (i>0) i--;
for (j=0xff; j>0; j--)
{
if (~TMDAT) break;
}
EA = 1;
if (j == 0) return(1); /* 无温度检测无件 */
return (0);
}
/***************************/
uchar readbyte(void)
{
uint i;
uchar k,j,dat = 0;
EA = 0;
for (k=1; k<=8; k++)
{
TMDAT = 0; i++;
TMDAT = 1; i++; i++;
j = TMDAT;
i = 8;while (i>0) i--;
dat = (j << 7)|(dat >> 1);
}
EA = 1;
return(dat);
}
/****************************/
void writebyte(uchar dat)
{
uint i;
uchar j;
bit testb;
EA = 0;
for (j=1; j<=8; j++)
{
testb = dat&0x01;
dat = dat >> 1;
if (testb)
{
TMDAT = 0;
i++; i++;
TMDAT = 1;
i = 8; while (i>0) i--;
}
else {
TMDAT = 0;
i = 8; while (i>0) i--;
TMDAT = 1;
i++; i++;
}
}
EA = 1;
}
/*******************************/
void tmstart (void)
{
bit flag_1;
do
{
flag_1 = tmreset();
if (flag_1) {sound(True,1);delay3(100);}
} while (flag_1);
delay3(1);
writebyte(0xcc);
writebyte(0x44);
}
uchar read_temper(bit C_F)
{
uchar a,b,y1,y2,y3;
uint i = 0;
tmreset();
delay3(1);
writebyte(0xcc);
writebyte(0xbe);
a = readbyte(); //LSB
b = readbyte(); //MSB
//*********************
if (C_F)
{
AD = a;
AD <<= 4;
if (AD_7) i = 50 ;
AD <<= 1;
if (AD_7) i= i + 25;
y1 = a >> 4;
y2 = b << 4;
y3 = y1 | y2;
y1 = (i * 9) / 100;
i = y3;
y3 = (i * 9 + y1)/5+32;
}
else
{
y1 = a >> 4;
y2 = b << 4;
y3 = y1 | y2;
}
disp(y3,1);
return (y3);
}
/***************************
id = True; 为温度
***************************/
void disp(uchar y3,bit id)
{
uchar a;
uchar *p;
p = WriteBuf;
a = y3/100;
if (a == 0)
{
if (id)
{
WriteBuf[0] = 0x07;
WriteBuf[1] = 0x64;
WriteBuf[2] = 0x1f;
Zlg_write(p);
}
a = y3%100;
}
else
{
if (id)
{
WriteBuf[0] = 0x07;
WriteBuf[1] = 0x64;
WriteBuf[2] = a;
Zlg_write(p);
}
a = y3%100;
}
if (id)
{
y3 = 4;
WriteBuf[0] = 0x07;
WriteBuf[1] = 0x63;
WriteBuf[2] = a/0x0a;
Zlg_write(p);
WriteBuf[0] = 0x07;
WriteBuf[1] = 0x62;
WriteBuf[2] = a%0x0a;
Zlg_write(p);
}
else
{
if (y3 > 99)
{
y3 = 1;
Zlg_writebyte(0x10,0x02);
Zlg_writebyte(0x11,0x02);
delay3(50);
}
else
{
y3 = 1;
WriteBuf[0] = 0x07;
WriteBuf[1] = 0x60;
WriteBuf[2] = a/0x0a;
Zlg_write(p);
WriteBuf[0] = 0x07;
WriteBuf[1] = 0x61;
WriteBuf[2] = a%0x0a;
Zlg_write(p);
}
}
}
void sound(bit f,uchar i)
{
for (;i>0;i--)
{
Speak = False;
if (f) delay3(200); else delay3(100);
Speak = True;
if (i>0) delay3(100);
}
}
void Watch(void)
{
WDTRST = 0x1e;
WDTRST = 0xe1;
}
uchar Zlg_read(uchar i)
{
uchar x;
start(); /* 开始 */
shift8(0x70); /* 写控制码 */
ack();
shift8(i); /* 发地址 */
ack(); /* 应答 */
start();
shift8(0x71);
ack();
SDA = True; delay(1);
for (x=0; x<8; x++) /* 读数据 */
{
AD = AD * 2;
SCL = True; delay(1);
AD_0 = SDA;
SCL = False; delay(1);
}
stop();
return(AD);
}
void Zlg_write(uchar *p)
{
start();
shift8(0x70);//SendByte(0x70);
ack();
Watch();
shift8(*p++);//SendByte(address);
ack();
shift8(*p++);//SendByte(i);
ack();
shift8(*p);
ack();
stop();
}
void Zlg_writebyte(uchar address,uchar i)
{
start();
shift8(0x70);
ack();
Watch();
shift8(address);
ack();
shift8(i);
ack();
stop();
}
void delay(uchar i)
{
while (i > 0)
{
i--;
}
Watch();
}
void shift8(uchar ch)
{
uchar i=8;
SCL=0;
delay_nop();
while(i--)
{
SDA=(bit)(ch&0x80);
_nop_();_nop_();_nop_();
ch<<=1;
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=0;
_nop_();_nop_();
}
}
/*void shift8(uchar a)
{
uchar i;
AD = a;
Watch();
for (i=0; i<8; i++)
{
SDA = AD_7; delay_nop();
SCL = True; delay_nop();
SCL = False; delay_nop();
AD = AD * 2;
}
}*/
void start()
{
SDA=1; /*发送起始条件的数据信号*/
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_(); /*起始条件建立时间大于4.7us,延时*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0; /*发送起始信号*/
_nop_(); /* 起始条件锁定时间大于4μs*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0; /*钳住I2C总线,准备发送或接收数据 */
_nop_();
_nop_();
_nop_();
//SDA = True; delay_nop();
//SCL = True; delay_nop();
//SDA = False; delay_nop();
//SCL = False; delay_nop();
}
void ack(void)
{
SDA=1;
SCL=0;
_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
//while(SDA){;} //This may make the CPU crash. 04-3-18 17:16
SCL=0;
_nop_();_nop_();
}
/*-----------------------------------------------
调用方式:void no_ack(void)
函数说明:无需应答位,在读程序中用到
-----------------------------------------------*/
void no_ack(void)
{
SDA=1;
delay_nop();
SCL=1;
delay_nop();
//while(SDA){;} //This may make the CPU crash. 04-3-18 17:16
SCL=0;
delay_nop();
}
void stop()
{
SDA=0; /*发送结束条件的数据信号*/
_nop_(); /*发送结束条件的时钟信号*/
_nop_();
SCL=1; /*结束条件建立时间大于4μs*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1; /*发送I2C总线结束信号*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//SDA = False; delay_nop(); /* 停止 */
//SCL = True; delay_nop();
//SDA = True; delay_nop();
}
void SendByte(uchar c)
{
uchar BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
{
if((c<<BitCnt)&0x80) SDA=1; /*判断发送位*/
else SDA=0;
_nop_();
_nop_();
_nop_();
SCL=1; /*置时钟线为高,通知被控器开始接收数据位*/
_nop_();
_nop_(); /*保证时钟高电平周期大于4μs*/
_nop_();
_nop_();
_nop_();
SCL=0;
}
_nop_();
_nop_();
_nop_();
SDA=1; /*8位发送完后释放数据线,准备接收应答位*/
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
if(SDA==1) YesNo_ack=0;
else YesNo_ack=1; /*判断是否接收到应答信号*/
SCL=0;
_nop_();
_nop_();
_nop_();
}
void delay_nop()
{
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -