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 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
//****************************************************************************** //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)); }
void SERIAL_Init(void) { transmitBuffer = (struct CircBuffer*) &outgoingUart; //set up buffer for receive newCircBuffer(transmitBuffer); receiveBuffer = (struct CircBuffer*) &incomingUart; //set up buffer for transmit newCircBuffer(receiveBuffer); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, 115200); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY | UART_INTERRUPT_ON_TX_BUFFER_EMPTY); INTSetVectorPriority(INT_UART_1_VECTOR, INT_PRIORITY_LEVEL_4); //set the interrupt priority //RPC5R = 1; PPSOutput(1, RPC5, U1TX); PORTSetPinsDigitalIn(IOPORT_C, BIT_3); //U1RXRbits.U1RXR = 0b111; PPSInput(3, U1RX, RPC3); UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); INTEnable(INT_U1RX, INT_ENABLED); //INTSetFlag(INT_U1RX); //INTEnable(INT_U1TX, INT_ENABLED); //PutChar('z'); }
void initUART2(){ UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);//configure uart for rx and tx UARTSetFifoMode(UART2, UART_INTERRUPT_ON_RX_NOT_EMPTY);//configure for interrupts UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);//set uart line options UARTSetDataRate(UART2, getPBusClock(), UART_BAUD);//data rate 9600 baud and pbus 10MHz UARTEnable(UART2, UART_PERIPHERAL | UART_RX | UART_TX | UART_ENABLE); //enable the uart for tx rx while(!UARTTransmitterIsReady(UART2)); }
//****************************************************** // void OpenUart ( const UARTx,long BaudRate) // Overture d'un port serie // @param : UARTx : choix du port // UART1,UART2,UART3,UART4,UART5,UART6 // BaudeRate : Vitesse du port serie // 1200,2400,4800,9600,19200,38400,57600,115200 // //****************************************************** void OpenUart ( const UARTx,long BaudRate) { UARTConfigure(UARTx, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UARTx, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UARTx, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UARTx, GetPeripheralClock(), BAUDERATE); UARTEnable(UARTx, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); }
void setupSerial(void) { 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, PBCLK, BAUDRATE); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); SendDataBuffer("\n\r***Radiometer Serial port configured***\n\r", sizeof("\n\r***Radiometer Serial port configured***\n\r")); return; }
void initUART(void) { PORTFbits.RF4 = 1; //U2RX PORTFbits.RF5 = 0; //U2TX UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, 0); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, SYS_CLOCK, 115200); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); }
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); } }
void UART_init(uint8_t id, uint32_t baudRate){ //will need buffers for both the UARTS if(id == UART1_ID){ transmitBufferUart1 = (struct CircBuffer*) &outgoingUart1; //set up buffer for receive newCircBuffer(transmitBufferUart1); receiveBufferUart1 = (struct CircBuffer*) &incomingUart1; //set up buffer for transmit newCircBuffer(receiveBufferUart1); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, baudRate); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY); mU1SetIntPriority(4); //set the interrupt priority UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); mU1RXIntEnable(1); mU1TXIntEnable(1); }else if(id == UART2_ID){ transmitBufferUart2 = (struct CircBuffer*) &outgoingUart2; //set up buffer for receive newCircBuffer(transmitBufferUart2); receiveBufferUart2 = (struct CircBuffer*) &incomingUart2; //set up buffer for transmit newCircBuffer(receiveBufferUart2); UARTConfigure(UART2, 0x00); UARTSetDataRate(UART2, F_PB, baudRate); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_RX_NOT_EMPTY); mU2SetIntPriority(4); //set the interrupt priority UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); mU2RXIntEnable(1); mU2TXIntEnable(1); } }
// ***************************************************************************** // 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)); }
// 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"); }
/**************************************************************************** Function: void SelfTest() Description: This routine performs a self test of the hardware. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ static void SelfTest(void) { char value = 0; char* buf[32]; Exosite_Init("microchip","dv102412",IF_WIFI, 1); // Configure Sensor Serial Port UARTConfigure(SENSOR_UART, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(SENSOR_UART, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(SENSOR_UART, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(SENSOR_UART, GetPeripheralClock(), 9600); UARTEnable(SENSOR_UART, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Verify MRF24WB/G0MA MAC Address if(AppConfig.MyMACAddr.v[0] == 0x00 && AppConfig.MyMACAddr.v[1] == 0x1E) { //******************************************************************** // Prints a label using ESC/P commands to a Brother PT-9800PCN printer //******************************************************************** // Send ESC/P Commands to setup printer UARTTxBuffer("\033ia\000\033@\033X\002",9); // ESC i a 0 = Put Printer in ESC/P Mode // ESC @ = Reset Printer to Default settings // ESC X 2 = Specify Character Size // Send the Info to Print for the MAC Address label UARTTxBuffer("MRF24WB0MA\r",11); sprintf((char *)buf,"MAC: %02X%02X%02X%02X%02X%02X",AppConfig.MyMACAddr.v[0],AppConfig.MyMACAddr.v[1],AppConfig.MyMACAddr.v[2],AppConfig.MyMACAddr.v[3],AppConfig.MyMACAddr.v[4], AppConfig.MyMACAddr.v[5]); UARTTxBuffer((char *)buf, strlen((const char *)buf)); // Print the label UARTTxBuffer("\f",1); // Toggle LED's while(1) { LED0_IO = value; LED1_IO = value >> 1; LED2_IO = value >> 2; DelayMs(400); if(value == 8) value = 0; else value++; } }
int main(void) { // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } /*************************************************************************************************** * Your code goes in between this comment and the following one with asterisks. **************************************************************************************************/ /*************************************************************************************************** * Your code goes in between this comment and the preceding one with asterisks. **************************************************************************************************/ // Returning from main() is bad form in embedded environments. So we sit and spin. 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); }
void SERIAL_Init(void) { transmitBuffer = (struct CircBuffer*) &outgoingUart; //set up buffer for receive newCircBuffer(transmitBuffer); receiveBuffer = (struct CircBuffer*) &incomingUart; //set up buffer for transmit newCircBuffer(receiveBuffer); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, 115200); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY | UART_INTERRUPT_ON_RX_NOT_EMPTY); INTSetVectorPriority(INT_UART_1_VECTOR, INT_PRIORITY_LEVEL_4); //set the interrupt priority UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); INTEnable(INT_U1RX, INT_ENABLED); INTEnable(INT_U1TX, INT_ENABLED); }
//TODO: implement sanity checks. hal_uart_port hal_uart_open(hal_uart_port port, hal_uart_baudrate baudrate, hal_uart_parity parity, hal_uart_stop_bits stop_bits, hal_uart_on_data_received_callback data_received){ on_data_received[port] = data_received; assert(port >= HAL_UART_PORT_1 && port <= HAL_UART_NUMBER_OF_PORTS ); /* Configure uart */ UART_MODULE uart = logic_uart2phy_uart(port); SetRxTxPins(uart); UARTConfigure(uart, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(uart, UART_INTERRUPT_ON_TX_DONE | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(uart, UART_DATA_SIZE_8_BITS | get_parity(parity) | get_stop_bits(stop_bits)); UARTSetDataRate(uart, PIC32_PERIPHERALBUS_FREQ, get_baudrate(baudrate)); UARTEnable(uart, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); INTClearFlag(INT_SOURCE_UART_RX(uart)); INTEnable(INT_SOURCE_UART_RX(uart),INT_ENABLED); return port; }
/* 1. Select the desired basic configuration from the UART_CONFIGURATION Enumeration and set the selected options using the UARTConfigure Function. 2. Set the desired FIFO trigger levels for receive and transmit from the UART_FIFO_MODE Enumeration and set the selected options using the UARTSetFifoMode Function. 3. Set the line control configuration from the UART_LINE_CONTROL_MODE Enumeration and set the selected options using the UARTSetLineControl Function. 4. Set the desired data rate using UARTSetDataRate Function. 5. Enable the mode for operation using UART_ENABLE_MODE Enumeration and set the selected options using the UARTEnable Function. */ UINT UARTiConfigure(UART_MODULE iUART, UINT32 uiDataRate) { UINT uiActualDataRate; //step 1 - set the configuration, not available to be set on UART5 if(iUART != UART5) UARTConfigure(iUART, UART_ENABLE_PINS_TX_RX_ONLY); //step 2 - set the fifo interrupt settings // UARTSetFifoMode(iUART, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetFifoMode(iUART, UART_INTERRUPT_ON_RX_NOT_EMPTY); //step 3 - set the data mode UARTSetLineControl(iUART, UART_DATA_SIZE_8_BITS|UART_PARITY_NONE|UART_STOP_BITS_1); //step 4 - set the baud (data) rate uiActualDataRate = UARTSetDataRate(iUART, GetPeripheralClock(), uiDataRate); //step 5 - enable uart UARTEnable(iUART, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); return(uiActualDataRate); }
/******************************************************************************* * FUNCTION: vUART2Init * * PARAMETERS: * ~ void * * RETURN: * ~ void * * DESCRIPTIONS: * Initialize the UART2 module. * UART 2 is used by bluetooth module. * *******************************************************************************/ void vUART2Init(void) { // Configure UART 2. UARTConfigure(UART2, UART_ENABLE_HIGH_SPEED | 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(), BT2_BAUDRATE); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure the interrupt. INTSetVectorPriority(INT_UART_2_VECTOR, INT_PRIORITY_LEVEL_7); INTClearFlag(INT_U2TX); INTClearFlag(INT_U2RX); INTClearFlag(INT_U2E); INTEnable(INT_U2RX, INT_ENABLED); INTEnable(INT_U2E, INT_ENABLED); }
void GPS_Init(void) { GtransmitBuffer = (struct CircBuffer*) &GoutgoingUart; //set up buffer for receive GPSnewCircBuffer(GtransmitBuffer); GreceiveBuffer = (struct CircBuffer*) &GincomingUart; //set up buffer for transmit GPSnewCircBuffer(GreceiveBuffer); UARTConfigure(UART2, 0x00); UARTSetDataRate(UART2, F_PB, 38400); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_RX_NOT_EMPTY | UART_INTERRUPT_ON_RX_NOT_EMPTY); mU2SetIntPriority(4); //set the interrupt priority UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); mU2RXIntEnable(1); mU2TXIntEnable(1); GPS_Configure(); }
void initUart( UART_MODULE umPortNum, uint32_t ui32WantedBaud ) { /* UART Configuration */ UARTConfigure( umPortNum, UART_ENABLE_PINS_TX_RX_ONLY ); /* UART FIFO Mode */ UARTSetFifoMode( umPortNum, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY ); /* UART Line Control */ UARTSetLineControl( umPortNum, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1 ); /* Set the Baud Rate of the UART */ UARTSetDataRate( umPortNum, (uint32_t)configPERIPHERAL_CLOCK_HZ, ui32WantedBaud ); /* Enable the UART for Transmit Only*/ UARTEnable( umPortNum, UART_ENABLE_FLAGS( UART_PERIPHERAL | UART_TX | UART_RX ) ); /* Set UART INterrupt Vector Priority*/ INTSetVectorPriority( INT_VECTOR_UART( UART1 ), INT_PRIORITY_LEVEL_2 ); disableUartISR( UART1 ); }
int main(void) { UINT8 buf[1024]; UARTConfigure(UART_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_MODULE_ID, GetPeripheralClock(), 9600); UARTEnable(UART_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); //Receive and echo back mode only while(1) { UINT32 rx_size; rx_size = GetDataBuffer(buf, 1024); SendDataBuffer(buf, rx_size); } return -1; }
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; }
int main(void) { // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } extern void T1Setup(); extern void T1Stop(); extern void T1Start(); // Enable LED outputs 0-7 by setting TRISE register TRISECLR = 0x00FF; // Initialize the PORTE to 0 PORTECLR = 0x00FF; // Set the lowest bit PORTESET = 1; OledInit(); OledDisplayOn(); printf("Starting Timer Set-up\n"); T1Setup(); int stopped = 0, reset = 0; extern volatile int milliseconds; int temp = 0; int count = 0; printTime(0, 0); while(1) { // Display the least significant part of the time for debugging int x = PORTD & 0xfff; if(x == 272) { if(stopped) { stopped = 0; T1Start(); } if(reset) reset = 0; if(milliseconds - temp >= 1000) { count++; int minutes = count/60; int seconds = count % 60; printTime(minutes,seconds); temp = milliseconds; } } else if(x == 784 || x == 528) { if(reset != 1) { printTime(0, 0); T1Stop(); stopped = 1; reset = 1; count = 0; } } else { stopped = 1; continue; } } }
/**************************************************************************** Function: static void InitializeBoard(void) Description: This routine initializes the hardware. It is a generic initialization routine for many of the Microchip development boards, using definitions in HardwareProfile.h to determine specific initialization. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ static void InitializeBoard(void) { // Note: WiFi Module hardware Initialization handled by StackInit() Library Routine // Enable multi-vectored interrupts INTEnableSystemMultiVectoredInt(); // Enable optimal performance SYSTEMConfigPerformance(GetSystemClock()); // Use 1:1 CPU Core:Peripheral clocks mOSCSetPBDIV(OSC_PB_DIV_1); // Disable JTAG port so we get our I/O pins back, but first // wait 50ms so if you want to reprogram the part with // JTAG, you'll still have a tiny window before JTAG goes away. // The PIC32 Starter Kit debuggers use JTAG and therefore must not // disable JTAG. DelayMs(50); DDPCONbits.JTAGEN = 0; // UART line configuration UARTConfigure(SENSOR_UART, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(SENSOR_UART, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(SENSOR_UART, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(SENSOR_UART, GetPeripheralClock(), 9600); UARTEnable(SENSOR_UART, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); UARTConfigure(SENSOR_UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(SENSOR_UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(SENSOR_UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(SENSOR_UART2, GetPeripheralClock(), 9600); UARTEnable(SENSOR_UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // ADC configuration // Define setup parameters for OpenADC10 function // Attention, the sample number need to be changed for multi sensor use, not 1 // Turn module on | Ouput in integer format | Trigger mode auto | Disable autosample , manually controlled #define config1 ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_MODULE_ON | ADC_IDLE_STOP // ADC ref external | Disable offset test | Disable scan mode | Perform 1 samples | Use dual buffers | Use alternate mode #define config2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF // Use ADC internal clock | Set sample time #define config3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31 // Do not assign channels to scan #define configscan ~(ENABLE_AN2_ANA | ENABLE_AN3_ANA) // Define the port for ADC, need to be modified for practical prox sensors #define configport ENABLE_AN2_ANA | ENABLE_AN3_ANA CloseADC10(); // here use on-board potentiometer for testing, change pin configuration when for actually prox sensor use SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF); // Configure ADC using the parameters defined above OpenADC10( config1, config2, config3, configport, configscan ); EnableADC10(); // Enable the ADC // LEDs LEDS_OFF(); LED0_TRIS = 0; LED1_TRIS = 0; LED2_TRIS = 0; // Push Button SW0_TRIS = 1; // MPU 9150 I2C Init and chip init Mpu_I2c_Init(I2C_NUM); // config the I2C module on PIC32 // wait at least 100 ms for sensor chip warm up, based on 40Mhz sysclk 4M times DelayMs(100); if (Mpu_Init(I2C_NUM)==I2C_ERROR) // Initialize the 9150 chip // UARTTxBuffer("Sensor is not connected",strlen("Sensor is not connected")); LEDS_ON(); }
int main(void) { #if defined( DUMP_DIAGNOSTICS_VIA_UART ) char ButtonMeasString[133]; # if defined( UART_DUMP_RAW_COUNTS ) || \ defined( UART_DUMP_ALL_COUNTS ) UINT16 iHF_Read; # endif//defined( UART_DUMP_RAW_COUNTS )... #endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) UINT16 CurrentButtonStatus, LastButtonStatus, CurrentButtonAsserts = 0, CurrentButtonMeasurements[MAX_ADC_CHANNELS], CurrentAveragedMeasurements[MAX_ADC_CHANNELS]; UINT16 CurrentWeights[MAX_ADC_CHANNELS]; CFGCONbits.JTAGEN = 0; // Disable JTAG // Initalize global interrupt enable INTEnableSystemMultiVectoredInt(); // Configure the device for maximum performance // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); #if defined( DUMP_DIAGNOSTICS_VIA_UART ) // Setup UART2 for transmission of button data. // Taken from C:\Program Files\Microchip\mplabc32\v1.12\examples\plib_examples\uart\uart_basic 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(), 115200 ); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX )); // RPS setup to bring U1TX out on pin 33 (RB4) to Switch 2 on board PPSOutput(4,RPC4,U2TX); #endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) // Setup data structures for using Direct Keys mTouchCapApp_MatrixKeys_Create(); /* This is mandatory function call to initilize physical layer. Call this function before any application layer code. */ mTouchCapAPI_Init(); // Initial startup demo of board LEDs. PORTB = PORTB_ROW_ALL | PORTB_COL_ALL; //Turn on all LEDs PORTC = PORTC_ROW_ALL | PORTC_COL_ALL; DelayMs(1000); mTouchCapLED_PowerUpSequence(); // turn on each LED in sliding motion PORTB = PORTB_ROW_ALL | PORTB_COL_ALL; //Turn on all LEDs PORTC = PORTC_ROW_ALL | PORTC_COL_ALL; DelayMs(1000); PORTB = PORTB_ROW_NIL | PORTB_COL_NIL; //Turn off all LEDs PORTC = PORTC_ROW_NIL | PORTC_COL_NIL; // Start button measurements Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer // Setup for detection/interpretation LastButtonStatus = 0; while(1) { if(EmTouchDataReady == 1) //This flag is set by Timer ISR when all channels have been read { // Calculate new button values and return the measurement updates mTouchCapPhy_UpdateData(CurrentButtonMeasurements, CurrentAveragedMeasurements, CurrentWeights, TRUE, FILTER_METHOD_USECHANNEL); // Update button and LED status and return button status for use with mTouch GUI on PC CurrentButtonStatus = mTouchCapApp_MatrixKeys_Demo(); // Check for button asserts: If buttons were pressed before, but none pressed now if ( LastButtonStatus > 0 && CurrentButtonStatus == 0 ) { CurrentButtonAsserts = LastButtonStatus; //Buttons have fired. } else { // Commented out to provide memory of last button asserted. //ButtonAsserts = 0; } /* Determining Channel Load Order: Button Layout: Column: Col 1 Col 2 Col 3 Col 4 AN0 AN1 AN4 AN5 Row 1: AN8 Btn 0 Btn 1 Btn 2 Btn 3 Row 2: AN7 Btn 4 Btn 5 Btn 6 Btn 7 Row 3: AN6 Btn 8 Btn 9 Btn10 Btn11 Resulting load Order (AN8,AN0), (AN8,AN1), (AN8,AN4), (AN8,AN5), (AN7,AN0), (AN7,AN1), (AN7,AN4), (AN7,AN5), (AN6,AN0), (AN6,AN1), (AN6,AN4), (AN6,AN5), Removing channels already loaded: (AN8,AN0), (---,AN1), (---,AN4), (---,AN5), (AN7,---), (---,---), (---,---), (---,---), (AN6,---), (---,---), (---,---), (---,---), So the channels are loaded into memory in this order: Channel Load: #0, #1, #2, #3, #4, #5, #6 ADC Channel: AN8, AN0, AN1, AN4, AN5, AN13, AN12 Button Func: Row1, Col1, Col2, Col3, Col4, Row2, Row3 Sorting to output rows in order followed by columns: Channel Load: #0, #5, #6, #1, #2, #3, #4, Button Func: Row1, Row2, Row3, Col1, Col2, Col3, Col4, This order is used below to output measurements to the mTouch GUI or to a hyperterminal for later analysis. */ # if defined( UART_DUMP_RAW_COUNTS ) for ( iHF_Read = 0; iHF_Read < NUM_HF_READS; iHF_Read++ ) # elif defined( UART_DUMP_ALL_COUNTS ) for ( iHF_Read = 0; iHF_Read < 3*NUM_HF_READS; iHF_Read++ ) # endif # if defined( UART_DUMP_RAW_COUNTS ) || defined( UART_DUMP_ALL_COUNTS ) { sprintf(ButtonMeasString, "%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d\r\n", CurrentButtonStatus,CurrentButtonAsserts, CurRawData[iHF_Read][0],CurRawData[iHF_Read][5],CurRawData[iHF_Read][6], 0, CurRawData[iHF_Read][1],CurRawData[iHF_Read][2],CurRawData[iHF_Read][3],CurRawData[iHF_Read][4], 0,0,0,0, 0,0,0,0, iHF_Read+1); SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); } # endif #if defined( DUMP_DIAGNOSTICS_VIA_UART ) // Format report string sprintf(ButtonMeasString, "%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d\r\n", CurrentButtonStatus, CurrentButtonAsserts, 0, // Zero for slider value CurrentButtonMeasurements[0], //Row 1: AN14: Load #0 CurrentButtonMeasurements[5], //Row 2: AN13: Load #5 CurrentButtonMeasurements[6], //Row 3: AN12: Load #6 0, //Placeholder for unused row CurrentButtonMeasurements[1], //Col 1: AN8: Load #1 CurrentButtonMeasurements[2], //Col 2: AN9: Load #2 CurrentButtonMeasurements[3], //Col 3: AN10: Load #3 CurrentButtonMeasurements[4]);//Col 4: AN11: Load #4 // Send report string back to mTouch GUI via UART #2 SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); # endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) LastButtonStatus = CurrentButtonStatus; //Remember button status EmTouchDataReady = 0; //clear flag Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer } // end if(EmTouchDataReady) } //end while (1) } // end main()
int main(void) { // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } // Enable LED outputs 0-7 by setting TRISE register TRISECLR = 0x00FF; // Initialize the PORTE to 0 PORTECLR = 0x00FF; // Set the lowest bit int mask = 1; PORTESET = mask; int delay = 0xA0000; // Loop forever, it is bad to exit in an embedded processor. int count=0; // move this into the delay function while (1) { // Move this printf into your getDelay function! printf("Hello, world! %d\n",count++); // Replace this with the getDelay function call! //int delay = getDelay(); // do nothing for a lot of cycles int i=0; for(i=0;i<delay;i++) ; // shift left by 1 mask = mask << 1; // rotate around if more than 8 bits if (mask & 0x0100) mask = 1; // Set the output to the new mask PORTE=mask; delay = 0xA0000 + 0x40000*sin((double)count/10) + 0x20000*cos((double)count/5); if(delay < 0) delay *= -1; } }