Advertisement
Um-ka

Untitled

Feb 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. #define VW_ID 54424
  2.  
  3. uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data){
  4. #define lo8(x) ((x)&0xff)
  5. #define hi8(x) ((x)>>8)
  6.   data ^= lo8 (crc);
  7.   data ^= data << 4;
  8.   return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4)
  9.           ^ ((uint16_t)data << 3));
  10. }
  11.  
  12. uint8_t vw_send(uint8_t* buf, uint8_t len){
  13. #define VW_SPEED 500
  14. #define GPIO_TX 0 // GPIO передатчика
  15. #define VW_HEADER_LEN 8
  16. #define VW_MAX_MESSAGE_LEN 40
  17. #define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3
  18.   static uint8_t vw_tx_buf[(VW_MAX_MESSAGE_LEN * 2) + VW_HEADER_LEN]
  19.     = {0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x38, 0x2c};
  20.   const uint8_t symbols[] =
  21.   {
  22.     0xd,  0xe,  0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c,
  23.     0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x32, 0x34
  24.   };
  25.  
  26.   // Number of symbols in vw_tx_buf to be sent;
  27.   uint8_t vw_tx_len = 0;
  28.   uint8_t i;
  29.   uint8_t index = 0;
  30.   uint16_t crc = 0xffff;
  31.   uint8_t *p = vw_tx_buf + VW_HEADER_LEN; // start of the message area
  32.   uint8_t count = len + 3; // Added byte count and FCS to get total number of bytes
  33.  
  34.   if (len > VW_MAX_PAYLOAD)
  35.     return false;
  36.   // Encode the message length
  37.   crc = _crc_ccitt_update(crc, count);
  38.   p[index++] = symbols[count >> 4];
  39.   p[index++] = symbols[count & 0xf];
  40.  
  41.   // Encode the message into 6 bit symbols. Each byte is converted into
  42.   // 2 6-bit symbols, high nybble first, low nybble second
  43.   for (i = 0; i < len; i++)
  44.   {
  45.     crc = _crc_ccitt_update(crc, buf[i]);
  46.     p[index++] = symbols[buf[i] >> 4];
  47.     p[index++] = symbols[buf[i] & 0xf];
  48.   }
  49.  
  50.   // Append the fcs, 16 bits before encoding (4 6-bit symbols after encoding)
  51.   // Caution: VW expects the _ones_complement_ of the CCITT CRC-16 as the FCS
  52.   // VW sends FCS as low byte then hi byte
  53.   crc = ~crc;
  54.   p[index++] = symbols[(crc >> 4)  & 0xf];
  55.   p[index++] = symbols[crc & 0xf];
  56.   p[index++] = symbols[(crc >> 12) & 0xf];
  57.   p[index++] = symbols[(crc >> 8)  & 0xf];
  58.  
  59.   // Total number of 6-bit symbols to send
  60.   vw_tx_len = index + VW_HEADER_LEN;
  61.   cli();
  62.   uint8_t vw_tx_bit = 0, vw_tx_index = 0;
  63.   for (; vw_tx_index < vw_tx_len;)
  64.   {
  65.     digitalWrite(GPIO_TX , vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++));
  66.     if (vw_tx_bit >= 6)
  67.     {
  68.       vw_tx_index++;
  69.       vw_tx_bit = 0;
  70.     }
  71.     delayMicroseconds((1000000 / VW_SPEED));
  72.   }
  73.   digitalWrite(GPIO_TX , 0);
  74.   sei();
  75.   return true;
  76. }
  77. uint8_t countsend;
  78.  
  79. void setup(){
  80.   pinMode(GPIO_TX, OUTPUT);
  81. }
  82.  
  83. void loop(){
  84.   int16_t hum = 610; // 60.0%
  85.   int16_t temp = 253; // 25.3 С
  86.   uint8_t msg[] = {26, VW_ID & 255, VW_ID / 256, countsend,
  87.                    1, temp & 255, temp / 256, // датчик температуры
  88.                    1, hum & 255, temp / 256, // датчик температуры
  89.                    1, temp & 255, temp / 256, // датчик температуры
  90.                    1, hum & 255, temp / 256, // датчик температуры
  91.                    2, hum & 255, hum / 256 // датчик влажности
  92.                   };
  93.   vw_send((uint8_t *)msg, sizeof(msg));
  94.   countsend++; // счетчик передач
  95.   delay(5000);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement