📄 18b20.c
字号:
读第三只18B20的温度的程序
******************************************************************************/
void readtemp_no2()
{
unsigned char temp_low,temp_high; //温度值
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0xD3);
DS1820_WriteByte(0x3B);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0xD9);
DS1820_WriteByte(0x44); // 开始转换
delay_800ms(); //延时800ms
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0xD3);
DS1820_WriteByte(0x3B);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0xD9);
DS1820_WriteByte(0xBE); // Read Scratch
temp_low=DS1820_ReadByte(); //读低位
temp_high=DS1820_ReadByte(); //读高位
temperature=(temp_high&0x0f);
temperature<<=8;
temperature|=temp_low;
Temper[3]=temperature*0.0625;
temper_c[3]=(char)Temper[3]*1;
}
/*****************************************************************************
读第四只18B20的温度的程序
******************************************************************************/
void readtemp_no3()
{
unsigned char temp_low,temp_high; //温度值
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0x6B);
DS1820_WriteByte(0x30);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x0F);
DS1820_WriteByte(0x44); // 开始转换
delay_800ms(); //延时800ms
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0x6B);
DS1820_WriteByte(0x30);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x0F);
DS1820_WriteByte(0xBE); // Read Scratch
temp_low=DS1820_ReadByte(); //读低位
temp_high=DS1820_ReadByte(); //读高位
temperature=(temp_high&0x0f);
temperature<<=8;
temperature|=temp_low;
Temper[4]=temperature*0.0625;
temper_c[4]=(char)Temper[4]*1;
}
/*****************************************************************************
读第五只18B20的温度的程序
******************************************************************************/
void readtemp_no4()
{
unsigned char temp_low,temp_high; //温度值
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0x83);
DS1820_WriteByte(0x49);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x19);
DS1820_WriteByte(0x44); // 开始转换
delay_800ms(); //延时800ms
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0x83);
DS1820_WriteByte(0x49);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x19);
DS1820_WriteByte(0xBE); // Read Scratch
temp_low=DS1820_ReadByte(); //读低位
temp_high=DS1820_ReadByte(); //读高位
temperature=(temp_high&0x0f);
temperature<<=8;
temperature|=temp_low;
Temper[5]=temperature*0.0625;
temper_c[5]=(char)Temper[5]*1;
}
/*****************************************************************************
读第六只18B20的温度的程序
******************************************************************************/
void readtemp_no5()
{
unsigned char temp_low,temp_high; //温度值
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0x83);
DS1820_WriteByte(0x4C);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0xCB);
DS1820_WriteByte(0x44); // 开始转换
delay_800ms(); //延时800ms
DS1820_Reset(); // 复位
DS1820_WriteByte(0x55); //match ROM,保证有相同序列号的芯片会响应
DS1820_WriteByte(0x28);
DS1820_WriteByte(0x83);
DS1820_WriteByte(0x4C);
DS1820_WriteByte(0xBB);
DS1820_WriteByte(0x01);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0x00);
DS1820_WriteByte(0xCB);
DS1820_WriteByte(0xBE); // Read Scratch
temp_low=DS1820_ReadByte(); //读低位
temp_high=DS1820_ReadByte(); //读高位
temperature=(temp_high&0x0f);
temperature<<=8;
temperature|=temp_low;
Temper[6]=temperature*0.0625;
temper_c[6]=(char)Temper[6]*1;
}
/*****************************************************************************
总线上只接一只1820时读测量温度的程序
******************************************************************************/
float Read_Temperature(void)
{
unsigned char temp_low,temp_high; //温度值
DS1820_Reset(); // 复位
DS1820_WriteByte(0xCC); // Skip ROM
DS1820_WriteByte(0x44); // 开始转换
delay_800ms(); //延时800ms
DS1820_Reset(); // 复位
DS1820_WriteByte(0xCC); // Skip ROM
DS1820_WriteByte(0xBE); // Read Scratch
temp_low=DS1820_ReadByte(); //读低位
temp_high=DS1820_ReadByte(); //读高位
temperature=(temp_high&0x0f);
temperature<<=8;
temperature|=temp_low;
Temper[0]=temperature*0.0625;
return Temper[0];
}
void read_6temp(void)
{
// Read_rom();
// Read_Temperature();
readtemp_no0();
readtemp_no1();
readtemp_no2();
readtemp_no3();
readtemp_no4();
readtemp_no5();
}
void main()
{
unsigned int iq0;
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
//使用XT2振荡器
BCSCTL1 &= ~XT2OFF; //打开XT2振荡器
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for (iq0 = 0xFF; iq0 > 0; iq0--); // 延时,等待XT2起振
}
while ((IFG1 & OFIFG) != 0); // 判断XT2是否起振
BCSCTL2 =SELM_2+SELS; //选择MCLK、SMCLK为XT2
P1DIR&=(~BIT2+~BIT3); //设置为输入方向
P1SEL&=(~BIT2+~BIT3); //设置为普通I/O口功能
P1IES|=BIT2+BIT3; //选择下降沿触发
P1IE|=BIT2+BIT3; //打开中断允许
P1IFG=0; //P1IES的切换可能使P1IFG置位,需清除
_EINT(); //开总中断允许
while(1)
{read_6temp();
}
}
#pragma vector=PORT1_VECTOR
__interrupt void P1_ISR()
{
P1IFG=0; //多源中断,需靠软件清除P1IFG
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -