Advertisement
maygapixel

Untitled

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