восстановление потеряной ветки...
- сделана привязка датчиков по индексу или по юзер байтам
This commit is contained in:
409
DS18B20/dallas_tools.c
Normal file
409
DS18B20/dallas_tools.c
Normal file
@@ -0,0 +1,409 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file dallas_tools.c
|
||||
* @brief Äðàéâåð äëÿ ðàáîòû ñ äàò÷èêàìè òåìïåðàòóðû DS18B20
|
||||
* @author MicroTechnics (microtechnics.ru)
|
||||
******************************************************************************
|
||||
@details
|
||||
Ýòîò ôàéë ñîäåðæèò ðåàëèçàöèþ ôóíêöèé äëÿ ðàáîòû ñ äàò÷èêîì DALLAS_HandleTypeDef
|
||||
÷åðåç èíòåðôåéñ 1-Wire. Îí ïðåäîñòàâëÿåò ôóíêöèè äëÿ ÷òåíèÿ è çàïèñè
|
||||
êîíôèãóðàöèè, âûïîëíåíèÿ èçìåðåíèé è îáðàáîòêè ïîëó÷åííûõ äàííûõ.
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
/* Includes ----------------------------------------------------------------*/
|
||||
|
||||
#include "dallas_tools.h"
|
||||
|
||||
|
||||
/* Declarations and definitions --------------------------------------------*/
|
||||
|
||||
struct
|
||||
{
|
||||
DALLAS_HandleTypeDef outdoor;
|
||||
DALLAS_HandleTypeDef indoor;
|
||||
DALLAS_HandleTypeDef bathroom;
|
||||
DALLAS_HandleTypeDef kitchen;
|
||||
DALLAS_HandleTypeDef big_room;
|
||||
DALLAS_HandleTypeDef small_room;
|
||||
DALLAS_HandleTypeDef living_room;
|
||||
DALLAS_HandleTypeDef basement;
|
||||
}AllSens;
|
||||
|
||||
/* Functions ---------------------------------------------------------------*/
|
||||
|
||||
void Dallas_ReadAll(void)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
result = Dallas_StartConvertTAll(&OW, DALLAS_WAIT_BUS, 0);
|
||||
result = Dallas_ReadTemperature(&AllSens.outdoor);
|
||||
result = Dallas_ReadTemperature(&AllSens.indoor);
|
||||
result = Dallas_ReadTemperature(&AllSens.bathroom);
|
||||
result = Dallas_ReadTemperature(&AllSens.kitchen);
|
||||
result = Dallas_ReadTemperature(&AllSens.big_room);
|
||||
result = Dallas_ReadTemperature(&AllSens.small_room);
|
||||
result = Dallas_ReadTemperature(&AllSens.living_room);
|
||||
result = Dallas_ReadTemperature(&AllSens.basement);
|
||||
}
|
||||
|
||||
void Dallas_FirstInit(void)
|
||||
{
|
||||
uint8_t mask = DALLAS_USER_BYTE_ALL;
|
||||
OneWire_Init(&OW);
|
||||
DS18B20_Search(&DS, &OW);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0);
|
||||
Dallas_WriteUserBytes(&AllSens.outdoor, 1, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.indoor, 1);
|
||||
Dallas_WriteUserBytes(&AllSens.indoor, 2, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 2);
|
||||
Dallas_WriteUserBytes(&AllSens.bathroom, 3, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.kitchen, 3);
|
||||
Dallas_WriteUserBytes(&AllSens.kitchen, 4, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.big_room, 4);
|
||||
Dallas_WriteUserBytes(&AllSens.big_room, 5, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.small_room, 5);
|
||||
Dallas_WriteUserBytes(&AllSens.small_room, 6, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.living_room, 6);
|
||||
Dallas_WriteUserBytes(&AllSens.living_room, 7, NULL, mask);
|
||||
Dallas_SensorInitByInd(&OW, &AllSens.basement, 7);
|
||||
Dallas_WriteUserBytes(&AllSens.basement, 8, NULL, mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî èíäåêó
|
||||
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @param UserBytes34 Ïîëüçîâàòåëüñêèå áàéòû 3 è 4, NULL äëÿ èãíîðà
|
||||
* @param UserBytes12 Ïîëüçîâàòåëüñêèå áàéòû 1 è 2, NULL äëÿ èãíîðà
|
||||
* @retval HAL Status
|
||||
* @details ñòàðøèé áàéò - UserByte4/UserByte2, ìëàäøèé - UserByte3/UserByte1.
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint16_t UserBytes34, uint16_t UserBytes12)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
uint8_t UserByte1 = UserBytes12 & 0xFF;
|
||||
uint8_t UserByte2 = UserBytes12 >> 8;
|
||||
uint8_t UserByte3 = UserBytes34 & 0xFF;
|
||||
uint8_t UserByte4 = UserBytes34 >> 8;
|
||||
uint8_t UserByte12Cmp = 0;
|
||||
uint8_t UserByte34Cmp = 0;
|
||||
|
||||
if(onewire == NULL)
|
||||
return HAL_ERROR;
|
||||
if(sensor == NULL)
|
||||
return HAL_ERROR;
|
||||
|
||||
for(int i = 0; i < DS18B20_DEVICE_AMOUNT; i++)
|
||||
{
|
||||
UserByte12Cmp = 0; UserByte34Cmp = 0;
|
||||
sensor->sensROM = &DS.DevAddr[i];
|
||||
sensor->onewire = onewire;
|
||||
|
||||
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
||||
result = Dallas_IsConnected(sensor);
|
||||
if (result != HAL_OK)
|
||||
return result;
|
||||
|
||||
|
||||
/* Ñðàâíåíèå UserByte1 è UserByte2, åñëè âûáðàíî */
|
||||
if(UserBytes12 != NULL)
|
||||
{
|
||||
if( (sensor->scratchpad.tHighRegister == UserByte1) &&
|
||||
(sensor->scratchpad.tLowRegister == UserByte2))
|
||||
{
|
||||
UserByte12Cmp = 1;
|
||||
}
|
||||
}/* Åñëè ðàâíåíèå UserByte1 è UserByte2 íå âûáðàíî, òî ñ÷èòàåì ÷òî îíè îäèíàêîâûå */
|
||||
else
|
||||
{
|
||||
UserByte12Cmp = 1;
|
||||
}
|
||||
/* Ñðàâíåíèå UserByte3 è UserByte4, åñëè âûáðàíî */
|
||||
if(UserBytes34 != NULL)
|
||||
{
|
||||
if( (sensor->scratchpad.UserByte3 == UserByte3) &&
|
||||
(sensor->scratchpad.UserByte4 == UserByte4))
|
||||
{
|
||||
UserByte34Cmp = 1;
|
||||
}
|
||||
}/* Åñëè ðàâíåíèå UserByte3 è UserByte4 íå âûáðàíî, òî ñ÷èòàåì ÷òî îíè îäèíàêîâûå */
|
||||
else
|
||||
{
|
||||
UserByte34Cmp = 1;
|
||||
}
|
||||
/* Åñëè íàøëè íóæíûé äàò÷èê - çàâåðøàåì ïîèñê */
|
||||
if(UserByte12Cmp && UserByte34Cmp)
|
||||
{
|
||||
return HAL_OK;
|
||||
}
|
||||
}
|
||||
/* Âîçâðàùàåì îøèáêó åñëè íå íàøëè */
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî èíäåêó
|
||||
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @param sens_ind Ïîðÿäêîâûé íîìåð äàò÷èêà â ñòðóêòóðå
|
||||
* @retval HAL Status
|
||||
* @details Èíäåêñ - ýòî ïîðÿäêîâûé íîìåð äàò÷èêà â ñïèñêå íàéäåííûõ.
|
||||
* Ò.å. êàêèì ïî ñ÷åòó ýòîò äàò÷èê áûë íàéäåí
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t sens_ind)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
if(onewire == NULL)
|
||||
return HAL_ERROR;
|
||||
if(sensor == NULL)
|
||||
return HAL_ERROR;
|
||||
|
||||
sensor->sensROM = &DS.DevAddr[sens_ind];
|
||||
sensor->onewire = onewire;
|
||||
|
||||
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
||||
result = Dallas_IsConnected(sensor);
|
||||
if (result != HAL_OK)
|
||||
return result;
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Çàïóñêàåò èçìåðåíèå òåìïåðàòóðû íà âñåõ äàò÷èêàõ
|
||||
* @param waitCondition Óñëîâèå îæèäàíèÿ çàâåðøåíèÿ ïðåîáðàçîâàíèÿ
|
||||
* @retval HAL Status
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitCondition waitCondition, uint8_t dallas_delay_ms)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
uint8_t rxDummyData;
|
||||
|
||||
// Îòïðàâêà êîìàíäû íà÷àëà ïðåîáðàçîâàíèÿ òåìïåðàòóðû
|
||||
result = DS18B20_StartConvTAll(onewire);
|
||||
if(result != HAL_OK)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
|
||||
if (waitCondition == DALLAS_WAIT_BUS)
|
||||
{
|
||||
result = DS18B20_WaitForEndConvertion(onewire);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì çàäåðæêè
|
||||
if (waitCondition == DALLAS_WAIT_DELAY)
|
||||
{
|
||||
uint32_t delayValueMs = 0;
|
||||
|
||||
switch (dallas_delay_ms)
|
||||
{
|
||||
case DALLAS_CONFIG_9_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_9_BITS;
|
||||
break;
|
||||
|
||||
case DALLAS_CONFIG_10_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_10_BITS;
|
||||
break;
|
||||
|
||||
case DALLAS_CONFIG_11_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_11_BITS;
|
||||
break;
|
||||
|
||||
case DALLAS_CONFIG_12_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_12_BITS;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
HAL_Delay(delayValueMs);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Èçìåðÿåò òåìïåðàòóðó íà äàò÷èêå
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @param waitCondition Óñëîâèå îæèäàíèÿ çàâåðøåíèÿ ïðåîáðàçîâàíèÿ
|
||||
* @retval HAL Status
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitCondition waitCondition)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
uint8_t rxDummyData;
|
||||
|
||||
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
||||
result = Dallas_IsConnected(sensor);
|
||||
if (result != HAL_OK)
|
||||
return result;
|
||||
|
||||
// Îòïðàâêà êîìàíäû íà÷àëà ïðåîáðàçîâàíèÿ òåìïåðàòóðû
|
||||
result = DS18B20_StartConvT(sensor->onewire, (uint8_t *)sensor->sensROM);
|
||||
if(result != HAL_OK)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
|
||||
if (waitCondition == DALLAS_WAIT_BUS)
|
||||
{
|
||||
result = DS18B20_WaitForEndConvertion(sensor->onewire);
|
||||
if(result == HAL_TIMEOUT)
|
||||
{
|
||||
sensor->f.timeout_convertion_cnt++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì çàäåðæêè
|
||||
if (waitCondition == DALLAS_WAIT_DELAY)
|
||||
{
|
||||
uint32_t delayValueMs = 0;
|
||||
|
||||
switch (sensor->scratchpad.ConfigRegister)
|
||||
{
|
||||
case DALLAS_CONFIG_9_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_9_BITS;
|
||||
break;
|
||||
|
||||
case DALLAS_CONFIG_10_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_10_BITS;
|
||||
break;
|
||||
|
||||
case DALLAS_CONFIG_11_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_11_BITS;
|
||||
break;
|
||||
|
||||
case DALLAS_CONFIG_12_BITS:
|
||||
delayValueMs = DALLAS_DELAY_MS_12_BITS;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
HAL_Delay(delayValueMs);
|
||||
}
|
||||
|
||||
/* Íå ñ÷èòûâàåì òåìïåðàòóðó, åñëè íå âûáðàíî îæèäàíèå îêîí÷àíèÿ ïðåîáðàçîâàíèÿ */
|
||||
if(waitCondition != DALLAS_WAIT_NONE)
|
||||
{
|
||||
result = Dallas_ReadTemperature(sensor);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief ×èòàåò ñîäåðæèìîå ïàìÿòè (scratchpad) äàò÷èêà DALLAS_HandleTypeDef
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @retval HAL Status
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_HandleTypeDef *sensor)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
||||
result = Dallas_IsConnected(sensor);
|
||||
if (result != HAL_OK)
|
||||
return result;
|
||||
|
||||
|
||||
result = DS18B20_CalcTemperature(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad, &sensor->temperature);
|
||||
|
||||
if (result != HAL_OK)
|
||||
{
|
||||
sensor->f.read_temperature_err_cnt++;
|
||||
return result;
|
||||
}
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Âûïîëíÿåò êîìàíäó èíèöèàëèçàöèè DALLAS_HandleTypeDef
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @retval HAL Status
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_IsConnected(DALLAS_HandleTypeDef *sensor)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
||||
|
||||
if (result == HAL_OK)
|
||||
{
|
||||
sensor->isConnected = 1;
|
||||
return HAL_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(sensor->isConnected == 1)
|
||||
{
|
||||
sensor->f.disconnect_cnt++;
|
||||
}
|
||||
sensor->isConnected = 0;
|
||||
return HAL_BUSY; // èñïîëüçóþ busy, ÷òîáû îòëè÷àòü ñèòóàöèþ îò HAL_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Çàïèñûâàåò ïîëüçîâàòåëüñêèå áàéòû
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @param UserBytes12 Ïîëüçîâàòåëüñêèå áàéòû 1 è 2
|
||||
* @param UserBytes34 Ïîëüçîâàòåëüñêèå áàéòû 3 è 4
|
||||
* @param UserBytesMask Ìàñêà, êàêèå áàéòû çàïèñûâàòü, à êàêèå íåò
|
||||
* @retval HAL Status
|
||||
* @details ñòàðøèé áàéò - UserByte4/UserByte2, ìëàäøèé - UserByte3/UserByte1.
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_WriteUserBytes(DALLAS_HandleTypeDef *sensor, uint16_t UserBytes12, uint16_t UserBytes34, uint8_t UserBytesMask)
|
||||
{
|
||||
HAL_StatusTypeDef result = DS18B20_WriteUserBytes(sensor->onewire, (uint8_t *)sensor->sensROM, UserBytes12, UserBytes34, UserBytesMask);
|
||||
if (result != HAL_OK)
|
||||
{
|
||||
sensor->f.read_temperature_err_cnt++;
|
||||
return result;
|
||||
}
|
||||
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
||||
if (result != HAL_OK)
|
||||
{
|
||||
sensor->f.read_temperature_err_cnt++;
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
///**
|
||||
// * @brief ×èòàåò óíèêàëüíûé ROM-êîä äàò÷èêà DALLAS_HandleTypeDef
|
||||
// * @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
// * @retval HAL Status
|
||||
// */
|
||||
//HAL_StatusTypeDef Dallas_ReadRom(DALLAS_HandleTypeDef *sensor)
|
||||
//{
|
||||
// HAL_StatusTypeDef result = HAL_OK;
|
||||
// uint8_t rxData[DALLAS_READ_ROM_RX_BYTES_NUM];
|
||||
//
|
||||
// DS18B20_ReadScratchpad(sensor->onewire, sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
||||
//
|
||||
//
|
||||
// if (result == HAL_OK)
|
||||
// {
|
||||
// for (uint8_t i = 0; i < DALLAS_SERIAL_NUMBER_LEN_BYTES; i++)
|
||||
// {
|
||||
// sensor->sensROM[i] = rxData[DALLAS_SERIAL_NUMBER_OFFSET_BYTES + i];
|
||||
// }
|
||||
// }
|
||||
|
||||
// return result;
|
||||
//}
|
||||
Reference in New Issue
Block a user