Advertisement
Guest User

Untitled

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