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); }
//****************************************************************************** //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) { // 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 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 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"); }
//****************************************************** // 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 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 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); }
/**************************************************************************** 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 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); }
//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; }
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); }
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(); }
/* 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); }
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 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 ); }
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(); }
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)); // unbuffered i/o works well for printf and gets, but not for scanf //setbuf(stdin, NULL); //no input buffer (for scanf) //setbuf(stdout, NULL); //no output buffer (for printf) // in the future want to set both to line buffering. // This would make the UART a character stream, a // different set up would be required for byte streams. // 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); }
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; } } }
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) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT BCSCTL1 = CALBC1_16MHZ; // Set DCO to 1MHz DCOCTL = CALDCO_16MHZ; // Set DCO to 1MHz UARTConfigure(); P2DIR |= PWRLED; // Set P2.4 as output P2OUT |= PWRLED; //P2OUT &= ~PWRLED; P1SEL &= ~ PROX; P1SEL2 &= ~ PROX; P1DIR &= ~ PROX; // Set switch pin as an input P1OUT |= PROX; // Set pull up resistor on for button P1REN |= PROX; // Enable pullup or pulldown resistors P1IES &= ~PROX; // Enable Interrupt to trigger on the rising edge P1IFG &= ~PROX; // Clear the interrupt flag for the button /* Configure timer A1 as a pulse width modulator */ TA1CCR0 = 420; // Set maximum count value to determine PWM Frequency = SMCLK/TACCR0 (1MHz/1000 = 1kHz) TA1CCR1 = 0; // Initialise counter compare value 1 to control Duty Cycle = TACCR1/TACCR0 (500/1000 = 50%) TA1CCR2 = 0; // Initialise counter compare value 1 to control Duty Cycle = TACCR1/TACCR0 (500/1000 = 50%) TA1CCTL2 = OUTMOD_7; // Set output to on when counter resets and off when counter equals TACCR1. Normal PWM. TA1CCTL1 = OUTMOD_7; // Set output to on when counter resets and off when counter equals TACCR1. Normal PWM. TA1CTL = TASSEL_2 + MC_1; // Use the SMCLK to clock the counter and set to count up mode P2DIR |= RIGHTMOTOR; // Set P2.4 as output P2SEL |= RIGHTMOTOR; // Select output P2.4 to be TA1.2 P2SEL2 &= ~RIGHTMOTOR; // Select output P2.4 to be TA1.2 P2DIR |= LEFTMOTOR; // Set P2.1 as output P2SEL |= LEFTMOTOR; // Select output P2.1 to be TA1.1 P2SEL2 &= ~LEFTMOTOR; // Select output P2.1 to be TA1.1 /* Configure timer A0 as a pulse width modulator */ TA0CCR0 = 422; // Set maximum count value to determine PWM Frequency = SMCLK/TACCR0 (1MHz/1000 = 1kHz) TA0CCR1 = 5; // Initialise counter compare value 1 to control Duty Cycle = TACCR1/TACCR0 (500/1000 = 50%) TA0CCTL1 = OUTMOD_7; // Set output to on when counter resets and off when counter equals TACCR1. Normal PWM. TA0CTL = TASSEL_2 | MC_1 | TAIE; // Use the SMCLK to clock the counter and set to count up mode P1IE = PROX; // Enable interrupts on port 1 for the proximaty sensor //TA0CCTL0 = CM_1 | CCIS_0 | SCS | CAP | CCIE ; // Raising Edge + CCI0A + Sync + Capture Mode + Interrupt enable //P1SEL |= BIT5; //set this bit as Input Capture (TA0.CCI0A) P1DIR |= IRLED; // Set P1.6 as output P1SEL |= IRLED; // Select output P1.6 to be TA0.1 P1SEL2 &= ~IRLED; // Select output P1.6 to be TA0.1 WDTCTL = WDT_MDLY_8; // Start and set watchdog timer (WDT) to trigger every 0ms //IFG1 &= ~WDTIFG; // Clear the interrupt flag for the WDT IE1 |= WDTIE; // enable WDT interrupt __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled }
/** * @fn void InitSystem(void) * * @brief This function initializes the microcontroller peripherals needed, the * application and the protocol. */ void InitSystem(void) { // Initialize microcontroller peripherals and application. // Disable all interrupts while setting up microcontroller. __BSP_DISABLE_INTERRUPT(); // Halt the watchdog. WatchdogHold(); // Stop the watchdog timer Nodes[0].ID = CreateRandomAddress(); // Generate a random address. This must be done // before the clocks and ADC are initialized. // Setup board clock system. BoardClockSystemInit(); // Intialize the system clocks SetOFIEBit(0); // Disable Osc Fault interrupt SetOFIFGBit(0); // Clear Osc Fault interrupt flag FCTL2 = FWKEY | FSSEL_1 | 19; SetMyNodeID(0); // Set node ID. The Flash block requires the clock // to be configured prior to calling this function. // Setup timers. TimerConfigure(__BSP_TIMER1); // Initialize Timer1 AttachEvent(__BSP_TIMER1_CCR0_EVENT_HANDLE, Timer1CCREventHandler); AttachEvent(__BSP_TIMER1_CCR1_EVENT_HANDLE, Timer1CCREventHandler); TimerConfigure(__BSP_TIMER2); // Initialize Timer2 AttachEvent(__BSP_TIMER2_CCR0_EVENT_HANDLE, Timer2CCREventHandler); TimerSetCCR(__BSP_TIMER2, __BSP_TIMER2_CCR0, __BSP_CONFIG_SWTIMER1_SOURCE_DIV); // Setup software timers. SoftwareTimerConfigure(); // Initialize Software Timer SoftwareTimerSetInterval(__BSP_SWTIMER[0], 100); SoftwareTimerEnable(__BSP_SWTIMER[0]); AttachEvent(__BSP_SWTIMER[0].event, SWTimer0EventHandler); // AttachEvent(__BSP_SWTIMER[1].event, SWTimer1EventHandler); RadioConfigure(&__BSP_RADIO1_CONNECTION); // Initialize the Radio Communication module // Setup UART. UARTConfigure(__BSP_UART1); // Initialize the UART Communication module AttachEvent(__BSP_UART1_RX_EVENT_HANDLE, UARTRXEventHandler); // Attach UART RX event handler // Setup LEDs. LEDConfigure(__BSP_LEDRED1); // Initialize the Red LED LEDConfigure(__BSP_LEDGREEN1); // Initialize the Green LED // Setup push button. PushButtonConfigure(__BSP_PUSHBUTTON1); // Initialize push button. AttachEvent(__BSP_PUSHBUTTON1_EVENT_HANDLE, PushButton1EventHandler); AttachEvent(__BSP_PORT1_EVENT_HANDLE, PortEventHandler); AttachEvent(__BSP_PORT2_EVENT_HANDLE, PortEventHandler); // Setup ADC. ADCChannelConfigure(__BSP_ADC1, __BSP_ADC1_CHAN9); // Initialize ADC channel. ADCReferenceOn(__BSP_ADC1); ADCEnableInterrupt(__BSP_ADC1); ADCOn(__BSP_ADC1); ADCEnableConversion(__BSP_ADC1); AttachEvent(__BSP_ADC1_EVENT_HANDLE, ADCEventHandler); // Setup Application. InitApplication(Nodes[0].ID); // Initialize the Application (application state of the module, supporting protocol) // Start timers in "UP" mode. TimerEnable(__BSP_TIMER1, UP_MODE); // Start the timer prior to enabling interrupts TimerEnable(__BSP_TIMER2, UP_MODE); // Start the timer prior to enabling interrupts // Enable interrupts now that microcontroller is ready for normal operation. __BSP_ENABLE_INTERRUPT(); }