Use predefined bit manipulation macros

This commit is contained in:
Rihards Skuja 2018-01-02 14:18:30 +02:00
parent b7458e4bd3
commit e6ec28b04c
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B
2 changed files with 36 additions and 39 deletions

View File

@ -18,14 +18,11 @@ References:
#include <avr/io.h>
//setup connection
#define DS18B20_PORT PORTC
#define DS18B20_DDR DDRC
#define DS18B20_PIN PINC
#define DS18B20_DQ PC0
#define DS18B20_BIT PC0
//commands
#define DS18B20_CMD_CONVERTTEMP 0x44
#define DS18B20_CMD_RSCRATCHPAD 0xbe
#define DS18B20_CMD_WSCRATCHPAD 0x4e
@ -41,7 +38,7 @@ References:
//stop any interrupt on read
#define DS18B20_STOPINTERRUPTONREAD 1
//functions
uint8_t ds18b20_reset(void);
void ds18b20_writebit(uint8_t bit);
uint8_t ds18b20_readbit(void);

View File

@ -10,8 +10,8 @@ Please refer to LICENSE file for licensing information.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "ds18b20.h"
#include "common.h"
#include "temperature.h"
/*
* ds18b20 init
@ -20,20 +20,20 @@ uint8_t ds18b20_reset()
{
uint8_t i;
//low for 480us
DS18B20_PORT &= ~(1 << DS18B20_DQ); //low
DS18B20_DDR |= (1 << DS18B20_DQ); //output
// low for 480us
CLR(DS18B20_PORT, DS18B20_BIT);
SET(DS18B20_DDR, DS18B20_BIT);
_delay_us(480);
//release line and wait for 60uS
DS18B20_DDR &= ~(1 << DS18B20_DQ); //input
// release line and wait for 60uS
CLR(DS18B20_DDR, DS18B20_BIT);
_delay_us(60);
//get value and wait 420us
i = (DS18B20_PIN & (1 << DS18B20_DQ));
// get value and wait 420us
i = CHK(DS18B20_PIN, DS18B20_BIT);
_delay_us(420);
//return the read value, 0=ok, 1=error
// return the read value, 0=ok, 1=error
return i;
}
@ -42,18 +42,18 @@ uint8_t ds18b20_reset()
*/
void ds18b20_writebit(uint8_t bit)
{
//low for 1uS
DS18B20_PORT &= ~(1 << DS18B20_DQ); //low
DS18B20_DDR |= (1 << DS18B20_DQ); //output
// low for 1uS
CLR(DS18B20_PORT, DS18B20_BIT);
SET(DS18B20_DDR, DS18B20_BIT);
_delay_us(1);
//if we want to write 1, release the line (if not will keep low)
// if we want to write 1, release the line (if not will keep low)
if (bit)
DS18B20_DDR &= ~(1 << DS18B20_DQ); //input
CLR(DS18B20_DDR, DS18B20_BIT);
//wait 60uS and release the line
// wait 60uS and release the line
_delay_us(60);
DS18B20_DDR &= ~(1 << DS18B20_DQ); //input
DS18B20_DDR &= ~(1 << DS18B20_BIT);
}
/*
@ -63,20 +63,20 @@ uint8_t ds18b20_readbit(void)
{
uint8_t bit = 0;
//low for 1uS
DS18B20_PORT &= ~(1 << DS18B20_DQ); //low
DS18B20_DDR |= (1 << DS18B20_DQ); //output
// low for 1uS
CLR(DS18B20_PORT, DS18B20_BIT);
SET(DS18B20_DDR, DS18B20_BIT);
_delay_us(1);
//release line and wait for 14uS
DS18B20_DDR &= ~(1 << DS18B20_DQ); //input
// release line and wait for 14uS
CLR(DS18B20_DDR, DS18B20_BIT);
_delay_us(14);
//read the value
if (DS18B20_PIN & (1 << DS18B20_DQ))
// read the value
if (CHK(DS18B20_PIN, DS18B20_BIT))
bit = 1;
//wait 45uS and return read value
// wait 45uS and return read value
_delay_us(45);
return bit;
}
@ -119,17 +119,17 @@ double ds18b20_gettemp()
cli();
#endif
ds18b20_reset(); //reset
ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM
ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion
ds18b20_reset(); // reset
ds18b20_writebyte(DS18B20_CMD_SKIPROM); // skip ROM
ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); // start temperature conversion
while (!ds18b20_readbit()); //wait until conversion is complete
while (!ds18b20_readbit()); // wait until conversion is complete
ds18b20_reset(); //reset
ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM
ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad
ds18b20_reset(); // reset
ds18b20_writebyte(DS18B20_CMD_SKIPROM); // skip ROM
ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); // read scratchpad
//read 2 byte from scratchpad
// read 2 byte from scratchpad
temperature_l = ds18b20_readbyte();
temperature_h = ds18b20_readbyte();
@ -137,7 +137,7 @@ double ds18b20_gettemp()
sei();
#endif
//convert the 12 bit value obtained
// convert the 12 bit value obtained
retd = ((temperature_h << 8) + temperature_l) * 0.0625;
return retd;