Permalink
Browse files

Added UART send example.

Removed binary image file
  • Loading branch information...
1 parent a470d76 commit 7418817bd077477ce8589755126570a7870baa4d @naves-thiago committed Dec 18, 2011
Showing with 127 additions and 2 deletions.
  1. +127 −2 main_LPC17xx.c
  2. BIN mbed_test.bin
View
@@ -69,6 +69,128 @@ __INLINE static void LED_Off (uint32_t led) {
LPC_GPIO1->FIOPIN &= ~(led); /* Turn Off LED */
}
+
+/*----------------------------------------------------------------------------
+ UART0 configuration
+ *----------------------------------------------------------------------------*/
+uint32_t UARTInit( uint32_t PortNum, uint32_t baudrate )
+{
+ uint32_t Fdiv;
+ uint32_t pclkdiv, pclk;
+
+ if ( PortNum == 0 )
+ {
+ LPC_PINCON->PINSEL0 &= ~0x000000F0;
+ LPC_PINCON->PINSEL0 |= 0x00000050; /* RxD0 is P0.3 and TxD0 is P0.2 */
+ /* By default, the PCLKSELx value is zero, thus, the PCLK for
+ * all the peripherals is 1/4 of the SystemCoreClock. */
+ /* Bit 6~7 is for UART0 */
+ pclkdiv = (LPC_SC->PCLKSEL0 >> 6) & 0x03;
+ switch ( pclkdiv )
+ {
+ case 0x00:
+ default:
+ pclk = SystemCoreClock/4;
+ break;
+ case 0x01:
+ pclk = SystemCoreClock;
+ break;
+ case 0x02:
+ pclk = SystemCoreClock/2;
+ break;
+ case 0x03:
+ pclk = SystemCoreClock/8;
+ break;
+ }
+
+ LPC_UART0->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
+ Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */
+ LPC_UART0->DLM = Fdiv / 256;
+ LPC_UART0->DLL = Fdiv % 256;
+ LPC_UART0->LCR = 0x03; /* DLAB = 0 */
+ LPC_UART0->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
+
+ return (1);
+ }
+ else if ( PortNum == 1 )
+ {
+ LPC_PINCON->PINSEL4 &= ~0x0000000F;
+ LPC_PINCON->PINSEL4 |= 0x0000000A; /* Enable RxD1 P2.1, TxD1 P2.0 */
+
+ /* By default, the PCLKSELx value is zero, thus, the PCLK for
+ * all the peripherals is 1/4 of the SystemCoreClock. */
+ /* Bit 8,9 are for UART1 */
+ pclkdiv = (LPC_SC->PCLKSEL0 >> 8) & 0x03;
+ switch ( pclkdiv )
+ {
+ case 0x00:
+ default:
+ pclk = SystemCoreClock/4;
+ break;
+ case 0x01:
+ pclk = SystemCoreClock;
+ break;
+ case 0x02:
+ pclk = SystemCoreClock/2;
+ break;
+ case 0x03:
+ pclk = SystemCoreClock/8;
+ break;
+ }
+
+ LPC_UART1->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
+ Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */
+ LPC_UART1->DLM = Fdiv / 256;
+ LPC_UART1->DLL = Fdiv % 256;
+ LPC_UART1->LCR = 0x03; /* DLAB = 0 */
+ LPC_UART1->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
+
+ return 1;
+ }
+ return 0;
+}
+
+/*****************************************************************************
+ * ** Function name: UARTSend
+ * **
+ * ** Descriptions: Send a block of data to the UART 0 port based
+ * ** on the data length
+ * **
+ * ** parameters: portNum, buffer pointer, and data length
+ * ** Returned value: None
+ * **
+ * *****************************************************************************/
+void UARTSend( uint32_t portNum, uint8_t *BufferPtr, uint32_t Length )
+{
+ if ( portNum == 0 )
+ {
+ while ( Length != 0 )
+ {
+ /* THRE status, contain valid data */
+ while ( !(LPC_UART0->LSR & 1<<5) );
+ LPC_UART0->THR = *BufferPtr;
+ BufferPtr++;
+ Length--;
+ }
+ }
+ else
+ {
+ while ( Length != 0 )
+ {
+ /* THRE status, contain valid data */
+ while ( !(LPC_UART1->LSR & 1<<5) );
+ LPC_UART1->THR = *BufferPtr;
+ BufferPtr++;
+ Length--;
+ }
+ }
+ return;
+}
+
+/******************************************************************************
+ * ** End Of File
+ * ******************************************************************************/
+
/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
@@ -81,14 +203,17 @@ int main (void) {
}
LED_Config();
-
+ UARTInit( 0, 9600 );
+
+
uint32_t led = 1<<18;
while(1) {
LED_On (led); /* Turn on the LED. */
Delay (100); /* delay 100 Msec */
LED_Off (led); /* Turn off the LED. */
Delay (100); /* delay 100 Msec */
+ UARTSend( 0, "1234567890---", 13 );
}
-
+
}
View
Binary file not shown.

0 comments on commit 7418817

Please sign in to comment.