'**************************************************************** '* Name : Orb_Rx_v1.pbp * '* Author : Pete Burnight * '* Notice : Copyright (c) 2007 Central Coast Software * '* : To be released under GNU Public License * '* Date : 3/7/07 * '* Version : 1.0 * '* Notes : Receives data from MiRF module & Orb Transmitter * '* : Transfers theCmd / theData to Loco via I2c * '**************************************************************** DEFINE __16F88 1 DEFINE OSC 8 DEFINE I2C_HOLD 1 ' ---------------------------------------------------- ' Connections to MiRF Transceiver pins MiRF_CE VAR PORTA.7 MiRF_CSN VAR PORTA.0 MiRF_SCK VAR PORTA.1 MiRF_MOSI VAR PORTA.3 MiRF_MISO VAR PORTA.2 ' This is an Input MiRF_IRQ VAR PORTA.4 ' This is an Input ' ---------------------------------------------------- ' I2c Pins DataPin VAR PORTB.6 ClockPin var PORTB.7 SDA Var TRISB.6 SCL Var TRISB.7 ' ---------------------------------------------------- Green_LED VAR PORTB.1 Red_LED VAR PORTB.0 ' ---------------------------------------------------- x Var Byte j Var Byte n var byte theDataByte Var Byte theStatusByte Var Byte theCmd Var Byte theData Var Byte pipeNum Var Byte nBytes Var Byte Rx_FIFO_Status Var Byte DataArray VAR Byte[8] Msg_Cmd var Byte Msg_Data Var Word ' ---------------------------------------------------- Init_Chip: OSCCON = %01110000 'Set Internal Osc for 8 Mhz While OSCCON.2 == 0 'Wait for Osc to stabilize WEnd ANSEL = 0 'Set all Analog pins to Digital CMCON = %00000111 'Turn off comparator on RA port PORTA = %00000000 TRISA = %00010100 '0 = Output, 1 = Input PORTB = %00000000 ' Set I2C lines to Inputs so TRISB = %11000000 ' resisters pull them High - Idle Bus MiRF_CE = 0 MiRF_MOSI = 0 MiRF_CSN = 1 Gosub Config_RX For x = 1 to 4 High Red_LED Pause 300 Low Red_LED High Green_LED Pause 300 LOW Green_LED Next x Goto Main_Loop ' ---------------------------------------------------- INCLUDE "MiRF_Inc_v4.inc" ' ---------------------------------------------------- Main_Loop: if (MiRF_IRQ == 0) Then ' poll for Events Gosub Handle_MiRF_IRQ MiRF_CE = 1 ' Enable Rx Mode EndIf Goto Main_Loop End ' ---------------------------------------------------------------------------- ' 2.4G Configuration - Rx Mode ' This sets up the nRF24L01 for shockburst Reception ' ' Description: ' This function initializes one nRF24L01 device to RX Mode, ' set RX address, writes RX payload width, ' select RF channel, datarate & LNA HCURR. ' ' After init, CE is toggled high, which means that this device is now ready to receive a datapacket. ' Config_RX: MiRF_CE = 0 Gosub Setup_MiRF_Address ' --- theCmd = WRITE_REG + SETUP_AW theData = $03 ' Setup RX/TX address width = 5 Gosub SPI_RW_Reg theCmd = WRITE_REG + RX_ADDR_P0 ' --- Setup RX Addresss nBytes = 5 Gosub SPI_Write_Buf ' --- theCmd = WRITE_REG + STATUS theData = $70 ' read status & clear IRQ flag's Gosub SPI_RW_Reg theCmd = WRITE_REG + EN_AA theData = $01 ' Enable Auto Ack Pipe0 Gosub SPI_RW_Reg theCmd = WRITE_REG + RX_PW_P0 theData = $04 ' Pipe0 payload width Gosub SPI_RW_Reg theCmd = WRITE_REG + EN_RXADDR theData = $01 ' Enable pipe0 Gosub SPI_RW_Reg theCmd = WRITE_REG + SETUP_RETR theData = $0A ' 250µs+86µs, 10 retrans... Gosub SPI_RW_Reg theCmd = WRITE_REG + RF_CH theData = 40 ' Channel 40 Gosub SPI_RW_Reg theCmd = WRITE_REG + RF_SETUP theData = $0F ' TX_PWR : 0dBm, DataRate : 2Mbps, LNA : HCURR Gosub SPI_RW_Reg theCmd = WRITE_REG + CONFIG theData = $0F ' PWR_UP, CRC(2bytes) & Prim:RX. Gosub SPI_RW_Reg ' --- MiRF_CE = 1 ' Enable Rx Mode ' This device is now ready to receive one packet of 8 bytes payload from a TX device sending to address ' "3443101001", with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps. RETURN ' ---------------------------------------------------------------------------------------------------------------------------------------- Handle_MiRF_IRQ: For n = 0 to 7 DataArray[n] = 0 Next n theCmd = WRITE_REG + STATUS_REG theData = $70 ' Read Status and clear IRQ flags Gosub SPI_RW_Reg ' Debug "Status Byte: ", dec theStatusByte, 13 ' If (theStatusByte & MAX_RT) Then ' Indicates max #of retransmit interrupt ' Debug "Max Retries Interupt.", 13 ' EndIf ' If (theStatusByte & TX_DS) Then ' Indicates TX data succsessfully sent ' Debug "Data Sent OK interupt flag.", 13 ' EndIf If (theStatusByte & RX_DR) Then ' in Rx Mode - check data Received Read_Pipe: pipeNum = ($07 & (theStatusByte >> 1)) ' Debug "Data Rcv IRQ - PipeNum: ", Dec pipeNum, 13 If pipeNum > 5 Then FIFO_Empty theCmd = READ_REG + Rx_PW_P0 + pipeNum Gosub SPI_Read nBytes = theDataByte ' Debug "nBytes in Packet: ", Dec nBytes, 13 theCmd = RD_RX_PLOAD Gosub SPI_Read_Buf Msg_Cmd = DataArray[0] Msg_Data.HighByte = DataArray[1] Msg_Data.LowByte = DataArray[2] ' Debug " theCmd: ", Dec Msg_Cmd, 13 ' Debug " theData: ", SDec Msg_Data, 13 I2CWRITE DataPin, ClockPin, $08, [Msg_Cmd, Msg_Data] If Msg_Cmd == 1 Then for n = 1 to 4 High RED_LED Pause 20 Low RED_LED next n EndIf If Msg_Cmd == 2 Then for n = 1 to 4 High Green_LED Pause 20 Low Green_LED next n EndIf Check_FIFO_Status: theCmd = READ_REG + FIFO_STATUS Gosub SPI_Read Rx_FIFO_Status = theDataByte ' Debug " FIFO Status: $", HEX Rx_FIFO_Status If Rx_FIFO_Status.Bit0 Then ' Debug " Rx FIFO Empty.", 13 Goto FIFO_Empty Else ' Debug " Data In Rx FIFO.", 13 Goto Read_Pipe EndIf FIFO_Empty: For n = 0 to 7 DataArray[n] = 0 Next n EndIf RETURN ' ---------------------------------------------------------------------------- ' End of File