'**************************************************************** '* Name : UNTITLED.BAS * '* Author : [select VIEW...EDITOR OPTIONS] * '* Notice : Copyright (c) 2017 [select VIEW...EDITOR OPTIONS] * '* : All Rights Reserved * '* Date : 20.12.2017 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** @ device pic16F84a, HS_osc, wdt_off, PWRT_on, protect_off define OSC 12 DEFINE SHIFT_PAUSEUS 20 output PORTA.1 ptt var PORTA.1 ' Переключение прием/педерача в сети RS-485, микросхема ST-485, 2 и з ноги ptt = 0 sda var PORTB.3 scl var PORTB.2 tx var PORTA.2 ' Передаваемые данные в RS-485 rx var PORTB.4 ' Принимаемые данные из RS-485 OUTPUT PORTB.5 led var PORTB.5 ' Светодиодик активности led = 0 reg var word addr var byte TLOW var byte THI var byte tir var word j var byte command var byte ACK var byte FIXHI var byte tempir var word tout var word a var byte b var byte c var byte sda = 1 scl = 1 pause 1000 begin: serin rx, 2, ["#305"], a, b, c ' Ловим сетевой адрес led = 1 pause 2 ' Обязательная задержка, чтобы MASTER успел выключить режим педерачи ptt = 1 ' Включаемся на педерачу pause 1 call bitbang ' Чтение пирометра SEROUT tx, 2, ["%T", #tout dig 2, #tout dig 1, #tout dig 0,"00000000000000001"] ' Отправляем ответ. Нули - это мусор. Нужно 20 байт в посылке ptt = 0:led = 0 ' Выключаем педерачу goto begin bitbang: SDA = 0 pauseus 15 SCL = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 '0 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 '0 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 '0 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 pauseus 15 scl = 1 ' W pauseus 15 scl = 0 pauseus 15 input sda 'make sda an input for acknowledge A part of sequence pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 output sda 'make sda an output again to send register 0x07 sda = 0 pauseus 15 for j = 1 to 5 'for/next loop is quick for the 5 leading zeroes scl = 1 pauseus 15 scl = 0 pauseus 15 next sda = 1 'for/next loop for the three 1's for j = 1 to 3 pauseus 15 scl = 1 pauseus 15 scl = 0 next pauseus 15 input sda 'make sda an input for acknowledge A pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 output sda 'make sda an output again for S pauseus 15 sda = 1 pauseus 15 scl = 1 pauseus 15 sda = 0 pauseus 15 scl = 0 pauseus 15 'Like the code for B4 this segment generates B5 1011010x where x is the 8th (unused) bit (actually R). The x=1 (0101 == 5) which R is. 'Again, there may be a more elegant way of sending these bits but this works and the timing and sequence are correct. sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 '0 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 '0 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 '1 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 0 '0 pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 sda = 1 pauseus 15 scl = 1 ' R pauseus 15 scl = 0 pauseus 15 sda = 0 pauseus 15 input sda 'make sda an input for acknowledge A part of sequence. It remains an input heading into shiftin pauseus 15 scl = 1 pauseus 15 scl = 0 pauseus 15 shiftin SDA, SCL, 0, [TLOW, ack\1, thi] 'Now we can use the shiftin command to read the 8 least significant bits, the single acknowledge 'bit, and the 8 most significant bits. tempir = thi * $100 'shifts 8 bits to the left to move this byte to the upper 8 bits of a word tempir = tempir + tlow 'add the lower byte to the upper byte to make a word tout = tempir/50 'each bit is .02 degrees K so either multiply by .02 or divide by 50 tout = tout - 273 'subtract 273.15 (to get temperature in degrees C 'added for readability during testing trisB.2 = 0 'both sda and scl are outputs to reset for the next time thru trisB.3 = 0 sda = 1 'they both start high scl = 1 return end