int main(void) { // Init PIC24 InitMain(); // Init modules AudioInInit(); Uart2Init(UART_BAUD_250000, 0); LedsInit(); // Main loop while(1) { if(AudioInIsGetReady()) { Fixed audioSample = AudioInGet(); // Update LEDs LedsUpdate(audioSample); // Print audio sample Uart2RxTasks(); if(Uart2IsPutReady() >= 6) { static const char asciiDigits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; int i = FIXED_TO_INT(audioSample); div_t n; int print = 0; if(i < 0) { Uart2PutChar('-'); i = -i; } if(i >= 10000) { n = div(i, 10000); Uart2PutChar(asciiDigits[n.quot]); i = n.rem; print = 1; } if(i >= 1000 || print) { n = div(i, 1000); Uart2PutChar(asciiDigits[n.quot]); i = n.rem; print = 1; } if(i >= 100 || print) { n = div(i, 100); Uart2PutChar(asciiDigits[n.quot]); i = n.rem; print = 1; } if(i >= 10 || print) { n = div(i, 10); Uart2PutChar(asciiDigits[n.quot]); i = n.rem; } Uart2PutChar(asciiDigits[i]); Uart2PutChar('\r'); } } } }
/** * @brief Loads byte array into software transmit buffer starts interrupt-driven * transmission. * * This function should only be called if space is available. Call * Uart1IsPutReady to determine the number of bytes that may be loaded into the * software transmit buffer. * * Example use: * @code * char array[] = { 1, 2, 3 }; * if(Uart1IsPutReady() > sizeof(array)) { * Uart1PutCharArray(array, sizeof(array)); * } * @endcode * * @param source Address of byte array. * @param numberOfBytes Number of bytes in byte array. * @return 0 if successful. */ int Uart2PutCharArray(const char* const source, const size_t numberOfBytes) { if (numberOfBytes > Uart2IsPutReady()) { return 1; // error: not enough space in buffer } int i; for (i = 0; i < numberOfBytes; i++) { Uart2PutChar(source[i]); } return 0; }
/** * @brief Loads string into software transmit buffer starts interrupt-driven * transmission. * * This function should only be called if space is available. Call * Uart1IsPutReady to determine the number of bytes that may be loaded into the * software transmit buffer. The terminating null character will not be sent. * * Example use: * @code * char string[] = "Hello World!"; * if(Uart1IsPutReady() > sizeof(string) - 1) { * Uart1PutString(string); * } * @endcode * * @param string */ void Uart2PutString(const char* string) { while (*string != '\0') { Uart2PutChar(*string++); } }