Define CONFIG = 0x23e4 Define CONFIG2 = 0x3eff '16F887 + DHT22 01May2015 'Remote TXR, ID = Pipe 1, Pipe 0 common RX to all remotes 'Normally in RX mode, TX when sensor PIC pin Input change, resumes to RX AllDigital ''Define SIMULATION_WAITMS_VALUE = 1 ''''''''''''''''''' Define CLOCK_FREQUENCY = 8 Define SPICLOCK_STRETCH = 5 Define LCD_INITMS = 2 Define LCD_LINES = 4 Define LCD_CHARS = 16 Define LCD_BITS = 4 Define LCD_DREG = PORTB Define LCD_DBIT = 4 Define LCD_EREG = PORTB Define LCD_EBIT = 3 Define LCD_RSREG = PORTB Define LCD_RSBIT = 2 Define LCD_RWREG = PORTB Define LCD_RWBIT = 1 Define LCD_READ_BUSY_FLAG = 1 ADCON0 = %01000011 ADCON1 = %10001110 ANSEL = %00000011 OSCCON = %01110000 'setup internal oscillator For 8mhz TRISA = %000011111 'PA.0 & 1 Analog, 5=SS, 7=redled,,, 3,4,6 spare TRISB = %00000001 'PB 0=nrf intr, 2 thru 7 LCD.??? TRISC = %11000011 'PC SPI 2,3,4,5 are SPI,, 6,7 UART, 0,1 spare TRISD = %00001111 'PD 0,1,2,3 Inputs,,, 5,6,7,8 Outputs TRISE = %00000111 'PE 0,1,2 spare Dim tx_dta(32) As Byte Dim rx_dta(32) As Byte Dim ascbfr4 As Byte @ 0x070 Dim ascbfr3 As Byte @ 0x071 Dim ascbfr2 As Byte @ 0x072 Dim ascbfr1 As Byte @ 0x073 Dim ascbfr0 As Byte @ 0x074 Dim b2aval As Word Dim temp3 As Word Dim param As Byte Dim per10us As Byte Dim humid As Word 'Relative Humidity Dim tempr As Word 'Temperature Dim tmpval As Byte 'used to calculate humidity and temp Dim dewpnt As Byte Dim sign As Byte 'minus temperature Dim sign2 As Byte 'minus dewpoint Dim bfr As Byte 'rxit bfrlector Dim dta(32) As Byte 'used to hold DHT data bits Dim rxi As Byte Dim dta_len As Byte Dim iflag As Byte Dim adc1 As Byte Dim cnt1 As Byte Dim old_inp As Byte Dim new_inp As Byte Dim x As Byte Dim vadc As Word Symbol rxd_led = PORTA.7 '#### Symbol txd_led = PORTA.6 'each remote has 3 H/L sensor inps, a change is sent to the Base PIC Symbol sw_inp1 = PORTD.0 Symbol sw_inp2 = PORTD.1 Symbol sw_inp3 = PORTD.2 Symbol sw_inp4 = PORTD.3 'each remote has 3 H/L drive outputs, controlled by the Base PIC Symbol sw_out1 = PORTD.4 Symbol sw_out2 = PORTD.5 Symbol sw_out3 = PORTD.6 Symbol sw_out4 = PORTD.7 'Status Byte always 1st byte recvd 'SPI(nRF24L01) commands Const rd_stats = 0x07 'Status Const max_rt = 0x10 'stat bits Const tx_ds = 0x20 Const rx_dr = 0x40 Const observe_tx = 0x08 'Observe TX Const cd = 0x09 'Carrier Detect register Const rd_fifo = 0x17 'FIFO Status Register 'SPI(nRF24L01) registers(addresses) Const wr_config = 0x20 'Config register Const wr_en_aa = 0x21 'Enable Auto Acknowledgment Const wr_en_rxaddr = 0x22 'Enabled RX addresses Const wr_setup_aw = 0x23 'Setup address width Const wr_setup_retr = 0x24 'Setup Auto. Retrans Const wr_rf_ch = 0x25 'RF channel Const wr_rf_setup = 0x26 'RF setup,pwr Const rd_stat = 0x07 Const wr_stat = 0x27 'Status Const wr_addr_p0 = 0x2a 'RX address pipe0 address Const wr_addr_p1 = 0x2b 'pipe1 Const wr_addr_p2 = 0x2c 'pipe2 Const wr_addr_p3 = 0x2d 'pipe3 Const wr_addr_p4 = 0x2e 'pipe4 Const wr_addr_p5 = 0x2f 'pipe5 Const wr_tx_addr = 0x30 'TX register address Const rx_pw_p0 = 0x31 'RX payload width, pipe0 address Const rx_pw_p1 = 0x32 'pipe1 Const rx_pw_p2 = 0x33 'pipe2 Const rx_pw_p3 = 0x34 'pipe3 Const rx_pw_p4 = 0x35 'pipe4 Const rx_pw_p5 = 0x36 'pipe5 Const rd_rx_pload = 0x61 'RX payload register address Const wr_tx_pload = 0xa0 'TX payload register address Const flush_tx = 0xe1 'flush TX register command Const flush_rx = 0xe2 'flush RX register command Const reuse_tx_pl = 0xe3 'reuse TX payload register command Const nop = 0xff 'NOP,used to read status register Symbol nrf_irq = PORTB.0 'low on interrupt, Wh/Bl Symbol nrf_ce = PORTC.1 'CE-nable for nrf radio, Black Define SPI_CS_REG = PORTC ''' was porta.5 '/SS Define SPI_CS_BIT = 2 'CSN SPI CS, Orange Define SPI_SCK_REG = PORTC Define SPI_SCK_BIT = 3 'Yellow/Blue Define SPI_SDI_REG = PORTC 'Green Define SPI_SDI_BIT = 4 Define SPI_SDO_REG = PORTC 'Blue Define SPI_SDO_BIT = 5 dta_len = 15 '+1 While OSCCON.HTS = 0 Wend SPIPrepare per10us = 5 'number of 10usec periods in DHT BIT Lcdinit WaitMs 1 Lcdcmdout LcdClear Lcdout "P1_DHTv5" Disable WaitMs 20 nrf_ce = 0 WaitMs 5 For x = 0 To dta_len rx_dta(x) = "0" Next x tx_dta(0) = "A" 'pipe 1==A For x = 1 To dta_len tx_dta(x) = "0" Next x sw_out1 = 0 sw_out2 = 0 sw_out3 = 1 cnt1 = 0 iflag = 0 old_inp = 0 rxd_led = 0 'for adc read T1CON = %00110100 TMR1H = 0 TMR1L = 0 INTCON = %11000000 OPTION_REG = 0 INTCON.INTF = 0 '''Gosub config_rx1 PIR1.TMR1IF = 0 PIE1.TMR1IE = 0 T1CON.TMR1ON = 0 listen: 'Enable Disable Gosub rd_dht 'there is a 2000ms wait! Gosub conv2asc Gosub show Goto send_tx Goto listen send_tx: Gosub config_tx1 WaitMs 1 Gosub tx_addr1 Gosub transmit1 txd_led = 1 'tx led WaitMs 10 '''Gosub config_rx1 WaitMs 50 txd_led = 0 WaitMs 1 Goto listen End 'receive On Interrupt Save System 'RX receive If PIR1.TMR1IF = 1 Then cnt1 = cnt1 + 1 PIR1.TMR1IF = 0 Endif receive1: If INTCON.INTF = 1 Then INTCON.INTF = 0 rxd_led = 1 Else Goto skip1 Endif SPICSOn SPISend rd_rx_pload WaitUs 10 For x = 0 To dta_len SPIReceive rx_dta(x) WaitUs 10 Next x SPICSOff SPICSOn SPISend flush_rx WaitUs 10 SPICSOff WaitUs 20 SPICSOn SPISend wr_stat WaitUs 10 SPISend 0x40 'reset any nrf intr SPICSOff WaitUs 20 '' If rx_dta(0) = "A" Then 'its for this pipe If rx_dta(1) = "1" Then sw_out1 = 1 'PA.4 Else sw_out1 = 0 Endif If rx_dta(2) = "1" Then sw_out2 = 1 'PB.6 Else sw_out2 = 0 Endif If rx_dta(3) = "1" Then sw_out3 = 1 'PD.6 Else sw_out3 = 0 Endif cnt1 = 17 Endif rxd_led = 0 skip1: INTCON.INTE = 1 're-enable PB.0 intr Resume 'this subr will be in the Remote nrf txr 'the show subr is only for DEV rd_dht: WaitMs 500 'wait for DHT TRISE.2 = 0 'pin to output PORTE.2 = 0 'init DHT WaitMs 20 PORTE.2 = 1 WaitUs 30 TRISE.2 = 1 'set data pin 'read DHT bit string ServoIn PORTE.2, dta(0) 'Wait for dht For bfr = 31 To 0 Step -1 'number of humidity and temp 10uSec periods/DHT bit ServoIn PORTE.2, dta(bfr) Next bfr For bfr = 7 To 0 Step -1 'humid HB rxi = bfr + 24 If dta(rxi) > per10us Then tmpval.bfr = 1 Else tmpval.bfr = 0 'if > cnt 10uSec its a '1' Next bfr humid.HB = tmpval For bfr = 7 To 0 Step -1 'humid LB rxi = bfr + 16 If dta(rxi) > per10us Then tmpval.bfr = 1 Else tmpval.bfr = 0 Next bfr humid.LB = tmpval For bfr = 7 To 0 Step -1 'temp HB rxi = bfr + 8 If dta(rxi) > per10us Then tmpval.bfr = 1 Else tmpval.bfr = 0 Next bfr tempr.HB = tmpval For bfr = 7 To 0 Step -1 'temp LB If dta(bfr) > per10us Then tmpval.bfr = 1 Else tmpval.bfr = 0 Next bfr tempr.LB = tmpval 'tmpval for minus tempr If tempr.HB > 0x7f Then sign = "-" Else sign = 0x20 Endif If tempr.HB > 0x7f Then tempr.HB = tempr.HB And 0x7f Endif 'Td=T -((100-RH)/5) dewpnt = tempr - ((1000 - humid) / 5) Return conv2asc: 'assume DHT22 all 3 params and load to TX bfr b2aval = tempr Gosub bin2asc tx_dta(0) = "A" 'remote pipe 1 tx_dta(1) = sign tx_dta(2) = ascbfr2 tx_dta(3) = ascbfr1 tx_dta(4) = ascbfr0 b2aval = humid Gosub bin2asc tx_dta(5) = ascbfr2 tx_dta(6) = ascbfr1 tx_dta(7) = ascbfr0 b2aval = dewpnt Gosub bin2asc tx_dta(8) = ascbfr2 tx_dta(9) = ascbfr1 tx_dta(10) = ascbfr0 'dummy tx_dta(11) = "1" tx_dta(12) = "2" tx_dta(13) = "3" tx_dta(14) = "4" tx_dta(15) = "5" Return show: Lcdcmdout LcdClear Lcdout " ", tx_dta(5), tx_dta(6), ".", tx_dta(7), "%RH" WaitMs 500 Lcdcmdout LcdClear Lcdout " ", tx_dta(8), tx_dta(9), ".", tx_dta(10), 0xdf, "DP" WaitMs 500 Lcdcmdout LcdClear Lcdout tx_dta(1), tx_dta(2), tx_dta(3), ".", tx_dta(4), 0xdf, "C " WaitMs 500 Return 'convert a binbfr value from 0000h to fffFh to 0000 to 65535 decimal, 'name the binbfr word as b2aval and call this subr and the ASCII 'result will be in ascbfr4,3,2,1,0, ready for your LCD or UART 'just pop the DP in the output to the LCD [as paramn above] bin2asc: ascbfr4 = b2aval / 10000 temp3 = b2aval Mod 10000 ascbfr3 = temp3 / 1000 temp3 = b2aval Mod 1000 ascbfr2 = temp3 / 100 temp3 = temp3 Mod 100 ascbfr1 = temp3 / 10 ascbfr0 = temp3 Mod 10 'results are BCD so 'convert To ascii For lcd Or uart ascbfr4 = ascbfr4 Or 0x30 ascbfr3 = ascbfr3 Or 0x30 ascbfr2 = ascbfr2 Or 0x30 ascbfr1 = ascbfr1 Or 0x30 ascbfr0 = ascbfr0 Or 0x30 If ascbfr3 = 0x30 Then ascbfr3 = 0x20 'make a msd '0' a space byte If ascbfr2 = 0x30 And ascbfr3 = 0x20 Then ascbfr2 = 0x20 Return 'RX config config_rx1: INTCON.INTF = 0 INTCON.INTE = 0 'dis intr during config nrf_ce = 0 WaitMs 10 SPICSOn SPISend wr_config '0x20 WaitUs 10 SPISend 0x37 ''b 'CONFIG PRX, CRC2, ints, power up SPICSOff WaitUs 20 SPICSOn SPISend wr_en_rxaddr '0x22 WaitUs 10 SPISend 0x03 'pipe 0,1 SPICSOff WaitUs 20 SPICSOn SPISend wr_setup_aw '0x23 WaitUs 10 SPISend 0x03 'width 5 bytes SPICSOff WaitUs 20 SPICSOn SPISend wr_rf_ch '0x25 WaitUs 10 SPISend 0x02 'rf chan 2 SPICSOff WaitUs 20 SPICSOn SPISend wr_rf_setup '0x26 'rf setup WaitUs 10 SPISend 0x03 '1mb data rate SPICSOff WaitUs 20 SPICSOn SPISend wr_addr_p0 '0x2a WaitUs 10 SPISend 0xe7 'pipe0 addr, using default WaitUs 10 SPISend 0xe7 WaitUs 10 SPISend 0xe7 WaitUs 10 SPISend 0xe7 WaitUs 10 SPISend 0xe7 SPICSOff WaitUs 20 SPICSOn SPISend wr_addr_p1 '0x2b WaitUs 10 SPISend 0xc2 'pipe1 addr WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 SPICSOff WaitUs 20 SPICSOn SPISend rx_pw_p0 '0x31 byte pl WaitUs 10 SPISend dta_len SPICSOff WaitUs 20 SPICSOn SPISend rx_pw_p1 '0x32 WaitUs 10 SPISend dta_len SPICSOff WaitUs 20 SPICSOn SPISend wr_en_aa WaitUs 10 SPISend 0x00 'Disable auto ack SPICSOff WaitUs 20 nrf_ce = 1 INTCON.INTF = 0 INTCON.INTE = 1 Return tx_addr0: nrf_ce = 0 SPICSOn SPISend wr_tx_addr '0x30 WaitUs 10 SPISend 0xe7 'pipe0 WaitUs 10 SPISend 0xe7 WaitUs 10 SPISend 0xe7 WaitUs 10 SPISend 0xe7 WaitUs 10 SPISend 0xe7 SPICSOff WaitUs 20 Return tx_addr1: 'pipe 1 nrf_ce = 0 SPICSOn SPISend wr_tx_addr '0x30 WaitUs 10 SPISend 0xc2 'pipe1 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 SPICSOff WaitUs 20 Return tx_addr2: 'pipe 2 nrf_ce = 0 SPICSOn SPISend wr_tx_addr '0x30 WaitUs 10 SPISend 0xc3 'pipe2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 SPICSOff WaitUs 20 Return tx_addr3: 'pipe 3 nrf_ce = 0 SPICSOn SPISend wr_tx_addr '0x30 WaitUs 10 SPISend 0xc4 'pipe3 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 WaitUs 10 SPISend 0xc2 SPICSOff WaitUs 20 Return transmit1: nrf_ce = 0 SPICSOn SPISend wr_stat '0x27 WaitUs 10 SPISend 0x7e SPICSOff WaitUs 10 SPICSOn SPISend flush_tx '0xe1 WaitUs 10 SPICSOff SPICSOn SPISend wr_tx_pload '0xa0 WaitUs 10 For x = 0 To dta_len SPISend tx_dta(x) WaitUs 10 Next x WaitMs 1 SPICSOff SPICSOn SPISend wr_config '0x20 WaitUs 10 SPISend 0x3a 'pwr up SPICSOff WaitUs 10 'Pulse CE to start transmission nrf_ce = 1 WaitMs 1 nrf_ce = 0 WaitMs 1 Return 'TX config config_tx1: INTCON.INTF = 0 INTCON.INTE = 0 'dis intr during config nrf_ce = 0 WaitMs 10 SPICSOn SPISend wr_config '0x20 WaitUs 10 SPISend 0x36 ''8 'CONFIG as TX, CRC2, No ints, power dwn SPICSOff WaitUs 20 SPICSOn SPISend wr_setup_retr '0x24 WaitUs 10 SPISend 0x00 'auto retry-tx off SPICSOff WaitUs 20 SPICSOn SPISend wr_setup_aw '0x23 WaitUs 10 SPISend 0x03 '5 bytes SPICSOff WaitUs 20 SPICSOn SPISend wr_rf_setup '0x26 WaitUs 10 SPISend 0x03 '1mbps, 0dB [max] SPICSOff WaitUs 20 SPICSOn SPISend wr_rf_ch '0x25 WaitUs 10 SPISend 0x02 'chan2 SPICSOff WaitUs 20 SPICSOn SPISend wr_en_aa '0x21 WaitUs 10 SPISend 0x00 'EN_AA Disable aouto Ack SPICSOff WaitUs 20 INTCON.INTF = 0 INTCON.INTE = 1 Return