
1820部分代码:
*Function:向18B20写入一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
void WriteByte (unsigned char wr) //单字节写入
{
unsigned char i;
for (i=0;i<8;i++)
{
GPIO_ResetBits(GPIOA , D18B20); //D18B20 = 0;
Delay_us(2);
//D18B20=wr&0x01;
if(wr&0x01) GPIO_SetBits(GPIOA , D18B20);
else GPIO_ResetBits(GPIOA , D18B20);
Delay_us(45); //delay 45 uS //5
GPIO_SetBits(GPIOA , D18B20); //D18B20=1;
wr >>= 1;
}
}
/************************************************************
*Function:读18B20的一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
unsigned char ReadByte (void) //读取单字节
{
unsigned char i,u=0;
for(i=0;i<8;i++)
{
GPIO_ResetBits(GPIOA , D18B20); //D18B20 = 0;
Delay_us (2);
u >>= 1;
GPIO_SetBits(GPIOA , D18B20); //D18B20 = 1;
Delay_us (4);
if(GPIO_ReadInputDataBit(GPIOA , D18B20) == 1)
u |= 0x80;
Delay_us (65);
}
return(u);
}
/************************************************************
*Function:读18B20
*parameter:
*Return:
*Modify:
*************************************************************/
void read_bytes (unsigned char j)
{
unsigned char i;
for(i=0;i<j;i++)
{
*p = ReadByte();
p++;
}
}
/************************************************************
*Function:CRC校验
*parameter:
*Return:
*Modify:
*************************************************************/
unsigned char Temp_CRC (unsigned char j)
{
unsigned char i,crc_data=0;
for(i=0;i<j;i++) //查表校验
crc_data = CrcTable[crc_data^temp_buff[i]];
return (crc_data);
}
/************************************************************
*Function:读取温度
*parameter:
*Return:
*Modify:
*************************************************************/
void GemTemp (void)
{
read_bytes (9);
if (Temp_CRC(9)==0) //校验正确
{
Temperature = temp_buff[1]*0x100 + temp_buff[0];
// Temperature *= 0.625;
Temperature /= 16;
Delay_us(10);
}
}
/************************************************************
*Function:内部配置
*parameter:
*Return:
*Modify:
*************************************************************/
void Config18b20 (void) //重新配置报警限定值和分辨率
{
Init18b20();
WriteByte(0xcc); //skip rom
WriteByte(0x4e); //write scratchpad
WriteByte(0x19); //上限
WriteByte(0x1a); //下限
WriteByte(0x7f); //set 12 bit (0.125)
Init18b20();
WriteByte(0xcc); //skip rom
WriteByte(0x48); //保存设定值
Init18b20();
WriteByte(0xcc); //skip rom
WriteByte(0xb8); //回调设定值
}
/************************************************************
*Function:读18B20ID
*parameter:
*Return:
*Modify:
*************************************************************/
void ReadID (void)//读取器件 id
{
Init18b20();
WriteByte(0x33); //read rom
read_bytes(8);
}
/************************************************************
*Function:18B20ID全处理
*parameter:
*Return:
*Modify:
*************************************************************/
void TemperatuerResult(void)
{
p = id_buff;
ReadID();
Config18b20();
Init18b20 ();
WriteByte(0xcc); //skip rom
WriteByte(0x44); //Temperature convert
Init18b20 ();
WriteByte(0xcc); //skip rom
WriteByte(0xbe); //read Temperature
p = temp_buff;
GemTemp();
}
void GetTemp()
{
if(Count == 3) //每隔 3s 读取温度
{ Count=0;
TemperatuerResult();
}
}






回复
打赏帖 | |
---|---|
【STM32F769】AI之与本地deepseek对接被打赏50分 | |
Buck电路工作在CCM模式下电感电流的计算公式是什么?被打赏5分 | |
buck电路工作原理被打赏5分 | |
基于MSPM0L1306的MODBUS-RTU协议通讯实验被打赏100分 | |
我想要一部加热台+多合一调试工具被打赏18分 | |
每周了解几个硬件知识+485硬件知识分享被打赏10分 | |
【换取手持数字示波器】树莓派PICO调试器官方固件本地化部署实践被打赏24分 | |
【换取手持数字示波器】分享一个KEIL无法识别CMSIS-DAP调试器的解决办法被打赏20分 | |
【换取手持数字示波器】分享一个自制的ArduinoNano扩展板底板被打赏23分 | |
【换取手持示波器】树莓派PICOW网页烟花被打赏18分 |