void InitApp(void) { /* Initialize peripherals */ // Configure UART modules UARTConfigure(UART_CMD_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_CMD_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_CMD_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_CMD_MODULE_ID, GetPeripheralClock(), DESIRED_CMD_BAUDRATE); UARTEnable(UART_CMD_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); UARTConfigure(UART_WIFI_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_WIFI_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_WIFI_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_WIFI_MODULE_ID, GetPeripheralClock(), DESIRED_WIFI_BAUDRATE); UARTEnable(UART_WIFI_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART Interrupts INTEnable(INT_SOURCE_UART_RX(UART_CMD_MODULE_ID), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_CMD_MODULE_ID), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART_CMD_MODULE_ID), INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_SOURCE_UART_RX(UART_WIFI_MODULE_ID), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_WIFI_MODULE_ID), INT_PRIORITY_LEVEL_1); INTSetVectorSubPriority(INT_VECTOR_UART(UART_WIFI_MODULE_ID), INT_SUB_PRIORITY_LEVEL_0); // Make the requests to Wifi service - they will be picked up when the service needs them ConnectToAccessPoint(&routerConnection, &DefaultWifiService); SendHttpRequest(&DnsDynamicHttpRequest, &DefaultWifiService); }
void MyCyclone_Init(void) { // Configure Reset Pin = GPIO_2[10] = RD7 mPORTDClearBits(RST_FPGA); mPORTDSetPinsDigitalOut(RST_FPGA); // Do a Reset mPORTDSetBits(RST_FPGA); mPORTDClearBits(RST_FPGA); // Do Interrupts Initialization // Set RD8/INT1 and RD9/INT2 as inputs mPORTDSetPinsDigitalIn(BIT_8 | BIT_9); // Clear corresponding bits in INTCON for falling edge trigger INTCONCLR = _INTCON_INT1EP_MASK | _INTCON_INT2EP_MASK; // Set up interrupt prioirty and sub-priority INTSetVectorPriority(INT_EXTERNAL_1_VECTOR, My_INT_EXTERNAL_1_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_1_VECTOR, My_INT_EXTERNAL_1_SUB_PRIORITY); INTSetVectorPriority(INT_EXTERNAL_2_VECTOR, My_INT_EXTERNAL_2_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_2_VECTOR, My_INT_EXTERNAL_2_SUB_PRIORITY); // Clear the interrupt flags INTClearFlag(INT_INT1); INTClearFlag(INT_INT2); // Enable INT1 & INT2 INTEnable(INT_INT1, INT_ENABLED); INTEnable(INT_INT2, INT_ENABLED); // Enable KEY0 and KEY1 interrupts and IOs of the MyExpansionBoard_IO_v2 MyCyclone_Write(CYCLONE_CONFIG,CYCLONE_ENABLE_INT_KEY0 | CYCLONE_ENABLE_INT_KEY1 | CYCLONE_ENABLE_IO_AB | CYCLONE_ENABLE_IO_CD); }
BOOL ConfigSPIComms(void) { SpiChnClose(RPI_SPI_CHANNEL); /* do I need to configure this? */ INTEnable(INT_SOURCE_SPI_RX(RPI_SPI_CHANNEL),INT_DISABLED); INTEnable(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL),INT_DISABLED); INTEnable(INT_SOURCE_SPI_ERROR(RPI_SPI_CHANNEL),INT_DISABLED); INTEnable(INT_SOURCE_SPI(RPI_SPI_CHANNEL),INT_DISABLED); SPI_DATA_IN_DIRECTION = TRIS_IN; SPI_DATA_OUT_DIRECTION = TRIS_OUT; SPI_CLOCK_IN_DIRECTION = TRIS_IN; SPI_SELECT_IN_DIRECTION = TRIS_IN; SPI.RXCount=0; SPI.TXCount=0; SPI.address=0; SPI.command=TRISTHIS_SPI_NO_COMMAND; SpiChnOpen(RPI_SPI_CHANNEL, SPI_OPEN_SLVEN|SPI_OPEN_CKE_REV|SPI_OPEN_MODE8|SPI_OPEN_SSEN, 0); //TODO: Not acting consistently? RPI needs to send -b 8 -H parameters to spidev /* configure interrupts */ INTSetVectorPriority(INT_VECTOR_SPI(RPI_SPI_CHANNEL), INT_PRIORITY_LEVEL_3); INTSetVectorSubPriority(INT_VECTOR_SPI(RPI_SPI_CHANNEL), INT_SUB_PRIORITY_LEVEL_1); INTClearFlag(INT_SOURCE_SPI_RX(RPI_SPI_CHANNEL)); INTEnable(INT_SOURCE_SPI_RX(RPI_SPI_CHANNEL),INT_ENABLED); INTClearFlag(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL)); INTClearFlag(INT_SOURCE_SPI_ERROR(RPI_SPI_CHANNEL)); INTClearFlag(INT_SOURCE_SPI(RPI_SPI_CHANNEL)); //INTEnable(INT_SOURCE_SPI(RPI_SPI_CHANNEL),INT_ENABLED); /* configure change notice, as I can't figure out any other way to */ /* trigger the beginning of the slave select with just the SPI peripheral */ /* buuut the change notice pins are not on the SS pins, so a white wire is*/ /* needed */ /* tie chip enable CE0 to pin20/RE5 CE1 */ SPI_SELECT_CN_DIRECTION=TRIS_IN; CNCONbits.w=0; CNCONSET=_CNCON_ON_MASK; CNENbits.w=0; CNENSET=_CNEN_CNEN7_MASK; CNTemp=CE_PORT; /* read for change notice */ RPI_SPI_RX_OVERFLOW_CLEAR; SPI1CONbits.STXISEL=0b01; SPI.status.w=0; INTClearFlag(INT_CN); INTSetVectorPriority(INT_CHANGE_NOTICE_VECTOR, INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_CHANGE_NOTICE_VECTOR, INT_SUB_PRIORITY_LEVEL_1); return TRUE; }
void configureRTC() { RtccInit(); while (RtccGetClkStat() != RTCC_CLK_ON); //printf("RTCC OK\r\n"); // set time and date // time is MSb: hour, min, sec, rsvd. date is MSb: year, mon, mday, wday. // please note that the rsvd field has to be 0 in the time field! // Time is in Hex! RtccOpen(0x17040000, 0x08100604, 0); // set the RTCC priority in the INT controller INTSetVectorPriority(INT_RTCC_VECTOR, INT_PRIORITY_LEVEL_5); // set the RTCC sub-priority in the INT controller INTSetVectorSubPriority(INT_RTCC_VECTOR, INT_SUB_PRIORITY_LEVEL_1); // enable the RTCC event interrupts in the INT controller. INTEnable(INT_RTCC, INT_ENABLED); // enable alaram or endless alaram - // it allows the alarm rpt to move back to the beginning //RtccChimeDisable(); - this is now changeable // how much repats - 0 means run the alarm only once //RtccSetAlarmRptCount(0); //the above means once we have alarm interrupt the alarm will be disabled // used for marching (i.e. window) when cpmparing RtccSetAlarmRpt(RTCC_RPT_MON); }
void WF_EintInit(void) { /* disable the external interrupt */ INTEnable(INT_INT1, INT_DISABLED); // disable interrupt #if !defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) SYS_INT_DynamicRegister(MRFWB0M_INT_SOURCE, WF_NoEventISR, 0); // #else the MRF ISR is already hooked by MRF24W_MACEventInit()! #endif /* configure IO pin as input and External Interrupt pin*/ /* set the I/O high since we do not have pull-ups */ INT1Rbits.INT1R = 0x0d; //select INT1 pin = RPE8 ANSELEbits.ANSE8 = 0; /* configure IO pin as input and External Interrupt pin*/ WF_INT_IO = 1; // PORTEbits.RE8 = 1; /* configure IO pin as input and External Interrupt pin*/ WF_INT_TRIS = 1; //TRISEbits.TRISE8 = 1 ; // ; /* set the I/O high since we do not have pull-ups */ WF_INT_EDGE = 0; //INTCONbits.INT1EP = 0; /* falling edge triggered */ /* clear and enable the interrupt */ INTClearFlag(INT_INT1) ; // clear status INTSetVectorPriority(INT_EXTERNAL_1_VECTOR, 5); //set security level to 5 INTSetVectorSubPriority(INT_EXTERNAL_1_VECTOR,1); //set sub_security level to 1 // INTEnable(INT_INT1, INT_ENABLED); // Should not enable interrupt here //--- }
/** * @Function AD_Init * @param None * @return SUCCESS or ERROR * @brief Initializes the A/D subsystem and enable battery voltage monitoring. * @author Max Dunne, 2013.08.10 */ char AD_Init(void) { if (ADActive) { return ERROR; } int pin = 0; //ensure that the battery monitor is active ActivePins = BAT_VOLTAGE_MONITOR; ADActive = TRUE; AD_SetPins(); for (pin = 0; pin < NUM_AD_PINS; pin++) { ADValues[pin] = -1; } INTEnable(INT_AD1, INT_DISABLED); INTClearFlag(INT_AD1); INTSetVectorPriority(INT_ADC_VECTOR, 1); INTSetVectorSubPriority(INT_ADC_VECTOR, 3); INTEnable(INT_AD1, INT_ENABLED); EnableADC10(); ADNewData = FALSE; //wait for first reading to ensure battery monitor starts in the right spot while (!AD_IsNewDataReady()) { #ifdef AD_DEBUG_VERBOSE PutChar('.'); #endif } //set the first values for the battery monitor filter Filt_BatVoltage = AD_ReadADPin(BAT_VOLTAGE_MONITOR); CurFilt_BatVoltage = Filt_BatVoltage; PrevFilt_BatVoltage = Filt_BatVoltage; return SUCCESS; }
void serial_init(void) { // Initialisation du fifo de réception InitFifo ( &descrFifoRX, FIFO_RX_SIZE, fifoRX, 0 ); // Initialisation du fifo d'émission InitFifo ( &descrFifoTX, FIFO_TX_SIZE, fifoTX, 0 ); // Utilisation des fonctions séparées (XC32) // ========================================= UARTConfigure(UART2, UART_ENABLE_HIGH_SPEED | UART_ENABLE_PINS_TX_RX_ONLY ); // UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_BUFFER_EMPTY | UART_INTERRUPT_ON_RX_HALF_FULL ); // Remarque HALF_FULL ne fonctionne pas // Pour INT RX au 3/4 UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_BUFFER_EMPTY | UART_INTERRUPT_ON_RX_3_QUARTER_FULL ); // Pour INT RX dés que min 1 char // UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_BUFFER_EMPTY | UART_INTERRUPT_ON_RX_NOT_EMPTY ); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UINT32 ActualBaudRate = UARTSetDataRate(UART2, PB_FREQ, 9600); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configuration Int UART2 avec les fonctions séparées // =================================================== // Configure UART RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_5); INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0); } // InitComm
void MyMIWI_Init(void) { // Configure Pins for MRF24J40MB mPORTESetBits(RST_MIWI); mPORTESetPinsDigitalOut(RST_MIWI); mPORTBSetBits(MIWI_WAKE); mPORTBSetPinsDigitalOut(MIWI_WAKE); // Configure the INT3 controller for MIWI // Set RD10/INT3 as input mPORTDSetPinsDigitalIn(BIT_10); // Clear corresponding bits in INTCON for falling edge trigger INTCONCLR = _INTCON_INT3EP_MASK; // Set up interrupt prioirty and sub-priority INTSetVectorPriority(INT_EXTERNAL_3_VECTOR, My_INT_EXTERNAL_3_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_3_VECTOR, My_INT_EXTERNAL_3_SUB_PRIORITY); // Clear the interrupt flags INTClearFlag(INT_INT3); // Enable INT3 INTEnable(INT_INT3, INT_ENABLED); // WARNING : Change in file MRF24J40.c in Microchip Application Library // the line : void __ISR(_EXTERNAL_1_VECTOR, ipl4) _INT1Interrupt(void) // by : void __ISR(_EXTERNAL_3_VECTOR, ipl4) _INT3Interrupt(void) }
//****************************************************************************** //Public Function Definitions //****************************************************************************** void FIFOUART1_initialize() { UARTConfigure(UART1, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | //Sets the data transfer size to 8-bits per frame.� UART_PARITY_NONE | //Disables parity bit generation.� UART_STOP_BITS_1); //1 stop bit per frame (default).� UARTSetDataRate(UART1, GetPeripheralClock(), FIFOUART1_BAUD_RATE); //Interrupt Stuff INTSetVectorPriority(INT_UART_1_VECTOR, INT_PRIORITY_LEVEL_5); INTSetVectorSubPriority(INT_UART_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTClearFlag(INT_U1RX); INTClearFlag(INT_U1TX); //configure what triggers UART1 itnerrupts UARTSetFifoMode(UART1, UART_INTERRUPT_ON_TX_BUFFER_EMPTY | //TX interrupt will occur when the TX buffer is empty.� UART_INTERRUPT_ON_RX_NOT_EMPTY); //RX interrupt will occur whenever the RX buffer has any data.� //Enable UART1 Rx Interrupt INTEnable(INT_U1RX, INT_ENABLED); //Enable UART1 Tx Interrupt //INTEnable(INT_U1TX, INT_ENABLED); UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); }
//****************************************************************************** //Public Function Definitions //****************************************************************************** void Orientation_start() { //Setup Timer5 INTClearFlag(INT_T5); INTSetVectorPriority(INT_TIMER_5_VECTOR, INT_PRIORITY_LEVEL_3); INTSetVectorSubPriority(INT_TIMER_5_VECTOR, INT_SUB_PRIORITY_LEVEL_1); INTEnable(INT_T5, INT_ENABLED); //Turn on clock OpenTimer5(T5_ON | T5_SOURCE_INT | T1_PS_1_64, 12500);//50hz @ 40MHz //OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_32, 3333); //375hz @ 40MHz (0.0026664 sec) }
void CommunicationLoop_initialize() { FIFOUART1_initialize(); //Setup Timer4 INTClearFlag(INT_T4); INTSetVectorPriority(INT_TIMER_4_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_TIMER_4_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T4, INT_ENABLED); //Turn on clock OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_64, 0x4FFF); }
/**************************************************************************** * Function: TCPIPEventInit * * PreCondition: None. * * Input: intPri - interrupt priority to use * intSubPri - interrupt sub-priority to use * * Output: None * * Side Effects: None * * Overview: This function initializes the ethernet event notification. * * Note: None ******************************************************************************/ void TCPIPEventInit(int intPri, int intSubPri) { INTEnable(INT_ETHERNET, INT_DISABLED); // stop Eth ints INTClearFlag(INT_ETHERNET); INTSetVectorPriority(INT_ETH_VECTOR, (INT_PRIORITY_LEVEL_1-1)+intPri); INTSetVectorSubPriority(INT_ETH_VECTOR, INT_SUB_PRIORITY_LEVEL_0+intSubPri); _TcpNotifyFnc=0; _TcpEnabledEvents=_TcpPendingEvents=0; }
static ALWAYSINLINE MUST_CHECK s32 nu__Can__init(struct nu__Can *c, u32 bus_speed_hz, CAN_BIT_CONFIG *timings, CAN_MODULE_EVENT interrupt_events, INT_PRIORITY int_priority, CAN_MODULE_FEATURES features) { s32 err; CANEnableModule(c->module, TRUE); if ((err = go_config_mode(c)) <0) { go_normal_mode(c); return err; } CANSetSpeed(c->module, timings, NU_HZ, bus_speed_hz); CANAssignMemoryBuffer(c->module, c->buf, sizeof(c->buf)); if (interrupt_events) { INT_VECTOR int_vec; INT_SOURCE int_src; CANEnableModuleEvent(CAN1, interrupt_events, TRUE); switch (c->module) { case CAN1: int_vec = INT_CAN_1_VECTOR; int_src = INT_CAN1; break; case CAN2: int_vec = INT_CAN_2_VECTOR; int_src = INT_CAN2; break; case CAN_NUMBER_OF_MODULES: default: break; } INTSetVectorPriority(int_vec, int_priority); INTSetVectorSubPriority(int_vec, INT_SUB_PRIORITY_LEVEL_0); INTEnable(int_src, INT_ENABLED); } enable_features(c, features); if ((err = go_normal_mode(c)) < 0) return err; return 0; }
void setup(bool use_interrupt = false, INT_PRIORITY int_priority = INT_PRIORITY_DISABLED, UART_CONFIGURATION uart_config = (UART_CONFIGURATION)UART_ENABLE_PINS_TX_RX_ONLY, UART_FIFO_MODE interrupt_modes = (UART_FIFO_MODE)0, UART_LINE_CONTROL_MODE line_control_modes = (UART_LINE_CONTROL_MODE)(UART_DATA_SIZE_8_BITS|UART_PARITY_NONE|UART_STOP_BITS_1), UART_ENABLE_MODE enable_modes = (UART_ENABLE_MODE)(UART_PERIPHERAL|UART_RX|UART_TX)) { UARTConfigure(module, uart_config); UARTSetFifoMode(module, interrupt_modes); UARTSetLineControl(module, line_control_modes); UARTSetDataRate(module, param::pbus_hz(), baud); UARTEnable(module, (UART_ENABLE_MODE) UART_ENABLE_FLAGS(enable_modes)); if (use_interrupt) { INT_VECTOR int_vect; INT_SOURCE int_src; switch(module) { case UART1: int_vect = INT_UART_1_VECTOR; int_src = INT_U1RX; break; case UART2: int_vect = INT_UART_2_VECTOR; int_src = INT_U2RX; break; case UART3: int_vect = INT_UART_3_VECTOR; int_src = INT_U3RX; break; case UART4: int_vect = INT_UART_4_VECTOR; int_src = INT_U4RX; break; case UART5: int_vect = INT_UART_5_VECTOR; int_src = INT_U5RX; break; case UART6: int_vect = INT_UART_6_VECTOR; int_src = INT_U6RX; break; case UART_NUMBER_OF_MODULES: default: return; // WTF } INTEnable(int_src, INT_ENABLED); INTSetVectorPriority(int_vect, int_priority); INTSetVectorSubPriority(int_vect, INT_SUB_PRIORITY_LEVEL_0); } }
// ***************************************************************************** // Initialize the Console serial port (BAUDRATE) // ***************************************************************************** void InitConsole(UINT32 baud) { UARTConfigure(UART_CONSOLE, UART_ENABLE_PINS_TX_RX_ONLY | UART_ENABLE_HIGH_SPEED); UARTSetFifoMode(UART_CONSOLE, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_CONSOLE, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_CONSOLE, GetPeripheralClock(), baud); #ifdef DEBUG_ALLOW_USER_INPUT INTEnable(INT_SOURCE_UART_RX(UART_CONSOLE), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_CONSOLE), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART_CONSOLE), INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_SOURCE_UART_TX(UART_CONSOLE), INT_DISABLED);//Disable TX interrupt! #endif UARTEnable(UART_CONSOLE, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); }
void CAN1Init(void) { CAN_BIT_CONFIG canBitConfig; UINT baudPrescalar; CANEnableModule(CAN1, TRUE); CANSetOperatingMode(CAN1, CAN_CONFIGURATION); while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION); // Standard Values canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ; canBitConfig.phaseSeg1Tq = CAN_BIT_5TQ; canBitConfig.propagationSegTq = CAN_BIT_1TQ; canBitConfig.phaseSeg2TimeSelect = TRUE; canBitConfig.sample3Time = TRUE; canBitConfig.syncJumpWidth = CAN_BIT_1TQ; // Set speed to 1Mbit/s CANSetSpeed(CAN1, &canBitConfig, SYSTEM_FREQ, CAN_BUS_SPEED); // 256 bytes of storage space CANAssignMemoryBuffer(CAN1, CAN1MessageFifoArea, (2*8*16)); CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY); CANConfigureChannelForRx(CAN1, CAN_CHANNEL1, 8, CAN_RX_FULL_RECEIVE); CANConfigureFilter(CAN1, CAN_FILTER0, 0x5F1, CAN_SID); CANConfigureFilterMask(CAN1, CAN_FILTER_MASK0, 0x7FF, CAN_SID, CAN_FILTER_MASK_IDE_TYPE); CANLinkFilterToChannel(CAN1, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL1); CANEnableFilter(CAN1, CAN_FILTER0, TRUE); CANEnableChannelEvent(CAN1, CAN_CHANNEL1, CAN_RX_CHANNEL_NOT_EMPTY, TRUE); CANEnableModuleEvent(CAN1, CAN_RX_EVENT, TRUE); INTSetVectorPriority(INT_CAN_1_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_CAN_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_CAN1, INT_ENABLED); CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION); while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION); }
// Initialize the serial port // Note: the NU32v2 is hard wired to use UART3 (= UART2A) void initSerialNU32v2() { int pbClk; // Configure the system performance pbClk = SYSTEMConfigPerformance(SYS_FREQ); UARTConfigure(UART3, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART3, UART_INTERRUPT_ON_TX_DONE | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART3, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART3, pbClk, DESIRED_BAUDRATE_NU32); UARTEnable(UART3, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART3 RX Interrupt INTEnable(INT_U3RX, INT_ENABLED); INTSetVectorPriority(INT_UART_3_VECTOR, INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_UART_3_VECTOR, INT_SUB_PRIORITY_LEVEL_0); }
void InitBluetooth(void) { #if DESIRED_BAUDRATE == 9600 mPORTBSetPinsDigitalOut(BIT_9); mPORTBSetBits(BIT_9);// BT high level, the reset polarity is active low mPORTASetPinsDigitalOut(BIT_3); mPORTASetBits(BIT_3);// Set Baud rate (high = force 9,600, low = 115 K or firmware setting) mPORTASetPinsDigitalOut(BIT_2); mPORTASetBits(BIT_2);// Set BT master (high = auto-master mode) #elif DESIRED_BAUDRATE == 115200 mPORTBSetPinsDigitalOut(BIT_9); mPORTBSetBits(BIT_9);// BT high level, the reset polarity is active low mPORTASetPinsDigitalOut(BIT_3); mPORTAClearBits(BIT_3);// Set Baud rate (high = force 9,600, low = 115 K or firmware setting) mPORTASetPinsDigitalOut(BIT_2); mPORTASetBits(BIT_2);// Set BT master (high = auto-master mode) #elif DESIRED_BAUDRATE ==921600 //nothing #endif UARTConfigure(UART_MODULE_ID2, UART_ENABLE_PINS_TX_RX_ONLY|UART_ENABLE_PINS_CTS_RTS); UARTConfigure(UART_MODULE_ID2, UART_ENABLE_HIGH_SPEED); UARTSetFifoMode(UART_MODULE_ID2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_MODULE_ID2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_MODULE_ID2, GetPeripheralClock(), DESIRED_BAUDRATE); UARTEnable(UART_MODULE_ID2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID2), INT_SUB_PRIORITY_LEVEL_1); // Enable multi-vector interrupts //WriteStringXbee("*** UART Bluetooth demarre ***\r\n"); }
int main(void) { BOARD_Init(); // Configure Timer 1 using PBCLK as input. This default period will make the LEDs blink at a // pretty reasonable rate to start. OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, 0xFFFF); // Set up the timer interrupt with a priority of 4. INTClearFlag(INT_T1); INTSetVectorPriority(INT_TIMER_1_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T1, INT_ENABLED); // Enable interrupts for the ADC ConfigIntADC10(ADC_INT_PRI_2 | ADC_INT_SUB_PRI_0 | ADC_INT_ON); // Set B2 to an input so AN0 can be used by the ADC. TRISBCLR = 1 << 2; // Configure and start the ADC // Read AN0 as sample a. We don't use alternate sampling, so setting sampleb is pointless. SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2); OpenADC10( ADC_MODULE_ON | ADC_IDLE_CONTINUE | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON, ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_8 | ADC_BUF_16 | ADC_ALT_INPUT_OFF, ADC_SAMPLE_TIME_29 | ADC_CONV_CLK_PB | ADC_CONV_CLK_51Tcy2, ENABLE_AN2_ANA, SKIP_SCAN_ALL); EnableADC10(); /*************************************************************************************************** * Your code goes in between this comment and the following one with asterisks. **************************************************************************************************/ printf("Welcome to the Lab 6 Extra Credit blank. Please remove before starting."); /*************************************************************************************************** * Your code goes in between this comment and the preceding one with asterisks **************************************************************************************************/ while (1); }
void NU32_EnableUART2Interrupt(void) { // turn off the module to change the settings UARTEnable(UART2, UART_ENABLE_FLAGS(UART_DISABLE)); // Configure UART1 RX Interrupt // UARTConfigure(UART2, UART_ENABLE_PINS_CTS_RTS ); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_DONE | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, SYSCLK, 115200); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); INTEnable(INT_U2RX, INT_ENABLED); INTSetVectorPriority(INT_UART_2_VECTOR, INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_UART_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); }
void initUART() { // PPS map UART1 pins RPB3R=0x01; // PPS MAP TX to RPB3 U1RXR=0x04; // PPS MAP RX to RPB2 // Setup UART1 UARTConfigure(UART1, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART1, 48000000, 115200); UARTEnable(UART1, UART_ENABLE_FLAGS(UART_ENABLE | UART_PERIPHERAL | UART_RX | UART_TX)); INTEnable(INT_SOURCE_UART_RX(UART1), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0); }
int main(void) { BOARD_Init(); // Configure Timer 1 using PBCLK as input. This default period will make the LEDs blink at a // pretty reasonable rate to start. OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, 0xFFFF); // Set up the timer interrupt with a priority of 4. INTClearFlag(INT_T1); INTSetVectorPriority(INT_TIMER_1_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T1, INT_ENABLED); /*************************************************************************************************** * Your code goes in between this comment and the following one with asterisks. **************************************************************************************************/ int x = 0x01; LEDS_INIT(); //give the initial value checkItem.event = 0; checkItem.value = 0; int direction = RIGHT; while (1) { //set the value LEDS_SET(x); if (checkItem.event == 1) { checkItem.event = 0; //two directions if(direction == RIGHT){ x = x << 1; }else{ x = x >> 1; } } //edge case of 0x100 if((x == 0x100) && (direction == RIGHT)){ x = 0x40; direction = LEFT; } //edge case of 0x00 if((x == 0) && (direction == LEFT)){ x = 0x02; direction = RIGHT; } }
/** * @brief If opened channels is equal to 0 let's open device, enable interrupts, * create mutex etc. If opened channels is greater than 0 just open another * channel. * * @param pUartDev Pointer to device which will be opened * @param pChannel Channel to open * @return Result value */ static retcode lldPic32ADCOpen(struct hldAdcDevice *pAdcDev, UINT8 pChannel) { UINT32 convReg, aqReg, scanFreq; assert(pAdcDev != NULL); if (pChannel > LLD_PIC32_ADC_MAX) return ERR_HLD_DEVICE_ADC_CHANNEL_NOT_EXISTS; AD1PCFG &= ~(1 << pChannel); adcInfo[pChannel].active = 1; if(openedChannels == 0) { // Enable and configure the ADC here AD1CSSL = 0xFFFF; AD1CON2 = 0b0000010000111100; convReg = (pAdcDev->config.adcClockPeriod * (configPERIPHERAL_CLOCK_HZ/1000000UL)); convReg /= (2UL*1000UL); convReg--; aqReg = pAdcDev->config.aquisitionTime / pAdcDev->config.adcClockPeriod; AD1CON3 = (convReg & 0xff) | ((aqReg & 0xff) << 8); AD1CON1 = 0b1000000011100110; DONE("LLD PIC 32 ADC device opened"); scanFreq = 1000000000UL/(pAdcDev->config.adcClockPeriod * (12+aqReg) * (LLD_PIC32_ADC_MAX+1)); LOG("LLD PIC 32 ADC Scanning frequency is %d Hz", scanFreq); // TODO: Add priority setting INTSetVectorPriority(INT_ADC_VECTOR, pAdcDev->config.intPriority); INTSetVectorSubPriority(INT_ADC_VECTOR, INT_SUB_PRIORITY_LEVEL_0); IEC1SET = 0x0002; } openedChannels++; pAdcDev->head.state |= HLD_DEVICE_STATE_RUNNING; return SUCCESS; }
int main(void) { BOARD_Init(); // Configure Timer 2 using PBCLK as input. We configure it using a 1:16 prescalar, so each timer // tick is actually at F_PB / 16 Hz, so setting PR2 to F_PB / 16 / 100 yields a .01s timer. OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_16, BOARD_GetPBClock() / 16 / 100); // Set up the timer interrupt with a medium priority of 4. INTClearFlag(INT_T2); INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_TIMER_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T2, INT_ENABLED); /****************************************************************************** * Your code goes in between this comment and the following one with asterisks. *****************************************************************************/ OledInit(); MorseInit(); while (1) { if (mflag) { if (mevent == MORSE_EVENT_DOT) { MorseDecode(MORSE_CHAR_DOT); } else if (mevent == MORSE_EVENT_DASH) { MorseDecode(MORSE_CHAR_DASH); } else if (mevent == MORSE_EVENT_INTER_LETTER) { templet[0] = MorseDecode(MORSE_CHAR_END_OF_CHAR); } else if (mevent == MORSE_EVENT_INTER_WORD) { MorseDecode(MORSE_CHAR_DECODE_RESET); } updateScreen(); mevent = 0; mflag = 0; } } /****************************************************************************** * Your code goes in between this comment and the preceding one with asterisks. *****************************************************************************/ while (1); }
//****************************************************************************** //Public Function Definitions //****************************************************************************** void SensorLoop_start() { //Setup Accelerometer ADXL362_startMeasurements(); //Setup Gyroscope L3G4200D_startMeasurements(); //Setup 3-axis compass HMC5883L_startMeasurements(); //Setup Timer1 INTClearFlag(INT_T1); INTSetVectorPriority(INT_TIMER_1_VECTOR, INT_PRIORITY_LEVEL_3); INTSetVectorSubPriority(INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T1, INT_ENABLED); //Turn on clock OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_64, 3125); //200hz 6250);//100hz @ 40MHz //2*6250);//400hz @ 40MHz //6250); //800hz @ 40MHz (T1_PS_1_8) //3125); //200hz }
void openSerial(unsigned char serialPortIndex, unsigned long baudRate) { // important to activate the RX for UART5. Information found on the net if (serialPortIndex == SERIAL_PORT_5) { PORTSetPinsDigitalIn(IOPORT_B, BIT_8); } UART_MODULE uart = getUartModule(serialPortIndex); UARTConfigure(uart, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(uart, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(uart, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(uart, GetPeripheralClock(), baudRate); UARTEnable(uart, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); INTEnable(INT_SOURCE_UART_RX(uart), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(uart), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(uart), INT_SUB_PRIORITY_LEVEL_0); // TODO : Move this code to Global Setup ! // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); }
int main(void) { //LOCALS unsigned int temp; unsigned int channel1, channel2; M1_stepPeriod = M2_stepPeriod = M3_stepPeriod = M4_stepPeriod = 50; // in tens of u-seconds unsigned char M1_state = 0, M2_state = 0, M3_state = 0, M4_state = 0; SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); /* TIMER1 - now configured to interrupt at 10 khz (every 100us) */ OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK); ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); /* TIMER2 - 100 khz interrupt for distance measure*/ OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, T2_TICK); ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_3); //It is off until trigger /* PORTA b2 and b3 for servo-PWM */ mPORTAClearBits(BIT_2 | BIT_3); mPORTASetPinsDigitalOut(BIT_2 | BIT_3); /* ULTRASONICS: some bits of PORTB for ultrasonic sensors */ PORTResetPins(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11 ); PORTSetPinsDigitalOut(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11); //trigger /* Input Capture pins for echo signals */ //interrupt on every risging/falling edge starting with a rising edge PORTSetPinsDigitalIn(IOPORT_D, BIT_8| BIT_9| BIT_10| BIT_11); //INC1, INC2, INC3, INC4 Pin mIC1ClearIntFlag(); OpenCapture1( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//front ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture2( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//back ConfigIntCapture2(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture3( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//left ConfigIntCapture3(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture4( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//right ConfigIntCapture4(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); /* PINS used for the START (RD13) BUTTON */ PORTSetPinsDigitalIn(IOPORT_D, BIT_13); #define CONFIG (CN_ON | CN_IDLE_CON) #define INTERRUPT (CHANGE_INT_ON | CHANGE_INT_PRI_2) mCNOpen(CONFIG, CN19_ENABLE, CN19_PULLUP_ENABLE); temp = mPORTDRead(); /* PORT D and E for motors */ //motor 1 mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); // Turn on PORTD on startup. mPORTDSetPinsDigitalOut(BIT_4 | BIT_5 | BIT_6 | BIT_7); // Make PORTD output. //motor 2 mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); // Turn on PORTC on startup. mPORTCSetPinsDigitalOut(BIT_1 | BIT_2 | BIT_3 | BIT_4); // Make PORTC output. //motor 3 and 4 mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // Turn on PORTE on startup. mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // Make PORTE output. // UART2 to connect to the PC. // This initialization assumes 36MHz Fpb clock. If it changes, // you will have to modify baud rate initializer. UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, GetPeripheralClock(), BAUD); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART2 RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0); /* PORTD for LEDs - DEBUGGING */ mPORTDClearBits(BIT_0 | BIT_1 | BIT_2); mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2); // Congifure Change/Notice Interrupt Flag ConfigIntCN(INTERRUPT); // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); counterDistanceMeasure=600; //measure ULTRASONICS distance each 60 ms while (1) { /***************** Robot MAIN state machine *****************/ unsigned char ret = 0; switch (Robo_State) { case 0: MotorsON = 0; Robo_State = 0; InvInitialOrientation(RESET); TestDog(RESET); GoToRoom4short(RESET); BackToStart(RESET); InitialOrientation(RESET); GoToCenter(RESET); GoToRoom4long(RESET); break; case 1: ret = InvInitialOrientation(GO); if (ret == 1) { Robo_State = 2; } break; case 2: ret = TestDog(GO); if (ret == 1) { Robo_State = 3; //DOG not found } else if (ret == 2) { Robo_State = 4; //DOG found } break; case 3: ret = GoToRoom4short(GO); if (ret == 1) { Robo_State = 0; } break; case 4: ret = BackToStart(GO); if (ret == 1) { Robo_State = 5; } break; case 5: ret = GoToCenter(GO); if (ret == 1) { Robo_State = 6; } break; case 6: ret = GoToRoom4long(GO); if (ret == 1) { Robo_State = 0; } break; } if (frontDistance < 30 || backDistance < 30 || leftDistance < 30 || rightDistance < 30) mPORTDSetBits(BIT_0); else mPORTDClearBits(BIT_0); /***************************************************************/ /***************** Motors State Machine ************************/ if (MotorsON) { /**************************** MOTOR MAP M1 O-------------O M2 ON EVEN MOTORS, STEPS MUST BE INVERTED | /\ | i.e. FORWARD IS BACKWARD | / \ | | || | | || | M3 O-------------O M4 *****************************/ if (M1_counter == 0) { switch (M1_state) { case 0: // set 0011 step (0x3 , 1); if (M1forward) M1_state = 1; else M1_state = 3; break; case 1: // set 1001 step (0x9 , 1); if (M1forward) M1_state = 2; else M1_state = 0; break; case 2: // set 1100 step (0xC , 1); if (M1forward) M1_state = 3; else M1_state = 1; break; case 3: // set 0110 default: step (0x6 , 1); if (M1forward) M1_state = 0; else M1_state = 2; break; } M1_counter = M1_stepPeriod; step_counter[0]--; if (directionNow == countingDirection) step_counter[1]--; } if (M2_counter == 0) { switch (M2_state) { case 0: // set 0011 step (0x3 , 2); if (M2forward) M2_state = 1; else M2_state = 3; break; case 1: // set 0110 step (0x6 , 2); if (M2forward) M2_state = 2; else M2_state = 0; break; case 2: // set 1100 step (0xC , 2); if (M2forward) M2_state = 3; else M2_state = 1; break; case 3: // set 1001 default: step (0x9 , 2); if (M2forward) M2_state = 0; else M2_state = 2; break; } M2_counter = M2_stepPeriod; } if (M3_counter == 0) { switch (M3_state) { case 0: // set 0011 step (0x3 , 3); if (M3forward) M3_state = 1; else M3_state = 3; break; case 1: // set 1001 step (0x9 , 3); if (M3forward) M3_state = 2; else M3_state = 0; break; case 2: // set 1100 step (0xC , 3); if (M3forward) M3_state = 3; else M3_state = 1; break; case 3: // set 0110 default: step (0x6 , 3); if (M3forward) M3_state = 0; else M3_state = 2; break; } M3_counter = M3_stepPeriod; } if (M4_counter == 0) { switch (M4_state) { case 0: // set 0011 step (0x3 , 4); if (M4forward) M4_state = 1; else M4_state = 3; break; case 1: // set 0110 step (0x6 , 4); if (M4forward) M4_state = 2; else M4_state = 0; break; case 2: // set 1100 step (0xC , 4); if (M4forward) M4_state = 3; else M4_state = 1; break; case 3: // set 1001 default: step (0x9 , 4); if (M4forward) M4_state = 0; else M4_state = 2; break; } M4_counter = M4_stepPeriod; } } else { //motors off mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); } /************************************************************/ /******* TEST CODE, toggles the servos (from 90 deg. to -90 deg.) every 1 s. ********/ /* if (auxcounter == 0) { servo1_angle = 0; if (servo2_angle == 90) servo2_angle = -90; else servo2_angle = 90; auxcounter = 20000; // toggle angle every 2 s. } */ servo1_angle = 0; servo2_angle = -90; /* if (frontDistance > 13 && frontDistance < 17) { servo2_angle = 90; } else servo2_angle = -90; */ /*******************************************************************/ /****************** SERVO CONTROL ******************/ /* Changing the global servoX_angle at any point in the code will move the servo to the desired angle. */ servo1_counter = (servo1_angle + 90)*(18)/180 + 6; // between 600 and 2400 us if (servo1_period == 0) { mPORTASetBits(BIT_2); servo1_period = SERVOMAXPERIOD; /* 200 * 100us = 20000us period */ } servo2_counter = (servo2_angle + 90)*(18)/180 + 6; // between 600 and 2400 us if (servo2_period == 0) { mPORTASetBits(BIT_3); servo2_period = SERVOMAXPERIOD; /* 200 * 100us = 20000us period */ } /*****************************************************/ } /* end of while(1) */ return 0; }
int32_t main(void) { #ifndef PIC32_STARTER_KIT /*The JTAG is on by default on POR. A PIC32 Starter Kit uses the JTAG, but for other debug tool use, like ICD 3 and Real ICE, the JTAG should be off to free up the JTAG I/O */ DDPCONbits.JTAGEN = 0; #endif /*Refer to the C32 peripheral library documentation for more information on the SYTEMConfig function. This function sets the PB divider, the Flash Wait States, and the DRM /wait states to the optimum value. It also enables the cacheability for the K0 segment. It could has side effects of possibly alter the pre-fetch buffer and cache. It sets the RAM wait states to 0. Other than the SYS_FREQ, this takes these parameters. The top 3 may be '|'ed together: SYS_CFG_WAIT_STATES (configures flash wait states from system clock) SYS_CFG_PB_BUS (configures the PB bus from the system clock) SYS_CFG_PCACHE (configures the pCache if used) SYS_CFG_ALL (configures the flash wait states, PB bus, and pCache)*/ /* TODO Add user clock/system configuration code if appropriate. */ SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); /* Initialize I/O and Peripherals for application */ InitApp(); /*Configure Multivector Interrupt Mode. Using Single Vector Mode is expensive from a timing perspective, so most applications should probably not use a Single Vector Mode*/ // Configure UART2 RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0); // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); /* TODO <INSERT USER APPLICATION CODE HERE> */ //Open UART2 OpenUART2(UART_EN, UART_BRGH_FOUR|UART_RX_ENABLE | UART_TX_ENABLE, 21); //Open SPI 1 channel PORTBbits.RB11 = 1; OpenSPI1( SPI_MODE8_ON | MASTER_ENABLE_ON | SEC_PRESCAL_1_1 | PRI_PRESCAL_1_1 | FRAME_ENABLE_OFF | CLK_POL_ACTIVE_HIGH | ENABLE_SDO_PIN , SPI_ENABLE ); SPI1BRG=39; initRadio(); setTXAddress("UNIT2"); setRXAddress(0,"UNIT1"); char temp; char text[6]; text[0]='H'; text[1]='e'; text[2]='l'; text[3]='l'; text[4]='o'; text[5]='!'; while(1) { setTransmitter(); PORTBbits.RB11 = 0; DelayMs(20); transmitData(&text[0],6); printf("Hello world! \r\n"); PORTBbits.RB11 = 1; DelayMs(20); } }
//****************************************************************************** //Public Function Definitions //****************************************************************************** void FIFOI2C2_initialize() { int i = 0; int device = 0; //Interrupt Stuff INTSetVectorPriority(INT_I2C_2_VECTOR, INT_PRIORITY_LEVEL_5); INTSetVectorSubPriority(INT_I2C_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTClearFlag(INT_I2C2B); INTClearFlag(INT_I2C2M); //Enable I2C Bus Collision Event interrupt INTEnable(INT_I2C2B, INT_ENABLED); //Enable I2C2 Master Event interrupt INTEnable(INT_I2C2M, INT_ENABLED); //Initialize each device's local variables #if FIFOI2C2_DEVICES_COUNT >= 1 device = 0; FIFOI2C2_Devices_List[device].address = FIFOI2C2_DEVICE0_ADDRESS; FIFOI2C2_Devices_List[device].transmit_buffer_current = 0; FIFOI2C2_Devices_List[device].transmit_buffer_length = 0; FIFOI2C2_Devices_List[device].receive_buffer_current = 0; FIFOI2C2_Devices_List[device].receive_buffer_length = 0; i = 0; while(i < FIFOI2C2_TRANSMIT_BUFFER_SIZE) { FIFOI2C2_TX_Byte txb; txb.tx_byte = 0; txb.device_command = 0; FIFOI2C2_Devices_List[device].transmit_buffer[i++] = txb; } i = 0; while(i < FIFOI2C2_RECEIVE_BUFFER_SIZE) { FIFOI2C2_RX_Byte rxb; rxb.rx_byte = 0; rxb.device_command = 0; FIFOI2C2_Devices_List[device].receive_buffer[i++] = rxb; } #endif #if FIFOI2C2_DEVICES_COUNT >= 2 device = 1; FIFOI2C2_Devices_List[device].address = FIFOI2C2_DEVICE1_ADDRESS; FIFOI2C2_Devices_List[device].transmit_buffer_current = 0; FIFOI2C2_Devices_List[device].transmit_buffer_length = 0; FIFOI2C2_Devices_List[device].receive_buffer_current = 0; FIFOI2C2_Devices_List[device].receive_buffer_length = 0; i = 0; while(i < FIFOI2C2_TRANSMIT_BUFFER_SIZE) { FIFOI2C2_TX_Byte txb; txb.tx_byte = 0; txb.device_command = 0; FIFOI2C2_Devices_List[device].transmit_buffer[i++] = txb; } i = 0; while(i < FIFOI2C2_RECEIVE_BUFFER_SIZE) { FIFOI2C2_RX_Byte rxb; rxb.rx_byte = 0; rxb.device_command = 0; FIFOI2C2_Devices_List[device].receive_buffer[i++] = rxb; } #endif #if FIFOI2C2_DEVICES_COUNT >= 3 device = 2; FIFOI2C2_Devices_List[device].address = FIFOI2C2_DEVICE2_ADDRESS; FIFOI2C2_Devices_List[device].transmit_buffer_current = 0; FIFOI2C2_Devices_List[device].transmit_buffer_length = 0; FIFOI2C2_Devices_List[device].receive_buffer_current = 0; FIFOI2C2_Devices_List[device].receive_buffer_length = 0; i = 0; while(i < FIFOI2C2_TRANSMIT_BUFFER_SIZE) { FIFOI2C2_TX_Byte txb; txb.tx_byte = 0; txb.device_command = 0; FIFOI2C2_Devices_List[device].transmit_buffer[i++] = txb; } i = 0; while(i < FIFOI2C2_RECEIVE_BUFFER_SIZE) { FIFOI2C2_RX_Byte rxb; rxb.rx_byte = 0; rxb.device_command = 0; FIFOI2C2_Devices_List[device].receive_buffer[i++] = rxb; } #endif #if FIFOI2C2_DEVICES_COUNT >= 4 device = 3; FIFOI2C2_Devices_List[device].address = FIFOI2C2_DEVICE3_ADDRESS; FIFOI2C2_Devices_List[device].transmit_buffer_current = 0; FIFOI2C2_Devices_List[device].transmit_buffer_length = 0; FIFOI2C2_Devices_List[device].receive_buffer_current = 0; FIFOI2C2_Devices_List[device].receive_buffer_length = 0; i = 0; while(i < FIFOI2C2_TRANSMIT_BUFFER_SIZE) { FIFOI2C2_TX_Byte txb; txb.tx_byte = 0; txb.device_command = 0; FIFOI2C2_Devices_List[device].transmit_buffer[i++] = txb; } i = 0; while(i < FIFOI2C2_RECEIVE_BUFFER_SIZE) { FIFOI2C2_RX_Byte rxb; rxb.rx_byte = 0; rxb.device_command = 0; FIFOI2C2_Devices_List[device].receive_buffer[i++] = rxb; } #endif #if FIFOI2C2_DEVICES_COUNT >= 5 device = 4; FIFOI2C2_Devices_List[device].address = FIFOI2C2_DEVICE4_ADDRESS; FIFOI2C2_Devices_List[device].transmit_buffer_current = 0; FIFOI2C2_Devices_List[device].transmit_buffer_length = 0; FIFOI2C2_Devices_List[device].receive_buffer_current = 0; FIFOI2C2_Devices_List[device].receive_buffer_length = 0; i = 0; while(i < FIFOI2C2_TRANSMIT_BUFFER_SIZE) { FIFOI2C2_TX_Byte txb; txb.tx_byte = 0; txb.device_command = 0; FIFOI2C2_Devices_List[device].transmit_buffer[i++] = txb; } i = 0; while(i < FIFOI2C2_RECEIVE_BUFFER_SIZE) { FIFOI2C2_RX_Byte rxb; rxb.rx_byte = 0; rxb.device_command = 0; FIFOI2C2_Devices_List[device].receive_buffer[i++] = rxb; } #endif //configure the clock for the I2C2 I2C2BRG = (1.0/(2.0*FIFOI2C2_BAUD_RATE) - 104e-9) * GetPeripheralClock() - 1.5; //-1.5 takes into account rounding for -2. //Turns on the I2C2 Module I2C2CONbits.ON = 1; }
int main(void) { SpiOpenFlags spiFlags; AudioStereo test_sine[]={ 0 , 0 , 946234 , 946234 , 1877546 , 1877546 , 2779247 , 2779247 , 3637119 , 3637119 , 4437630 , 4437630 , 5168158 , 5168158 , 5817180 , 5817180 , 6374462 , 6374462 , 6831216 , 6831216 , 7180237 , 7180237 , 7416020 , 7416020 , 7534850 , 7534850 , 7534850 , 7534850 , 7416020 , 7416020 , 7180237 , 7180237 , 6831216 , 6831216 , 6374462 , 6374462 , 5817180 , 5817180 , 5168158 , 5168158 , 4437630 , 4437630 , 3637119 , 3637119 , 2779247 , 2779247 , 1877546 , 1877546 , 946234 , 946234 , 0 , 0 , -946234 , -946234 , -1877546 , -1877546 , -2779247 , -2779247 , -3637119 , -3637119 , -4437630 , -4437630 , -5168158 , -5168158 , -5817180 , -5817180 , -6374462 , -6374462 , -6831216 , -6831216 , -7180237 , -7180237 , -7416020 , -7416020 , -7534850 , -7534850 , -7534850 , -7534850 , -7416020 , -7416020 , -7180237 , -7180237 , -6831216 , -6831216 , -6374462 , -6374462 , -5817180 , -5817180 , -5168158 , -5168158 , -4437630 , -4437630 , -3637119 , -3637119 , -2779247 , -2779247 , }; // Initialize audio codec. WM8960CodecOpen(); WM8960CodecConfigVolume(0,0); WM8960CodecConfigSampleRate(SAMPLE_RATE_16000_HZ); WM8960CodecConfigVolume(volADC,volDAC); //Congigure MIPS, Prefetch Cache module. SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); INTEnableSystemMultiVectoredInt(); //Test tone vector sampled at 48kHz. txBuffer = test_sine; //Configure the direction of used pins and //configure as digital pins. PORTSetPinsDigitalIn(IOPORT_G, BIT_7); PORTSetPinsDigitalOut(IOPORT_G, BIT_6); PORTSetPinsDigitalOut(IOPORT_G, BIT_8); PORTSetPinsDigitalOut(IOPORT_G, BIT_9); // //Configure Reference Clock Output to 12.288MHz. // mOSCREFOTRIMSet(REFTRIM); // OSCREFConfig(OSC_REFOCON_USBPLL, //USB-PLL clock output used as REFCLKO source // OSC_REFOCON_OE | OSC_REFOCON_ON, //Enable and turn on the REFCLKO // RODIV); //Configure SPI in I2S mode with 24-bit stereo audio. spiFlags= SPI_OPEN_MSTEN | //Master mode enable SPI_OPEN_SSEN | //Enable slave select function SPI_OPEN_CKP_HIGH | //Clock polarity Idle High Actie Low SPI_OPEN_MODE32 | //Data mode: 32b SPI_OPEN_FRMEN | // Enable Framed SPI SPI_OPEN_FSP_IN | // Frame Sync Pulse is input SPI_OPEN_FSP_HIGH; //Frame Sync Pulse is active high //Configure and turn on the SPI1 module. SpiChnEnable(WM8960DRV_SPI_MODULE, FALSE); SpiChnConfigure(WM8960DRV_SPI_MODULE, spiFlags); SpiChnSetBitRate(WM8960DRV_SPI_MODULE, GetPeripheralClock(), 1024000); SpiChnEnable(WM8960DRV_SPI_MODULE, TRUE); //Enable SPI2 interrupt. INTSetVectorPriority(INT_SPI_2_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_SPI_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_SPI2, INT_ENABLED); SpiChnPutC(SPI_CHANNEL2, 0); //Dummy write to start the SPI //while (1); return 0; }