реклама на сайте
подробности

 
 
 
Reply to this topicStart new topic
> Как подключить датчик температуры к AVR?
ShimkoMax
сообщение Jun 27 2018, 19:13
Сообщение #1





Группа: Новичок
Сообщений: 1
Регистрация: 27-06-18
Пользователь №: 105 495



Здравствуйте. Требуется вывести данные температуры и влажности на экран. С LCD дисплеем разобрался, осталось разобраться с SHT21. Постоянно выводит 0 на дисплей:

Main.c:
CODE
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>

#include "globals.h"
#include "LCD_lib.h"
#include "twi.h" // I2C
#include "sht21.h"

int main(void)
{
I2C_Init();
_delay_ms(100);
LCD_init();

char buffer[20];

roomAdrInit();

SHT21_reset();

while(1)
{
SHT21_reset();
LCD_sendString(itoa((int)get_temperature(), buffer, 10), 4, 0, 0);
_delay_ms(1000);
}
}


twi.c:
CODE
#include#inclu <avr/io.h>
#include "twi.h"

void I2C_Init(void)
{
TWSR = 0;
TWBR = 0x20;
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
}

void I2C_StartCondition(void)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)));
}

void I2C_StopCondition(void)
{
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}

void I2C_SendByte(uint8_t data)
{
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT)));
}

unsigned char I2C_ReadByteAck(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
while(!(TWCR & (1<<TWINT)));

return TWDR;
}

unsigned char I2C_ReadByteNak(void)
{
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT)));

return TWDR;
}


sht21.c:
CODE
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "twi.h"
#include "sht21.h"

void SHT21_reset()
{
unsigned char reg[1];
reg[0]=SHT21_reset_cmd;
I2C_StartCondition();
I2C_SendByte(SHT21_i2c_write);
I2C_SendByte(*reg);
I2C_StopCondition();
_delay_ms(100);
}

uint16_t checksum(unsigned char data[],uint8_t byte, uint8_t check)
{
uint8_t crc=0;
uint8_t bytectr,bit;
for (bytectr=0; bytectr<byte;bytectr++)
{
crc^=(data[bytectr]);
for (bit=8;bit>0;bit--)
{
if(crc&0x80)
{
crc=(crc<<1)^polynomial;
}
else
{
crc=crc<<1;
}
}
}
if (crc!=check)
{
return 0;
}
else
{
return data;
}
}

void write_user_register()
{
unsigned char reg[3];
reg[0]=user_register_write;
reg[1]=0x44;

I2C_StartCondition();
I2C_SendByte(SHT21_i2c_write);
I2C_SendByte(*reg);
I2C_StopCondition();
}

uint16_t read_value(uint8_t reg)
{
char data[4],crc;
uint16_t result;
data[0]=reg;

I2C_StartCondition();
I2C_SendByte(SHT21_i2c_write);
I2C_SendByte(reg);

I2C_StartCondition();
I2C_SendByte(SHT21_i2c_read);
data[1] = I2C_ReadByteAck();
data[2] = I2C_ReadByteAck();
crc=I2C_ReadByteNak();
I2C_StopCondition();

result=(data[1]<<8) | data[2];
checksum(result,4,crc);
result &= 0xFFFC;
return result;
}

float get_humidity()
{
//char buffer2[4];
uint16_t hum_value = read_value(humidity_hold_mode);
return -6 + 125.0 / 65536.0 * hum_value;
//dtostrf(rh,5,2,buffer2);
_delay_ms(100);
}

float get_temperature()
{
//char buffer1[4];
uint16_t temp_value = read_value(temperature_hold_mode);
return -46.85 + 175.72 / 65536.0 * temp_value;
//dtostrf(tc,5,2,buffer1);
_delay_ms(100);
}


Сообщение отредактировал IgorKossak - Jun 28 2018, 20:52
Причина редактирования: [codebox] для длинного кода, [code] - для короткого!
Go to the top of the page
 
+Quote Post
k155la3
сообщение Jun 29 2018, 07:46
Сообщение #2


Профессионал
*****

Группа: Свой
Сообщений: 1 123
Регистрация: 8-03-09
Из: Днепр
Пользователь №: 45 848



Цитата(ShimkoMax @ Jun 27 2018, 22:13) *
. . . Постоянно выводит 0 на дисплей . .
Ну, если Ваша система не выдала никакого сообщения об ошибке, то температура равна нулю. Смиритесь с этим sm.gif
Где в коде обработка ошибок ?
Отсоедините датчик. Проверьте, изменится ли, так сказать, результат.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 16th April 2024 - 13:58
Рейтинг@Mail.ru


Страница сгенерированна за 0.0138 секунд с 7
ELECTRONIX ©2004-2016