maygapixel

Untitled

Dec 16th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1.  
  2. #include <LPC21xx.h>                                        // LPC2148 MPU Register
  3. #include "uart0.h"
  4. #include <string.h>
  5. /****************************************************/
  6. void uart_print(unsigned char* str)
  7. {
  8.      unsigned char i = 0;
  9.      for (i=0;i<strlen(str);i++){putchar(str[i]);}
  10.    
  11. }
  12.  
  13.  
  14.  
  15. /****************************************************/
  16.  void uart_init(void)  
  17. {
  18.   PINSEL0 &= 0xFFFFFFF0;                                    // Reset P0.0,P0.1 Pin Config
  19.   PINSEL0 |= 0x00000001;                                    // Select P0.0 = TxD(UART0)
  20.   PINSEL0 |= 0x00000004;                                    // Select P0.1 = RxD(UART0)
  21.  
  22.   U0LCR &= 0xFC;                                            // Reset Word Select(1:0)
  23.   U0LCR |= 0x03;                                            // Data Bit = 8 Bit
  24.   U0LCR &= 0xFB;                                            // Stop Bit = 1 Bit
  25.   U0LCR &= 0xF7;                                            // Parity = Disable
  26.   U0LCR &= 0xBF;                                            // Disable Break Control
  27.   U0LCR |= 0x80;                                            // Enable Programming of Divisor Latches
  28.  
  29.   // U0DLM:U0DLL = 60.00 MHz / [16 x Baud]
  30.   //             = 60.00 MHz / [16 x 9600]
  31.   //             = 390.6 = 391 = 0187H
  32.   U0DLM = 0x01;                                             // Program Divisor Latch(391) for 9600 Baud
  33.   U0DLL = 0x87;
  34.  
  35.   U0LCR &= 0x7F;                                            // Disable Programming of Divisor Latches
  36.  
  37.   U0FCR |= 0x01;                                            // FIF0 Enable
  38.   U0FCR |= 0x02;                                            // RX FIFO Reset
  39.   U0FCR |= 0x04;                                            // TX FIFO Reset
  40.   U0FCR &= 0x3F;                      
  41. }
  42.  
  43. /****************************/
  44. /* Write Character To UART0 */
  45. /****************************/
  46. int putchar (int ch)  
  47. {                  
  48.   if (ch == '\n')  
  49.   {
  50.     while (!(U0LSR & 0x20));                                // Wait TXD Buffer Empty
  51.     U0THR = 0x0D;                                           // Write CR
  52.   }
  53.   while (!(U0LSR & 0x20));                                  // Wait TXD Buffer Empty
  54.   return (U0THR = ch);                                      // Write Character
  55. }
  56.  
  57. /*****************************/
  58. /* Read Character From UART0 */
  59. /*****************************/
  60. int getchar (void)  
  61. {                    
  62.   while (!(U0LSR & 0x01));                                  // Wait RXD Receive Data Ready
  63.   return (U0RBR);                                           // Get Receice Data & Return
  64. }
Add Comment
Please, Sign In to add comment