📄 ds18b20.c
字号:
t=1;
else
t=0;
delayUs(8); //14.92us
delayUs(8);
delayUs(8); //14.92us
RLS_DS18B20; //释放总线
delayUs(12);
return(t);
}
/**********************************************************************
functionName: BYTE readByteDS18B20(void)
description :读DS18B20一个字节
**********************************************************************/
unsigned char OWReadbyte(void)
{
unsigned char i;
unsigned char retVal=0;
RLS_DS18B20; //释放总线
for(i=8;i>0;i--)
{
retVal>>=1;
if (OWReadBit())
retVal|=0x80;
}
delayUs(1); //2.71us(大于1us就行了)
return(retVal);
}
/////////////////////////////////////////
void OWWriteBit(unsigned char temp)
{
HLD_DS18B20; //Maga16控制总线
CLR_DS18B20; //强制拉低
if(temp)
//RLS_DS18B20; //释放总线
SET_DS18B20;
else
CLR_DS18B20; //强制拉低
delayUs(35); //60us
RLS_DS18B20; //释放总线
delayUs(50);
}
/**********************************************************************
functionName: BYTE readByteDS18B20(void)
description :写DS18B20一个字节
**********************************************************************/
void OWWriteByte(unsigned char wb)
{
unsigned char i;
unsigned char temp;
RLS_DS18B20; //释放总线
for(i=0;i<8;i++)
{
temp=wb>>i;
temp&=0x01;
OWWriteBit(temp);
}
}
///////////////////////////////////////////////
void delayms(void)
{
unsigned int i;
int j;
for(j=0;j<10;j++)
{
for(i=0;i<40000;i++);
}
}
unsigned char OWSearch(void)
{
unsigned char id_bit_number;
unsigned char last_zero, rom_byte_number, search_result;
unsigned char id_bit, cmp_id_bit;
unsigned char rom_byte_mask, search_direction;
// initialize for search
id_bit_number = 1;
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = 0;
crc8 = 0;
// if the last call was not the last one
if (!LastDeviceFlag)
{
// 1-Wire reset
if (!OWReset())
{
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = 0;
LastFamilyDiscrepancy = 0;
return 0;
}
// issue the search command
OWWriteByte(0xf0);
// loop to do the search
do
{
// read a bit and its complement
id_bit = OWReadBit();
cmp_id_bit = OWReadBit();
// check for no devices on 1-Wire
if ((id_bit == 1) && (cmp_id_bit == 1))
break;
else
{
// all devices coupled have 0 or 1
if (id_bit != cmp_id_bit)
search_direction = id_bit; // bit write value for search
else
{
// if this discrepancy if before the Last Discrepancy
// on a previous next then pick the same as last time
if (id_bit_number < LastDiscrepancy)
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
else
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == LastDiscrepancy);
// if 0 was picked then record its position in LastZero
if (search_direction == 0)
{
last_zero = id_bit_number;
// check for Last discrepancy in family
if (last_zero < 9)
LastFamilyDiscrepancy = last_zero;
}
}
// set or clear the bit in the ROM byte rom_byte_number
// with mask rom_byte_mask
if (search_direction == 1)
ROM_NO[rom_byte_number] |= rom_byte_mask;
else
ROM_NO[rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit
OWWriteBit(search_direction);
// increment the byte counter id_bit_number
// and shift the mask rom_byte_mask
id_bit_number++;
rom_byte_mask <<= 1;
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
if (rom_byte_mask == 0)
{
docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC
rom_byte_number++;
rom_byte_mask = 1;
}
}
}while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!((id_bit_number < 65) || (crc8 != 0)))
{
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
LastDiscrepancy = last_zero;
// check for last device
if (LastDiscrepancy == 0)
LastDeviceFlag = 1;
// check for last family group
if (LastFamilyDiscrepancy == LastDiscrepancy)
LastFamilyDiscrepancy = 0;
search_result = 1;
}
}
// if no device found then reset counters so next 'search' will be like a first
if (!search_result || !ROM_NO[0])
{
LastDiscrepancy = 0;
LastDeviceFlag = 0;
LastFamilyDiscrepancy = 0;
search_result = 0;
}
return search_result;
}
unsigned char docrc8(unsigned char value)
{
// See Application Note 27
// TEST BUILD
crc8 = dscrc_table[crc8 ^ value];
return crc8;
}
int OWFirst(void)
{
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = 0;
LastFamilyDiscrepancy = 0;
return OWSearch();
}
int OWNext(void)
{
// leave the search state alone
return OWSearch();
}
void init_port(void)
{
PORTB=0b11111111; //
DDRB=0b00101100; //PB0,PB1,SCK MOSI SS输出 MISO输入
PORTC=0b11111010;
DDRC=0Xbe; //reset AD0输入,其余输出
RecMode; //上电接收模式
PORTD=0b11111111;
DDRD=0b00000010; //PD2~PD7输入,RXD输入,TXD输出
}
void init_timer1(void)
{
TCCR1A=0;
TCCR1B= (1<<CS12); //256预分频 晶振为7.3728MHz 一条指令大约34.72us
TCNT1=65535-((7372800/256)*50)/1000; //50ms中断初值
//TIMSK=(1<<TOIE1); //中断允许
TIMSK1=(1<<TOIE1); //中断允许
}
void init_USART(void)//USART 初始化
{
UCSR0A = 0x00;
UCSR0B=(1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0); //使能接收中断,使能接收,使能发送
UCSR0C=0x06; //异步,8位数据,无奇偶校验,一个停止位
UBRR0L= (F_CPU/BAUDRATE/16-1)%256;
UBRR0H= (F_CPU/BAUDRATE/16-1)/256;
}
SIGNAL(SIG_OVERFLOW1) //T1中断
{
TCNT1=65535-((7372800/256)*50)/1000;
R_Delay++;
if (R_Delay>=20)
{
R_Delay=0;
R_Status|=BIT0;
}
}
void put_c(unsigned char c) //发送采用查询方式
{
while( !(UCSR0A & (1<<UDRE0)) );
UDR0=c;
}
void put_s(unsigned char *s,unsigned char Len)
{
unsigned char i;
TraMode;
NOP;NOP;NOP;
for(i=0;i<Len;i++)
{
put_c(*s++);
}
_delay_ms(5);
RecMode;
}
SIGNAL(SIG_USART_RECV) //串口接收中断服务程序
{
PC_COMMAND=UDR0;
RX_BUFFER[RX_index]=PC_COMMAND; //保存数据到数组里面
RX_index++;
if (RX_index>=sizeof(RX_BUFFER)) RX_index=0; //防止数组溢出
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -