Сб май 19, 2018 23:52:23
Вс май 20, 2018 00:07:58
korob писал(а):Опрос только слишком часто.
#include <xc.h>
/*************************************************************************************/
#pragma config MCLRE=ON
#pragma config CP=ON
#pragma config CPD=OFF
#pragma config BOREN=ON
#pragma config WDTE=OFF
#pragma config PWRTE=ON
#pragma config FOSC=INTRCIO
/*************************************************************************************/
#define _XTAL_FREQ 4000000UL
/*************************************************************************************/
#define WIRE1_PORT GP1
#define WIRE1_TRIS TRISIO1
#define SENSOR_POWER_PIN GP0
#define SENSOR_POWER_TRIS TRISIO0
#define OUT_PIN GP2
#define OUT_TRIS TRISIO2
/*************************************************************************************/
#define WIRE1_OUT(val) do{WIRE1_TRIS=val; WIRE1_PORT=val;}while(0)
/*************************************************************************************/
/*************************************************************************************/
unsigned char Wire_Reset(void){
WIRE1_OUT(0);
__delay_us(500);
WIRE1_OUT(1);
__delay_us(10);
if(!WIRE1_PORT) return 0;
__delay_us(100);
if(WIRE1_PORT) return 0;
__delay_us(200);
if(!WIRE1_PORT) return 0;
return 1;
}
/*************************************************************************************/
unsigned char Wire_Bit(unsigned char rx){
unsigned char ret;
WIRE1_OUT(0);
__delay_us(2);
WIRE1_OUT(rx);
__delay_us(3);
ret = WIRE1_PORT;
__delay_us(55);
WIRE1_OUT(1);
return ret;
}
/*************************************************************************************/
unsigned char Wire_Write(unsigned char byte){
unsigned char ret=0, i;
for(i=0;i<8;i++){
ret = ret>>1;
if(Wire_Bit(byte&1)) ret|=0x80;
byte = byte>>1;
}
return ret;
}
/*************************************************************************************/
const unsigned char crcval[256]={
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
};
/**************************************************************************/
unsigned char CRC(unsigned char *pArr, unsigned char len){
unsigned char ret=0;
do{
ret=crcval[ret^*pArr++];
}while(--len);
return ret;
}
/*************************************************************************************/
/*************************************************************************************/
unsigned char Is_Sensor(void){
unsigned char err_cnt;
err_cnt=0;
do{
__delay_ms(100);
}while(!Wire_Reset() && (++err_cnt<3));
__delay_ms(10);
return (err_cnt<3);
}
/*************************************************************************************/
/*************************************************************************************/
unsigned char Read_Sensor(unsigned char *pArr, unsigned char len, unsigned char cmd, unsigned char skip_rom){
unsigned char i, err_cnt;
err_cnt=0;
do{
if(!Is_Sensor()) return 0;
if(skip_rom) Wire_Write(0xCC);
Wire_Write(cmd);
for(i=0;i<len;i++) pArr[i]=Wire_Write(0xFF);
}while(CRC(pArr, len) && (++err_cnt<3));
return (err_cnt<3);
}
/*************************************************************************************/
/*************************************************************************************/
void main(void){
static unsigned char arr[9], dev_fam, i;
static signed int temp;
//----------
CMCON = 0x07;
//----------
SENSOR_POWER_TRIS=0;
SENSOR_POWER_PIN=0;
OUT_TRIS=0;
OUT_PIN=0;
//----------
__delay_ms(1000);
//----------
while(1){
//----------
SENSOR_POWER_PIN=1;
__delay_ms(300);
//----------
if(!Is_Sensor()){
OUT_PIN=0;
continue;
}
//----------
Wire_Write(0xCC);
Wire_Write(0x44);
__delay_ms(800);
//----------
if(!Read_Sensor(arr, 8, 0x33, 0)){
OUT_PIN=0;
continue;
}
//----------
dev_fam=arr[0];
//----------
if(!Read_Sensor(arr, 9, 0xBE, 1)){
OUT_PIN=0;
continue;
}
//----------
temp = *(int*)arr * 10;
temp /= (dev_fam==0x10)?2:16;
//----------
if(temp<=20) OUT_PIN=1;
if(temp>=40) OUT_PIN=0;
//----------
SENSOR_POWER_PIN=0;
//----------
for(i=0;i<60;i++) __delay_ms(1000);
//----------
}
}
/*************************************************************************************/
Вс май 20, 2018 07:44:58
Вс май 20, 2018 07:52:50
Вс май 20, 2018 08:08:19
Вс май 20, 2018 09:02:22
Вс май 20, 2018 12:33:36
Неа.musor писал(а):выходит датчик у ТС фэйк? перетерка ис DS18S20 ???
Вс май 20, 2018 12:39:34
Вс май 20, 2018 18:36:55
Вс май 20, 2018 19:23:39
Вс май 20, 2018 19:35:21
Вс май 20, 2018 20:18:24
korob писал(а):Ну а остальные у Вас тоже нормально работают?
Ведёт себя точно так же как и с проверкой CRC (не работает). Странно.oleg110592 писал(а):лучше пробовать эту:
Вс май 20, 2018 20:21:41
Пн май 21, 2018 06:47:50
Вт май 22, 2018 09:08:34
#include <xc.h>
#pragma config MCLRE=OFF
#pragma config CP=ON
#pragma config CPD=OFF
#pragma config BOREN=ON
#pragma config WDTE=ON
#pragma config PWRTE=ON
#pragma config FOSC=INTRCIO
#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 4000000U
#endif
#define DQ GP1
#define DQ_TRIS TRISIO1
#define CONVERT_TEMP 0x44
#define WRITE_SCRATCHPAD 0x4E
#define READ_SCRATCHPAD 0xBE
#define COPY_SCRATCHPAD 0x48
#define RECALL_E2 0xB8
#define READ_POWER_SUPPLY 0xB4
#define SKIP_ROM 0xCC
int temperature;
unsigned char ds18b20[9];
const unsigned char crc_array[256] =
{
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83,
0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e,
0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0,
0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d,
0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5,
0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58,
0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6,
0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b,
0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f,
0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92,
0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c,
0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1,
0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49,
0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4,
0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a,
0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7,
0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35,
};
unsigned char Count_crc_dallas(unsigned char *buf, unsigned char size)
{
unsigned char crc = 0;
for (; size > 0; size--)
{
crc = crc_array[(*buf) ^ crc];
buf++;
}
return (crc);
}
//Application Note 162: Interfacing the DS18x20/DS1822 1- Wire Temperature Sensor in a Microcontroller Environment
//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is 480. Presence checked,
// another 70us later.
//
unsigned char ow_reset(void)
{
unsigned char presence;
DQ = 0; //pull DQ line low
DQ_TRIS = 0;
__delay_us(480); // leave it low for 480us
DQ_TRIS = 1; // allow line to return high
__delay_us(70); // wait for presence
presence = DQ; // get presence signal
__delay_us(440); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part
//////////////////////////////////////////////////////////////////////////////
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//
unsigned char read_bit(void)
{
DQ = 0; // pull DQ low to start timeslot
DQ_TRIS = 0;
__delay_us(2);
DQ_TRIS = 1; // then return high
__delay_us(15); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}
//////////////////////////////////////////////////////////////////////////////
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
void write_bit(char bitval)
{
DQ = 0; // pull DQ low to start timeslot
DQ_TRIS = 0;
if(bitval==1) DQ_TRIS = 1; // return DQ high if write 1
__delay_us(104); // hold value for remainder of timeslot
DQ_TRIS = 1;
}// Delay provides = 104us
//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
// shifts it left
__delay_us(120); // wait for rest of timeslot
}
return(value);
}
//////////////////////////////////////////////////////////////////////////////
// WRITE_BYTE - writes a byte to the one-wire bus.
//
void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
__delay_us(104);
}
void main(void)
{
unsigned char i;
GPIO = 0x00;
CMCON = 0x07;
TRISIO = 0x00;
CLRWDT();
PS0 = 1; // :128 – 2.3 sec
PS1 = 1;
PS2 = 1;
PSA = 1; // WDT
while (1)
{
CLRWDT();
GP0=1;
__delay_ms(300);
ow_reset();
write_byte(SKIP_ROM);
write_byte(CONVERT_TEMP);
__delay_ms(1000);
ow_reset();
write_byte(SKIP_ROM);
write_byte(READ_SCRATCHPAD);
ds18b20[0] = read_byte();
ds18b20[1] = read_byte();
ds18b20[2] = read_byte();
ds18b20[3] = read_byte();
ds18b20[4] = read_byte();
ds18b20[5] = read_byte();
ds18b20[6] = read_byte();
ds18b20[7] = read_byte();
ds18b20[8] = read_byte();
temperature=ds18b20[0];
temperature+=(unsigned int)ds18b20[1]<<8;
if((ds18b20[1] & (1<<7))==0)
{
temperature*=5;
temperature>>=3;
}
else
{
temperature=-temperature;
temperature*=5;
temperature>>=3;
temperature=-temperature;
}
GP0=0;
//if(presence)
if(Count_crc_dallas(ds18b20, 8) == ds18b20[8])
{
if(temperature >= 300) GP2=0;
if(temperature <= 280) GP2=1;
}
else
GP2=0;
// for (i = 0; i < 60; i++)
// {
// CLRWDT();
// __delay_ms(1000);
// }
}
}
Сб июн 09, 2018 11:05:50
Сб июн 09, 2018 11:11:55
vk696 писал(а):Если действительно хочешь сам наваять, могу поделиться функциями работы с DS18B20 на Си. все работает 100%
Сб июн 09, 2018 12:11:03
Вс июн 10, 2018 18:42:13
Ср июн 13, 2018 20:56:32