/****************************************************************************** * Function: void UserInit(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This routine should take care of all of the demo code * initialization that is required. * * Note: * *****************************************************************************/ void UserInit(void) { // Real time clock start RtccInitClock(); RtccWrOn(); { rtccTimeDate initd; initd.f.year=0x10; initd.f.mon=0x01; initd.f.mday=0x01; initd.f.wday=5; // 2010.01.01 is friday initd.f.hour=0; initd.f.min=0; initd.f.sec=0; RtccWriteTimeDate(&initd,FALSE); RtccWriteAlrmTimeDate(&initd); } mRtccOn(); mRtccAlrmEnable(); RtccSetAlarmRpt(RTCC_RPT_MIN,TRUE); // Mtouch init mTouchInit(); mTouchCalibrate(); cmdstr[0]=0; screen=0; screenvalid=0; position=0; buttonstate.Val=0; buttonpressed.Val=0; devicereset=0; clockss=0; resetcounter=0; alarmcnt=0; TRISBbits.TRISB1=0; PPSUnLock(); iPPSOutput(OUT_PIN_PPS_RP4,OUT_FN_PPS_CCP1P1A); //Configre RP24 as C1OUT pin PPSLock(); //----Configure pwm ---- period = 0xFF; OpenPWM1( period); //Configure PWM module and initialize PWM period //-----set duty cycle---- duty_cycle = 256; SetDCPWM1(duty_cycle); //set the duty cycle //----set pwm output---- outputconfig = HALF_OUT ; outputmode = PWM_MODE_1; SetOutputPWM1( outputconfig, outputmode); //output PWM in respective modes ADCON0bits.CHS=4; /* Make sure A/D interrupt is not set */ PIR1bits.ADIF = 0; /* Begin A/D conversion */ }//end UserInit
void remapAllPins() { PPSUnLock(); /* pins used by mimac to comunicate radio */ iPPSInput(IN_FN_PPS_INT1, IN_PIN_PPS_RP17); /* pins used by usart loging */ iPPSOutput(OUT_PIN_PPS_RP21, OUT_FN_PPS_TX2CK2); // Mapping USART2 TX iPPSInput(IN_FN_PPS_RX2DT2, IN_PIN_PPS_RP22); // Mapping USART2 RX iPPSInput(IN_FN_PPS_INT2, IN_PIN_PPS_RP4); iPPSOutput(OUT_PIN_PPS_RP23, OUT_FN_PPS_CCP1P1A); // LEDs mapped to PWM iPPSOutput(OUT_PIN_PPS_RP24, OUT_FN_PPS_P1B); // Lock System PPSLock(); }
int main(void) #endif { BYTE i, j; BYTE TxSynCount = 0; BOOL bReceivedMessage = FALSE; _U16 m; #define BAUDRG 77 /*******************************************************************/ // Initialize the system /*******************************************************************/ ANCON0 = 0XFF; /*desactiva entradas analogicas*/ ANCON1 = 0XFF; /*desactiva entradas analogicas*/ PPSUnLock(); PPSOutput(PPS_RP10, PPS_TX2CK2); // TX2 RP17/RC6 PPSInput(PPS_RX2DT2, PPS_RP9); // RX2 RP18/RC7 PPSOutput(PPS_RP23, PPS_SDO2); // SDO2 RP23/RD6 PPSInput(PPS_SDI2, PPS_RP24); // SDI2 RP24/RD7 PPSOutput(PPS_RP22, PPS_SCK2); // SCK2 RP22/RD5 PPSLock(); System_PeripheralPinSelect( ExternalInterrupt3, 19); /*external interrupt 3 B3*/ BoardInit(); ConsoleInit(); Open2USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, BAUDRG); baud2USART(BAUD_IDLE_TX_PIN_STATE_HIGH & BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_OFF); Gpios_PinDirection(GPIOS_PORTD, 7, GPIOS_INPUT); /*pin C0 como salida para SDI*/ Gpios_PinDirection(GPIOS_PORTD, 6, GPIOS_OUTPUT); /*pin C1 como salida para SDO*/ Gpios_PinDirection(GPIOS_PORTD, 5, GPIOS_OUTPUT); /*pin C2 como salida para SCK*/ Spi_Init(SPI_PORT1, SPI_64DIV); /*Inicializamos SPI2*/ Spi_Init(SPI_PORT2, SPI_64DIV); /*Inicializamos SPI2*/ //Spi_SetMode(SPI_PORT1, 1); //Spi_SetMode(SPI_PORT1, 1); LED_1 = 1; LED_2 = 1; Read_MAC_Address(); LED_1 = 0; LED_2 = 0; ConsolePutROMString((ROM char *)"\r\n<MAC Addr:"); PrintChar(myLongAddress[3]); PrintChar(myLongAddress[2]); PrintChar(myLongAddress[1]); PrintChar(myLongAddress[0]); ConsolePutROMString((ROM char *)"\r>"); Printf("\r\nStarting Testing Interface for MiWi(TM) PRO Stack ..."); #if defined(MRF24J40) Printf("\r\n RF Transceiver: MRF24J40"); #elif defined(MRF49XA) Printf("\r\n RF Transceiver: MRF49XA"); #elif defined(MRF89XA) Printf("\r\n RF Transceiver: MRF89XA"); #endif Printf("\r\n Demo Instruction:"); Printf("\r\n Press Enter to bring up the menu."); Printf("\r\n Type in hyper terminal to choose"); Printf("\r\n menu item. "); Printf("\r\n\r\n"); /*******************************************************************/ // Following block display demo information on LCD of Explore 16 or // PIC18 Explorer demo board. /*******************************************************************/ #if defined(MRF49XA) LCDDisplay((char *)"MiWi PRO Test Interface MRF49XA", 0, TRUE); #elif defined(MRF24J40) LCDDisplay((char *)"MiWi PRO Test Interface MRF24J40", 0, TRUE); #elif defined(MRF89XA) LCDDisplay((char *)"MiWi PRO Test Interface MRF89XA", 0, TRUE); #endif //if( (PUSH_BUTTON_1 == 0) || ( MiApp_ProtocolInit(TRUE) == FALSE ) ) if (PUSH_BUTTON_1 == 1) { MiApp_ProtocolInit(FALSE); LED_1 = 0; LED_2 = 0; #ifdef ENABLE_ACTIVE_SCAN myChannel = 0xFF; ConsolePutROMString((ROM char *)"\r\nStarting Active Scan..."); LCDDisplay((char *)"Active Scanning", 0, FALSE); /*******************************************************************/ // Function MiApp_SearchConnection will return the number of // existing connections in all channels. It will help to decide // which channel to operate on and which connection to add. // The return value is the number of connections. The connection // data are stored in global variable ActiveScanResults. // Maximum active scan result is defined as // ACTIVE_SCAN_RESULT_SIZE // The first parameter is the scan duration, which has the same // definition in Energy Scan. 10 is roughly 1 second. 9 is a // half second and 11 is 2 seconds. Maximum scan duration is 14, // or roughly 16 seconds. // The second parameter is the channel map. Bit 0 of the // double word parameter represents channel 0. For the 2.4GHz // frequency band, all possible channels are channel 11 to // channel 26. As the result, the bit map is 0x07FFF800. Stack // will filter out all invalid channels, so the application // only needs to pay attention to the channels that are not // preferred. /*******************************************************************/ i = MiApp_SearchConnection(10, 0x02000000); if( i > 0 ) { // now print out the scan result. Printf("\r\nActive Scan Results: \r\n"); for(j = 0; j < i; j++) { Printf("Channel: "); PrintDec(ActiveScanResults[j].Channel ); Printf(" RSSI: "); PrintChar(ActiveScanResults[j].RSSIValue); Printf("\r\n"); myChannel = ActiveScanResults[j].Channel; Printf("PeerInfo: "); PrintChar( ActiveScanResults[j].PeerInfo[0]); } } #endif /*******************************************************************/ // Function MiApp_ConnectionMode sets the connection mode for the // protocol stack. Possible connection modes are: // - ENABLE_ALL_CONN accept all connection request // - ENABLE_PREV_CONN accept only known device to connect // - ENABL_ACTIVE_SCAN_RSP do not accept connection request, but // allow response to active scan // - DISABLE_ALL_CONN disable all connection request, including // active scan request /*******************************************************************/ MiApp_ConnectionMode(ENABLE_ALL_CONN); if( i > 0 ) { /*******************************************************************/ // Function MiApp_EstablishConnection try to establish a new // connection with peer device. // The first parameter is the index to the active scan result, which // is acquired by discovery process (active scan). If the value // of the index is 0xFF, try to establish a connection with any // peer. // The second parameter is the mode to establish connection, either // direct or indirect. Direct mode means connection within the // radio range; Indirect mode means connection may or may not // in the radio range. /*******************************************************************/ if( MiApp_EstablishConnection(0, CONN_MODE_DIRECT) == 0xFF ) { Printf("\r\nJoin Fail"); } } else { /*******************************************************************/ // Function MiApp_StartConnection tries to start a new network // // The first parameter is the mode of start connection. There are // two valid connection modes: // - START_CONN_DIRECT start the connection on current // channel // - START_CONN_ENERGY_SCN perform an energy scan first, // before starting the connection on // the channel with least noise // - START_CONN_CS_SCN perform a carrier sense scan // first, before starting the // connection on the channel with // least carrier sense noise. Not // supported on currrent radios // // The second parameter is the scan duration, which has the same // definition in Energy Scan. 10 is roughly 1 second. 9 is a // half second and 11 is 2 seconds. Maximum scan duration is // 14, or roughly 16 seconds. // // The third parameter is the channel map. Bit 0 of the // double word parameter represents channel 0. For the 2.4GHz // frequency band, all possible channels are channel 11 to // channel 26. As the result, the bit map is 0x07FFF800. Stack // will filter out all invalid channels, so the application // only needs to pay attention to the channels that are not // preferred. /*******************************************************************/ #ifdef ENABLE_ED_SCAN //LCDDisplay((char *)"Active Scanning Energy Scanning", 0, FALSE); ConsolePutROMString((ROM char *)"\r\nActive Scanning Energy Scanning"); MiApp_StartConnection(START_CONN_ENERGY_SCN, 10, 0x02000000); #endif } // Turn on LED 1 to indicate ready to accept new connections LED_1 = 1; } else { //LCDDisplay((char *)" Network Freezer ENABLED", 0, TRUE); Printf("\r\nNetwork Freezer Feature is enabled. There will be no hand-shake process.\r\n"); LED_1 = 1; DumpConnection(0xFF); } LCDDisplay((char *)"Start Connection on Channel %d", currentChannel, TRUE); LCDDisplay((char *)"Testing Menu on Hyper Terminal", 0, FALSE); while(1) { /*******************************************************************/ // Function MiApp_MessageAvailable will return a boolean to indicate // if a message for application layer has been received by the // transceiver. If a message has been received, all information will // be stored in the rxMessage, structure of RECEIVED_MESSAGE. /*******************************************************************/ if( MiApp_MessageAvailable() ) { /*******************************************************************/ // If a packet has been received, following code prints out some of // the information available in rxFrame. /*******************************************************************/ if( rxMessage.flags.bits.secEn ) { ConsolePutROMString((ROM char *)"Secured "); } if( rxMessage.flags.bits.broadcast ) { ConsolePutROMString((ROM char *)"Broadcast Packet with RSSI "); } else { ConsolePutROMString((ROM char *)"Unicast Packet with RSSI "); } PrintChar(rxMessage.PacketRSSI); if( rxMessage.flags.bits.srcPrsnt ) { ConsolePutROMString((ROM char *)" from "); if( rxMessage.flags.bits.altSrcAddr ) { PrintChar(rxMessage.SourceAddress[1]); PrintChar(rxMessage.SourceAddress[0]); } else { for(i = 0; i < MY_ADDRESS_LENGTH; i++) { PrintChar(rxMessage.SourceAddress[MY_ADDRESS_LENGTH-1-i]); } } } ConsolePutROMString((ROM char *)": "); for(i = 0; i < rxMessage.PayloadSize; i++) { ConsolePut(rxMessage.Payload[i]); } // Toggle LED2 to indicate receiving a packet. LED_2 ^= 1; /*******************************************************************/ // Function MiApp_DiscardMessage is used to release the current // received message. After calling this function, the stack can // start to process the next received message. /*******************************************************************/ MiApp_DiscardMessage(); bReceivedMessage = TRUE; /*******************************************************************/ // Following block update the total received and transmitted messages // on the LCD of the demo board. /*******************************************************************/ LCDTRXCount(TxNum, ++RxNum); } else { ++m; if(m > 8000) { m=0; //LED_1 ^= 1; MiApp_FlushTx(); MiApp_WriteData('H'); MiApp_WriteData('o'); MiApp_WriteData('l'); MiApp_WriteData('a'); MiApp_WriteData('a'); MiApp_WriteData('a'); MiApp_WriteData(0x0D); MiApp_WriteData(0x0A); MiApp_BroadcastPacket(FALSE); } if ( ConsoleIsGetReady() ) { //ProcessMenu(); } } } }
void main(void) { #define BAUDRG 77 BYTE SecNum = 0; BOOL Tx_Success = FALSE; BYTE Tx_Trials = 0, scanresult = 0; /*******************************************************************/ // Initialize the system /*******************************************************************/ ANCON0 = 0XFF; /*desactiva entradas analogicas*/ ANCON1 = 0XFF; /*desactiva entradas analogicas*/ PPSUnLock(); PPSOutput(PPS_RP10, PPS_TX2CK2); // TX2 RP17/RC6 icsp PPSInput(PPS_RX2DT2, PPS_RP9); // RX2 RP18/RC7 PPSOutput(PPS_RP23, PPS_SDO2); // SDO2 RP23/RD6 PPSInput(PPS_SDI2, PPS_RP24); // SDI2 RP24/RD7 PPSOutput(PPS_RP22, PPS_SCK2); // SCK2 RP22/RD5 PPSLock(); System_PeripheralPinSelect( ExternalInterrupt3, 19); /*external interrupt 3 B3*/ BoardInit(); ConsoleInit(); Gpios_PinDirection(GPIOS_PORTC, 7, GPIOS_INPUT); /*pin C0 como salida para SDI*/ Gpios_PinDirection(GPIOS_PORTC, 6, GPIOS_OUTPUT); /*pin C1 como salida para SDO*/ //Gpios_PinDirection(GPIOS_PORTD, 4, GPIOS_OUTPUT); /*pin D4 como salida */ Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, BAUDRG); baud1USART(BAUD_IDLE_TX_PIN_STATE_HIGH & BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_OFF); Open2USART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_EIGHT_BIT & USART_ASYNCH_MODE & USART_ADDEN_OFF, BAUDRG); baud2USART(BAUD_IDLE_TX_PIN_STATE_HIGH & BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF & BAUD_16_BIT_RATE & USART_RX_INT_OFF); OpenTimer1( TIMER_INT_OFF &T1_16BIT_RW &T1_SOURCE_FOSC_4 & T1_PS_1_8 &T1_OSC1EN_OFF &T1_SYNC_EXT_OFF, TIMER_GATE_OFF & TIMER_GATE_INT_OFF); Gpios_PinDirection(GPIOS_PORTD, 7, GPIOS_INPUT); /*pin C0 como salida para SDI*/ Gpios_PinDirection(GPIOS_PORTD, 6, GPIOS_OUTPUT); /*pin C1 como salida para SDO*/ Gpios_PinDirection(GPIOS_PORTD, 5, GPIOS_OUTPUT); /*pin C2 como salida para SCK*/ Spi_Init(SPI_PORT1, SPI_64DIV); /*Inicializamos SPI2*/ Spi_Init(SPI_PORT2, SPI_64DIV); /*Inicializamos SPI2*/ //Adc_Init(ADC_10BITS); LED_1 = 1; LED_2 = 0; //RLY_1 = 0; RLY_2 = 0; ON_RADIO = 1; ON_MAC = 1; ON_TEMP = 1; StartWirelessConnection(); //myDevicesRequiredStatus[0] = 0x55; EEPROMRead(&myDevicesRequiredStatus, 0, 1); if(myDevicesRequiredStatus[0] == 0x55) { RLY_1 = 1; EEPROMCHG = 1; ConsolePutROMString((ROM char *)"RELAY ON "); } if(myDevicesRequiredStatus[0] == 0xAA) { RLY_1 = 0; EEPROMCHG = 0; ConsolePutROMString((ROM char *)"RELAY OFF "); } for(j=0;j<10;j++) { DelayMs(50); LED_1 ^= 1; LED_2 ^= 1; } LED_1 = 0; LED_2 = 0; //RLY_1 = 0; RLY_2 = 0; TickScaler = 4; EndDevStateMachine =0; /* while(!Tx_Success) { if(myChannel < 8) scanresult = MiApp_SearchConnection(10, (0x00000001 << myChannel)); else if(myChannel < 16) scanresult = MiApp_SearchConnection(10, (0x00000100 << (myChannel-8))); else if(myChannel < 24) scanresult = MiApp_SearchConnection(10, (0x00010000 << (myChannel-16))); else scanresult = MiApp_SearchConnection(10, (0x01000000 << (myChannel-24))); if(scanresult == 0) { Tx_Trials++; if(Tx_Trials > 2) break; } else Tx_Success = TRUE; } if(Tx_Success) { ConsolePutROMString((ROM char *)"RADIO OK "); } else { ConsolePutROMString((ROM char *)"RADIO FAIL "); } */ //.VBGOE = 0; //ANCON1bits.VBGEN = 1; // Enable Band gap reference voltage //DelayMs(10); //VBGResult = Adc_u16Read(15); //ANCON1bits.VBGEN = 0; // Disable Bandgap //Adc_Init(ADC_10BITS); ANCON0 = 0xFF; ANCON1 = 0x9F; ANCON1bits.VBGEN = 1; // Enable Band gap reference voltage ADCON0bits.VCFG = 0; // vreff VDD-VSS ADCON0bits.CHS = 0x0F; // VBG channel select ADCON1 = 0xBE; ADCON0bits.ADON = 1; //for(j=0;j<16;j++) //{ //myDevicesOutputStatus[j] = j; //} //EEPROMWRITE(myDevicesOutputStatus,0,16); //for(j=0;j<16;j++) //{ //myDevicesOutputStatus[j] = 0; //} //DelayMs(500); EEPROMRead(&myDevicesOutputStatus, 0, 16); ConsolePutROMString((ROM char *)"EEPROM READ: "); //PrintChar(TemperatureCalibrationValue); for(j=0;j<1;j++) { PrintChar(myDevicesOutputStatus[j]); } SwTimer3 = 0; SwTimer4 = 0; TRISB&=0xEF; //JL: Configuro el pin B4 como salida si modificar el estado de los demas pines while(1) { /* WirelessTxRx(); WirelesStatus(); Bypass(); */ //No se utilizaron //Menu(); //Timer1Tick(); //WirelessTxRxPANCOORD(); //TaskScheduler(); //JLEstas funciones deben habilitarse para trabajar como repetidora Timer1Tick(); Repeater(); } }