// ***********************
// Funcion para enviar un caracter por el UART 2
void UART2_Sendchar(char c)
{
while( (LPC_UART2->LSR & LSR_THRE) == 0 ); // Esperamos hasta que Transmit Holding Register Empty esté en 1
LPC_UART2->THR = c; //Escrivo en el Transmit Holding Register para que se envíe el caracter
}
// ***********************
// Funcion para leer un caracter desde el UART 2
char UART2_Getchar()
{
char c;
while( (LPC_UART2->LSR & LSR_RDR) == 0 ); // Espero hasta que haya datos mirando el Receive Data Register
c = LPC_UART2->RBR; // Leo el Receive Buffer Register
return c;
}
// ***********************
// Funcion para enviar una cadena de caracteres por el UART 2
void UART2_PrintString(char *pcString)
{
int i = 0;
// loop through until reach string's zero terminator
while (pcString[i] != 0) {
UART2_Sendchar(pcString[i]); // print each character
i++;
}
}
void uart2_printBytes(uint8_t* buf, uint32_t len) {
// transfer all bytes to HW Tx FIFO
while ( len != 0 ) {
// send next byte
UART2_Sendchar(*buf);
// update the buf ptr and length
buf++;
len--;
}
}
void uart2_printUint32(uint32_t n, uint8_t base) {
uint32_t i = 0;
uint8_t buf[8 * sizeof(uint32_t)]; // binary is the largest
// check for zero case, print and bail out if so
if (n == 0) {
UART2_Sendchar((uint8_t)'0');
return;
}
while (n > 0) {
buf[i] = n % base;
i++;
n /= base;
}
for (; i > 0; i--) {
if (buf[i - 1] < 10)
UART2_Sendchar((uint8_t)('0' + buf[i - 1]));
else
UART2_Sendchar((uint8_t)('A' + buf[i - 1] - 10));
}
}
void uart2_printInt32(int32_t n, uint8_t base) {
uint32_t i = 0;
// print '-' for negative numbers, also negate
if (n < 0) {
UART2_Sendchar((uint8_t)'-');
n = ((~n) + 1);
}
// cast to unsigned and print using uint32_t printer
i = n;
uart2_printUint32(i, base);
}
void uart2_printDouble(double n, uint8_t frac_digits) {
uint8_t i;
uint32_t i32;
double rounding, remainder;
// test for negatives
if (n < 0.0) {
UART2_Sendchar((uint8_t)'-');
n = -n;
}
// round correctly so that print(1.999, 2) prints as "2.00"
rounding = 0.5;
for (i=0; i<frac_digits; i++)
rounding /= 10.0;
n += rounding;
// extract the integer part of the number and print it
i32 = (uint32_t)n;
remainder = n - (double)i32;
uart2_printUint32(i32, 10);
// print the decimal point, but only if there are digits beyond
if (frac_digits > 0)
UART2_Sendchar((uint8_t)'.');
// extract digits from the remainder one at a time
while (frac_digits-- > 0) {
remainder *= 10.0;
i32 = (uint32_t)remainder;
uart2_printUint32(i32, 10);
remainder -= i32;
}
}