Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define VW_SPEED 500
- #define VW_ID 54423
- #define GPIO_TX 13 // GPIO передатчика
- #define VW_HEADER_LEN 8
- #define VW_MAX_MESSAGE_LEN 40
- #define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3
- static uint8_t vw_tx_buf[(VW_MAX_MESSAGE_LEN * 2) + VW_HEADER_LEN]
- = {0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x38, 0x2c};
- const uint8_t symbols[] =
- {
- 0xd, 0xe, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c,
- 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x32, 0x34
- };
- #define lo8(x) ((x)&0xff)
- #define hi8(x) ((x)>>8)
- uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data)
- {
- data ^= lo8 (crc);
- data ^= data << 4;
- return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4)
- ^ ((uint16_t)data << 3));
- }
- uint8_t vw_send(uint8_t* buf, uint8_t len)
- {
- // Number of symbols in vw_tx_buf to be sent;
- uint8_t vw_tx_len = 0;
- uint8_t i;
- uint8_t index = 0;
- uint16_t crc = 0xffff;
- uint8_t *p = vw_tx_buf + VW_HEADER_LEN; // start of the message area
- uint8_t count = len + 3; // Added byte count and FCS to get total number of bytes
- if (len > VW_MAX_PAYLOAD)
- return false;
- // Encode the message length
- crc = _crc_ccitt_update(crc, count);
- p[index++] = symbols[count >> 4];
- p[index++] = symbols[count & 0xf];
- // Encode the message into 6 bit symbols. Each byte is converted into
- // 2 6-bit symbols, high nybble first, low nybble second
- for (i = 0; i < len; i++)
- {
- crc = _crc_ccitt_update(crc, buf[i]);
- p[index++] = symbols[buf[i] >> 4];
- p[index++] = symbols[buf[i] & 0xf];
- }
- // Append the fcs, 16 bits before encoding (4 6-bit symbols after encoding)
- // Caution: VW expects the _ones_complement_ of the CCITT CRC-16 as the FCS
- // VW sends FCS as low byte then hi byte
- crc = ~crc;
- p[index++] = symbols[(crc >> 4) & 0xf];
- p[index++] = symbols[crc & 0xf];
- p[index++] = symbols[(crc >> 12) & 0xf];
- p[index++] = symbols[(crc >> 8) & 0xf];
- // Total number of 6-bit symbols to send
- vw_tx_len = index + VW_HEADER_LEN;
- cli();
- uint8_t vw_tx_bit = 0,vw_tx_index=0;
- for (; vw_tx_index < vw_tx_len;)
- {
- digitalWrite(GPIO_TX , vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++));
- if (vw_tx_bit >= 6)
- {
- vw_tx_index++;
- vw_tx_bit = 0;
- }
- delayMicroseconds((1000000/VW_SPEED));
- }
- digitalWrite(GPIO_TX , 0);
- sei();
- return true;
- }
- uint8_t countsend;
- void setup()
- {
- pinMode(GPIO_TX, OUTPUT);
- }
- void loop()
- {
- int16_t hum=610; // 60.0%
- int16_t temp=253; // 25.3 С
- uint8_t msg[]={26,VW_ID&255,VW_ID/256,countsend,
- 1, temp&255,temp/256, // датчик температуры
- 2, hum&255,hum/256 // датчик влажности
- };
- vw_send((uint8_t *)msg, sizeof(msg));
- countsend++; // счетчик передач
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement